|
26 | 26 | % John M. O' Toole, University College Cork
|
27 | 27 | % Started: 07-04-2016
|
28 | 28 | %
|
29 |
| -% last update: Time-stamp: <2020-04-22 17:27:24 (otoolej)> |
| 29 | +% last update: Time-stamp: <2020-08-17 16:53:32 (otoolej)> |
30 | 30 | %-------------------------------------------------------------------------------
|
31 | 31 | function featx=spectral_features(x,Fs,feat_name,params_st)
|
32 | 32 | if(nargin<2), error('need 2 input arguments'); end
|
|
48 | 48 | total_freq_bands=params_st.total_freq_bands;
|
49 | 49 |
|
50 | 50 |
|
| 51 | +if(length(x) < (params_st.L_window * Fs)) |
| 52 | + warning('SPECTRAL features: signal length < window length; set shorter L_window'); |
| 53 | + warning('SPECTRAL features: not calculating spectral features.'); |
| 54 | + featx = NaN; |
| 55 | + return; |
| 56 | +end |
| 57 | + |
51 | 58 |
|
52 | 59 | switch feat_name
|
53 | 60 | case {'spectral_power','spectral_relative_power'}
|
54 |
| - %--------------------------------------------------------------------- |
55 |
| - % use periodogram to estimate spectral power |
56 |
| - %--------------------------------------------------------------------- |
57 |
| - params_st.method='periodogram'; |
58 |
| - [pxx,itotal_bandpass,f_scale,N,fp]=gen_spectrum(x,Fs,params_st,1); |
59 |
| - pxx=pxx.*Fs; |
60 |
| - |
61 |
| - Nh=length(pxx); |
62 |
| - |
63 |
| - if(DBplot) |
64 |
| - figure(1); clf; hold all; |
65 |
| - plot(fp,20*log10(pxx)); |
66 |
| - end |
67 |
| - |
68 |
| - if(strcmp(feat_name,'spectral_relative_power')) |
69 |
| - pxx_total=sum( pxx(itotal_bandpass) )/N; |
| 61 | + %--------------------------------------------------------------------- |
| 62 | + % use periodogram to estimate spectral power |
| 63 | + %--------------------------------------------------------------------- |
| 64 | + params_st.method='periodogram'; |
| 65 | + [pxx,itotal_bandpass,f_scale,N,fp]=gen_spectrum(x,Fs,params_st,1); |
| 66 | + pxx=pxx.*Fs; |
| 67 | + |
| 68 | + Nh=length(pxx); |
| 69 | + |
| 70 | + if(DBplot) |
| 71 | + figure(1); clf; hold all; |
| 72 | + plot(fp,20*log10(pxx)); |
| 73 | + end |
| 74 | + |
| 75 | + if(strcmp(feat_name,'spectral_relative_power')) |
| 76 | + pxx_total=sum( pxx(itotal_bandpass) )/N; |
| 77 | + else |
| 78 | + pxx_total=1; |
| 79 | + end |
| 80 | + |
| 81 | + spec_pow=NaN(1,size(freq_bands,1)); |
| 82 | + |
| 83 | + for p=1:size(freq_bands,1) |
| 84 | + if(p==1) |
| 85 | + istart=ceil(freq_bands(p,1)*f_scale); |
70 | 86 | else
|
71 |
| - pxx_total=1; |
| 87 | + istart=ibandpass(end)-1; |
72 | 88 | end
|
| 89 | + ibandpass=istart:floor(freq_bands(p,2)*f_scale); |
| 90 | + ibandpass=ibandpass+1; |
| 91 | + ibandpass(ibandpass<1)=1; ibandpass(ibandpass>Nh)=Nh; |
73 | 92 |
|
74 |
| - spec_pow=NaN(1,size(freq_bands,1)); |
75 |
| - |
76 |
| - for p=1:size(freq_bands,1) |
77 |
| - if(p==1) |
78 |
| - istart=ceil(freq_bands(p,1)*f_scale); |
79 |
| - else |
80 |
| - istart=ibandpass(end)-1; |
81 |
| - end |
82 |
| - ibandpass=istart:floor(freq_bands(p,2)*f_scale); |
83 |
| - ibandpass=ibandpass+1; |
84 |
| - ibandpass(ibandpass<1)=1; ibandpass(ibandpass>Nh)=Nh; |
85 |
| - |
86 |
| - spec_pow(p)=sum( pxx(ibandpass) )/(N*pxx_total); |
87 |
| - |
88 |
| - if(DBplot) |
89 |
| - line([fp(ibandpass(1)) fp(ibandpass(1))],ylim,'color','k'); |
90 |
| - line([fp(ibandpass(end)) fp(ibandpass(end))],ylim,'color','k'); |
91 |
| - end |
92 |
| - end |
93 |
| - featx=spec_pow; |
| 93 | + spec_pow(p)=sum( pxx(ibandpass) )/(N*pxx_total); |
94 | 94 |
|
| 95 | + if(DBplot) |
| 96 | + line([fp(ibandpass(1)) fp(ibandpass(1))],ylim,'color','k'); |
| 97 | + line([fp(ibandpass(end)) fp(ibandpass(end))],ylim,'color','k'); |
| 98 | + end |
| 99 | + end |
| 100 | + featx=spec_pow; |
| 101 | + |
95 | 102 |
|
96 | 103 | case 'spectral_flatness'
|
97 | 104 | %---------------------------------------------------------------------
|
|
225 | 232 |
|
226 | 233 |
|
227 | 234 |
|
| 235 | + |
| 236 | +end |
| 237 | + |
0 commit comments