-
Notifications
You must be signed in to change notification settings - Fork 54
/
realizeNTF.m
executable file
·363 lines (349 loc) · 8.94 KB
/
realizeNTF.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
function [a,g,b,c] = realizeNTF(ntf,form,stf)
% [a,g,b,c] = realizeNTF(ntf,form='CRFB',stf=1)
% Convert a noise transfer function into coefficients for the desired structure.
% Supported structures are
% CRFB Cascade of resonators, feedback form.
% CRFF Cascade of resonators, feedforward form.
% CIFB Cascade of integrators, feedback form.
% CIFF Cascade of integrators, feedforward form.
% CRFBD CRFB with delaying quantizer.
% CRFFD CRFF with delaying quantizer.
% PFF Parallel feed-forward.
% Stratos A CIFF-like structure with non-delaying resonator feedbacks,
% contributed in 2007 by Jeff Gealow
% See the accompanying documentation for block diagrams of each structure
%
% The order of the NTF zeros must be (real, complex conj. pairs).
% The order of the zeros is used when mapping the NTF onto the chosen topology.
%
% stf is a zpk transfer function
% The basic idea is to equate the loop filter at a set of
% points in the z-plane to L1 = 1-1/ntf at those points.
stderr = 2;
% Handle the input arguments
parameters = {'ntf';'form';'stf'};
defaults = {NaN, 'CRFB', []};
for i=1:length(defaults)
parameter = char(parameters(i));
if i>nargin | ( eval(['isnumeric(' parameter ') ']) & ...
eval(['any(isnan(' parameter ')) | isempty(' parameter ') ']) )
eval([parameter '=defaults{i};'])
end
end
% Code common to all functions
ntf_p = ntf.p{1};
ntf_z = ntf.z{1};
order = length(ntf_p);
order2 = floor(order/2);
odd = order - 2*order2;
a = zeros(1,order);
g = zeros(1,order2);
b = zeros(1,order+1);
c = ones(1,order);
% Choose a set of points in the z-plane at which to try to make L1 = 1-1/H
% I don't know how to do this properly, but the code below seems to work
% for order <= 11
N = 200;
min_distance = 0.09;
C = zeros(N,1);
j = 1;
for i=1:N
z = 1.1*exp(2i*pi*i/N);
if all( abs(ntf_z-z) > min_distance )
C(j) = z;
j = j+1;
end
end
C(j:end) = [];
zSet = C;
switch form
case 'CRFB'
%Find g
%Assume the roots are ordered, real first, then cx conj. pairs
for i=1:order2
g(i) = 2*(1-real(ntf_z(2*i-1+odd)));
end
L1 = zeros(1,order);
%Form the linear matrix equation a*T*=L1
for i=1:order*2
z = zSet(i);
%L1(z) = 1-1/H(z)
L1(i) = 1-evalRPoly(ntf_p,z)/evalRPoly(ntf_z,z);
Dfactor = (z-1)/z;
product=1;
for j=order:-2:(1+odd)
product = z/evalRPoly(ntf_z((j-1):j),z)*product;
T(j,i) = product*Dfactor;
T(j-1,i) = product;
end
if( odd )
T(1,i)=product/(z-1);
end
end
a = -real(L1/T);
if isempty(stf)
b = a;
b(order+1) = 1;
end
case 'CRFF'
%Find g
%Assume the roots are ordered, real first, then cx conj. pairs
for i=1:order2
g(i) = 2*(1-real(ntf_z(2*i-1+odd)));
end
L1 = zeros(1,order);
%Form the linear matrix equation a*T*=L1
for i=1:order*2
z = zSet(i);
%L1(z) = 1-1/H(z)
L1(i) = 1-evalRPoly(ntf_p,z)/evalRPoly(ntf_z,z);
if( odd )
Dfactor = z-1;
product = 1/Dfactor;
T(1,i) = product;
else
Dfactor = (z-1)/z;
product=1;
end
for j=1+odd:2:order
product = z/evalRPoly(ntf_z(j:j+1),z)*product;
T(j,i) = product*Dfactor;
T(j+1,i) = product;
end
end
a = -real(L1/T);
if isempty(stf)
b = [ 1 zeros(1,order-1) 1];
end
case 'CIFB'
%Assume the roots are ordered, real first, then cx conj. pairs
%Note ones which are moved significantly.
if any( abs(real(ntf_z)-1) > 1e-3)
fprintf(stderr,'%s Warning: The ntf''s zeros have had their real parts set to one.\n', mfilename);
end
ntf_z = 1 + 1i*imag(ntf_z);
for i=1:order2
g(i) = imag(ntf_z(2*i-1+odd))^2;
end
L1 = zeros(1,order);
%Form the linear matrix equation a*T*=L1
for i=1:order*2
z = zSet(i);
%L1(z) = 1-1/H(z)
L1(i) = 1-evalRPoly(ntf_p,z)/evalRPoly(ntf_z,z);
Dfactor = (z-1);
product = 1;
for j=order:-2:(1+odd)
product = product/evalRPoly(ntf_z((j-1):j),z);
T(j,i) = product*Dfactor;
T(j-1,i) = product;
end
if( odd )
T(1,i) = product/(z-1);
end
end
a = -real(L1/T);
if isempty(stf)
b = a;
b(order+1) = 1;
end
case 'CIFF'
%Assume the roots are ordered, real first, then cx conj. pairs
%Note ones which are moved significantly.
if any( abs(real(ntf_z)-1) > 1e-3 )
fprintf(stderr,'%s Warning: The ntf''s zeros have had their real parts set to one.\n', mfilename);
end
ntf_z = 1 + 1i*imag(ntf_z);
for i=1:order2
g(i) = imag(ntf_z(2*i-1+odd))^2;
end
L1 = zeros(1,order);
%Form the linear matrix equation a*T*=L1
for i=1:order*2
z = zSet(i);
%L1(z) = 1-1/H(z)
L1(i) = 1-evalRPoly(ntf_p,z)/evalRPoly(ntf_z,z);
Dfactor = (z-1);
if( odd )
product = 1/(z-1);
T(1,i) = product;
else
product = 1;
end
for j=odd+1:2:order-1
product = product/evalRPoly(ntf_z(j:j+1),z);
T(j,i) = product*Dfactor;
T(j+1,i) = product;
end
end
a = -real(L1/T);
if isempty(stf)
b = [ 1 zeros(1,order-1) 1];
end
case 'CRFBD'
%Find g
%Assume the roots are ordered, real first, then cx conj. pairs
for i=1:order2
g(i) = 2*(1-real(ntf_z(2*i-1+odd)));
end
L1 = zeros(1,order);
%Form the linear matrix equation a*T*=L1
for i=1:order*2
z = zSet(i);
%L1(z) = 1-1/H(z)
L1(i) = 1-evalRPoly(ntf_p,z)/evalRPoly(ntf_z,z);
Dfactor = (z-1);
product=1/z;
for j=order:-2:(1+odd)
product = z/evalRPoly(ntf_z((j-1):j),z)*product;
T(j,i) = product*Dfactor;
T(j-1,i) = product;
end
if( odd )
T(1,i)=product*z/(z-1);
end
end
a = -real(L1/T);
if isempty(stf)
b = a;
b(order+1) = 1;
end
case 'CRFFD'
%Find g
%Assume the roots are ordered, real first, then cx conj. pairs
for i=1:order2
g(i) = 2*(1-real(ntf_z(2*i-1+odd)));
end
%zL1 = z*(1-1/H(z))
zL1 = zSet .* (1-1./evalTF(ntf,zSet));
%Form the linear matrix equation a*T*=zL1
for i=1:order*2
z = zSet(i);
if( odd )
Dfactor = (z-1)/z;
product = 1/Dfactor;
T(1,i) = product;
else
Dfactor = z-1;
product=1;
end
for j=1+odd:2:order
product = z/evalRPoly(ntf_z(j:j+1),z)*product;
T(j,i) = product*Dfactor;
T(j+1,i) = product;
end
end
a = -real(zL1/T);
if isempty(stf)
b = [ 1 zeros(1,order-1) 1];
end
case 'PFF'
%Find g
%Assume the roots are ordered, real first, then cx conj. pairs
% with the secondary zeros after the primary zeros
for i=1:order2
g(i) = 2*(1-real(ntf_z(2*i-1+odd)));
end
% Find the dividing line between the zeros
theta0 = abs(angle(ntf_z(1)));
% !! 0.5 radians is an arbitrary separation !!
i = find( abs(abs(angle(ntf_z)) - theta0) > 0.5 );
order_1 = i(1)-1;
order_2 = order-order_1;
if length(i) ~= order_2
keyboard
error('For the PFF form, the NTF zeros must be sorted into primary and secondary zeros');
end
odd_1 = mod(order_1,2);
odd_2 = mod(order_2,2);
L1 = zeros(1,order);
%Form the linear matrix equation a*T*=L1
for i=1:order*2
z = zSet(i);
%L1(z) = 1-1/H(z)
L1(i) = 1-evalRPoly(ntf_p,z)/evalRPoly(ntf_z,z);
if( odd_1 )
Dfactor = z-1;
product = 1/Dfactor;
T(1,i) = product;
else
Dfactor = (z-1)/z;
product=1;
end
for j=1+odd_1:2:order_1
product = z/evalRPoly(ntf_z(j:j+1),z)*product;
T(j,i) = product*Dfactor;
T(j+1,i) = product;
end
if( odd_2 )
Dfactor = z-1;
product = 1/Dfactor;
T(order_1+1,i) = product;
else
Dfactor = (z-1)/z;
product=1;
end
for j=order_1+1+odd_2:2:order
product = z/evalRPoly(ntf_z(j:j+1),z)*product;
T(j,i) = product*Dfactor;
T(j+1,i) = product;
end
end
a = -real(L1/T);
if isempty(stf)
b = [ 1 zeros(1,order_1-1) 1 zeros(1,order_2-1) 1];
end
case 'Stratos'
% code copied from case 'CRFF':
%Find g
%Assume the roots are ordered, real first, then cx conj. pairs
for i=1:order2
g(i) = 2*(1-real(ntf_z(2*i-1+odd)));
end
% code copied from case 'CIFF':
L1 = zeros(1,order);
%Form the linear matrix equation a*T*=L1
for i=1:order*2
z = zSet(i);
%L1(z) = 1-1/H(z)
L1(i) = 1-evalRPoly(ntf_p,z)/evalRPoly(ntf_z,z);
Dfactor = (z-1);
if( odd )
product = 1/(z-1);
T(1,i) = product;
else
product = 1;
end
for j=odd+1:2:order-1
product = product/evalRPoly(ntf_z(j:j+1),z);
T(j,i) = product*Dfactor;
T(j+1,i) = product;
end
end
a = -real(L1/T);
if isempty(stf)
b = [ 1 zeros(1,order-1) 1];
end
end
if ~isempty(stf)
% Compute the TF from each feed-in to the output
% and solve for coefficients which yield the best match
% THIS CODE IS NOT OPTIMAL, in terms of computational efficiency.
stfList = cell(1,order+1);
for i = 1:order+1
bi = zeros(1,order+1); bi(i)=1;
ABCD = stuffABCD(a,g,bi,c,form);
if strcmp(form(3:4),'FF')
ABCD(1,order+2) = -1; % b1 is used to set the feedback
end
[junk stfList{i}] = calculateTF(ABCD);
end
% Build the matrix equation b A = x and solve it.
A = zeros(order+1,length(zSet));
for i = 1:order+1
A(i,:) = evalTF(stfList{i},zSet);
end
x = evalTF(stf,zSet);
x = x(:).';
b = real(x/A);
end