Home > voicebox > activlev.m

activlev

PURPOSE ^

ACTIVLEV Measure active speech level as in ITU-T P.56 [LEV,AF,FSO]=(sp,FS,MODE)

SYNOPSIS ^

function [lev,af,fso,vad]=activlev(sp,fs,mode)

DESCRIPTION ^

ACTIVLEV Measure active speech level as in ITU-T P.56 [LEV,AF,FSO]=(sp,FS,MODE)

Usage: (1) lev=activlev(s,fs);     % speech level in units of power
       (2) db=activlev(s,fs,'d');  % speech level in dB
       (3) s=activlev(s,fs,'n');   % normalize active level to 0 dB

Inputs: sp     is the speech signal (with better than 20dB SNR)
        FS     is the sample frequency in Hz (see also FSO below)
        MODE   is a combination of the following:
               0 - omit high pass filter at 40 or 200 Hz (i.e. include DC)
               4 - high pass filter at 40 Hz instead of 200 Hz (but allows mains hum to pass)
               1 - use cheybyshev 1 filter
               2 - use chebyshev 2 filter (default)
               e - use elliptic filter
               h - omit low pass filter at 5.5 kHz
               d - give outputs in dB rather than power
               n - output a normalized speech signal as the first argument
               N - output a normalized filtered speech signal as the first argument
               l - give both active and long-term power levels
               a - include A-weighting filter
               i - include ITU-R-BS.468/ITU-T-J.16 weighting filter

