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