Mercurial > hg > ishara
annotate dsp/powspec.m @ 32:c3b0cd708782
Imported core dsp tools.
author | samer |
---|---|
date | Sun, 20 Jan 2013 13:48:47 +0000 |
parents | |
children |
rev | line source |
---|---|
samer@32 | 1 function [S,F]=powspec(X,N) |
samer@32 | 2 % powspec - power spectra of columns of x |
samer@32 | 3 % |
samer@32 | 4 % powspec :: |
samer@32 | 5 % [[K,L]] ~'sequence of L K-point signal frames', |
samer@32 | 6 % N:natural ~'optional size of FFT (defaults to K)' |
samer@32 | 7 % -> [[M,L]] ~'sequence of L M-point power spectra, M=dftbins(N)', |
samer@32 | 8 % [[M]] ~'normalised centre frequencies for each spectral bin'. |
samer@32 | 9 % |
samer@32 | 10 % This function is careful to account for the energy in the original |
samer@32 | 11 % signal properly: we return the total energy in the signal in each |
samer@32 | 12 % band of the DFT such that sum(powspec(x))/4 = sum(x.^2). |
samer@32 | 13 % Don't ask about the 4 - I don't know why it's in there. It just is |
samer@32 | 14 % NB. this is not strictly the power spectral *density* because |
samer@32 | 15 % when N is even, first and last bands, the singleton bands, are |
samer@32 | 16 % half as wide as the others. |
samer@32 | 17 |
samer@32 | 18 if nargin<2, N=size(X,1); end |
samer@32 | 19 |
samer@32 | 20 select=1:(1+floor(N/2)); % non-redundant bands |
samer@32 | 21 singles=1:ceil(N/2):select(end); % bands which don't come in pairs |
samer@32 | 22 Y=fft(X,N); |
samer@32 | 23 S=(8/N)*abs(Y(select,:)).^2; % power in selected bands |
samer@32 | 24 S(singles,:)=S(singles,:)/2; % undo double counting of singleton bands |
samer@32 | 25 if nargout>1, F=(select-1)/N; end % normalised centre frequencies |
samer@32 | 26 |