Outputs:
    If the "n" option is specified, a speech signal normalized to 0dB will be given as
    the first output followed by the other outputs.
        LEV    gives the speech level in units of power (or dB if mode='d')
               if mode='l' is specified, LEV is a row vector with the "long term
               level" as its second element (this is just the mean power)
        AF     is the activity factor (or duty cycle) in the range 0 to 1
        FSO    is a column vector of intermediate information that allows
               you to process a speech signal in chunks. Thus:
                       fso=fs; for i=1:inc:nsamp,[lev,fso]=activlev(sp(i:i+inc-1),fso,mode); end
               is equivalent to:
                       lev=activlev(sp(1:nsamp),fs,mode)
               but is much slower. The two methods will not give identical results
               because they will use slightly different thresholds.
        VAD    is a boolean vector the same length as sp that acts as an approximate voice activity detector

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [lev,af,fso,vad]=activlev(sp,fs,mode)
0002 %ACTIVLEV Measure active speech level as in ITU-T P.56 [LEV,AF,FSO]=(sp,FS,MODE)
0003 %
0004 %Usage: (1) lev=activlev(s,fs);     % speech level in units of power
0005 %       (2) db=activlev(s,fs,'d');  % speech level in dB
0006 %       (3) s=activlev(s,fs,'n');   % normalize active level to 0 dB
0007 %
0008 %Inputs: sp     is the speech signal (with better than 20dB SNR)
0009 %        FS     is the sample frequency in Hz (see also FSO below)
0010 %        MODE   is a combination of the following:
0011 %               0 - omit high pass filter at 40 or 200 Hz (i.e. include DC)
0012 %               4 - high pass filter at 40 Hz instead of 200 Hz (but allows mains hum to pass)
0013 %               1 - use cheybyshev 1 filter
0014 %               2 - use chebyshev 2 filter (default)
0015 %               e - use elliptic filter
0016 %               h - omit low pass filter at 5.5 kHz
0017 %               d - give outputs in dB rather than power
0018 %               n - output a normalized speech signal as the first argument
0019 %               N - output a normalized filtered speech signal as the first argument
0020 %               l - give both active and long-term power levels
0021 %               a - include A-weighting filter
0022 %               i - include ITU-R-BS.468/ITU-T-J.16 weighting filter
0023 %
0024 %Outputs:
0025 %    If the "n" option is specified, a speech signal normalized to 0dB will be given as
0026 %    the first output followed by the other outputs.
0027 %        LEV    gives the speech level in units of power (or dB if mode='d')
0028 %               if mode='l' is specified, LEV is a row vector with the "long term
0029 %               level" as its second element (this is just the mean power)
0030 %        AF     is the activity factor (or duty cycle) in the range 0 to 1
0031 %        FSO    is a column vector of intermediate information that allows
0032 %               you to process a speech signal in chunks. Thus:
0033 %                       fso=fs; for i=1:inc:nsamp,[lev,fso]=activlev(sp(i:i+inc-1),fso,mode); end
0034 %               is equivalent to:
0035 %                       lev=activlev(sp(1:nsamp),fs,mode)
0036 %               but is much slower. The two methods will not give identical results
0037 %               because they will use slightly different thresholds.
0038 %        VAD    is a boolean vector the same length as sp that acts as an approximate voice activity detector
0039 
0040 %For completeness we list here the contents of the FSO structure:
0041 %
0042 %   ffs : sample frequency
0043 %   fmd : mode string
0044 %    nh : hangover time in samples
0045 %    ae : smoothing filter coefs
0046 %    bl : 200Hz HP filter numerator
0047 %    al : 200Hz HP filter denominator
0048 %    bh : 5.5kHz LP filter numerator
0049 %    ah : 5.5kHz LP filter denominator
0050 %    ze : smoothing filter state
0051 %    zl : 200Hz HP filter state
0052 %    zh : 5.5kHz LP filter state
0053 %    zx : hangover max filter state
0054 %  emax : maximum envelope exponent + 1
0055 %   ssq : signal sum of squares
0056 %    ns : number of signal samples
0057 %    ss : sum of speech samples (not actually used here)
0058 %    kc : cumulative occupancy counts
0059 %    aw : weighting filter denominator
0060 %    bw : weighting filter numerator
0061 %    zw : weighting filter state
0062 
0063 %      Copyright (C) Mike Brookes 2008-2012
0064 %      Version: $Id: activlev.m 4795 2014-07-09 12:27:53Z dmb $
0065 %
0066 %   VOICEBOX is a MATLAB toolbox for speech processing.
0067 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
0068 %
0069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0070 %   This program is free software; you can redistribute it and/or modify
0071 %   it under the terms of the GNU General Public License as published by
0072 %   the Free Software Foundation; either version 2 of the License, or
0073 %   (at your option) any later version.
0074 %
0075 %   This program is distributed in the hope that it will be useful,
0076 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0077 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0078 %   GNU General Public License for more details.
0079 %
0080 %   You can obtain a copy of the GNU General Public License from
0081 %   http://www.gnu.org/copyleft/gpl.html or by writing to
0082 %   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
0083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0084 
0085 persistent nbin thresh c25zp c15zp e5zp
0086 if isempty(nbin)
0087     nbin=20;    % 60 dB range at 3dB per bin
0088     thresh=15.9;    % threshold in dB
0089     % High pass s-domain zeros and poles of filters with passband ripple<0.25dB, stopband<-50dB, w0=1
0090     %    w0=fzero(@ch2,0.5); [c2z,c2p,k]=cheby2(5,50,w0,'high','s');
0091     %    function v=ch2(w); [c2z,c2p,k]=cheby2(5,50,w,'high','s'); v= 20*log10(prod(abs(1i-c2z))/prod(abs(1i-c2p)))+0.25;
0092     c25zp=[0.37843443673309i 0.23388534441447i; -0.20640255179496+0.73942185906851i -0.54036889596392+0.45698784092898i];
0093     c25zp=[[0; -0.66793268833792] c25zp conj(c25zp)];
0094     %       [c1z,c1p,c1k] = cheby1(5,0.25,1,'high','s');
0095     c15zp=[-0.659002835294875+1.195798636925079i -0.123261821596263+0.947463030958881i];
0096     c15zp=[zeros(1,5); -2.288586431066945 c15zp conj(c15zp)];
0097     %      [ez,ep,ek] = ellip(5,0.25,50,1,'high','s')
0098     e5zp=[0.406667680649209i 0.613849362744881i; -0.538736390607201+1.130245082677107i -0.092723126159100+0.958193646330194i];
0099     e5zp=[[0; -1.964538608244084]  e5zp conj(e5zp)];
0100     %    w=linspace(0.2,2,100);
0101     %    figure(1); plot(w,20*log10(abs(freqs(real(poly(c15zp(1,:))),real(poly(c15zp(2,:))),w)))); title('Chebyshev 1');
0102     %    figure(2); plot(w,20*log10(abs(freqs(real(poly(c25zp(1,:))),real(poly(c25zp(2,:))),w)))); title('Chebyshev 2');
0103     %    figure(3); plot(w,20*log10(abs(freqs(real(poly(e5zp(1,:))),real(poly(e5zp(2,:))),w)))); title('Elliptic');
0104 end
0105 
0106 if ~isstruct(fs)                        % no state vector given
0107     if nargin<3
0108         mode=' ';
0109     end
0110     fso.ffs=fs;                           % sample frequency
0111     if any(mode=='r')                   % included for backward compatibility
0112         mode=['0h' mode];               % abolish both filters
0113     elseif fs<14000
0114         mode=['h' mode];               % abolish lowpass filter at low sample rates
0115     end
0116     fso.fmd=mode;                      % save mode flags
0117     ti=1/fs;
0118     g=exp(-ti/0.03);                    % pole position for envelope filter
0119     fso.ae=[1 -2*g g^2]/(1-g)^2;        % envelope filter coefficients (DC gain = 1)
0120     fso.ze=zeros(2,1);
0121     fso.nh=ceil(0.2/ti)+1;              % hangover time in samples
0122     fso.zx=-Inf;                        % initial value for maxfilt()
0123     fso.emax=-Inf;                      % maximum exponent
0124     fso.ns=0;
0125     fso.ssq=0;
0126     fso.ss=0;
0127     fso.kc=zeros(nbin,1);               % cumulative occupancy counts
0128     % s-plane zeros and poles of high pass 5'th order filter -0.25dB at w=1 and -50dB stopband
0129     if any(mode=='1')
0130         szp=c15zp;            % Chebyshev 1
0131     elseif any(mode=='e')
0132         szp=e5zp;             % Elliptic
0133     else
0134         szp=c25zp;            % Chebyshev 2
0135     end
0136     if all(mode~='0')
0137         if any(mode=='4')
0138             fl=40;               % 40 Hz cutoff
0139         else
0140             fl=200;              % 200 Hz cutoff
0141         end
0142         zl=2./(1-szp*tan(fl*pi/fs))-1;      % 200 Hz LF limit
0143         al=real(poly(zl(2,:)));              % high pass filter
0144         bl=real(poly(zl(1,:)));
0145         sw=1-2*rem(0:5,2).';
0146         fso.bl=bl*(al*sw)/(bl*sw);                  % scale to give HF gain of 1
0147         fso.al=al;
0148         fso.zl=zeros(5,1);                   % LF filter state
0149     end
0150     if all(mode~='h')
0151         zh=2./(szp/tan(5500*pi/fs)-1)+1;
0152         ah=real(poly(zh(2,:)));
0153         bh=real(poly(zh(1,:)));
0154         fso.bh=bh*sum(ah)/sum(bh);
0155         fso.ah=ah;
0156         fso.zh=zeros(5,1);
0157     end
0158     if any(mode=='a')
0159         [fso.bw fso.aw]=stdspectrum(2,'z',fs);
0160         fso.zw=zeros(length(fso.aw)-1,1);
0161     elseif any(mode=='i')
0162         [fso.bw fso.aw]=stdspectrum(8,'z',fs);
0163         fso.zw=zeros(length(fso.aw)-1,1);
0164     end
0165 else
0166     fso=fs;             % use existing structure
0167 end
0168 md=fso.fmd;
0169 ns=length(sp);
0170 if ns                       % process this speech chunk
0171     % apply the input filters to the speech
0172     if all(md~='0')
0173         [sq,fso.zl]=filter(fso.bl,fso.al,sp(:),fso.zl);     % highpass filter
0174     else
0175         sq=sp(:);
0176     end
0177     if all(md~='h')
0178         [sq,fso.zh]=filter(fso.bh,fso.ah,sq(:),fso.zh);     % lowpass filter
0179     end
0180     if any(md=='a') || any(md=='i')
0181         [sq,fso.zw]=filter(fso.bw,fso.aw,sq(:),fso.zw);     % weighting filter
0182     end
0183     fso.ns=fso.ns+ns;      % count the number of speech samples
0184     fso.ss=fso.ss+sum(sq);  % sum of speech samples
0185     fso.ssq=fso.ssq+sum(sq.*sq);    % sum of squared speech samples
0186     [s,fso.ze]=filter(1,fso.ae,abs(sq(:)),fso.ze);     % envelope filter
0187     [qf,qe]=log2(s.^2);         % take efficient log2 function, 2^qe is upper limit of bin
0188     qe(qf==0)=-Inf;           % fix zero values
0189     [qe,qk,fso.zx]=maxfilt(qe,1,fso.nh,1,fso.zx);       % apply the 0.2 second hangover
0190     oemax=fso.emax;
0191     fso.emax=max(oemax,max(qe)+1);
0192     if fso.emax==-Inf
0193         fso.kc(1)=fso.kc(1)+ns;
0194     else
0195         qe=min(fso.emax-qe,nbin);   % force in the range 1:nbin. Bin k has 2^(emax-k-1)<=s^2<=2^(emax-k)
0196         wqe=ones(length(qe),1);
0197         % below: could use kc=cumsum(accumarray(qe,wqe,nbin)) but unsure about backwards compatibility
0198         kc=cumsum(full(sparse(qe,wqe,wqe,nbin,1)));    % cumulative occupancy counts
0199         esh=fso.emax-oemax;         % amount to shift down previous bin counts
0200         if esh<nbin-1               % if any of the previous bins are worth keeping
0201             kc(esh+1:nbin-1)=kc(esh+1:nbin-1)+fso.kc(1:nbin-esh-1);
0202             kc(nbin)=kc(nbin)+sum(fso.kc(nbin-esh:nbin));
0203         else
0204             kc(nbin)=kc(nbin)+sum(fso.kc); % otherwise just add all old counts into the last (lowest) bin
0205         end
0206         fso.kc=kc;
0207     end
0208 end
0209 if fso.ns                       % now calculate the output values
0210     if fso.ssq>0
0211         aj=10*log10(fso.ssq*(fso.kc).^(-1));
0212         % equivalent to cj=20*log10(sqrt(2).^(fso.emax-(1:nbin)-1));
0213         cj=10*log10(2)*(fso.emax-(1:nbin)-1);   % lower limit of bin j in dB
0214         mj=aj'-cj-thresh;
0215         %  jj=find(mj*sign(mj(1))<=0); % Find threshold
0216         jj=find(mj(1:end-1)<0 &  mj(2:end)>=0,1); % find +ve transition through threshold
0217         if isempty(jj)  % if we never cross the threshold
0218             if mj(end)<=0 % if we end up below if
0219                 jj=length(mj)-1; % take the threshold to be the bottom of the last (lowest) bin
0220                 jf=1;
0221             else          % if we are always above it
0222                 jj=1;     % take the threshold to be the bottom of the first (highest) bin
0223                 jf=0;
0224             end
0225         else
0226             jf=1/(1-mj(jj+1)/mj(jj));   % fractional part of j using linear interpolation
0227         end
0228         lev=aj(jj)+jf*(aj(jj+1)-aj(jj)); % active level in decibels
0229         lp=10.^(lev/10);
0230         if any(md=='d')
0231             lev=[lev 10*log10(fso.ssq/fso.ns)];
0232         else
0233             lev=[lp fso.ssq/fso.ns];
0234         end
0235         af=fso.ssq/(fso.ns*lp);
0236     else
0237         af=0;
0238         if all(md~='d')
0239             lev=[0 0];
0240         else
0241             lev=[-200 -200];
0242         end
0243     end
0244     if all(md~='l')
0245         lev=lev(1);         % only output the first element of lev normally
0246     end
0247 end
0248 if nargout>3
0249     vad=maxfilt(s,1,fso.nh,1);
0250     vad=vad>(sqrt(lp)/10^(thresh/20));
0251 end
0252 if ~nargout
0253     vad=maxfilt(s,1,fso.nh,1);
0254     vad=vad>(sqrt(lp)/10^(thresh/20));
0255     levdb=10*log10(lp);
0256     clf;
0257     subplot(2,2,[1 2]);
0258     tax=(1:ns)/fso.ffs;
0259     plot(tax,sp,'-y',tax,s,'-r',tax,(vad>0)*sqrt(lp),'-b');
0260     xlabel('Time (s)');
0261     title(sprintf('Active Level = %.2g dB, Activity = %.0f%% (ITU-T P.56)',levdb,100*af));
0262     axisenlarge([-1 -1 -1.4 -1.05]);
0263     ylabel('Amplitude');
0264     legend('Signal','Smoothed envelope','VAD * Active-Level','Location','SouthEast');
0265     subplot(2,2,4);
0266     plot(cj,repmat(levdb,nbin,1),'k:',cj,aj(:),'-b',cj,cj,'-r',levdb-thresh*ones(1,2),[levdb-thresh levdb],'-r');
0267     xlabel('Threshold (dB)');
0268     ylabel('Active Level (dB)');
0269     legend('Active Level','Speech>Thresh','Threshold','Location','NorthWest');
0270     texthvc(levdb-thresh,levdb-0.5*thresh,sprintf('%.1f dB ',thresh),'rmr');
0271     axisenlarge([-1 -1.05]);
0272      ylim=get(gca,'ylim');
0273      set(gca,'ylim',[levdb-1.2*thresh max(ylim(2),levdb+1.9*thresh)]);
0274     kch=filter([1 -1],1,kc);
0275     subplot(2,2,3);
0276     bar(5*log10(2)+cj(end:-1:1),kch(end:-1:1)*100/kc(end));
0277     set(gca,'xlim',[cj(end) cj(1)+10*log10(2)]);
0278     ylim=get(gca,'ylim');
0279     hold on
0280     plot(lev([1 1]),ylim,'k:',lev([1 1])-thresh,ylim,'r:');
0281     hold off
0282     texthvc(lev(1),ylim(2),sprintf(' Act\n Lev'),'ltk');
0283      texthvc(lev(1)-thresh,ylim(2),sprintf('Threshold '),'rtr');
0284     xlabel('Frame power (dB)')
0285     ylabel('% frames');
0286 elseif any(md=='n') || any(md=='N')
0287     fsx=fso;
0288     fso=af;
0289     af=lev;
0290     if any(md=='n')
0291         sq=sp;
0292     end
0293     if fsx.ns>0 && fsx.ssq>0
0294         lev=sq/sqrt(lp);
0295     else
0296         lev=sq;
0297     end
0298 end

Generated on Thu 24-Jul-2014 09:49:39 by m2html © 2003