Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/welch.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % WELCH Welch spectral estimation method. | |
2 % | |
3 % [pxx, f, info] = welch(x,type,pl) | |
4 % [pxx, f, info] = welch(x,y,type,pl) | |
5 % | |
6 % INPUTS: x - input analysis objects | |
7 % y - input analysis objects | |
8 % type - type of spectrum: | |
9 % 'psd' - compute Power Spectral Denstiy (PSD) | |
10 % 'ms' - compute Mean-square (Power) Spectrum (MS) | |
11 % 'cpsd' - compute cross-spectral density | |
12 % 'tfe' - estimate transfer function between inputs | |
13 % 'mscohere' - estimate magnitude-squared cross-coherence | |
14 % 'cohere' - estimate complex cross-coherence | |
15 % pl - input parameter list | |
16 % | |
17 % PARAMETERS: 'Win' - a specwin window object [default: Kaiser -200dB psll] | |
18 % 'Olap' - segment percent overlap [default: taken from window function] | |
19 % 'Nfft' - number of samples in each fft [default: length of input data] | |
20 % 'Scale' - one of | |
21 % 'ASD' - amplitude spectral density | |
22 % 'PSD' - power spectral density [default] | |
23 % 'AS' - amplitude spectrum | |
24 % 'PS' - power spectrum | |
25 % * applies only to spectrum 'Type' 'psd' | |
26 % 'Order' - order of segment detrending | |
27 % -1 - no detrending | |
28 % 0 - subtract mean [default] | |
29 % 1 - subtract linear fit | |
30 % N - subtract fit of polynomial, order N | |
31 % | |
32 % Copied directly from MATLAB and extended to do segment-wise detrending, | |
33 % compute transfer function variance and to take a plist input | |
34 % | |
35 % M Hewitson 08-05-08 | |
36 % | |
37 % $Id: welch.m,v 1.34 2011/03/14 16:06:05 mauro Exp $ | |
38 % | |
39 | |
40 % Author(s): P. Pacheco | |
41 % Copyright 1988-2006 The MathWorks, Inc. | |
42 % $Revision: 1.34 $ $Date: 2011/03/14 16:06:05 $ | |
43 | |
44 % References: | |
45 % [1] Petre Stoica and Randolph Moses, Introduction To Spectral | |
46 % Analysis, Prentice-Hall, 1997, pg. 15 | |
47 % [2] Monson Hayes, Statistical Digital Signal Processing and | |
48 % Modeling, John Wiley & Sons, 1996. | |
49 % [3] JS Bendat and AG Piersol, Engineering applications of correlation | |
50 % and spectral analysis, John Wiley & Sons, 1993. | |
51 | |
52 | |
53 % Compute the periodogram power spectrum of each segment and average always | |
54 % compute the whole power spectrum, we force Fs = 1 to get a PS not a PSD. | |
55 | |
56 function varargout = welch(varargin) | |
57 | |
58 if nargin == 3 | |
59 a = varargin{1}; | |
60 esttype = varargin{2}; | |
61 pl = varargin{3}; | |
62 x = a.data.y; | |
63 inunits = a.data.yunits; | |
64 else | |
65 a = varargin{1}; | |
66 b = varargin{2}; | |
67 esttype = varargin{3}; | |
68 pl = varargin{4}; | |
69 if a.data.fs ~= b.data.fs | |
70 error('### Two time-series have different sample rates.'); | |
71 end | |
72 inunits = b.data.yunits / a.data.yunits; | |
73 x = {a.data.y, b.data.y}; | |
74 end | |
75 | |
76 % Parse inputs | |
77 win = find(pl, 'Win'); | |
78 nfft = find(pl, 'Nfft'); | |
79 olap = find(pl, 'Olap')/100; | |
80 scale = find(pl, 'scale'); | |
81 Xolap = round(olap*nfft); | |
82 fs = a.data.fs; | |
83 order = find(pl, 'order'); | |
84 | |
85 [x,M,isreal_x,y,Ly,win,winName,winParam,noverlap,k,L,options] = ... | |
86 ao.welchparse(x,esttype,win.win, Xolap, nfft, fs); | |
87 | |
88 % Initialize | |
89 Sxx = zeros(options.nfft,1); | |
90 | |
91 % Frequency vector was specified, return and plot two-sided PSD | |
92 freqVectorSpecified = false; nrow = 1; | |
93 if length(options.nfft) > 1, | |
94 freqVectorSpecified = true; | |
95 [ncol,nrow] = size(options.nfft); | |
96 end | |
97 | |
98 % Compute the periodogram power spectrum of each segment and average always | |
99 % compute the whole power spectrum, we force Fs = 1 to get a PS not a PSD. | |
100 | |
101 % Initialize | |
102 Mnxx = 0; Mn2xx = 0; | |
103 if freqVectorSpecified, | |
104 Sxx = zeros(length(options.nfft),1); | |
105 else | |
106 Sxx = zeros(options.nfft,1); | |
107 end | |
108 range = options.range; | |
109 | |
110 LminusOverlap = L-noverlap; | |
111 xStart = 1:LminusOverlap:k*LminusOverlap; | |
112 xEnd = xStart+L-1; | |
113 switch lower(esttype) | |
114 case {'ms','psd'} | |
115 for ii = 1:k, | |
116 if order < 0 | |
117 seg = x(xStart(ii):xEnd(ii)); | |
118 else | |
119 [seg,coeffs] = ltpda_polyreg(x(xStart(ii):xEnd(ii)), order); | |
120 end | |
121 [Sxxk,w] = ao.computeperiodogram(seg,win,... | |
122 options.nfft,esttype,options.Fs); | |
123 % Welford's algorithm for updating mean and variance | |
124 if ii == 1 | |
125 Mnxx = Sxxk; | |
126 else | |
127 Qxx = Sxxk - Mnxx; | |
128 Mnxx = Mnxx + Qxx/ii; | |
129 Mn2xx = Mn2xx + Qxx.*(Sxxk - Mnxx); | |
130 end | |
131 end | |
132 Sxx = Mnxx; | |
133 if k == 1 | |
134 Svxx = []; | |
135 else | |
136 Svxx = Mn2xx/(k-1)/k; | |
137 end | |
138 case 'cpsd' | |
139 for ii = 1:k, | |
140 if order < 0 | |
141 xseg = x(xStart(ii):xEnd(ii)); | |
142 else | |
143 [xseg,coeffs] = ltpda_polyreg(x(xStart(ii):xEnd(ii)), order); | |
144 end | |
145 if order < 0 | |
146 yseg = y(xStart(ii):xEnd(ii)); | |
147 else | |
148 [yseg,coeffs] = ltpda_polyreg(y(xStart(ii):xEnd(ii)), order); | |
149 end | |
150 [Sxxk,w] = ao.computeperiodogram({xseg,... | |
151 yseg},win,options.nfft,esttype,options.Fs); | |
152 % Welford's algorithm to update mean and variance | |
153 Qxx = Sxxk - Mnxx; | |
154 Mnxx = Mnxx +Qxx/ii; | |
155 Mn2xx = Mn2xx + abs(Qxx.*conj(Sxxk - Mnxx)); | |
156 end | |
157 Sxx = Mnxx; | |
158 if k ==1 | |
159 Svxx = []; | |
160 else | |
161 Svxx = Mn2xx/(k-1)/k; | |
162 end | |
163 case 'tfe' | |
164 % compute transfer function | |
165 Sxy = zeros(options.nfft,1); % Initialize | |
166 Syy = zeros(options.nfft,1); % Initialize | |
167 for ii = 1:k, | |
168 if order < 0 | |
169 xseg = x(xStart(ii):xEnd(ii)); | |
170 else | |
171 [xseg,coeffs] = ltpda_polyreg(x(xStart(ii):xEnd(ii)), order); | |
172 end | |
173 if order < 0 | |
174 yseg = y(xStart(ii):xEnd(ii)); | |
175 else | |
176 [yseg,coeffs] = ltpda_polyreg(y(xStart(ii):xEnd(ii)), order); | |
177 end | |
178 [Sxxk,w] = ao.computeperiodogram(xseg,... | |
179 win,options.nfft,esttype,options.Fs); | |
180 [Sxyk,w] = ao.computeperiodogram({yseg,... | |
181 xseg},win,options.nfft,esttype,options.Fs); | |
182 [Syyk,w] = ao.computeperiodogram(yseg,... | |
183 win,options.nfft,esttype,options.Fs); | |
184 Sxx = Sxx + Sxxk; | |
185 Sxy = Sxy + Sxyk; | |
186 Syy = Syy + Syyk; | |
187 % don't need to be divided by k because only rations are used here | |
188 end | |
189 case {'mscohere','cohere'} | |
190 % Note: (Sxy1+Sxy2)/(Sxx1+Sxx2) ~= (Sxy1/Sxy2) + (Sxx1/Sxx2) | |
191 % ie, we can't push the computation of Cxy into computeperiodogram. | |
192 Sxy = zeros(options.nfft,1); % Initialize | |
193 Syy = zeros(options.nfft,1); % Initialize | |
194 for ii = 1:k, | |
195 if order < 0 | |
196 xseg = x(xStart(ii):xEnd(ii)); | |
197 else | |
198 [xseg,coeffs] = ltpda_polyreg(x(xStart(ii):xEnd(ii)), order); | |
199 end | |
200 if order < 0 | |
201 yseg = y(xStart(ii):xEnd(ii)); | |
202 else | |
203 [yseg,coeffs] = ltpda_polyreg(y(xStart(ii):xEnd(ii)), order); | |
204 end | |
205 [Sxxk,w] = ao.computeperiodogram(xseg,... | |
206 win,options.nfft,esttype,options.Fs); | |
207 [Syyk,w] = ao.computeperiodogram(yseg,... | |
208 win,options.nfft,esttype,options.Fs); | |
209 [Sxyk,w] = ao.computeperiodogram({xseg,... | |
210 yseg},win,options.nfft,esttype,options.Fs); | |
211 Sxx = Sxx + Sxxk; | |
212 Sxy = Sxy + Sxyk; | |
213 Syy = Syy + Syyk; | |
214 % don't need to be divided by k because only rations are used here | |
215 end | |
216 end | |
217 % Generate the freq vector directly in Hz to avoid roundoff errors due to | |
218 % conversions later. | |
219 if ~freqVectorSpecified, | |
220 w = psdfreqvec('npts',options.nfft, 'Fs',options.Fs); | |
221 else | |
222 if strcmpi(options.range,'onesided') | |
223 warning(generatemsgid('InconsistentRangeOption'),... | |
224 'Ignoring ''onesided'' option. When a frequency vector is specified, a ''twosided'' PSD is computed'); | |
225 end | |
226 options.range = 'twosided'; | |
227 end | |
228 | |
229 switch lower(esttype) | |
230 case {'psd','cpsd'} | |
231 % Compute the 1-sided or 2-sided PSD [Power/freq] or mean-square [Power]. | |
232 % Also, corresponding freq vector and freq units. | |
233 % Here we use our own 'computepsd' to scale correctly the variance | |
234 if k == 1 | |
235 [Pxx,w,units] = computepsd(Sxx,w,options.range,options.nfft,options.Fs,esttype); | |
236 P = Pxx; | |
237 dP = []; | |
238 else | |
239 [Pxx,Pvxx,w,units] = utils.math.computepsd(Sxx,Svxx,w,options.range,options.nfft,options.Fs,esttype); | |
240 P = Pxx; | |
241 % the 1/k factor should come after welchscale if we don't want to apply sqrt() to it. | |
242 % we correct for that here. It is only needed for 'asd','as' in | |
243 % psd/cpsd, the other cases go always through 'PSD'. | |
244 if (strcmpi(scale,'PSD') || strcmpi(scale,'PS')) | |
245 dP = Pvxx; | |
246 elseif (strcmpi(scale,'ASD') || strcmpi(scale,'AS')) | |
247 dP = Pvxx/k; | |
248 else | |
249 error('### Unknown scale') | |
250 end | |
251 end | |
252 case 'tfe' | |
253 % Compute the 1-sided or 2-sided PSD [Power/freq] or mean-square [Power]. | |
254 % Also, corresponding freq vector and freq units. | |
255 % In the Cross PSD, the frequency vector and xunits are not used. | |
256 [Pxx,w,units] = computepsd(Sxx,w,options.range,options.nfft,options.Fs,esttype); | |
257 [Pxy,w,units] = computepsd(Sxy,w,options.range,options.nfft,options.Fs,esttype); | |
258 [Pyy,w,units] = computepsd(Syy,w,options.range,options.nfft,options.Fs,esttype); | |
259 % mean and std | |
260 P = Pxy ./ Pxx; % Txy | |
261 if k == 1 | |
262 dP =[]; | |
263 else | |
264 dP = (k/(k-1)^2)*(Pyy./Pxx).*(1 - (abs(Pxy).^2)./(Pxx.*Pyy)); | |
265 end | |
266 case 'mscohere' | |
267 % Magnitude Square Coherence estimate. | |
268 % Auto PSD for 2nd input vector. The freq vector & xunits are not | |
269 % used. | |
270 [Pxx,w,units] = computepsd(Sxx,w,options.range,options.nfft,options.Fs,esttype); | |
271 [Pxy,w,units] = computepsd(Sxy,w,options.range,options.nfft,options.Fs,esttype); | |
272 [Pyy,w,units] = computepsd(Syy,w,options.range,options.nfft,options.Fs,esttype); | |
273 % mean and std | |
274 P = (abs(Pxy).^2)./(Pxx.*Pyy); % Magnitude-squared coherence | |
275 dP = (2*P/k).*(1-P).^2; | |
276 case 'cohere' | |
277 % Complex Coherence estimate. | |
278 % Auto PSD for 2nd input vector. The freq vector & xunits are not | |
279 % used. | |
280 [Pxx,w,units] = computepsd(Sxx,w,options.range,options.nfft,options.Fs,esttype); | |
281 [Pxy,w,units] = computepsd(Sxy,w,options.range,options.nfft,options.Fs,esttype); | |
282 [Pyy,w,units] = computepsd(Syy,w,options.range,options.nfft,options.Fs,esttype); | |
283 P = Pxy./sqrt(Pxx.*Pyy); % Complex coherence | |
284 dP = (2*abs(P)/k).*(1-abs(P)).^2; | |
285 end | |
286 % end | |
287 | |
288 % Scale to required units | |
289 [P, dP, info] = ao.welchscale(P, dP, win, fs, scale, inunits); | |
290 info.navs = k; | |
291 | |
292 if k ==1 | |
293 dev = []; | |
294 else | |
295 dev = sqrt(dP); | |
296 end | |
297 | |
298 varargout = {P, w, info, dev}; | |
299 | |
300 end | |
301 |