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 SDPT3: 828 variables, 21 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 21
 dim. of socp   var  = 456,   num. of socp blk  = 228
 dim. of linear var  = 372
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      mean(obj)   cputime
-------------------------------------------------------------------
 0|0.000|0.000|5.8e+03|6.9e+01|4.5e+05| 3.225897e+02| 0:0:00| chol  1  1 
 1|0.691|0.699|1.8e+03|2.1e+01|1.7e+05| 7.512804e+02| 0:0:00| chol  1  1 
 2|0.688|0.858|5.6e+02|3.1e+00|5.6e+04| 9.122614e+02| 0:0:00| chol  1  1 
 3|0.934|0.783|3.7e+01|6.8e-01|5.7e+03| 4.783896e+02| 0:0:00| chol  1  1 
 4|0.893|1.000|3.9e+00|2.7e-03|9.2e+02| 9.740333e+01| 0:0:00| chol  1  1 
 5|0.972|0.815|1.1e-01|7.2e-04|7.6e+01|-2.204596e+01| 0:0:00| chol  1  1 
 6|1.000|1.000|3.0e-07|2.7e-05|2.1e+01|-5.867632e+00| 0:0:00| chol  1  1 
 7|0.727|1.000|8.8e-08|2.8e-06|8.4e+00|-2.054497e+00| 0:0:00| chol  1  1 
 8|0.925|0.906|7.1e-09|5.2e-07|1.2e+00|-2.630310e-01| 0:0:00| chol  1  1 
 9|0.912|1.000|6.3e-10|2.8e-08|2.6e-01|-6.407341e-02| 0:0:00| chol  1  1 
10|0.980|1.000|1.3e-11|2.8e-09|7.6e-02|-2.192630e-02| 0:0:00| chol  1  1 
11|0.926|0.928|9.5e-13|4.6e-10|9.8e-03|-2.664391e-03| 0:0:00| chol  1  1 
12|0.767|0.926|2.2e-13|6.0e-11|3.9e-03|-1.042857e-03| 0:0:01| chol  1  1 
13|1.000|0.825|1.9e-13|1.4e-11|1.1e-03|-3.612894e-04| 0:0:01| chol  1  1 
14|0.893|0.964|3.4e-14|1.8e-12|3.4e-04|-9.521374e-05| 0:0:01| chol  1  1 
15|0.545|0.599|2.0e-13|1.7e-12|2.1e-04|-8.125219e-05| 0:0:01| chol  1  1 
16|0.678|0.974|8.7e-12|1.0e-12|1.0e-04|-7.776574e-05| 0:0:01| chol  1  2 
17|0.645|0.736|4.1e-12|1.8e-12|5.7e-05|-9.103753e-05| 0:0:01| chol  1  1 
18|0.821|1.000|1.3e-12|1.0e-12|1.9e-05|-9.749883e-05| 0:0:01| chol  2  2 
19|0.854|0.922|1.0e-11|1.1e-12|4.4e-06|-1.029658e-04| 0:0:01| chol  2  2 
20|0.881|0.910|5.6e-12|1.6e-12|7.1e-07|-1.045233e-04| 0:0:01| chol  1  2 
21|0.949|0.971|5.7e-12|1.2e-12|5.2e-08|-1.048124e-04| 0:0:01| chol  2  1 
22|0.991|0.992|2.8e-11|1.2e-12|1.1e-09|-1.048363e-04| 0:0:01|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 22
 primal objective value = -1.04835733e-04
 dual   objective value = -1.04836785e-04
 gap := trace(XZ)       = 1.05e-09
 relative gap           = 1.05e-09
 actual relative gap    = 1.05e-09
 rel. primal infeas     = 2.81e-11
 rel. dual   infeas     = 1.15e-12
 norm(X), norm(y), norm(Z) = 7.5e-01, 3.1e-01, 7.4e+00
 norm(A), norm(b), norm(C) = 1.6e+02, 2.0e+00, 9.9e+00
 Total CPU time (secs)  = 0.9  
 CPU time per iteration = 0.0  
 termination code       =  0
 DIMACS: 2.8e-11  0.0e+00  5.1e-12  0.0e+00  1.1e-09  1.1e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.000104837
Problem is Solved
The max attenuation in the stopband is -39.79 dB.