Chebychev design of an FIR filter given a desired H(w)

% "Filter design" lecture notes (EE364) by S. Boyd
% (figures are generated)
%
% Designs an FIR filter given a desired frequency response H_des(w).
% The design is judged by the maximum absolute error (Chebychev norm).
% This is a convex problem (after sampling it can be formulated as an SOCP).
%
%   minimize   max |H(w) - H_des(w)|     for w in [0,pi]
%
% where H is the frequency response function and variable is h
% (the filter impulse response).
%
% Written for CVX by Almir Mutapcic 02/02/06

%********************************************************************
% problem specs
%********************************************************************
% number of FIR coefficients (including the zeroth one)
n = 20;

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

%********************************************************************
% construct the desired filter
%********************************************************************
% fractional delay
D = 8.25;            % delay value
Hdes = exp(-j*D*w);  % desired frequency response

% Gaussian filter with linear phase (uncomment lines below for this design)
% var = 0.05;
% Hdes = 1/(sqrt(2*pi*var))*exp(-(w-pi/2).^2/(2*var));
% Hdes = Hdes.*exp(-j*n/2*w);

%*********************************************************************
% solve the minimax (Chebychev) design problem
%*********************************************************************
% A is the matrix used to compute the frequency response
% A(w,:) = [1 exp(-j*w) exp(-j*2*w) ... exp(-j*n*w)]
A = exp( -j*kron(w,[0:n-1]) );

% optimal Chebyshev filter formulation
cvx_begin
  variable h(n,1)
  minimize( max( abs( A*h - Hdes ) ) )
cvx_end

% check if problem was successfully solved
disp(['Problem is ' cvx_status])
if ~strcmp(cvx_status,'Solved')
  h = [];
end

%*********************************************************************
% plotting routines
%*********************************************************************
% plot the FIR impulse reponse
figure(1)
stem([0:n-1],h)
xlabel('n')
ylabel('h(n)')

% plot the frequency response
H = [exp(-j*kron(w,[0:n-1]))]*h;
figure(2)
% magnitude
subplot(2,1,1);
plot(w,20*log10(abs(H)),w,20*log10(abs(Hdes)),'--')
xlabel('w')
ylabel('mag H in dB')
axis([0 pi -30 10])
legend('optimized','desired','Location','SouthEast')
% phase
subplot(2,1,2)
plot(w,angle(H))
axis([0,pi,-pi,pi])
xlabel('w'), ylabel('phase H(w)')
 
Calling SeDuMi: 919 variables (20 free), 898 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 20 free variables
eqs m = 898, order n = 641, dim = 940, blocks = 300
nnz(A) = 897 + 23962, nnz(ADA) = 2691, nnz(L) = 1795
Handling 42 + 0 dense columns.
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            5.62E-002 0.000
  1 :  8.11E-001 1.88E-002 0.000 0.3348 0.9000 0.9000   0.74  1  1  1.8E+000
  2 :  6.76E-001 6.55E-003 0.000 0.3483 0.9000 0.9000   3.34  1  1  2.3E-001
  3 :  6.48E-001 2.95E-003 0.000 0.4512 0.9000 0.9000   2.08  1  1  8.2E-002
  4 :  7.01E-001 8.48E-004 0.000 0.2870 0.9000 0.9000   1.08  1  1  2.4E-002
  5 :  7.07E-001 7.53E-006 0.000 0.0089 0.9990 0.9990   1.04  1  1  2.0E-004
  6 :  7.07E-001 1.22E-006 0.097 0.1626 0.9094 0.9000   1.00  1  1  3.6E-005
  7 :  7.07E-001 4.29E-008 0.000 0.0350 0.9903 0.9900   1.00  1  1  1.6E-006
  8 :  7.07E-001 9.64E-009 0.296 0.2246 0.9187 0.9000   1.00  1  1  3.9E-007
  9 :  7.07E-001 2.17E-009 0.050 0.2252 0.9188 0.9000   1.00  1  1  9.7E-008
 10 :  7.07E-001 1.88E-012 0.197 0.0009 0.9999 0.9999   1.00  2  2  8.3E-011

iter seconds digits       c*x               b*y
 10      0.9  10.1  7.0710678141e-001  7.0710678135e-001
|Ax-b| =  3.2e-012, [Ay-c]_+ =  4.1E-011, |x|= 1.3e+001, |y|= 1.4e+000

Detailed timing (sec)
   Pre          IPM          Post
8.012E-002    9.113E-001    0.000E+000    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 2, ||L.L|| = 1.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.707107
Problem is Solved