Minimize stopband ripple of a linear phase lowpass FIR filter

% "Filter design" lecture notes (EE364) by S. Boyd
% (figures are generated)
%
% Designs a linear phase FIR lowpass filter such that it:
% - minimizes the maximum passband ripple
% - has a constraint on the maximum stopband attenuation
%
% This is a convex problem.
%
%   minimize   delta
%       s.t.   1/delta <= H(w) <= delta     for w in the passband
%              |H(w)| <= atten_level        for w in the stopband
%
% where H is the frequency response function and variables are
% delta and h (the filter impulse response).
%
% Written for CVX by Almir Mutapcic 02/02/06

%********************************************************************
% user's filter specifications
%********************************************************************
% filter order is 2n+1 (symmetric around the half-point)
n = 10;

wpass = 0.12*pi;        % passband cutoff freq (in radians)
wstop = 0.24*pi;        % stopband start freq (in radians)
atten_level = -30;      % stopband attenuation level in dB

%********************************************************************
% create optimization parameters
%********************************************************************
N = 30*n+1;                            % freq samples (rule-of-thumb)
w = linspace(0,pi,N);
A = [ones(N,1) 2*cos(kron(w',[1:n]))]; % matrix of cosines

% passband 0 <= w <= w_pass
ind = find((0 <= w) & (w <= wpass));   % passband
Ap  = A(ind,:);

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

% stopband (w_stop <= w)
ind = find((wstop <= w) & (w <= pi));  % stopband
Us  = 10^(atten_level/20)*ones(length(ind),1);
As  = A(ind,:);

%********************************************************************
% optimization
%********************************************************************
% formulate and solve the linear-phase lowpass filter design
cvx_begin
  variable delta
  variable h(n+1,1);

  minimize( delta )
  subject to
    % passband bounds
    Ap*h <= delta;
    inv_pos(Ap*h) <= delta;

    % stopband bounds
    abs( As*h ) <= Us;
cvx_end

% check if problem was successfully solved
disp(['Problem is ' cvx_status])
if ~strcmp(cvx_status,'Solved')
  return
else
  % construct the full impulse response
  h = [flipud(h(2:end)); h];
  fprintf(1,'The optimal minimum passband ripple is %4.3f dB.\n\n',...
            20*log10(delta));
end

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

figure(2)
% frequency response
H = exp(-j*kron(w',[0:2*n]))*h;
% magnitude
subplot(2,1,1)
plot(w,20*log10(abs(H)),[wstop pi],[atten_level atten_level],'r--');
axis([0,pi,-40,10])
xlabel('w'), ylabel('mag H(w) in dB')
% phase
subplot(2,1,2)
plot(w,angle(H))
axis([0,pi,-pi,pi])
xlabel('w'), ylabel('phase H(w)')
 
Calling SeDuMi: 617 variables (11 free), 605 equality constraints
------------------------------------------------------------------------
SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 11 free variables
eqs m = 605, order n = 592, dim = 666, blocks = 38
nnz(A) = 7802 + 0, nnz(ADA) = 96929, nnz(L) = 49397
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            6.05E-002 0.000
  1 :  5.31E+000 1.24E-002 0.000 0.2042 0.9000 0.9000  -0.46  1  1  2.0E+000
  2 :  1.67E+000 6.01E-003 0.000 0.4868 0.9000 0.9000   4.53  1  1  3.0E-001
  3 :  1.14E+000 3.43E-003 0.000 0.5697 0.9000 0.9000   6.94  1  1  4.2E-002
  4 :  1.06E+000 1.22E-003 0.000 0.3569 0.9000 0.9000   1.89  1  1  1.2E-002
  5 :  1.05E+000 5.34E-004 0.000 0.4367 0.9000 0.9000   1.35  1  1  4.6E-003
  6 :  1.05E+000 1.37E-004 0.000 0.2568 0.9000 0.9000   1.16  1  1  1.1E-003
  7 :  1.05E+000 5.08E-005 0.000 0.3703 0.9000 0.9000   1.04  1  1  4.2E-004
  8 :  1.05E+000 6.01E-006 0.000 0.1183 0.9000 0.0000   1.01  1  1  2.4E-004
  9 :  1.05E+000 7.56E-007 0.000 0.1258 0.9252 0.9000   1.01  1  1  5.5E-005
 10 :  1.05E+000 2.10E-007 0.000 0.2781 0.9000 0.8845   1.00  1  1  1.5E-005
 11 :  1.05E+000 7.82E-008 0.000 0.3718 0.9067 0.9000   1.00  1  1  5.7E-006
 12 :  1.05E+000 2.13E-008 0.000 0.2727 0.9000 0.9063   1.00  1  1  1.5E-006
 13 :  1.05E+000 5.08E-009 0.000 0.2382 0.9000 0.8283   1.00  1  2  3.6E-007
 14 :  1.05E+000 9.98E-010 0.000 0.1966 0.9000 0.9000   1.00  2  2  7.2E-008
 15 :  1.05E+000 2.53E-011 0.000 0.0254 0.9901 0.9900   1.00  2  2  1.9E-009

iter seconds digits       c*x               b*y
 15      1.7   Inf  1.0515780094e+000  1.0515780105e+000
|Ax-b| =  4.5e-009, [Ay-c]_+ =  1.5E-009, |x|= 1.2e+001, |y|= 1.2e+000

Detailed timing (sec)
   Pre          IPM          Post
1.502E-001    1.662E+000    0.000E+000    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1348.41.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.05158
Problem is Solved
The optimal minimum passband ripple is 0.437 dB.