You are on page 1of 47

Signal Processing

Lecture 10: FIR Filter Design

Ahmet Taha Koru, Ph. D.

Yildiz Technical University

2017-2018 Fall

ATK (YTU) Signal Processing 2017-2018 Fall 1 / 47


Introduction

Introduction

ATK (YTU) Signal Processing 2017-2018 Fall 2 / 47


Introduction

What is FIR filter?

FIR = Finite impulse response


Impulse response has finite duration
Settles to zero in finite time.

x[n]

Figure: A FIR filter impulse response.

ATK (YTU) Signal Processing 2017-2018 Fall 3 / 47


Introduction

Definition

For a causal discrete-time FIR filter of order N, the output is

y [n] = a0 x[n] + a1 x[n − 1] + a2 x[n − 2] + · · · + aN x[n − N]


N
X
y [n] = ak x[n − k]
k=0

The impulse response of the filter is


N
X
h[n] = ak δ[n − k]
k=0

ATK (YTU) Signal Processing 2017-2018 Fall 4 / 47


Introduction

Structure of FIR Filter

x[n]
z −1 z −1 z −1

h[0] h[1] h[2] h[3]


y [n]

Figure: Block diagram of a FIR filter

ATK (YTU) Signal Processing 2017-2018 Fall 5 / 47


Introduction

FIR Properties

Require no feedback of the output. Simpler implementation.


Always stable. For
N
X
|x| ≤ Bx ⇒ |y | ≤ |ak |Bx
k=0

Can easily be designed to be linear phase by making the coefficient


sequence symmetric.

Disadvantage:
Requires more taps and computational power is required compared to
an IIR filter with similar sharpness or selectivity, especially for low
frequency cut-offs.

ATK (YTU) Signal Processing 2017-2018 Fall 6 / 47


Introduction

Design Methods

Window design method


Frequency sampling method
Weighted least squares design
Linear programming
Parks-McClellan method
Equiripple FIR filters design
Etc...

ATK (YTU) Signal Processing 2017-2018 Fall 7 / 47


Introduction

Filtering Specifications

H(e jω )
1+δ
1 ∆ω Transition band
1−δ δ Ripple
[ωs , π] Stop band
[0, ωp ] Pass band

∆ω

δ
ω
ωp ωc ωs π

ATK (YTU) Signal Processing 2017-2018 Fall 8 / 47


Window Design Method

Window Design Method

ATK (YTU) Signal Processing 2017-2018 Fall 9 / 47


Window Design Method

What is window design method?

Ideal low-pass filters have infinite length of duration.


Windowing is truncating the ideal low pass impulse response.
Windowed impulse response is not ideal but an approximation
There are different types of windows.

ATK (YTU) Signal Processing 2017-2018 Fall 10 / 47


Window Design Method

An example: Low pass filter

hi [n]

w [n]

hFIR [n] = hi [n] · w [n]

ATK (YTU) Signal Processing 2017-2018 Fall 11 / 47


Window Design Method Ideal Filters

Window Design Method: Ideal Filters

ATK (YTU) Signal Processing 2017-2018 Fall 12 / 47


Window Design Method Ideal Filters

Low-pass filter

HLP (e jω )

jω 1, |ω| ≤ ωc
HLP (e ) =
0, ωc < |ω| ≤ π

n
−π −ωc ωc π

hLP [n]

sin(ωc n)
hLP [n] = ,
πn
n
Matlab implementation:
>> hlp = sinc(wc/pi*n)*wc/pi;

ATK (YTU) Signal Processing 2017-2018 Fall 13 / 47


Window Design Method Ideal Filters

High-pass filter

HHP (e jω )

jω 0, |ω| ≤ ωc
HHP (e ) =
1, ωc < |ω| ≤ π

n
−π −ωc ωc π

hHP [n]

sin(ωc n)
hHP [n] = δ[n] −
πn n
Matlab implementation:
>> hhp = [dirac(n2)>1] - sinc(wc/pi*n2)*wc/pi;

ATK (YTU) Signal Processing 2017-2018 Fall 14 / 47


Window Design Method Ideal Filters

Band-pass filter


 0, |ω| ≤ ωc1
HBP (e jω ) = 1, ωc1 < |ω| ≤ ωc2

0, ωc2 < |ω| ≤ π HBP (e jω )

n
−ωc2 -ωc1 ωc1 ωc2 π

sin(ωc2 n) sin(ωc1 n)
hBP [n] = −
πn πn
Matlab implementation:
>> hbp = sinc(wc2/pi*n)*wc2/pi - sinc(wc1/pi*n)*wc1/pi;

