Digital Filter Design Using Optimization
Digital Filter Design Using Optimization
Digital Filter Design Using Optimization
M ATS V IBERG
Chalmers University of Technology
Department of Signals and Systems
SE-412 96 Göteborg, Sweden
Email: viberg@chalmers.se
Mini-project for the Applied Optimization Course, Spring 2008.
& %
Applied Optimization Slide 1
'CHALMERS
$
Outline
Sampling
2.5
2
x(t) and samples x(n)
1.5
0.5
0
0 5 10 15 20 25 30
time [s]
Digital Filters
x(n) y(n)
Digital Filter
First-order filter:
& %
Applied Optimization Slide 4
'CHALMERS
$
Time-Domain Characterization
Impulse response h(n) = filter output when x(n) = δ(n). I/O relation:
∞
y(n) = h(k)x(n − k)
k=−∞
Discrete-Time Fourier
Transform
Frequency Response
Stability
x(n) = sin(Ωn)
The filter changes the frequency content of a signal - we can suppress noise
and interference!
Filter design = choice of coefficients {ak }N
k=1 and {bk }k=1 so that
M
0.9
0.8
0.7
0.6
|H|2
0.5
0.4
0.3
0.2
0.1
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Normalized frequency F/Fs
& %
Applied Optimization Slide 11
'CHALMERS
$
0.9
0.8
0.7
0.6
|H|2
0.5
0.4
0.3
0.2
0.1
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Normalized frequency F/Fs
& %
Applied Optimization Slide 12
'CHALMERS
$
• A digital filter is stable if all poles are inside the unit circle
& %
Applied Optimization Slide 13
'CHALMERS
$
−10
−20
−30
Gain [dB]
−40
−50
−60
−70
−80
−90
0 0.5 1 1.5 2 2.5
Frequency [Hz] 4
x 10
where
MΩ (M − 2)Ω Ω
A(Ω) = 2b0 cos + 2b1 cos + · · · + 2b(M −1)/2 cos
2 2 2
is a real function! Thus, the phase Arg{H(ejΩ )} = −(M/2)Ω is linear in
Ω - corresponds to a time-delay by M/2 samples and no phase distortion!
Similar calculations when bi = −bM −i and/or M even - still linear phase,
but real function A(Ω) slightly different.
& %
Applied Optimization Slide 16
'CHALMERS
$
A(Ωk , b) = Ad (Ωk ), k = 1, 2, . . . , K
A(Ω, b) = φT (Ω)b
where
MΩ (M − 2)Ω Ω
φT (Ω) = 2[cos , cos , . . . , cos ]
2 2 2
& %
Applied Optimization Slide 18
'CHALMERS
$
& %
Applied Optimization Slide 19
'CHALMERS
$
ΦT WΦ bLS = ΦT Wad
& %
Applied Optimization Slide 21
'CHALMERS
$
"Easy" to see that the error |E(Ω, bP M )| has equi-ripple - the maximum
|E(Ωi , bP M )| = δ is "generically" attained at (M + 3)/2 frequencies Ωi ,
with alternating sign of E(Ω). This is the "alternation theorem".
If the Ωi s were known, we could use this insight to find bP M by solving a
linear equation system ((M + 3)/2 equations for (M + 3)/2 unknowns,
including δ). Parks and McClellan’s algorithm iterates between solving for
b and δ, and updating the Ωi s by finding all local maxima in an efficient
way. In Matlab, this is called firpm. Your goal is to implement your own
algorithm, and compare with firpm!
& %
Applied Optimization Slide 22
'CHALMERS
$
& %
Applied Optimization Slide 23
'CHALMERS
$
1. Warm-up. The simple averaging filter
M
1
y(n) = x(n − k)
M +1 k=0
corresponds to an FIR filter with bk = 1/(M + 1), k = 0, . . . , M . Compute and display the
corresponding frequency response, using the Matlab commands:
» M = 10; %%% Filter order
» h = ones(M+1,1)/(M+1); %%% Impulse response
» [H,w] = freqz(h,1,1024); %%% Frequency response
» plot(w/2/pi,20*log10(abs(H))); %%% Magnitude plot in dB
» xlabel(’Frequency [normalized]’)
» ylabel(’Magnitude [dB]’)
Hopefully, you will see that this is a lowpass filter with bandwidth approximately 1/M and stopband
damping −13 dB (which does not improve with increasing M !).
2. Least-squares design. Write a Matlab function that implements the weighted least-squares digital FIR
filter design. The input should be:
• M , the filter order (corresponding to M + 1 filter taps)
• F , a vector of frequency samples
• A, a vector of desired response values (of equal length as F )
• W , a vector of weights to be used in the LS solution
& %
Applied Optimization Slide 24
'CHALMERS
$
The output of the function is the filter coefficients b LS . Hint: the following commands are useful for
setting up the matrix Φ, avoiding for-loops that are slow in Matlab:
» MM = (M:-2:1)/2;
» F = [linspace(0,Fp,K/2) linspace(Fs,0.5,K/2)];
» Phi = 2*cos(2*pi*F’*MM);
3. Run your LS design for increasing filter orders M and different choices of passband and stopband
weights W . Use the following set of commands for the first run:
» M = 11; %%% Filter order
» K = 100; %%% Number of frequency samples
» Fp = 0.25; Fs = 0.3; %%% Passband and stopband edge
» F = [linspace(0,Fp,K/2) linspace(Fs,0.5,K/2)]; %%% Frequency
samples
» A = [ones(1,K/2) zeros(1,K/2)]; %%% Desired response
» Wpass = 1; Wstop = 1; %%% Passband and stopband weights
» W = [Wpass*ones(1,K/2) Wstop*ones(1,K/2)]; %%%
» bLS = LSdesign(M,F,A,W); %%% Name your function "LSdesign.m"
» [H,w] = freqz(bLS,1,1024); %%% Frequency response
» plot(w/2/pi,20*log10(abs(H))); %%% Check if the specifications
are met
» xlabel(’Frequency [normalized]’)
& %
Applied Optimization Slide 25
'CHALMERS
$
» ylabel(’Magnitude [dB]’)
Spend some time to select the input to the filter so you get something that almost meets the
specifications of the test problem. Compare your results with that of Matlab’s implementation:
firls. Note that the syntax is slightly different for firls. You should use the following command:
» bFIRLS = firls(M,2*[0 0.25 0.3 0.5],[1 1 0 0],[Wpass Wstop]);
4. Parks-McClellan design. Write a Matlab function that implements the Parks-McClellan digital FIR
filter design for the low-pass case. The input should be:
• M , the filter order (corresponding to M + 1 filter taps).
• F , a vector of frequencies used to separate the frequency bands, i.e. F = [0, F p, F s, 0.5]
(assume a lowpass filter!).
• A = [1, 1, 0, 0], a vector used to define that there are two bands, where the first is a passband
and the second a stopband (only this case needs to be implemented).
• W = [Wpass , Wstop ], specifies the weights for the passband and stopband respectively.
The output is the filter coefficients b P M , obtained by solving the minimax optimization problem:
The simplest way to solve the optimization problem is to use Matlab’s fminimax routine. See help
& %
Applied Optimization Slide 26
'CHALMERS
$
fminimax for details. If you are really interested, you could also try to implement your own code
based on the iterative procedure described above. Contact viberg@chalmers.se in that case.
Regardless which implementation you have used, you should verify that your resulting filter is indeed
equi-ripple!
5. Spend some time to select the input to the filter (M and the weights) so that the resulting amplitude
response meets the specifications of the test problem. Compare your results with that of Matlab’s
implementation firpm. Use:
» bFIRPM = firpm(M,2*[0 0.25 0.3 0.5],[1 1 0 0],[Wpass Wstop]);
(there is also a command firpmord that estimates the necessary filter order to satisfy the given
specifications).
& %
Applied Optimization Slide 27