Maximize stopband attenuation of a lowpass FIR filter (magnitude design)

% "FIR Filter Design via Spectral Factorization and Convex Optimization"
% by S.-P. Wu, S. Boyd, and L. Vandenberghe
% (figures are generated)
%
% Designs an FIR lowpass filter using spectral factorization method where we:
% - minimize maximum stopband attenuation
% - have a constraint on the maximum passband ripple
%
%   minimize   max |H(w)|                      for w in the stopband
%       s.t.   1/delta <= |H(w)| <= delta      for w in the passband
%
% We change variables via spectral factorization method and get:
%
%   minimize   max R(w)                        for w in the stopband
%       s.t.   (1/delta)^2 <= R(w) <= delta^2  for w in the passband
%              R(w) >= 0                       for all w
%
% where R(w) is the squared magnited of the frequency response
% (and the Fourier transform of the autocorrelation coefficients r).
% Variables are coeffients r. delta is the allowed passband ripple.
% This is a convex problem (can be formulated as an LP after sampling).
%
% Written for CVX by Almir Mutapcic 02/02/06

%*********************************************************************
% user's filter specs (for a low-pass filter example)
%*********************************************************************
% number of FIR coefficients (including the zeroth one)
n = 20;

wpass = 0.12*pi;   % end of the passband
wstop = 0.24*pi;   % start of the stopband
delta = 1;         % maximum passband ripple in dB (+/- around 0 dB)

%*********************************************************************
% create optimization parameters
%*********************************************************************
% rule-of-thumb discretization (from Cheney's Approx. Theory book)
m = 15*n;
w = linspace(0,pi,m)'; % omega

% A is the matrix used to compute the power spectrum
% A(w,:) = [1 2*cos(w) 2*cos(2*w) ... 2*cos(n*w)]
A = [ones(m,1) 2*cos(kron(w,[1:n-1]))];

% passband 0 <= w <= w_pass
ind = find((0 <= w) & (w <= wpass));    % passband
Lp  = 10^(-delta/20)*ones(length(ind),1);
Up  = 10^(+delta/20)*ones(length(ind),1);
Ap  = A(ind,:);

% transition band is not constrained (w_pass <= w <= w_stop)

% stopband (w_stop <= w)
ind = find((wstop <= w) & (w <= pi));   % stopband
As  = A(ind,:);

%********************************************************************
% optimization
%********************************************************************
% formulate and solve the magnitude design problem
cvx_begin
  variable r(n,1)

  % this is a feasibility problem
  minimize( max( abs( As*r ) ) )
  subject to
    % passband constraints
    Ap*r >= (Lp.^2);
    Ap*r <= (Up.^2);
    % nonnegative-real constraint for all frequencies (a bit redundant)
    A*r >= 0;
cvx_end

% check if problem was successfully solved
disp(['Problem is ' cvx_status])
if ~strfind(cvx_status,'Solved')
  return
end

% compute the spectral factorization
h = spectral_fact(r);

% compute the max attenuation in the stopband (convert to original vars)
Ustop = 10*log10(cvx_optval);
fprintf(1,'The max attenuation in the stopband is %3.2f dB.\n\n',Ustop);

%*********************************************************************
% plotting routines
%*********************************************************************
% frequency response of the designed filter, where j = sqrt(-1)
H = [exp(-j*kron(w,[0:n-1]))]*h;

figure(1)
% FIR impulse response
plot([0:n-1],h','o',[0:n-1],h','b:')
xlabel('t'), ylabel('h(t)')

figure(2)
% magnitude
subplot(2,1,1)
plot(w,20*log10(abs(H)), ...
     [0 wpass],[delta delta],'r--', ...
     [0 wpass],[-delta -delta],'r--', ...
     [wstop pi],[Ustop Ustop],'r--')
xlabel('w')
ylabel('mag H(w) in dB')
axis([0 pi -50 5])
% phase
subplot(2,1,2)
plot(w,angle(H))
axis([0,pi,-pi,pi])
xlabel('w'), ylabel('phase H(w)')
 
Calling sedumi: 828 variables, 21 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 21, order n = 829, dim = 829, blocks = 229
nnz(A) = 12228 + 0, nnz(ADA) = 441, nnz(L) = 231
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.25E+02 0.000
  1 :  -8.10E-01 4.27E+01 0.000 0.3408 0.9000 0.9000   4.90  1  1  7.7E+01
  2 :  -6.93E-01 1.09E+01 0.000 0.2556 0.9000 0.9000   2.08  1  1  1.2E+01
  3 :  -3.57E-01 4.67E+00 0.000 0.4282 0.9000 0.9000   2.20  1  1  3.0E+00
  4 :  -7.18E-02 2.30E+00 0.000 0.4935 0.9000 0.9000   3.56  1  1  6.3E-01
  5 :  -2.40E-02 1.01E+00 0.000 0.4372 0.9000 0.9000   1.84  1  1  2.2E-01
  6 :  -8.79E-03 3.98E-01 0.000 0.3955 0.9000 0.9000   1.31  1  1  8.8E-02
  7 :  -4.47E-03 2.04E-01 0.000 0.5118 0.9000 0.9000   1.11  1  1  5.1E-02
  8 :  -1.95E-03 8.02E-02 0.000 0.3931 0.9000 0.9000   1.07  1  1  2.7E-02
  9 :  -9.23E-04 3.51E-02 0.000 0.4375 0.9000 0.9000   1.01  1  1  1.8E-02
 10 :  -2.58E-04 3.98E-03 0.000 0.1135 0.9145 0.9000   0.98  1  1  7.1E-03
 11 :  -1.65E-04 1.21E-03 0.000 0.3036 0.9331 0.9000   1.00  1  1  4.3E-03
 12 :  -1.65E-04 7.62E-04 0.410 0.6307 0.9000 0.0000   0.99  1  1  3.9E-03
 13 :  -1.44E-04 5.85E-04 0.162 0.7670 0.9000 0.9000   0.98  1  1  3.0E-03
 14 :  -1.27E-04 2.80E-04 0.000 0.4782 0.9439 0.9000   0.95  1  1  2.1E-03
 15 :  -1.07E-04 5.69E-05 0.000 0.2035 0.9194 0.9000   0.99  1  1  6.2E-04
 16 :  -1.05E-04 1.20E-05 0.000 0.2109 0.9118 0.9000   0.99  1  1  1.4E-04
 17 :  -1.05E-04 2.46E-06 0.000 0.2050 0.9080 0.9000   1.00  1  1  3.1E-05
 18 :  -1.05E-04 2.86E-07 0.000 0.1162 0.9070 0.9000   1.00  1  1  3.7E-06
 19 :  -1.05E-04 1.38E-09 0.000 0.0048 0.9990 0.9601   1.00  2  2  1.3E-08

iter seconds digits       c*x               b*y
 19      0.1   6.1 -1.0483614480e-04 -1.0483646538e-04
|Ax-b| =   6.6e-10, [Ay-c]_+ =   3.2E-10, |x|=  7.5e-01, |y|=  3.1e-01

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    1.200E-01    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1.258925e+00,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 2.26418.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.000104836
Problem is Solved
The max attenuation in the stopband is -39.79 dB.