ATK (YTU) Signal Processing 2017-2018 Fall 15 / 47


Window Design Method Ideal Filters

Band-stop filter


 1, |ω| ≤ ωc1

HBS (e ) = 0, ωc1 < |ω| ≤ ωc2

1, ωc2 < |ω| ≤ π HBS (e jω )

n
−ωc2 -ωc1 ωc1 ωc2 π

sin(ωc1 n) sin(ωc2 n)
hBS [n] = +δ[n]−
πn πn
Matlab implementation:
>> hbs = sinc(wc1/pi*n)*wc1/pi + [dirac(n)>1] ...
- sinc(wc2/pi*n)*wc2/pi;

ATK (YTU) Signal Processing 2017-2018 Fall 16 / 47


Window Design Method Ideal Filters

Multi-level Frequency Filter

HML (e jω ) = Ak , for ωk−1 ≤ ω ≤ ωk HML (e jω )

L  
X sin(ωl n)
hML [n] = (Al − Al+1 ) ·
πn n
l=1
ω1 ω2 ω3 π
with AL+1 = 0.

Matlab implementation:
>> n = -100:100; % Length of filter
>> A = [3, 1, 0, 2, 0]; % List magnitudes
>> w = [0.3*pi, 0.55*pi, 0.8*pi,1*pi]; % List frequencies
>> h = zeros(size(n));
>> for k = 1:4
>> h = h + (A(k) - A(k+1))*sinc(w(k)/pi*n)*w(k)/pi;
>> end
ATK (YTU) Signal Processing 2017-2018 Fall 17 / 47
Window Design Method Ideal Filters

Hilbert Transformer


jω j, −π ≤ ω < 0
HHT (e ) =
−j, 0 ≤ ω ≤ π


0, for n even
hHT [n] = 2
πn , for n odd

Matlab implementation:
>> h = mod(n,2)*2/pi./n;

ATK (YTU) Signal Processing 2017-2018 Fall 18 / 47


Window Design Method Ideal Filters

Differantiator

HDIFF (e jω ) = jω


0, n=0
hDIFF [n] = cos(πn)
n , |n| > 0

Matlab implementation:
>> h = cos(pi*n)./n;
>> h(find(isinf(h))) = 0;

ATK (YTU) Signal Processing 2017-2018 Fall 19 / 47


Window Design Method Window Types

Window Design Method: Window Types

ATK (YTU) Signal Processing 2017-2018 Fall 20 / 47


Window Design Method Window Types

Window Types: Rectangular

w [n] = u[n + M] − u[n − M − 1]

w [n]
1

n
−M M

ATK (YTU) Signal Processing 2017-2018 Fall 21 / 47


Window Design Method Window Types

Window Types: Bartlett

|n|
w [n] = 1 − , −M ≤ n ≤ M
M +1

w [n]
1

n
−M − 1 M +1

ATK (YTU) Signal Processing 2017-2018 Fall 22 / 47


Window Design Method Window Types

Window Types: Hann


  
1 2πn
w [n] = 1 + cos , −M ≤ n ≤ M
2 2M + 1

w [n]
1

n
−M M

ATK (YTU) Signal Processing 2017-2018 Fall 23 / 47


Window Design Method Window Types

Window Types: Hamming


 
2πn
w [n] = 0.54 + 0.46 cos , −M ≤ n ≤ M
2M + 1

w [n]
1

n
−M M

ATK (YTU) Signal Processing 2017-2018 Fall 24 / 47


Window Design Method Window Types

Window Types: Blackman

   
2πn 4πn
w [n] = 0.42 + 0.5 cos + 0.08 cos , −M ≤ n ≤ M
2M + 1 2M + 1

w [n]
1

n
−M M
ATK (YTU) Signal Processing 2017-2018 Fall 25 / 47
Window Design Method Window Types

Properties of some fixed window functions

Type of Main Lobe Relative Sideslobe Minimum Stopband Transition


Window Width Level Attenuation Bandwidth ∆ω
Rectangular 4π/(2M + 1) 13.3 dB 20.9 dB 0.92 π/M
Bartlett 4π/(2M + 1) 26.5 dB - -
Hann 8π/(2M + 1) 31.5 dB 43.9 dB 3.11 π/M
Hamming 8π/(2M + 1) 42.7 dB 54.5 dB 3.32 π/M
Blackman 12π/(2M + 1) 58.1 dB 75.3 dB 5.56 π/M

ATK (YTU) Signal Processing 2017-2018 Fall 26 / 47


Parks-McClellan Method

Parks-McClellan Method

ATK (YTU) Signal Processing 2017-2018 Fall 27 / 47


Parks-McClellan Method

