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