-
Notifications
You must be signed in to change notification settings - Fork 0
/
dz_cvx_old.m
79 lines (65 loc) · 2.64 KB
/
dz_cvx_old.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
function [b] = dz_cvx(Nt,tb,d1,d2,quiet,phase)
osfact = 10; % oversampling factor for frequency grid
nn = (0:Nt/2*osfact)'/(Nt/2*osfact); % 0 to 1 - profile indices
d = zeros(Nt/2*osfact+1,1); % passband mask
s = zeros(Nt/2*osfact+1,1); % stopband mask
wts = zeros(Nt/2*osfact+1,1); % ripple taper weights
% dinflin = dinf(d1,d2); % d-infinity for a linear phase pulse with the same ripples
% w = dinflin/tb; % transition width
% set transition width
w = dinf(d1,d2)/tb;
% % start out the f, m and w vectors with the DC band
% f = [0 (1-w)*(tb/2) (1+w)*(tb/2)];%*di/dilp;
% d = nn <= f(2)/(Nt/2); % target pattern
% wts = 1./abs(nn).^2; % quadratically-decaying ripple weights
% % append the last stopband
% s = s | (nn >= f(end)/(Nt/2));
% wts = wts.*s;wts(isnan(wts)) = 0;wts = wts./max(wts);
%-------------- debug start
if strcmp(phase,'linear');
% start out the f, m and w vectors with the DC band
f = [0 (1-w)*(tb/2) (1+w)*(tb/2)];%*di/dilp;
d = nn <= f(2)/(Nt/2); % target pattern
wts = 1./abs(nn).^2; % quadratically-decaying ripple weights
% append the last stopband
s = s | (nn >= f(end)/(Nt/2));
wts = wts.*s;wts(isnan(wts)) = 0;wts = wts./max(wts);
else
% start out the f, m and w vectors with the DC band
f = [0 (1-w)*(tb/2) (1+w)*(tb/2)];%*di/dilp;
d = nn <= f(2)/(Nt); % target pattern
wts = 1./abs(nn).^2; % quadratically-decaying ripple weights
% append the last stopband
s = s | (nn >= f(end)/(Nt));
wts = wts.*s;wts(isnan(wts)) = 0;wts = wts./max(wts);
end
%-------------- debug end
% build system matrix for cvx design
A = 2*cos(2*pi*(0:Nt/2*osfact)'*(-Nt/2:0)/(Nt*osfact));
A(:,end) = 1/2*A(:,end);
Ad = A(s | d,:);
dd = double(d(s | d));
ss = wts(s | d).*double(s(s | d));
% use cvx to do the constrained optimization
if quiet
cvx_begin quiet
variable delta(1)
variable x(Nt/2+1)
minimize( delta )
subject to
-delta*dd <= Ad*x - dd <= delta*dd + delta*d2/d1*ss
cvx_end
else
cvx_begin
variable delta(1)
variable x(Nt/2+1)
minimize( delta )
subject to
-delta*dd <= Ad*x - dd <= delta*dd + delta*d2/d1*ss
cvx_end
end
% stack two halves together to get full linear-phase filter
x = [x;x(end-1:-1:1)]';
% b = x./max(abs(fft(x,osfact*Nt))); % normalized beta coefficients
b = x./max(abs(fft(x,osfact*Nt))); % normalized beta coefficients
end