What is Parks-McClellan Algorithm

Is an iterative algorithm for finding the optimal Chebyshev FIR filters.


Powerful tool for design and implement efficient and optimal FIR
filters.
Can easily be calculated with help of a computer.

ATK (YTU) Signal Processing 2017-2018 Fall 28 / 47


Parks-McClellan Method

What does algorithm do?

A desired filter frequency response


Tries to minimize error the difference with desired response
Matlab Code: firpm

ATK (YTU) Signal Processing 2017-2018 Fall 29 / 47


Parks-McClellan Method

Example: Low-pass Filter

Hd (e jω )
Desired frequency
1
response

n
ωp ωs π
H(e jω )
Parks-McClellan
algorithm design

n
ωs π

ATK (YTU) Signal Processing 2017-2018 Fall 30 / 47


Parks-McClellan Method

Matlab Implementation

N = 5; % Length of filter
wc = pi/2; % Cut-off Frequency
dw = 0.1*pi; % Transition Band

% Parks-McClellan Algorithm
h = firpm(N*2, [0 (wc-dw)/pi (wc+dw)/pi 1], [1 1 0 0]);

%Plot the response


[H,W] = freqz(h);
plot(W/pi, abs(H))

ATK (YTU) Signal Processing 2017-2018 Fall 31 / 47


Linear Programming Method

Linear Programming Method

ATK (YTU) Signal Processing 2017-2018 Fall 32 / 47


Linear Programming Method

What is Linear Programming?

Linear programming is optimization


Linear cost function
Linear constraints

General form of a linear program is

minimize cT x
subject to Ax ≤ b

where x is the vector to be designed.

ATK (YTU) Signal Processing 2017-2018 Fall 33 / 47


Linear Programming Method

The General Structure of a FIR Filter


Let us consider a symmetric FIR filter such that

h[−n] = h[n], for all n

Suppose the coefficients of the filters are ak such that


N
X
h[n] = ak δ[n − k]
k=−N

Then, the frequency response of the filter is


N
X
H(e jω ) = a0 + 2 ak cos(kω)
k=1

We would like H(e jω ) to approximate an desired filter Hd (e jω ).


ATK (YTU) Signal Processing 2017-2018 Fall 34 / 47
Linear Programming Method Low-pass Filter

Linear Programming Method: Low-pass Filter

ATK (YTU) Signal Processing 2017-2018 Fall 35 / 47


Linear Programming Method Low-pass Filter

Filtering Specifications

H(e jω )
1+δ
1 ∆ω Transition band
1−δ δ Ripple
[ωs , π] Stop band
[0, ωp ] Pass band

∆ω

δ
ω
ωp ωc ωs π

ATK (YTU) Signal Processing 2017-2018 Fall 36 / 47


Linear Programming Method Low-pass Filter

Constraints: Pass-band (1/2)

In the pass-band:

N
X
a0 + 2 ak cos(kω) − 1 ≤ δ


k=1

for all 0 ≤ ω ≤ ωc .
Equivalently,
N
!
X
a0 + 2 ak cos(kω) −δ ≤1
k=1
N
!
X
− a0 + 2 ak cos(kω) − δ ≤ −1
k=1

ATK (YTU) Signal Processing 2017-2018 Fall 37 / 47


Linear Programming Method Low-pass Filter

Constraints: Pass-band (2/2)

If we re-write the conditions in vector form


 
a0
  a1   
1 2 cos(ω) 2 cos(2ω) · · · 2 cos(Nω) −1 1
 
..
≤
 
1 2 cos(ω) 2 cos(2ω) · · · 2 cos(Nω) −1

 .  −1
 aN 
δ

for any 0 ≤ ω ≤ ωp .

ATK (YTU) Signal Processing 2017-2018 Fall 38 / 47


Linear Programming Method Low-pass Filter

Constraints: Transition-band

In the transition-band: No constraints

ATK (YTU) Signal Processing 2017-2018 Fall 39 / 47


Linear Programming Method Low-pass Filter

Constraints: Stop-band (1/2)

In the stop-band:
N
X
a0 + 2 ak cos(kω) ≤ δ


k=1

Equivalently,
N
!
X
a0 + 2 ak cos(kω) −δ ≤0
k=1
N
!
X
− a0 + 2 ak cos(kω) −δ ≤0
k=1

ATK (YTU) Signal Processing 2017-2018 Fall 40 / 47


Linear Programming Method Low-pass Filter

Constraints: Stop-band (2/2)

If we re-write the conditions in vector form


 
a0
  a1   
1 2 cos(ω) 2 cos(2ω) · · · 2 cos(Nω) −1 0
 
