0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 % VDFIT: Fit discrete models to frequency responses
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 % DESCRIPTION:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 % Fits a discrete model to a frequency response using relaxed z-domain
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % vector fitting algorithm [1 - 3]. The function is able to fit more
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 % than one frequency response per time. In case that more than one
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % frequency response is passed as input, they are fitted with a set of
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % common poles. Model functions are expanded in partial fractions:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % r1 rN
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 % f(z) = ----------- + ... + ----------- + d
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % 1-p1*z^{-1} 1-pN*z^{-1}
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % CALL:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 % [res,poles,dterm,mresp,rdl,mse] = vdfit(y,f,poles,weight,fitin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % INPUTS:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 % - y: Is a vector with the frequency response data.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % - f: Is the frequency vector in Hz.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 % - poles: are a set of starting poles.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 % - weight: are a set of weights used in the fitting procedure.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % - fitin: is a struct containing fitting options and parameters. fitin
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 % fields are:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % - fitin.stable = 0; fit without forcing poles to be stable.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 % - fitin.stable = 1; force poles to be stable flipping unstable
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % poles in the unit circle. z -> 1/z*.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % - fitin.dterm = 0; fit with d = 0.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 % - fitin.dterm = 1; fit with d different from 0.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 % - fitin.fs = fs; input the sampling frequency in Hz (default value
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 % is 1 Hz).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 % - fitin.polt = 0; fit without plotting results.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 % - fitin.plot = 1; plot fit results in loglog scale.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 % - fitin.plot = 2; plot fit results in semilogx scale.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 % - fitin.plot = 3; plot fit results in semilogy scale.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 % - fitin.plot = 4; plot fit results in linear xy scale.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 % - fitin.ploth = #; a plot handle to define the figure target for
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 % plotting. Default: [1]
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 % OUTPUT:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 % - res: vector or residues.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 % - poles: vector of poles.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 % - dterm: direct term d.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 % - mresp: frequency response of the fitted model
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 % - rdl: residuals y - mresp
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 % - mse: normalized men squared error
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 % EXAMPLES:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 % - Fit on a single transfer function:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 % INPUT
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 % y is a (Nx1) or (1xN) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 % f is a (Nx1) or (1xN) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 % poles is a (Npx1) or (1xNp) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 % weight is a (Nx1) or (1xN) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 % OUTPUT
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 % res is a (Npx1) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 % poles is a (Npx1) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 % dterm is a constant
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 % mresp is a (Nx1) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 % rdl is a (Nx1) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 % mse is a constant
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 % - Fit Nt transfer function at the same time:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 % INPUT
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 % y is a (NxNt) or (NtxN) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 % f is a (Nx1) or (1xN) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 % poles is a (Npx1) or (1xNp) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 % weight is a (NxNt) or (NtxN) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 % OUTPUT
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 % res is a (NpxNt) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 % poles is a (Npx1) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 % dterm is a (1xNt) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 % mresp is a (NxNt) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 % rdl is a (NxNt) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83 % mse is a (1xNt) vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 % REFERENCES:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 % [1] B. Gustavsen and A. Semlyen, "Rational approximation of frequency
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 % domain responses by Vector Fitting", IEEE Trans. Power Delivery
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 % vol. 14, no. 3, pp. 1052-1061, July 1999.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90 % [2] B. Gustavsen, "Improving the Pole Relocating Properties of Vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 % Fitting", IEEE Trans. Power Delivery vol. 21, no. 3, pp.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 % 1587-1592, July 2006.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93 % [3] Y. S. Mekonnen and J. E. Schutt-Aine, "Fast broadband
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 % macromodeling technique of sampled time/frequency data using
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 % z-domain vector-fitting method", Electronic Components and
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 % Technology Conference, 2008. ECTC 2008. 58th 27-30 May 2008 pp.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 % 1231 - 1235.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 % HISTORY: 12-09-2008 L Ferraioli
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102 % Creation
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 % VERSION: '$Id: vdfit.m,v 1.12 2009/08/06 09:57:37 miquel Exp $';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 function [res,poles,dterm,mresp,rdl,mse] = vdfit(y,f,poles,weight,fitin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 warning off all
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111 % Collecting inputs
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 % Default input struct
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 defaultparams = struct('stable',0, 'dterm',0, 'fs',1, 'plot',0, 'ploth',1,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115 'regout',-1, 'idsamp',1e3, 'idamp', 1e-3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 names = {'stable','dterm','fs','plot','ploth','regout','idsamp','idamp'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 % collecting input and default params
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 if ~isempty(fitin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121 for jj=1:length(names)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 if isfield(fitin, names(jj)) && ~isempty(fitin.(names{1,jj}))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 defaultparams.(names{1,jj}) = fitin.(names{1,jj});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 stab = defaultparams.stable; % Enforce pole stability is is 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129 dt = defaultparams.dterm; % 1 to fit with direct term
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 fs = defaultparams.fs; % sampling frequency
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131 plotting = defaultparams.plot; % set to 1 if plotting is required
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132 plth = defaultparams.ploth; % set the figure target
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133 regout = defaultparams.regout; % set the strategy for complex plane fitting
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134 idsamp = defaultparams.idsamp; % number of samples to define impulse response
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
135 idamp = defaultparams.idamp; % maximum aplitude of impulse response adimtted at idsamp
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
136
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 % Inputs in row vectors
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140 [a,b] = size(y);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
141 if a > b % shifting to row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 y = y.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
144
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
145 [a,b] = size(f);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 if a > b % shifting to row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147 f = f.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
149
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
150 [a,b] = size(poles);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
151 if a > b % shifting to row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
152 poles = poles.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
153 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
154
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
155 clear w
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
156 w = weight;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
157 [a,b] = size(w);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
158 if a > b % shifting to row
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
159 w = w.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
160 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
161
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
162 N = length(poles); % Model order
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
163
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
164 if dt
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
165 dl = 1; % Fit with direct term
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
166 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
167 dl = 0; % Fit without direct term
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
168 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
169
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
170 % definition of z
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
171 z = cos(2.*pi.*f./fs)+1i.*sin(2.*pi.*f./fs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
172
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
173 Nz = length(z);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
174
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
175 [Nc,Ny] = size(y);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
176 if Ny ~= Nz
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
177 error('### The number of data points is different from the number of frequency points.')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
178 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
179
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
180 %Tolerances used by relaxed version of vector fitting
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
181 TOLlow=1e-8;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
182 TOLhigh=1e8;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
183
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
185 % Normalizing y
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
186
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
187 for nn = 1:Nc
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
188 y(nn,:) = y(nn,:)./z;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
189 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
190
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
192 % Marking complex and real poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
193
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
194 % cindex = 1; pole is complex, next conjugate pole is marked with cindex
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
195 % = 2. cindex = 0; pole is real
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
196 cindex=zeros(1,N);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
197 for m=1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
198 if imag(poles(m))~=0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
199 if m==1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
200 cindex(m)=1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
201 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
202 if cindex(m-1)==0 || cindex(m-1)==2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
203 cindex(m)=1; cindex(m+1)=2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
204 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
205 cindex(m)=2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
206 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
207 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
208 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
209 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
210
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
212 % Augmented problem
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
214
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
215 % Matrix initialinzation
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
216 BA = zeros(Nc*Nz+1,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
217 Ak=zeros(Nz,N+1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
218 AA=zeros(Nz*Nc+1,(N+dl)*Nc+N+1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
219 nf = zeros(1,Nc*(N+dl)+N+1); % Normalization factor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
220
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
221 % Defining Ak
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
222 for jj = 1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
223 if cindex(jj) == 1 % conjugate complex couple of poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
224 Ak(:,jj) = 1./(z-poles(jj)) + 1./(z-conj(poles(jj)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
225 Ak(:,jj+1) = 1i./(z-poles(jj)) - 1i./(z-conj(poles(jj)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
226 elseif cindex(jj) == 0 % real pole
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
227 Ak(:,jj) = 1./(z-poles(jj));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
228 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
229 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
230
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
231 Ak(1:Nz,N+1) = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
232
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
233 % Scaling factor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
234 sc = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
235 for mm = 1:Nc
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
236 sc = sc + (norm(w(mm,:).*y(mm,:)))^2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
237 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
238 sc=sqrt(sc)/Nz;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
239
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
240 for nn = 1:Nc
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
241
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
242 wg = w(nn,:).'; % Weights
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
243
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
244 ida=(nn-1)*Nz+1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
245 idb=nn*Nz;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
246 idc=(nn-1)*(N+dl)+1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
247
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
248 for mm =1:N+dl % Diagonal blocks
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
249 AA(ida:idb,idc-1+mm) = wg.*Ak(1:Nz,mm);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
250 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
251 for mm =1:N+1 % Last right blocks
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
252 AA(ida:idb,Nc*(N+dl)+mm) = -wg.*(Ak(1:Nz,mm).*y(nn,1:Nz).');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
253 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
254
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
255 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
256
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
257 % setting the last row of AA and BA for the relaxion condition
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
258 for qq = 1:N+1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
259 AA(Nc*Nz+1,Nc*(N+dl)+qq) = real(sc*sum(Ak(:,qq)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
260 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
261
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
262 AA = [real(AA);imag(AA)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
263
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
264 % Last element of the solution vector
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
265 BA(Nc*Nz+1) = Nz*sc;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
266
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
267 xBA = real(BA);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
268 xxBA = imag(BA);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
269
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
270 Nrow = Nz*Nc+1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
271
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
272 BA = zeros(2*Nrow,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
273
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
274 BA(1:Nrow,1) = xBA;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
275 BA(Nrow+1:2*Nrow,1) = xxBA;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
276
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
277 % Normalization factor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
278 % nf = zeros(2*N+dl+1,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
279 for pp = 1:length(AA(1,:))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
280 nf(pp) = norm(AA(:,pp),2); % Euclidean norm
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
281 AA(:,pp) = AA(:,pp)./nf(pp); % Normalization
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
282 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
283
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
285 % Solving augmented problem
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
286
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
287 % XA = pinv(AA)*BA;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
288 % XA = inv((AA.')*AA)*(AA.')*BA;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
289
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
290 % XA = AA.'*AA\AA.'*BA;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
291
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
292 XA = AA\BA;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
293
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
294 XA = XA./nf.'; % renormalization
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
295
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
297 % Checking the tolerance
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
298
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
299 if abs(XA(end))<TOLlow || abs(XA(end))>TOLhigh
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
300
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
301 if XA(end)==0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
302 Dnew=1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
303 elseif abs(XA(end))<TOLlow
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
304 Dnew=sign(XA(end))*TOLlow;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
305 elseif abs(XA(end))>TOLhigh
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
306 Dnew=sign(XA(end))*TOLhigh;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
307 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
308
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
309 for pp = 1:length(AA(1,:))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
310 AA(:,pp) = AA(:,pp).*nf(pp); %removing previous scaling
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
311 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
312
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
313 ind=length(AA(:,1))/2; %index to additional row related to relaxation
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
314
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
315 AA(ind,:)=[]; % removing relaxation term
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
316
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
317 BA=-Dnew*AA(:,end); %new right side
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
318
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
319 AA(:,end)=[];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
320
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
321 nf(end)=[];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
322
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
323 for pp = 1:length(AA(1,:))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
324 nf(pp) = norm(AA(:,pp),2); % Euclidean norm
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
325 AA(:,pp) = AA(:,pp)./nf(pp); % Normalization
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
326 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
327
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
328 % XA=(AA.'*AA)\(AA.'*BA); % using normal equation
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
329
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
330 XA=AA\BA;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
331
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
332 XA = XA./nf.'; % renormalization
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
333
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
334 XA=[XA;Dnew];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
335
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
336 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
337
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
339 % Finding zeros of sigma
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
340
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
341 lsr = XA((N+dl)*Nc+1:(N+dl)*Nc+N,1); % collect the least square results
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
342
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
343 D = XA(end); % direct term of sigma
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
344
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
345 CPOLES = diag(poles);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
346 B = ones(N,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
347 C = lsr.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
348
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
349 for kk = 1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
350 if cindex(kk) == 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
351 CPOLES(kk,kk)=real(poles(kk));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
352 CPOLES(kk,kk+1)=imag(poles(kk));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
353 CPOLES(kk+1,kk)=-1*imag(poles(kk));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
354 CPOLES(kk+1,kk+1)=real(poles(kk));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
355 B(kk,1) = 2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
356 B(kk+1,1) = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
357 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
358 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
359
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
360 H = CPOLES-B*C/D;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
361
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
362 szeros=eig(H);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
363
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
365 % Exclude a region of the complex plane
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
366 switch regout
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
367 case -1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
368 % do nothing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
369 case 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
370 % do nothing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
371 case 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
372 % set the maximum admitted value for stable poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
373 target_pole = (idamp)^(1/idsamp);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
374 % get stable poles outside the fixed limit
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
375 uptgr = ((abs(szeros) > target_pole) & (abs(szeros) <= 1) & (imag(szeros)==0));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
376 uptgi = ((abs(szeros) > target_pole) & (abs(szeros) <= 1) & (imag(szeros)~=0));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
377 % get unstable polse smaller than minimum value
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
378 lwtgr = ((abs(szeros) < 1/target_pole) & (abs(szeros) > 1) & (imag(szeros)==0));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
379 lwtgi = ((abs(szeros) < 1/target_pole) & (abs(szeros) > 1) & (imag(szeros)~=0));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
380 % get the maximum shift needed
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
381 ushiftr = max(abs(abs(szeros(uptgr))-target_pole));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
382 ushifti = max(abs(abs(szeros(uptgi))-target_pole));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
383 lshiftr = max(abs(abs(szeros(lwtgr))-1/target_pole));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
384 lshifti = max(abs(abs(szeros(lwtgi))-1/target_pole));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
385 % shifting inside
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
386 szeros(uptgr) = (abs(szeros(uptgr))-ushiftr).*sign(szeros(uptgr));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
387 szeros(uptgi) = (abs(szeros(uptgi))-ushifti).*exp(1i.*angle(szeros(uptgi)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
388 % shifting outside
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
389 szeros(lwtgr) = (abs(szeros(lwtgr))+lshiftr).*sign(szeros(lwtgr));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
390 szeros(lwtgi) = (abs(szeros(lwtgi))+lshifti).*exp(1i.*angle(szeros(lwtgi)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
391 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
392
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
393
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
395 % Ruling out unstable poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
396
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
397 % This option force the poles of f to stay inside the unit circle
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
398 if stab
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
399 unst = abs(szeros) > 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
400 szeros(unst) = 1./conj(szeros(unst));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
401 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
402 szeros = sort(szeros);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
403 N = length(szeros);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
404
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
406 % Separating complex poles from real poles and ordering
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
407
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
408 rnpoles = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
409 inpoles = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
410 for tt = 1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
411 if imag(szeros(tt)) == 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
412 % collecting real poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
413 rnpoles = [rnpoles; szeros(tt)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
414 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
415 % collecting complex poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
416 inpoles = [inpoles; szeros(tt)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
417 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
418 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
419
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
420 % Sorting complex poles in order to have them in the expected order a+jb
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
421 % and a-jb a>0 b>0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
422 inpoles = sort(inpoles);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
423 npoles = [rnpoles;inpoles];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
424 npoles = npoles - 2.*1i.*imag(npoles);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
425
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
427 % Marking complex and real poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
428
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
429 cindex=zeros(N,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
430 for m=1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
431 if imag(npoles(m))~=0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
432 if m==1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
433 cindex(m)=1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
434 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
435 if cindex(m-1)==0 || cindex(m-1)==2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
436 cindex(m)=1; cindex(m+1)=2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
437 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
438 cindex(m)=2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
439 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
440 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
441 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
442 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
443
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
445 % Direct problem
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
447
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
448 % Matrix initialinzation
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
449 nB(1:Nz,1:Nc) = real(w.*y).';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
450 nB(Nz+1:2*Nz,1:Nc) = imag(w.*y).';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
451
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
452 B = zeros(2*Nz,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
453 nAD = zeros(Nz,N+dl);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
454 AD = zeros(2*Nz,N+dl);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
455 Ak = zeros(Nz,N+dl);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
456
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
457 for jj = 1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
458 if cindex(jj) == 1 % conjugate complex couple of poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
459 Ak(:,jj) = 1./(z-npoles(jj)) + 1./(z-npoles(jj+1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
460 Ak(:,jj+1) = 1i./(z-npoles(jj)) - 1i./(z-npoles(jj+1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
461 elseif cindex(jj) == 0 % real pole
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
462 Ak(:,jj) = 1./(z-npoles(jj));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
463 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
464 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
465
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
466 if dt
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
467 % Ak(1:Nz,N+dl) = ones(Nz,1); % considering the direct term
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
468 Ak(1:Nz,N+dl) = 1./z;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
469 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
470
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
471 XX = zeros(Nc,N+dl);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
472 for nn = 1:Nc
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
473
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
474 % Defining AD
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
475 for m=1:N+dl
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
476 nAD(1:Nz,m) = w(nn,:).'.*Ak(1:Nz,m);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
477 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
478
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
479 B(1:2*Nz,1) = nB(1:2*Nz,nn);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
480
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
481 AD(1:Nz,:) = real(nAD);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
482 AD(Nz+1:2*Nz,:) = imag(nAD);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
483
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
484 % Normalization factor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
485 nf = zeros(N+dl,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
486 for pp = 1:N+dl
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
487 nf(pp,1) = norm(AD(:,pp),2); % Euclidean norm
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
488 AD(:,pp) = AD(:,pp)./nf(pp,1); % Normalization
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
489 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
490
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
491 % Solving direct problem
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
492
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
493 % XD = inv((AD.')*AD)*(AD.')*B;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
494 % XD = AD.'*AD\AD.'*B;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
495 % XD = pinv(AD)*B;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
496 XD = AD\B;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
497
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
498 XD = XD./nf; % Renormalization
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
499 XX(nn,1:N) = XD(1:N).';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
500
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
501 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
502
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
504 % Final residues and poles of f
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
505
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
506 lsr = XX(:,1:N);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
507
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
508 res = zeros(N,Nc);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
509 % Real poles have real residues, complex poles have comples residues
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
510 for nn = 1:Nc
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
511 for tt = 1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
512 if cindex(tt) == 1 % conjugate complex couple of poles
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
513 res(tt,nn) = lsr(nn,tt)+1i*lsr(nn,tt+1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
514 res(tt+1,nn) = lsr(nn,tt)-1i*lsr(nn,tt+1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
515 elseif cindex(tt) == 0 % real pole
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
516 res(tt,nn) = lsr(nn,tt);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
517 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
518 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
519 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
520
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
521 poles = npoles;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
522
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
523 if dt
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
524 dterm = XX(:,N+dl).';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
525 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
526 dterm = zeros(1,Nc);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
527 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
528
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
530 % Calculating responses and residuals
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
531
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
532 mresp = zeros(Nz,Nc);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
533 rdl = zeros(Nz,Nc);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
534 yr = zeros(Nz,Nc);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
535 mse = zeros(1,Nc);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
536
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
537 for nn = 1:Nc
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
538 % freq resp of the fit model
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
539 r = res(:,nn);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
540 p = poles;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
541 d = dterm(:,nn);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
542
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
543 Nf = length(f);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
544 N = length(p);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
545
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
546 % Defining normalized frequencies
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
547 fn = f./fs;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
548
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
549 rsp = zeros(Nf,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
550 indx = 0:length(d)-1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
551 for ii = 1:Nf
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
552 for jj = 1:N
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
553 rsptemp = exp(1i*2*pi*fn(ii))*r(jj)/(exp(1i*2*pi*fn(ii))-p(jj));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
554 rsp(ii) = rsp(ii) + rsptemp;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
555 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
556 % Direct terms response
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
557 rsp(ii) = rsp(ii) + sum(((exp((1i*2*pi*f(ii))*ones(length(d),1))).^(-1.*indx)).*d);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
558 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
559
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
560 % Model response
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
561 mresp(:,nn) = rsp;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
562
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
563 % Residual
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
564 yr(:,nn) = (y(nn,:).*z).';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
565 rdl(:,nn) = yr(:,nn) - rsp;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
566
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
567 % RMS error
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
568 % rmse(:,nn) = sqrt(sum((abs(rdl(:,nn)./yr(:,nn)).^2))/(Nf-N));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
569
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
570 % Chi Square or mean squared error
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
571 % Note that this error is normalized to the input data in order to
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
572 % comparable between different sets of data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
573 mse(:,nn) = sum((rdl(:,nn)./yr(:,nn)).*conj((rdl(:,nn)./yr(:,nn))))/(Nf-N);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
574
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
575 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
576
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
578 % Plotting response
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
579 nf = f./fs;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
580
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
581 switch plotting
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
582
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
583 case 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
584 % No plot
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
585
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
586 case 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
587 % LogLog plot for absolute value
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
588 figure(plth)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
589 subplot(2,1,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
590 p1 = loglog(nf,abs(yr),'k');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
591 hold on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
592 p2 = loglog(nf,abs(mresp),'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
593 p3 = loglog(nf,abs(rdl),'b');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
594 xlabel('Normalized Frequency [f/fs]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
595 ylabel('Amplitude')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
596 legend([p1(1) p2(1) p3(1)],'Original', 'VDFIT','Residual')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
597 hold off
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
598
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
599 subplot(2,1,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
600 p4 = semilogx(nf,(180/pi).*unwrap(angle(yr)),'k');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
601 hold on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
602 p5 = semilogx(nf,(180/pi).*unwrap(angle(mresp)),'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
603 xlabel('Normalized Frequency [f/fs]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
604 ylabel('Phase [Deg]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
605 legend([p4(1) p5(1)],'Original', 'VDFIT')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
606 hold off
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
607
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
608 case 2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
609 % Semilogx plot for absolute value
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
610 figure(plth)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
611 subplot(2,1,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
612 p1 = semilogx(nf,abs(yr),'k');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
613 hold on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
614 p2 = semilogx(nf,abs(mresp),'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
615 p3 = semilogx(nf,abs(rdl),'b');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
616 xlabel('Normalized Frequency [f/fs]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
617 ylabel('Amplitude')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
618 legend([p1(1) p2(1) p3(1)],'Original', 'VDFIT','Residual')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
619 hold off
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
620
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
621 subplot(2,1,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
622 p4 = semilogx(nf,(180/pi).*unwrap(angle(yr)),'k');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
623 hold on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
624 p5 = semilogx(nf,(180/pi).*unwrap(angle(mresp)),'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
625 xlabel('Normalized Frequency [f/fs]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
626 ylabel('Phase [Deg]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
627 legend([p4(1) p5(1)],'Original', 'VDFIT')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
628 hold off
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
629
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
630 case 3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
631 % Semilogy plot for absolute value
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
632 figure(plth)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
633 subplot(2,1,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
634 p1 = semilogy(nf,abs(yr),'k');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
635 hold on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
636 p2 = semilogy(nf,abs(mresp),'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
637 p3 = semilogy(nf,abs(rdl),'b');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
638 xlabel('Normalized Frequency [f/fs]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
639 ylabel('Amplitude')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
640 legend([p1(1) p2(1) p3(1)],'Original', 'VDFIT','Residual')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
641 hold off
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
642
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
643 subplot(2,1,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
644 p4 = semilogy(nf,(180/pi).*unwrap(angle(yr)),'k');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
645 hold on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
646 p5 = semilogy(nf,(180/pi).*unwrap(angle(mresp)),'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
647 xlabel('Normalized Frequency [f/fs]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
648 ylabel('Phase [Deg]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
649 legend([p4(1) p5(1)],'Original', 'VDFIT')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
650 hold off
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
651
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
652 case 4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
653 % Linear plot for absolute value
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
654 figure(plth)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
655 subplot(2,1,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
656 p1 = plot(nf,abs(yr),'k');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
657 hold on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
658 p2 = plot(nf,abs(mresp),'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
659 p3 = plot(nf,abs(rdl),'b');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
660 xlabel('Normalized Frequency [f/fs]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
661 ylabel('Amplitude')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
662 legend([p1(1) p2(1) p3(1)],'Original', 'VDFIT','Residual')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
663 hold off
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
664
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
665 subplot(2,1,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
666 p4 = plot(nf,(180/pi).*unwrap(angle(yr)),'k');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
667 hold on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
668 p5 = plot(nf,(180/pi).*unwrap(angle(mresp)),'r');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
669 xlabel('Normalized Frequency [f/fs]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
670 ylabel('Phase [Deg]')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
671 legend([p4(1) p5(1)],'Original', 'VDFIT')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
672 hold off
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
673
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
674 end |