Design a 1/f spectrum shaping (pink-noise) filter

% "Filter design" lecture notes (EE364) by S. Boyd
% "FIR filter design via spectral factorization and convex optimization"
% by S.-P. Wu, S. Boyd, and L. Vandenberghe
% (a figure is generated)
%
% Designs a log-Chebychev filter magnitude design given as:
%
%   minimize   max| log|H(w)| - log D(w) |   for w in [0,pi]
%
% where variables are impulse response coefficients h, and data
% is the desired frequency response magnitude D(w).
%
% We can express and solve the log-Chebychev problem above as
%
%   minimize   max( R(w)/D(w)^2, D(w)^2/R(w) )
%       s.t.   R(w) = |H(w)|^2   for w in [0,pi]
%
% where we now use the auto-correlation coeffients r as variables.
%
% As an example we consider the 1/sqrt(w) spectrum shaping filter
% (the so-called pink-noise filter) where D(w) = 1/sqrt(w).
% Here we use a logarithmically sampled freq range w = [0.01*pi,pi].
%
% Written for CVX by Almir Mutapcic 02/02/06

% parameters
n = 40;      % filter order
m = 15*n;    % frequency discretization (rule-of-thumb)

% log-space frequency specification
wa = 0.01*pi; wb = pi;
wl = logspace(log10(wa),log10(wb),m)';

% desired frequency response (pink-noise filter)
D = 1./sqrt(wl);

% matrix of cosines to compute the power spectrum
Al = [ones(m,1) 2*cos(kron(wl,[1:n-1]))];

% solve the problem using cvx
cvx_begin
  variable r(n,1)   % auto-correlation coefficients
  variable R(m,1)   % power spectrum

  % log-chebychev minimax design
  minimize( max( max( [R./(D.^2)  (D.^2).*inv_pos(R)]' ) ) )
  subject to
     % power spectrum constraint
     R == Al*r;
cvx_end

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

% spectral factorization
h = spectral_fact(r);

% figures
figure(1)
H = exp(-j*kron(wl,[0:n-1]))*h;
loglog(wl,abs(H),wl,D,'r--')
set(gca,'XLim',[wa pi])
xlabel('freq w')
ylabel('mag H(w) and D(w)')
legend('optimized','desired')
 
Calling SDPT3: 3000 variables, 641 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 641
 dim. of sdp    var  = 1200,   num. of sdp  blk  = 600
 dim. of linear var  = 600
 dim. of free   var  = 600 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      mean(obj)   cputime
-------------------------------------------------------------------
 0|0.000|0.000|7.8e+03|6.1e+01|5.4e+06| 0.000000e+00| 0:0:00| spchol  1  1 
 1|0.022|0.178|7.6e+03|5.1e+01|1.8e+06|-2.773746e+01| 0:0:01| spchol  1  1 
 2|0.915|0.966|6.5e+02|1.8e+00|8.9e+04|-5.283716e+01| 0:0:01| spchol  1  1 
 3|0.986|0.979|9.1e+00|4.5e-02|1.3e+03|-4.650149e+01| 0:0:01| spchol  2  2 
 4|0.993|0.998|6.4e-02|9.8e-04|8.5e+01|-3.915471e+01| 0:0:02| spchol  2  2 
 5|1.000|0.957|3.3e-04|1.3e-02|4.2e+00|-2.095337e+00| 0:0:02| spchol  1  2 
 6|0.361|0.924|2.1e-04|1.1e-03|2.2e+00|-1.519518e+00| 0:0:03| spchol  2  2 
 7|1.000|0.233|9.0e-06|8.6e-04|1.4e+00|-1.608899e+00| 0:0:03| spchol  2  2 
 8|1.000|0.453|1.5e-05|4.7e-04|7.9e-01|-1.373633e+00| 0:0:03| spchol  2  2 
 9|1.000|0.219|2.8e-06|3.7e-04|6.5e-01|-1.313597e+00| 0:0:04| spchol  2  2 
10|1.000|0.270|1.2e-06|2.7e-04|5.0e-01|-1.264834e+00| 0:0:04| spchol  2  2 
11|1.000|0.318|3.0e-07|1.9e-04|3.5e-01|-1.232707e+00| 0:0:05| spchol  2  1 
12|1.000|0.351|4.4e-08|1.2e-04|2.3e-01|-1.215121e+00| 0:0:05| spchol  2  2 
13|1.000|0.407|5.5e-09|6.6e-05|1.2e-01|-1.203334e+00| 0:0:06| spchol  2  2 
14|1.000|0.818|6.3e-09|2.3e-05|3.1e-02|-1.179959e+00| 0:0:06| spchol  2  2 
15|0.789|0.831|3.1e-09|5.3e-06|1.3e-02|-1.182598e+00| 0:0:06| spchol  2  2 
16|0.817|0.900|5.2e-10|2.1e-06|4.1e-03|-1.185630e+00| 0:0:07| spchol  2  2 
17|0.856|0.809|1.1e-10|6.6e-07|8.1e-04|-1.186983e+00| 0:0:07| spchol  1  1 
18|0.904|0.891|1.7e-11|1.3e-07|1.1e-04|-1.187284e+00| 0:0:08| spchol  1  1 
19|0.974|0.964|7.3e-12|1.8e-08|5.6e-06|-1.187329e+00| 0:0:08| spchol  1  1 
20|0.994|0.988|1.0e-12|9.0e-10|1.2e-07|-1.187331e+00| 0:0:08| spchol  1  1 
21|1.000|0.986|2.7e-13|2.0e-11|3.2e-09|-1.187331e+00| 0:0:09|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 21
 primal objective value = -1.18733108e+00
 dual   objective value = -1.18733108e+00
 gap := trace(XZ)       = 3.17e-09
 relative gap           = 9.39e-10
 actual relative gap    = 9.30e-10
 rel. primal infeas     = 2.67e-13
 rel. dual   infeas     = 1.95e-11
 norm(X), norm(y), norm(Z) = 1.2e+01, 2.6e+02, 2.7e+02
 norm(A), norm(b), norm(C) = 3.1e+02, 2.0e+00, 3.6e+01
 Total CPU time (secs)  = 8.8  
 CPU time per iteration = 0.4  
 termination code       =  0
 DIMACS: 2.7e-13  0.0e+00  3.5e-10  0.0e+00  9.3e-10  9.4e-10
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.18733
Problem is Solved