..
≤
 
1 2 cos(ω) 2 cos(2ω) · · · 2 cos(Nω) −1

 .  0
 aN 
δ

for any ωs ≤ ω ≤ π.

ATK (YTU) Signal Processing 2017-2018 Fall 41 / 47


Linear Programming Method Low-pass Filter

Optimization Variables and Other Parameters


Optimization variables to be designed are listed in the vector
 
a0
 a1 
 
x =  ... 
 
 
 aN 
δ

Given parameters are


Number of taps: N
Cut-off frequency: ωc
Transition bandwidth: ∆ω
From ∆ω, pass-band freq: ωp = ωc − ∆ω/2
From ∆ω, stop-band freq: ωs = ωc + ∆ω/2
ATK (YTU) Signal Processing 2017-2018 Fall 42 / 47
Linear Programming Method Low-pass Filter

Cost Function and the Optimization Problem


The cost function to be minimized is the amount of ripple δ and
 
δ = 0 0 0 ··· 0 1 · x

Then, the optimization problem is


 
minimize 0 0 0 ··· 0 1 ·x
 
a0
 a
 1

   
1 2 cos(ω) ··· 2 cos(Nω) −1  ..
 
≤ 1  
subject to , ∀ω ∈ 0, ωp
−1 −2 cos(ω) ··· −2 cos(Nω) −1  .


 −1
 a 
N
δ
 
a0
 a
 1

   
1 2 cos(ω) ··· 2 cos(Nω) −1  . 
≤ 0
 . , ∀ω ∈ [ωs , π]
−1 −2 cos(ω) ··· −2 cos(Nω) −1  .


 0
 a 
N
δ

There are infinitely many constraints. Problem is not solvable.


ATK (YTU) Signal Processing 2017-2018 Fall 43 / 47
Linear Programming Method Low-pass Filter

Discretization
We can not evaluate at every ω point but instead in M samples among
ω ∈ [0, π]. Let’s choose
M = 15N
The samples are chosen as
π·k
ωk = , k = 0, 1, · · · , M
M
Then, the constraints become
   
1 2 ··· 2 −1   1
 −1 −2 ··· −2 −1  −1
 a0
 
  
 1 2 cos(ω1 ) · · · 2 cos(Nω1 ) −1   a1   1 
    
 −1 −2 cos(ω1 ) · · · −2 cos(Nω1 ) −1   .. −1
≤
  
  . 
 ..    .. 

 .   aN

 
 . 

 1 2 cos(ωM ) · · · 2 cos(NωM ) −1  δ  0 
−1 −2 cos(ωM ) · · · −2 cos(NωM ) −1 0
ATK (YTU) Signal Processing 2017-2018 Fall 44 / 47
Linear Programming Method Low-pass Filter

Matlab Implementation (1/3)

% % Set the given parameters


L = 20; % Length of the filter N = 2L + 1
wc = pi/2; % Cut-off frequency
dw = 0.1*pi; % Transition bandwidth
wp = wc - dw/2; ws = wc + dw/2; % Pass-band and stop-band freqs.
% % Discretize
M = 15*L;
wl = linspace(0, pi, M + 1);
R = sum(wl <= wp) + sum(wl >=ws) - 2; % Number of constraints
A = zeros(R, L+2); b = zeros(R,1);
cnt = 1;

ATK (YTU) Signal Processing 2017-2018 Fall 45 / 47


Linear Programming Method Low-pass Filter

Matlab Implementation (2/3)

for k = 1:length(wl)
w = wl(k);
if w <= wp
A(cnt,:) = [1, 2*cos([1:L]*w), -1];
A(cnt+1,:) = [-1, -2*cos([1:L]*w), -1];
b(cnt) = 1;
b(cnt+1) = -1;
cnt = cnt+2;
elseif w >= ws
A(cnt,:) = [1, 2*cos([1:L]*w), -1];
A(cnt+1,:) = [-1, -2*cos([1:L]*w), -1];
b(cnt) = 0;
b(cnt+1) = 0;
cnt = cnt+2;
end
end

ATK (YTU) Signal Processing 2017-2018 Fall 46 / 47


Linear Programming Method Low-pass Filter

Matlab Implementation (3/3)

c = [zeros(1,L+1), 1]; % Cost function to minimize c'*x


x = linprog(c, A, b); % Minimize c'*x subject to Ax <= b
hhalf = x(1:L+1);
h = [hhalf(end:-1:2); hhalf]; % Construct the filter

[H, W] = freqz(h);
plot(W/pi, abs(H))

ATK (YTU) Signal Processing 2017-2018 Fall 47 / 47

You might also like