0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 % COMPUTEPERIODOGRAM Periodogram spectral estimation.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 % This function is used to calculate the Power Spectrum Sxx, and the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 % Cross Power Spectrum Sxy.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 % Sxx = COMPUTEPERIODOGRAM(X,WIN,NFFT) where x is a vector returns the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % Power Spectrum over the whole Nyquist interval, [0, 2pi).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % Sxy = COMPUTEPERIODOGRAM({X,Y},WIN,NFFT) returns the Cross Power
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % Spectrum over the whole Nyquist interval, [0, 2pi).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % Inputs:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 % X - Signal vector or a cell array of two elements containing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % two signal vectors.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 % WIN - Window
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % NFFT - Number of frequency points (FFT) or vector of
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 % frequencies at whicch periodogram is desired (Goertzel)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 % WINCOMPFLAG - A string indicating the type of window compensation to
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 % be done. The choices are:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % 'ms' - compensate for Mean-square (Power) Spectrum;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 % maintain the correct power peak heights.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 % 'psd' - compensate for Power Spectral Denstiy (PSD);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % maintain correct area under the PSD curve.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 % Output:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % Sxx - Power spectrum [Power] over the whole Nyquist interval.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 % or
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % Sxy - Cross power spectrum [Power] over the whole Nyquist
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 % interval.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % A direct copy of MATLAB's function for LTPDA
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 % M Hewitson 08-05-08
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 % $Id: computeperiodogram.m,v 1.2 2008/08/01 13:19:42 ingo Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 % Author(s): P. Pacheco
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 % Copyright 1988-2006 The MathWorks, Inc.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 % $Revision: 1.2 $ $Date: 2008/08/01 13:19:42 $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 function [P,f] = computeperiodogram(x,win,nfft,esttype,varargin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 error(nargchk(3,5,nargin,'struct'));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 if nargin < 4,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 esttype = 'psd'; % Default, compenstate for window's power.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 if nargin < 5 || isempty(varargin{1}),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 Fs = 2*pi;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 Fs = varargin{1};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 % Validate inputs and convert row vectors to column vectors.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 [x,Lx,y,is2sig,win,errid,errmsg] = validateinputs(x,win,nfft);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 if ~isempty(errmsg), error(errid, errmsg); end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 % Determine if FFT or Goertzel
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 freqVectorSpecified = false;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 if (length(nfft)> 1), freqVectorSpecified = true; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 % Window the data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 xw = x.*win;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 if is2sig, yw = y.*win; end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 % Evaluate the window normalization constant. A 1/N factor has been
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 % omitted since it will cancel below.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 if strcmpi(esttype,'ms'),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 % The window is convolved with every power spectrum peak, therefore
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 % compensate for the DC value squared to obtain correct peak heights.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 U = sum(win)^2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 U = win'*win; % compensates for the power of the window.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 % Compute the periodogram power spectrum [Power] estimate
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 % A 1/N factor has been omitted since it cancels
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 [Xx,f] = ao.computeDFT(xw,nfft,Fs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 if is2sig, [Yy,f] = ao.computeDFT(yw,nfft,Fs); end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 P = Xx.*conj(Xx)/U; % Auto spectrum.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83 if is2sig,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84 P = Xx.*conj(Yy)/U; % Cross spectrum.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90 function [x,Lx,y,is2sig,win,errid,errmsg] = validateinputs(x,win,nfft)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 % Validate the inputs to computexperiodogram and convert the input signal
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 % vectors into column vectors.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 errid = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 errmsg = ''; % in case of early return.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 % Set defaults and convert to row vectors to columns.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 y = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 Ly = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 is2sig= false;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 win = win(:);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102 Lw = length(win);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 % Determine if one or two signal vectors was specified.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 Lx = length(x);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 if iscell(x),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 if length(x) > 1,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108 y = x{2};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 y = y(:);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 is2sig = true;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112 x = x{1};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 Lx = length(x);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115 x = x(:);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 if is2sig,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118 Ly = length(y);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 if Lx ~= Ly,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 errid = generatemsgid('invalidInputSignalLength');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121 errmsg = 'The length of the two input vectors must be equal to calculate the cross spectral density.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126 if Lx ~= Lw,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127 errid = generatemsgid('invalidWindow');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 errmsg = 'The WINDOW must be a vector of the same length as the signal.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134
|