forked from analogdevicesinc/ad936x-filter-wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_design_filter.m
683 lines (624 loc) · 21.8 KB
/
internal_design_filter.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
% Copyright 2014-2015(c) Analog Devices, Inc.
%
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without modification,
% are permitted provided that the following conditions are met:
% - Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% - Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the
% distribution.
% - Neither the name of Analog Devices, Inc. nor the names of its
% contributors may be used to endorse or promote products derived
% from this software without specific prior written permission.
% - The use of this software may or may not infringe the patent rights
% of one or more patent holders. This license does not release you
% from the requirement that you obtain separate licenses from these
% patent holders to use this software.
% - Use of the software either in source or binary form or filter designs
% resulting from the use of this software, must be connected to, run
% on or loaded to an Analog Devices Inc. component.
%
% THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
% INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE ARE DISCLAIMED.
%
% IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY
% RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
% STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
% THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%
% Inputs (structure containing the following fields)
% ============================================
% Rdata = input/output sample data rate (in Hz)
% FIR = FIR interpolation/decimation factor
% PLL_mult = PLL multiplication
% Fpass = passband frequency (in Hz)
% Fstop = stopband frequency (in Hz)
% Apass = max ripple allowed in passband (in dB)
% Astop = min attenuation in stopband (in dB)
% FIRdBmin = min rejection that FIR is required to have (in dB)
% phEQ = phase equalization on (not -1)/off (-1)
% int_FIR = use AD9361 FIR on (1)/off (0)
% wnom = analog cutoff frequency (in Hz)
%
% Outputs (structure containing the following fields)
% ===============================================
% firtaps = fixed point FIR coefficients
% filter = system object for visualization (does not include analog filters)
% Apass_actual = actual passband ripple
% Astop_actual = actual stopband attentuation
% delay = actual delay used in phase equalization
function output = internal_design_filter(input)
% support a simple data rate input otherwise it must be a structure
if isfloat(input)
input = struct('Rdata', input);
end
input = cook_input(input);
% use the internal FIR if unspecified
if ~isfield(input, 'int_FIR')
input.int_FIR = 1;
end
% nominal frequency can't be zero
if ~input.wnom
input.wnom = double(calculate_rfbw(input.PLL_rate, input.caldiv, input.RxTx, true));
end
if strcmp(input.RxTx, 'Rx')
wTIA = input.wnom*(2.5/1.4);
% Define the analog filters (for design purpose)
[b1,a1] = butter(1,2*pi*wTIA,'s'); % 1st order
[b2,a2] = butter(3,2*pi*input.wnom,'s'); % 3rd order
% Digital representation of the analog filters (It is an approximation for group delay calculation only)
[z1,p1,k1] = butter(3,coerce_cutoff(input.wnom/(input.converter_rate/2)),'low');
[sos1,g1] = zp2sos(z1,p1,k1);
Hd1=dsp.BiquadFilter('SOSMatrix',sos1,'ScaleValues',g1);
[z2,p2,k2] = butter(1,coerce_cutoff(wTIA/(input.converter_rate/2)),'low');
[sos2,g2] = zp2sos(z2,p2,k2);
Hd2=dsp.BiquadFilter('SOSMatrix',sos2,'ScaleValues',g2);
Hanalog = cascade(Hd2,Hd1);
% Define the Pluto DEC8 filter
ast = 80;
n = 128;
f = fdesign.decimator(8, 'Nyquist', 8, 'N,Ast', n, ast);
hf = design(f,'SystemObject',true);
% Define the digital filters with fixed coefficients
allpass_coeff = 1;
hb1_coeff = 2^(-11)*[-8 0 42 0 -147 0 619 1013 619 0 -147 0 42 0 -8];
hb2_coeff = 2^(-8)*[-9 0 73 128 73 0 -9];
hb3_coeff = 2^(-4)*[1 4 6 4 1];
dec_int3_coeff = 2^(-14)*[55 83 0 -393 -580 0 1914 4041 5120 4041 1914 0 -580 -393 0 83 55];
dec_int_func = @dsp.FIRDecimator;
else
wreal = input.wnom*(5.0/1.6);
% Define the analog filters (for design purpose)
[b1,a1] = butter(3,2*pi*input.wnom,'s'); % 3rd order
[b2,a2] = butter(1,2*pi*wreal,'s'); % 1st order
% Digital representation of the analog filters (It is an approximation for group delay calculation only)
[z1,p1,k1] = butter(3,coerce_cutoff(input.wnom/(input.converter_rate/2)),'low');
[sos1,g1] = zp2sos(z1,p1,k1);
Hd1=dsp.BiquadFilter('SOSMatrix',sos1,'ScaleValues',g1);
[z2,p2,k2] = butter(1,coerce_cutoff(wreal/(input.converter_rate/2)),'low');
[sos2,g2] = zp2sos(z2,p2,k2);
Hd2=dsp.BiquadFilter('SOSMatrix',sos2,'ScaleValues',g2);
Hanalog = cascade(Hd1,Hd2);
% Define the Pluto INT8 filter
ast = 80;
n = 128;
f = fdesign.interpolator(8,'Nyquist', 8,'N,Ast', n, ast);
hf = design(f,'kaiserwin','SystemObject',true);
hf.Numerator = hf.Numerator./8;
% Define the digital filters with fixed coefficients
allpass_coeff = 1;
hb1_coeff = 2^(-14)*[-53 0 313 0 -1155 0 4989 8192 4989 0 -1155 0 313 0 -53];
hb2_coeff = 2^(-8)*[-9 0 73 128 73 0 -9];
hb3_coeff = 2^(-2)*[1 2 1];
dec_int3_coeff = (1/3)*2^(-13)*[36 -19 0 -156 -12 0 479 223 0 -1215 -993 0 3569 6277 8192 6277 3569 0 -993 -1215 0 223 479 0 -12 -156 0 -19 36];
dec_int_func = @dsp.FIRInterpolator;
end
Hallpass = dec_int_func(1, allpass_coeff);
Hm1 = dec_int_func(2, hb1_coeff);
Hm1.FullPrecisionOverride = false;
Hm1.OutputDataType='Custom';
Hm1.CustomOutputDataType=numerictype([],16,14);
Hm1.CoefficientsDataType='Custom';
Hm1.CustomCoefficientsDataType=numerictype([],16);
Hm1.ProductDataType='Custom';
Hm1.AccumulatorDataType = 'Custom';
if strcmp(input.RxTx, 'Rx')
Hm1.CustomProductDataType=numerictype([],31,30);
Hm1.CustomAccumulatorDataType=numerictype([],33,30);
else
Hm1.CustomProductDataType=numerictype([],31,29);
Hm1.CustomAccumulatorDataType=numerictype([],31,29);
end
Hm1c34 = dec_int_func(2, hb1_coeff);
Hm1c34.FullPrecisionOverride = false;
Hm1c34.OutputDataType='Custom';
Hm1c34.CustomOutputDataType=numerictype([],4,2);
Hm1c34.CoefficientsDataType='Custom';
Hm1c34.CustomCoefficientsDataType=numerictype([],16);
Hm1c34.ProductDataType='Custom';
Hm1c34.AccumulatorDataType = 'Custom';
if strcmp(input.RxTx, 'Rx')
Hm1c34.CustomProductDataType=numerictype([],31,30);
Hm1c34.CustomAccumulatorDataType=numerictype([],33,30);
else
Hm1c34.CustomProductDataType=numerictype([],31,29);
Hm1c34.CustomAccumulatorDataType=numerictype([],31,29);
end
Hm2 = dec_int_func(2, hb2_coeff);
Hm2.FullPrecisionOverride = false;
Hm2.OutputDataType='Custom';
Hm2.CustomOutputDataType=numerictype([],16,14);
Hm2.CoefficientsDataType='Custom';
Hm2.CustomCoefficientsDataType=numerictype([],16);
Hm2.ProductDataType='Custom';
Hm2.CustomProductDataType=numerictype([],31,29);
Hm2.AccumulatorDataType = 'Custom';
if strcmp(input.RxTx, 'Rx')
Hm2.CustomAccumulatorDataType=numerictype([],32,29);
else
Hm2.CustomAccumulatorDataType=numerictype([],31,29);
end
Hm2c34 = dec_int_func(2, hb2_coeff);
Hm2c34.FullPrecisionOverride = false;
Hm2c34.OutputDataType='Custom';
Hm2c34.CustomOutputDataType=numerictype([],4,2);
Hm2c34.CoefficientsDataType='Custom';
Hm2c34.CustomCoefficientsDataType=numerictype([],16);
Hm2c34.ProductDataType='Custom';
Hm2c34.CustomProductDataType=numerictype([],31,29);
Hm2c34.AccumulatorDataType = 'Custom';
if strcmp(input.RxTx, 'Rx')
Hm2c34.CustomAccumulatorDataType=numerictype([],32,29);
else
Hm2c34.CustomAccumulatorDataType=numerictype([],31,29);
end
Hm3 = dec_int_func(2, hb3_coeff);
Hm3.FullPrecisionOverride = false;
Hm3.OutputDataType='Custom';
Hm3.CustomOutputDataType=numerictype([],8,6);
Hm3.CoefficientsDataType='Custom';
Hm3.CustomCoefficientsDataType=numerictype([],16);
Hm3.ProductDataType='Custom';
Hm3.AccumulatorDataType = 'Custom';
if strcmp(input.RxTx, 'Rx')
Hm3.CustomProductDataType=numerictype([],19,18);
Hm3.CustomAccumulatorDataType=numerictype([],21,18);
else
Hm3.CustomProductDataType=numerictype([],19,17);
Hm3.CustomAccumulatorDataType=numerictype([],19,17);
end
Hm4 = dec_int_func(3, dec_int3_coeff);
Hm4.FullPrecisionOverride = false;
Hm4.OutputDataType='Custom';
Hm4.CustomOutputDataType=numerictype([],16,14);
Hm4.CoefficientsDataType='Custom';
Hm4.CustomCoefficientsDataType=numerictype([],16);
Hm4.ProductDataType='Custom';
Hm4.CustomProductDataType=numerictype([],19,18);
Hm4.AccumulatorDataType = 'Custom';
if strcmp(input.RxTx, 'Rx')
Hm4.CustomAccumulatorDataType=numerictype([],21,18);
else
Hm4.CustomAccumulatorDataType=numerictype([],20,18);
end
hf.FullPrecisionOverride = false;
hf.OutputDataType='Custom';
hf.CustomOutputDataType=numerictype([],16,15);
hf.CoefficientsDataType='Custom';
hf.CustomCoefficientsDataType=numerictype([],16,15);
hf.ProductDataType='Custom';
hf.CustomProductDataType=numerictype([],16,15);
hf.AccumulatorDataType = 'Custom';
hf.CustomAccumulatorDataType=numerictype([],16,15);
hb1 = input.HB1;
hb2 = input.HB2;
if input.HB3 == 2
hb3 = 2;
dec_int3 = 1;
elseif input.HB3 == 3
hb3 = 1;
dec_int3 = 3;
else
hb3 = 1;
dec_int3 = 1;
end
% convert the enables into a string
enables = strrep(num2str([hb1 hb2 hb3 dec_int3]), ' ', '');
switch enables
case '1111' % only FIR
filter_stages = {Hallpass};
case '2111' % Hb1
filter_stages = {Hm1};
case '1211' % Hb2
filter_stages = {Hm2};
case '1121' % Hb3
filter_stages = {Hm3};
case '2211' % Hb2,Hb1
filter_stages = {Hm2,Hm1};
case '2121' % Hb3,Hb1
filter_stages = {Hm3,Hm1c34};
case '1221' % Hb3,Hb2
filter_stages = {Hm3,Hm2c34};
case '2221' % Hb3,Hb2,Hb1
filter_stages = {Hm3,Hm2c34,Hm1};
case '1113' % Dec/Int3
filter_stages = {Hm4};
case '2113' % Dec/Int3,Hb1
filter_stages = {Hm4,Hm1c34};
case '1213' % Dec/Int3,Hb2
filter_stages = {Hm4,Hm2c34};
case '2213' % Dec/Int3,Hb2,Hb1
filter_stages = {Hm4,Hm2c34,Hm1};
otherwise
error('ddcresponse:IllegalOption', 'At least one of the stages must be there.')
end
% filter stages are reversed for Tx path
if strcmp(input.RxTx, 'Tx')
filter_stages = fliplr(filter_stages);
end
dfilter = cascade(filter_stages{:});
Hmiddle = clone(dfilter);
if strcmp(input.RxTx, 'Rx')
if strcmp(enables,'1111') || strcmp(enables,'2111') || strcmp(enables,'1211') || strcmp(enables,'1121') || strcmp(enables,'1113')
addStage(Hmiddle,Hd1,1);
else
addStage(Hmiddle,Hd1,1);
end
addStage(Hmiddle,Hd2,1);
else
if strcmp(enables,'1111') || strcmp(enables,'2111') || strcmp(enables,'1211') || strcmp(enables,'1121') || strcmp(enables,'1113')
addStage(Hmiddle,Hd1);
else
addStage(Hmiddle,Hd1);
end
addStage(Hmiddle,Hd2);
end
% Find out the best fit delay on passband
Nw = 2048;
w = zeros(1,Nw);
phi = zeros(1,Nw);
invariance = zeros(1,Nw);
w(1) = -input.Fpass;
for i = 2:(Nw)
w(i) = w(1)-2*w(1)*i/(Nw);
end
if strcmp(input.RxTx, 'Rx')
response = analogresp('Rx',w,input.converter_rate,b1,a1,b2,a2).*freqz(dfilter,w,input.converter_rate);
else
response = freqz(dfilter,w,input.converter_rate).*analogresp('Tx',w,input.converter_rate,b1,a1,b2,a2);
end
for i = 1:(Nw)
invariance(i) = real(response(i))^2+imag(response(i))^2;
end
phi(1)=atan2(imag(response(1)),real(response(1)));
for i = 2:(Nw)
phi(i) = phi(i-1)+alias_b(atan2(imag(response(i)),real(response(i)))-phi(i-1),2*pi);
end
sigma = sum(invariance);
sigmax = sum(w.*invariance);
sigmay = sum(phi.*invariance);
sigmaxx = sum(w.*w.*invariance);
sigmaxy = sum(w.*phi.*invariance);
delta = sigma*sigmaxx-sigmax^2;
b = (sigma*sigmaxy-sigmax*sigmay)/delta;
if input.phEQ == 0 || input.phEQ == -1
delay = -b/(2*pi);
else
delay = input.phEQ*(1e-9);
end
% Design the FIR
G = 16384;
clkFIR = input.Rdata*input.FIR;
Gpass = floor(G*input.Fpass/clkFIR);
Gstop=ceil(G*input.Fstop/clkFIR);
Gpass = min(Gpass,Gstop-1);
fg = zeros(1,Gpass);
omega = zeros(1,Gpass);
% passband
for i = 1:(Gpass+1)
fg(i) = (i-1)/G;
omega(i) = fg(i)*clkFIR;
end
if strcmp(input.RxTx, 'Rx')
rg1 = analogresp('Rx',omega,input.converter_rate,b1,a1,b2,a2).*freqz(dfilter,omega,input.converter_rate);
else
rg1 = freqz(dfilter,omega,input.converter_rate).*analogresp('Tx',omega,input.converter_rate,b1,a1,b2,a2);
end
phase = unwrap(angle(rg1));
gd1 = group_delay(omega,phase); % group delay on passband for Analog + Converter + HB
omega1 = omega; % frequency grid on pass band
rg2 = exp(-1i*2*pi*omega*delay);
rg = rg2./rg1;
w = abs(rg1)/(dBinv(input.Apass/2)-1);
g = Gpass+1;
% stop band
for m = Gstop:(G/2)
g = g+1;
fg(g) = m/G;
omega(g) = fg(g)*clkFIR;
rg(g) = 0;
end
if strcmp(input.RxTx, 'Rx')
wg1 = abs(analogresp('Rx',omega(Gpass+2:end),input.converter_rate,b1,a1,b2,a2).*freqz(dfilter,omega(Gpass+2:end),input.converter_rate));
wg2 = (wg1)/(dBinv(-input.Astop));
else
wg1 = abs(freqz(dfilter,omega(Gpass+2:end),input.converter_rate).*analogresp('Tx',omega(Gpass+2:end),input.converter_rate,b1,a1,b2,a2));
wg2 = (sqrt(input.FIR)*wg1)/(dBinv(-input.Astop));
end
wg3 = dBinv(input.FIRdBmin);
wg = max(wg2,wg3);
grid = fg;
if input.phEQ == -1
resp = abs(rg);
else resp = rg;
end
weight = [w wg];
weight = weight/max(weight);
% design FIR filter
cr = real(resp);
B = 2;
F1 = grid(1:Gpass+1)*2;
F2 = grid(Gpass+2:end)*2;
A1 = cr(1:Gpass+1);
A2 = cr(Gpass+2:end);
W1 = weight(1:Gpass+1);
W2 = weight(Gpass+2:end);
% Determine the number of taps for FIR
if strcmp(input.RxTx, 'Rx')
if hb3 == 1
N = min(16*floor(input.converter_rate/(input.Rdata)),128);
else
N = min(16*floor(input.converter_rate/(2*input.Rdata)),128);
end
else
switch input.FIR
case 1
Nmax = 64;
case 2
Nmax = 128;
case 4
Nmax = 128;
end
N = min(16*floor(input.converter_rate*input.DAC_div/(2*input.Rdata)),Nmax);
end
tap_store = zeros(N/16,N);
Apass_actual_vector = zeros(N/16,1);
Astop_actual_vector = zeros(N/16,1);
i = 1;
while (1)
if input.int_FIR
d = fdesign.arbmag('N,B,F,A',N-1,B,F1,A1,F2,A2);
else
d = fdesign.arbmag('B,F,A,R');
d.NBands = 2;
d.B1Frequencies = F1;
d.B1Amplitudes = A1;
d.B1Ripple = db2mag(-input.Astop);
d.B2Frequencies = F2;
d.B2Amplitudes = A2;
d.B2Ripple = db2mag(-input.Astop);
end
Hd = design(d,'equiripple','B1Weights',W1,'B2Weights',W2,'SystemObject',false);
ccoef = Hd.Numerator;
M = length(ccoef);
if input.phEQ ~= -1
sg = 0.5-grid(end:-1:1);
sr = imag(resp(end:-1:1));
sw = weight(end:-1:1);
F3 = sg(1:G/2-Gstop+1)*2;
F4 = sg(G/2-Gstop+2:end)*2;
A3 = sr(1:G/2-Gstop+1);
A4 = sr(G/2-Gstop+2:end);
W3 = sw(1:G/2-Gstop+1);
W4 = sw(G/2-Gstop+2:end);
if input.int_FIR
d2 = fdesign.arbmag('N,B,F,A',N-1,B,F3,A3,F4,A4);
else
d2 = fdesign.arbmag('N,B,F,A',M-1,B,F3,A3,F4,A4);
end
Hdeq = design(d2,'equiripple','B1Weights',W3,'B2Weights',W4,'SystemObject',false);
scoef = Hdeq.Numerator;
for k = 1:length(scoef)
scoef(k) = -scoef(k)*(-1)^(k-1);
end
else
scoef = 0;
end
tap_store(i,1:M)=ccoef+scoef;
Hmd = dec_int_func(input.FIR,tap_store(i,1:M));
if ~isempty(ver('fixedpoint')) % Make sure fixed-point toolbox is installed
if license('test','fixed_point_toolbox') % Try to checkout a license
Hmd.Numerator = double(fi(Hmd.Numerator,true,16));
end
end
if strcmp(input.RxTx, 'Rx')
if strcmp(enables,'1111') || strcmp(enables,'2111') || strcmp(enables,'1211') || strcmp(enables,'1121') || strcmp(enables,'1113')
addStage(dfilter,Hmd);
else
addStage(dfilter,Hmd);
end
rg_pass = abs(analogresp('Rx',omega(1:Gpass+1),input.converter_rate,b1,a1,b2,a2).*freqz(dfilter,omega(1:Gpass+1),input.converter_rate));
rg_stop = abs(analogresp('Rx',omega(Gpass+2:end),input.converter_rate,b1,a1,b2,a2).*freqz(dfilter,omega(Gpass+2:end),input.converter_rate));
else
if strcmp(enables,'1111') || strcmp(enables,'2111') || strcmp(enables,'1211') || strcmp(enables,'1121') || strcmp(enables,'1113')
addStage(dfilter, Hmd, 1);
else
addStage(dfilter, Hmd, 1);
end
rg_pass = abs(freqz(dfilter,omega(1:Gpass+1),input.converter_rate).*analogresp('Tx',omega(1:Gpass+1),input.converter_rate,b1,a1,b2,a2));
rg_stop = abs(freqz(dfilter,omega(Gpass+2:end),input.converter_rate).*analogresp('Tx',omega(Gpass+2:end),input.converter_rate,b1,a1,b2,a2));
end
% quantitative values about actual passband and stopband
Apass_actual_vector(i) = mag2db(max(rg_pass))-mag2db(min(rg_pass));
Astop_actual_vector(i) = -mag2db(max(rg_stop));
if input.int_FIR == 0
h = tap_store(1,1:M);
Apass_actual = Apass_actual_vector(1);
Astop_actual = Astop_actual_vector(1);
if strcmp(input.RxTx, 'Rx')
removeStage(dfilter);
else
removeStage(dfilter, 1);
end
break
elseif Apass_actual_vector(1) > input.Apass || Astop_actual_vector(1) < input.Astop
h = tap_store(1,1:N);
Apass_actual = Apass_actual_vector(1);
Astop_actual = Astop_actual_vector(1);
if strcmp(input.RxTx, 'Rx')
removeStage(dfilter);
else
removeStage(dfilter, 1);
end
break
elseif Apass_actual_vector(i) > input.Apass || Astop_actual_vector(i) < input.Astop
h = tap_store(i-1,1:N+16);
Apass_actual = Apass_actual_vector(i-1);
Astop_actual = Astop_actual_vector(i-1);
if strcmp(input.RxTx, 'Rx')
removeStage(dfilter);
else
removeStage(dfilter, 1);
end
break
else
N = N-16;
i = i+1;
if strcmp(input.RxTx, 'Rx')
removeStage(dfilter);
else
removeStage(dfilter, 1);
end
end
end
if strcmp(input.RxTx, 'Tx')
if input.int_FIR == 1 && input.FIR == 2
R = rem(length(h),32);
if R ~= 0
h = [zeros(1,8),h,zeros(1,8)];
end
elseif input.int_FIR == 1 && input.FIR == 4
R = rem(length(h),64);
if R ~= 0
newlength = ceil(length(h)/64)*64;
addlength = (newlength-length(h))/2;
h = [zeros(1,addlength),h,zeros(1,addlength)];
end
end
end
Hmd = dec_int_func(input.FIR,h);
if ~isempty(ver('fixedpoint')) && license('test','fixed_point_toolbox') && license('checkout','fixed_point_toolbox')
Hmd.Numerator = double(fi(Hmd.Numerator,true,16));
end
if strcmp(input.RxTx, 'Rx')
addStage(dfilter, Hmd);
else
addStage(dfilter, Hmd, 1);
end
gd2 = grpdelay(Hmd,omega1,clkFIR).*(1/clkFIR);
if input.phEQ == -1
groupdelay = gd1 + gd2;
else
groupdelay = gd1 + gd2';
end
grpdelayvar = max(groupdelay)-min(groupdelay);
aTFIR = 1 + ceil(log2(max(Hmd.Numerator)));
switch aTFIR
case 2
gain = 6;
case 1
gain = 0;
case 0
gain = -6;
otherwise
gain = -12;
end
if strcmp(input.RxTx, 'Rx')
if aTFIR > 2
gain = 6;
end
else
if input.FIR == 2
gain = gain+6;
elseif input.FIR == 4
gain = gain+12;
end
if gain > 0
gain = 0;
elseif gain < -6
gain = -6;
end
end
bTFIR = min([16 - aTFIR,16]);
firtaps = Hmd.Numerator.*(2^bTFIR);
if ~isequal(double(int16(firtaps)),double(int32(firtaps)))
firtaps = Hmd.Numerator.*(2^(bTFIR-1));
end
if length(firtaps) < 128
firtaps = [firtaps,zeros(1,128-length(firtaps))];
end
output = input;
% externally accessible fields
output.firtaps = firtaps;
output.nfirtaps = length(h);
output.filter = dfilter;
output.gain = gain;
output.Hm1 = Hm1;
output.Hm2 = Hm2;
output.Hm3 = Hm3;
output.Hm4 = Hm4;
output.Hmd = Hmd;
output.enables = enables;
if isfield(input,'FPGAfilter')
output.FPGAfilter = input.FPGAfilter;
else
output.FPGAfilter = false;
end
% internal fields used by the GUI
output.Hanalog = Hanalog;
output.Apass_actual = Apass_actual;
output.Astop_actual = Astop_actual;
output.delay = delay;
output.grpdelayvar = grpdelayvar;
output.Hd1 = Hd1;
output.Hd2 = Hd2;
output.Hmiddle = Hmiddle;
output.a1 = a1;
output.b1 = b1;
output.a2 = a2;
output.b2 = b2;
function output = alias_b(f,fs)
output = f-fs*floor(f/fs+0.5);
% coerces the normalized cutoff frequency passed between 0.0 and 1.0
% for digital Butterworth filter designs
function Wn = coerce_cutoff(freq)
Wn = freq;
if Wn < 0.0
Wn = 0.0 + eps;
elseif Wn > 1.0
Wn = 1.0 - eps;
end
function dBoutput = dBinv(dBinput)
dBmin = -150;
if dBinput>dBmin
dBoutput = 10^(dBinput/20);
else
dBoutput = 0;
end
function t = group_delay(freq,phase)
% calculates the group delay from frequency data (in Hz) and phase data (in radians)
k = length(phase);
% unwrap phase data
phase = (180/pi)*unwrap(phase);
% calculate group delay
for n = 2:k-1
t(n) = (-1/720) * (((phase(n) - phase(n - 1)) / (freq(n) - freq(n - 1)))+ ((phase(n + 1) - phase(n)) / (freq(n + 1) - freq(n))));
end
t(1) = (-1/360) * (((phase(2) - phase(1))/(freq(2) - freq(1))));
t(k) = (-1/360) * (((phase(k) - phase(k - 1))/(freq(k) - freq(k - 1))));