Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Fir

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Design of FIR filters

Giuseppe Scarpa
ENS 2013

Contents
1

Problem statement

Linear-phase FIR
2.1 Type-I-IV linear-phase FIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Amplitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Zero locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4
5
7
9

Windowing
3.1 Direct truncation of an ideal impulse response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 Smoothing the frequency response using fixed windows . . . . . . . . . . . . . . . . . . . . . . . . .
3.3 Filter design using the adjustable Kaiser window . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10
10
12
14

Frequency-sampling

18

Minimax
5.1 Definition and properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2 Minimax approximation optimality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

20
20
22

Equiripple
6.1 Problem formulation . . . . . . . . . . . . . . . .
6.2 Specifying the optimum Chebyshev approximation
6.3 Finding the optimum Chebyshev approximation . .
6.4 Design examples using MATLAB . . . . . . . . .

.
.
.
.

22
22
23
24
25

Special FIR
7.1 Discrete-time differentiators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.2 Discrete-time Hilbert transformers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.3 Ideal raised-cosine pulse-shaping lowpass filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26
26
27
27

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

Problem statement

Steps for discrete-time filter design


1. Specification (in this lecture) .... of the desired frequency response
2. Approximation (in this lecture) To find the rational or polynomial transfer function minimizing the implementation cost and satisfies the give specification
3. Quantization (out of the scope of this course)
4. Verification (partly covered in this course) Check whether the filter satisfies the performance requirements by
simulation or testing with real data. If the filter does not satisfy the requirements, iterate the process and change
the requirements or the approximation.
5. Implementation (Structures for discrete-time filters)
Specification
Practical realizability has three key implications:
Not flat passband response
Not null stopband response
Finite transition band
The specifications of practical filters usually take the form of a tolerance diagram
|H(ej )|
Specifications
Analog

1 + p

0
Ap

Passband
Brick wall

1
1+2

As
1/A

Transition band

Relative Absolute

1 p

Passband
ripple

Stopband ripple
Stopband

p c s

Passband edge

Cutoff-freq.

Stopband edge

Absolute specifications [p , s ]
(

1 p |H(ej )| 1 + p , 0 p
|H(ej )| s ,
s

The peak ripples, p and s , specify the acceptable tolerances in terms of absolute values.
Good filters: p , s << 1
Relative specifications [Ap , As ]
(
Ap |H(ej )|dB 0, 0 p
|H(ej )|dB As ,
s
Ap , 20 log10

1+p
1p

(passband ripple)



1+
As , 20 log10 s p 20 log10 s (stopband attenuation)
Good filters: Ap 0 and As
Analog specifications [Fpass , Fstop , , A]
Specifications may refer to an underlying analog problem:
p = 2

Fpass
,
Fs

s = 2

Fstop
,
Fs

F s, sampling freq.

(normalized frequencies obtained from those in Hz)


Analog filter masks are usually defined through  and A:
(
(


 = 100.1Ap 1
20 log10
1 +  2 = Ap

20 log10 A = As
A = 100.05As
Observe that some classes of filters (differentiators, Hilbert transformers, shaping lowpass) cannot be designed
specifying a tolerance mask.
Approximation
Given a desired response hd [n] (or Hd (ej )) we need to find a practically realizable filter H(ej ) approximating
Hd (ej ) within a given tolerance.
Practically means:
causal
stable
with finite-order, rational or polinomial, transfer function:
M
P

bk z
k=0
N
P

H(z) =

ak z k

1+

k=1

Implications of causality
Theorem 1 (Paley-Wiener). If h[n] has finite energy and h[n] = 0 for n < 0, then
Z


ln H ej d <

Conversely, for any square integrable |H(ej )| satisfying the above inequality there exist a phase response H(ej )
j
such that |H(ej )| eH(e ) is causal.
Only one such phase response gives a minimum-phase H(z).
Important to know: for rational system functions there exist 2M +N possible phase responses which give causality!
Consequences:
Ideal LPF, HPF,... are non-causal
Typical specifications on the phase response are:

no requirements
linear response
In any case there is a finite set (2M +N ) of possible solutions
Real and imaginary part of the spectral response of a real, stable and causal system are strictly related by the
discrete Hilbert transform


Z

1
j
j
HR (e ) cot
HI (e ) =
d
2
2
Consequently amplitude and phase responses are interdependent, as well.
Optimality Criteria
Mean-squared-error approximation. That is the minimization of


1
E2 ,
2



Hd (ej ) H(ej ) 2 d

1/2

Minimax approximation. The minimization of the maximum absolute deviation:


E , max |Hd (ej ) H(ej )|
B

The solution is called minimax or best uniform or Chebyshev approximation


Maximally-flat approximation. Indicated if the maximum fidelity is required in the neighbourhood of a given
frequency 0 .
This is achieved imposing the larger number of derivatives of Hd (ej ) and H(ej ) to be equal.
Formalization requires the Taylors expansion
A special case is the well known Butterworth approximation

Linear-phase FIR

Without loss of generality let us focus on an ideal low pass with linear phase response:

ej , || < c
sin c (n )
j
Hlp (e ) =
hlp [n] =
0,
(n )
< ||
c

hlp [n] can be obtained by sampling the corresponding ideal continuous-time LPF with cutoff frequency c =
c /T at t = nT :

sin c (t T )
c
hlp [n] = hlp (t)|t=nT =
(t T )
t=nT

hclp (t)

Observe that
is always symmetric about T , while hlp [n] is symmetric depending on ...
In particular, there are three cases for the delay :
= nd Z symmetric about nd
= m + 21 , m Z symmetric about (not integer)
2
/ Z not symmetric
4

=6
n
6

12

= 6.5
n
6.5

13

= 6.25
n
6.25

12

Focusing on the symmetric case, for which M , 2 Z, lets create a causal FIR by setting

hlp [n], 0 n M
h[n] =
0,
elsewhere
Observe that:
the time delay =

M
2

is integer only for M even

h[n] is a practically realizable FIR with linear phase response or, equivalently, constant group delay
gd ,

dH(ej )
d

The amplitude of the spectral response is, instead, only approximatively that of a LPF
In general
Practical filters with linear phase
It can be shown that any causal IIR with rational response cannot have linear phase response. Therefore practical
filters having linear phase response are necessarily FIR satisfying one of the following 4 symmetry conditions:
h[n] = h[M n],

M even/odd

M is the filter order (polynomial order)


L = M + 1 will be the length of h[n]

2.1

Type-I-IV linear-phase FIR

Type-I
This is the case symmetric (h[n] = h[M n]) with even M .
Example 2. Let M = 4 h[4] = h[0], h[3] = h[1]. Therefore:
H(ej )

= h[0] + h[1]ej + h[2]ej2 + h[3]ej3 + h[4]ej4


=

(h[2] + 2h[1] cos + 2h[0] cos 2) ej2

, (a[0] + a[1] cos + a[2] cos 2) ej2

In general:
Type-I FIR

M/2

H(ej ) =

a[k] cos k ejM/2 , A(ej )ejM/2

k=0

A(ej ) real, even and periodic.


a[0] , h[M/2];

a[k] , 2h [M/2 k],

k = 1, 2, . . . , M/2.

Type-II
Similar manipulations lead to the Type-II spectrum
Type-II FIR (symmetric h[n] and odd M )

H(ej ) = A(ej )ejM/2

with
M +1
2

A(ej ) =

X
k=1

M 1
 

2
1
X
b[k] cos k
b[k] cos k
= cos
2
2

k=0

where

k=1
2 (b[1] + 2b[0]),



M +1
b[k] , 2h
k = 21 (b[k] + b[k 1]), 2 k M21

1 b  M 1 ,
k = M2+1
2
2
Observe: = A(ej ) = 0 unsuited for highpass filtering.
Type-III
Type-III FIR (antisymmetric h[n] and even M )
H(ej ) = jA(ej )ejM/2
with
M/2

A(ej ) =

M/2

c[k] sin k = sin

k=1

c[k] cos k

k=0

where

21 (2
c[0] c[1]),
k=1




M
k = 12 (
c[k] , 2h
c[k 1] c[k]), 2 k M
2 1


1 M

2 1 ,
k = M/2
2c
(
Observe:

{0, } A(ej ) = 0
imaginary frequency responce

differentiators, Hibert tr.

Type-IV
Type-IV FIR (antisymmetric h[n] and odd M )
H(ej ) = jA(ej )ejM/2
with
M +1
2

A(ej ) =

k=1

M 1
 

2
1
X
cos k
d[k] sin k
d[k]
= sin
2
2

k=0

where

1
d[1]),

k=1
2 (2d[0]

M +1
1] d[k]),

d[k] , 2h
k = 12 (d[k
2 k M21

1 d M 1 ,
k = M2+1
2
2


(
Observe:

2.2

= 0 A(ej ) = 0
imaginary frequency responce

differentiators, Hibert tr.

Amplitude response

Eventually all linear-phase FIRs share a spectral response of the kind


H(ej ) =

M
X

h[n]ejn , A(ej )e(e

n=0

where
A(ej ), the amplitude response (takes real, positive or negative, values)
(ej ) , + , the generalized linear phase
( accounts for the eventual presence of j)
j

Group delay: gd () = d(e


d

The use of A(ej ) and (ej ) is preferable because:


A(ej ) is analytic while |H(ej )| is not
(ej ) is continuous while H(ej ) is not
allowing for optimization techniques.
Properties of the linear-phase FIR responses

Type

h[k]

even

even

A(ej )

A(ej )

(ej )

M/2
P

a[k] cos k

even
no restriction

M
2



b[k] cos k 21

even
A(ej ) = 0

M
2

c[k] sin k

odd
A(ej0 ) = 0
A(ej ) = 0

M
2



d[k] sin k 12

odd
A(ej0 ) = 0

M
2

k=0
M +1
2

II

even

odd

k=1

III

odd

even

M/2
P
k=1
M +1
2

IV

odd

odd

k=1

Recall the following symmetry properties:

x[n] real, even DTFT


X(ej ) real, even
DTFT

x[n] real, odd X(ej ) immaginary, odd

Therefore:

he [n] DTFT
Ae (ej )

he [n ] Ae (ej )ej

DTFT

ho [n] jAo (ej )

ho [n ] Ao (ej )ej+j/2

DTFT

DTFT

with
he [n], Ae (ej ): real and even
ho [n], Ao (ej ): real and odd
M ATLAB problem
Computation of the amplitude response A(ej )
Write a matlab function A = amplresp(h,w) whose output A is the amplitude response A(ej ) computed at
frequency locations given in w, being h the impulse response of any linear-phase causal FIR (type-IIV).
Unified representation
The filter design by minimax criterion can be unified considering the following factorization
A(ej ) = Q(ej )P (ej )
where:
Q(ej ) is a fixed function dependent on the type of the filter
P (ej ) is combination of cosines dependent on the filter coefficients

Type

Q(ej )

even

P (ej )
M/2
P

H(ej ) = 0

LP, HP, BP, BS


multiband filters

a
[k] cos k

k=0

II

III

odd

even

M 1
2

P
b[k] cos k

cos 2

LP, BP

= 0,

differentiators
Hilbert transform.

=0

differentiators
Hilbert transform.

k=0
M/2
P

sin

c[k] cos k

k=0

IV

2.3

odd

Uses

M 1
2

P
d[k] cos k

sin 2

k=0

Zero locations

The above mentioned restrictions on the amplitude response of type-IIV filters can be also recognized in terms of
zero locations.
In fact, for these filters we have:
H(z) = z M H(z 1 )

(mirror image polynomial)

For example, for type-I or type-II, we have


H(z) =

M
X

h[n]z

n=0

M
X

h[M n]z

n=0

0
X

h[k]z k z M = z M H(z 1 )

k=M

As direct consequence we have the mirror-image symmetry wrt the unit circle:
H(z0 ) = 0 = H(z01 ) = 0
Moreover, is h[n] is real than:

H(z0 ) = 0 = H(z0 ) = 0

Hence for real linear-phase FIR possible zero patterns are:


Neither real nor on the unit circle:
(1 rej z 1 )(1 rej z 1 )(1 r1 ej z 1 )(1 r1 ej z 1 )
On unit circle, not real:
(1 rej z 1 )(1 rej z 1 )
Real but 6= 1:

(1 rz 1 )(1 r1 z 1 )

1:
(1 z 1 )
In addition the following constraints apply:
1 is always a zero of H(z) for type-II and type-III. Indeed:

type-II: H(1) = (1)M H(1) = H(1) H(1) = 0


type-III: H(1) = (1)M H(1) = H(1) H(1) = 0
1 is always a zero of H(z) for type-III and type-IV. Indeed:
H(1) = (1)M H(1) = H(1) H(1) = 0
M ATLAB : type-IIV filters analysis
M ATLAB : linphaseFIR.m
% Linear-phase FIR
h=[1 2 3 -2 5 -2 3 2 1]; % type-I (M=8)
% h=[1 2 3 -2 -2 3 2 1]; % type-II (M=7)
% h=[1 2 3 -2 0 2 -3 -2 -1]; %type-III (M=8)
% h=[1 2 3 -2 2 -3 -2 -1]; %type-IV (M=7)
M=length(h)-1;
[H W] = freqz(h); % frequency responce in [0, )
W4 = [W; W+pi; W+2*pi; W+3*pi]; % span over [0, 4)
H4 = [H; flipud(conj(H)); H; flipud(conj(H))];
E = exp(-i*W4*M/2);
if h(1) = h(M+1),
E=i*E; % only for type-III and type-IV
end
A4=real(H4./E); % recall: H(ej ) = jA(ej )ejM/2
j
Psi4=angle(H4./A4); % recall: H(ej ) = A(ej )ej(e )

Design by window method


The easiest way to get a FIR in the time domain is truncation of the IIR response
Direct truncation minimizes the mean square error of the spectral response
Gives unacceptable size ripples
Designing filters by windowing means reshape and truncate to minimize the ripples
The resulting FIR is no longer optimal in the mean square error sense

3.1

Direct truncation of an ideal impulse response

Minimum square error truncation

P
Let Hd (ej ) =
hd [n]ejn be the desired response, and h[n], 0 n M a candidate FIR approximating
n=

the target IIR.


Question: which is the FIR which minimizes the mean-square error?

1
2

M
X

X


Hd (ej ) H(ej ) 2 d =
(hd [n] h[n])2

n=
2

(hd [n] h[n]) +

n=0

1
X
n=

10

h2d [n]

X
n=M +1

h2d [n]

Therefore 2 is minimized wrt h[n] if and only if


h[n] = hd [n], 0 n M

Frequency domain effects of truncation


DTFT

h[n] = hd [n]w[n] H(ej ) =

1
2

Hd (ej )W (ej() )d

Direct truncation corresponds to the rectangular window whose spectral response is

W (e )

M
X

ejn =

n=0

sin[(M + 1)/2] jM/2


1 ej(M +1)
=
e
1 ej
sin[/2]

AW (ej )ejM/2

To understand the effect of the circular convolution between the spectrum of the window the desired filter response,
focus on an ideal lowpass filter and convolve the corresponding amplitude responses...
AW (ej(0 ) ), Ad (ej )

AW (ej(0 ) ), Ad (ej )

0 = 0

0 = c +

2
L

2
L

AW (ej() ) ~ Ad (ej )

AW (ej(0 ) ), Ad (ej )
0 = c

1 c

2
L

2 c +

2
L

For large values of M , it can be easily proved that


Ripples (bandpass and stopband) are symmetrical, therefore:
same distance from c , about 2/(M + 1)
about the same maximum peak deviation
Ripple peak deviation is insensitive to M . In particular:
(
1+
20 log10 1 p 0.75dB
p s 0.0895
20 log10 1s 21dB

11

(passband ripple)
(stopband attenuation)

Ad (ej )

1 + p

A(ej )

1
1 p

= s p

1.8
M

4
L

p = s 0.09
s

4/L window main lobe bandwidth

The window size L = M + 1 controls the transition bandwidth


1.8
4

M
L

, s p

The ripple peak, instead, is only related to the window shape.

3.2

Smoothing the frequency response using fixed windows

Window selection table


Window
Side lobe
name
level (dB)

Upper
bound

Exact

p s

Ap
(dB)

As
(dB)

Rectang.

13

4/L

1.8/M

0.09

0.75

21

Bartlett

25

8/L

6.1/M

0.05

0.45

26

Hann

31

8/L

6.2/M

0.0063

0.055

44

Hamming

41

8/L

6.6/M

0.0022

0.019

53

Blackman

57

12/L

11/M

0.0002

0.0017

74

where the Blackman window is defined as


(
0.42 0.5 cos(2n/M ) + 0.08 cos(4n/M ),
w[n] =
0,

0nM
otherwise

Observe that the Hamming window gives the best compromise


Moreover, since all windows satisfy the symmetry condition
w[n] = w[M n],

0nM

then
hd [n] hd [M n]

h[n] = w[n]hd [n] = w[M n]hd [M n]= h[M n]


Hence, the windowing preserves the eventual symmetry (linear-phase systems)
12

The following steps describe how to design a lowpass filter using fixed windows. The same procedure can be
easily generalized to other filters.
Design using fixed windows
Let {p , s , Ap , As } be the specifications
1. {Ap , As } {p , s } = min{p , s }
2. set c = (p + s )/2 (ideal lowpass cutoff frequency)
3. set A = 20 log10 and = s p (design parameters)
4. select window (from table):
select the window shape having the smallest As larger than A
select minimum window length such that /M M = d/e
for odd M , if needed, increase it by one to get a more flexible type-I filter
5. Compute the impulse response of the ideal lowpass filter by
hd [n] =

sin[c (n M/2)]
= (c /pi)*sinc((c /pi)*(n-M/2))
(n M/2)

6. Compute the windowed impulse response by h[n] = hd [n]w[n]


7. Verify that the specifications are satisfied; if not, increase M and go back to 5.
Example 3. Specifications: p = 0.25, s = 0.35, Ap = 0.1dB, As = 50dB
(
(
Ap /20
1+
1
Ap = 20 log10 1pp
p = 10
= 0.0058
10Ap /20 +1

As
As 20 log10 s
s = 10 20 = 0.0032 = A = 50dB
Hamming ( = 6.6)
M = d/e = d6.6/(0.1)e= 66
Since M = 66 (even) we have a type-I filter.
(M +1)

h[n] = wham

c (nM/2)]
[n] sin[
(nM/2) ,

c = (s + p )/2 = 0.3

Finally verify that the specifications are satisfied, for example, by means of freqz...
Using M ATLAB : fixwindesign.m

13

wp = 0.25*pi; ws = 0.35*pi; Ap = 0.1; As = 50; %


spec.
%
deltap = (10(Ap/20)-1)/(10(Ap/20)+1);
deltas = (1+deltap)/(10(As/20));
delta = min(deltap,deltas); A = -20*log10(delta)
A =
50
Deltaw = ws-wp; omegac = (ws+wp)/2;
M = ceil(6.6*pi/Deltaw); L=M+1;
n = 0:M;
h = hamming(L).*(omegac/pi).*sinc((omegac/pi)*(n-M/2));
%
% verification
figure(1); freqz(h);
Wlow= W(find(W<=wp)); NP=length(Wlow);
Whigh= W(find(W>=ws)); NS =length(Whigh); N =
length(W);
errp=abs(H(1:NP))-1; errs=abs(H(N-NS+1:N));
figure(2); plot(Wlow,errp,Whigh,errs);

3.3

Filter design using the adjustable Kaiser window


The main limitation of the fixed window method is that the only controllable parameter is M , and it plays a role
on the roll-off of the transition bandwidth
There is no control over the ripple size
parametrized variable-shape windows (like the Kaisers window)

Kaiser window
w[n] ,

where =

M
2

h
i
I0 1[(n)/]2
I0 ()

0,

, 0nM
otherwise

and I0 (x) is the zero-order modified Bessel function of the first kind I0 (x) = 1 +

i
h
P
(x/2)2
m=1

m!

The Kaiser windows approximate (in the discrete-time domain) the optimal prolate spheroidal wave functions
Prolate functions are optimal continuous-time windows which get the best trade-off between mainlobe bandwidth and relative height of the side lobes
M ATLAB : kaiser(L,beta)
Now, the fundamental question is how to relate the two parameters, M and , to the specifications A and .
Setting Kaiser parameters

14

Kaiser has found experimentally the following relationship

0,
A < 21

= 0.5842(A 21)0.4 + 0.07886(A 21), 21 A 50

0.1102(A 8.7),
A > 50.
where A = 20 log10 , with = min{p , s }.
The transition bandwidth can be controlled, instead, through the length
M=

A8
2.285

Blackman

Hamming

Hann

4
Bartlett

Rectangular

20

40

60

M ATLAB : kaiserWinAnalysis.m

15

80

M=[10,20,40]; beta = [0,5,8];


figure(1); hold on; title(...); ...
for k=1:3,
win=kaiser(M(2)+1,beta(k));
plot(0:M(2),win,col(k));
end
figure(2); hold on; title(...); ...
for k=1:3,
win=kaiser(M(2)+1,beta(k)); [H W] =
freqz(win);
plot(W,20*log10(abs(H)/abs(H(1))),col(k));
end
figure(3); hold on; title(...); ...
for k=1:3,
win=kaiser(M(k)+1,beta(2)); [H W] =
freqz(win);
plot(W,20*log10(abs(H)/abs(H(1))),col(k));
end

The following steps describe how to design a lowpass filter using Kaiser windows.
Design using Kaiser windows
Let {p , s , Ap , As } be the specifications
1. {Ap , As } {p , s } = min{p , s }
2. set c = (p + s )/2 (ideal lowpass cutoff frequency)
3. set A = 20 log10 and = s p (design parameters)
4. determine the setting parameters, M and , through the above relationships between {A, } and {M, }
(increase by 1 the value of M if needed to get a different type of filter)
5. Compute the impulse response of the ideal lowpass filter by
hd [n] =

sin[c (n M/2)]
= (c /pi)*sinc((c /pi)*(n-M/2))
(n M/2)

6. Compute the windowed impulse response by h[n] = hd [n]w[n]


7. Verify that the specifications are satisfied; if not, increase M and go back to 5.

16

Generalization
The generalization of the above procedures (fixed and Kaiser windows) can be achieved as long as an analytical
expression for the required ideal impulse response hd [n] is given.
For example:
Bandpass: hbp [n] =

sin[c2 (nM/2)]
(nM/2)

Multiband: hmb [n] =

K
P

(Ak Ak+1 )

k=1

sin[c1 (nM/2)]
(nM/2)

sin[ck (nM/2)]
(nM/2)

where |Hmb (ej )| = Ak for k1 k , 1 k K and AK+1 = 0


Highpass: it is a particular case (c2 = ) of the bandpass
Observe that only one ripple size (the minimum) and only one transition bandwidth (the minimum) can be controlled
M ATLAB
Further M ATLAB utility for FIR design by windowing are:
fir1: outputs impulse responses of different kinds (lowpass, multiband, ...) and different windows (hamming
[default] or user-defined ones)
kaiserord: computes the parameters M and for given specifications and A.
fdatool the Filter Design and Analysis tool
Limitations of the windowing method
(Nearly) equal size ripples
Maximum ripple sizes and edges of each band cannot be specified precisely (because of convolution)
Difficult analytical derivation of impulse responses for arbitrary frequency responses
No specific optimality criterion is satisfied (with the exception of the rectangular window which minimizes the
mean square error)
M ATLAB practice
Design (and verify) a multiband FIR with the technique of the fixed window satisfying the following constraints:

0.47 |H(ej )| 0.53,

0 0.3

0.32 0.62

0.68

0.98 |H(e )| 1.02,


0.68 |H(e )| 0.72,

Redesign the filter using the a Kaiser window (explore the use of kaiserord)
Explore the use of the function fir1 on the given specifications
Explore the use of the fdatool on the given specifications

17

Design by frequency sampling

Let Hd (ej ) be the desired frequency response


DTFT

Hd (ej ) hd [n]
Assume to know only L equispaced (on the unit circle) samples:
Hd [k] , Hd (ej2k/L ),

k = 0, 1, . . . , L 1

Hence, the inverse DFT yields


L1

X
1 X
=
Hd [k]WLkn , h[n]
hd [n mL].
L
m=
k=0

yields a FIR:
Finally, a windowing of h[n]
DTFT

h[n] = h[n]w[n]
H(ej ) =

L1
1 X
Hd [k]W (ej(2k/L) )
L
k=0

Fundamental question: how do frequency sampling and time-domain windowing affect the fitting of H(ej )
with the desired Hd (ej )?
sampling: time-domain aliasing:
The faster the variations in the frequency domain (for example, ideal LPF), the larger the number of samples L
to avoid/mitigate the aliasing
windowing: Using an L-points rectangular window yields the ideal interpolation with Dirichlet functions.
The rectangular window ensures H(ej2k/L ) = Hd (ej2k/L ) (exact fitting in the sampled frequencies) which
is paid with a strong side effect (Gibbs phenomenon)
Which strategies can be used to reduce the fitting error?
Oversampling For a fixed required response length (say M ), it is anyhow convenient to use a much larger L to reduce
the aliasing effect, then keep only the first M elements of the retrieved impulse response
Smoothing To reduce the Gibbs effect more regular windows can be used. Exact matching at the sampling frequencies
is lost, but it is a negligible price to pay.
In addition, it is worth noting that no specifications need to be satisfied within the transition bands, hence:
Smooth transitions One can fix arbitrarily the samples of Hd (ej ) in the transition bands. But, in particular, it is
convenient to force regularity by distributing the samples with linear law or, even better, with a rised-cosine
function.
Linear-phase filters
In order to have linear-phase filters additional constraints have to be superimposed. In particular, the samples of
the spectral response, Hd [k] = Ad [k]ejd [k] , should be assigned according to the following table:

18

Type

h[k]

even

even

A(ej )

A(ej )

(ej )

M/2
P

a[k] cos k

even
no restriction

M
2



b[k] cos k 21

even
A(ej ) = 0

M
2

c[k] sin k

odd
A(ej0 ) = 0
A(ej ) = 0

M
2



d[k] sin k 12

odd
A(ej0 ) = 0

M
2

k=0
M +1
2

II

even

odd

k=1

III

odd

M/2
P

even

k=1
M +1
2

IV

odd

odd

k=1

Advantages and weakness


There is no automatic procedure to optimize the filter design for fixed specifications. Iterative procedures can
be used, instead.
Unsuited for selective filtering (LPF, BPF, etc.)
Useful for equalization
DAC equalization
Digital-to-Analog Converters (DAC) are usually implemented by means of sample and hold interpolation. This
interpolation introduce a sinc-shaped distortion of the spectrum.
It can be compensated with a proper equalizer in the discrete domain prior to make the conversion

Hd (ej ) =

/2
,
sin(/2)

Hd (ej )
1

Observe that Hd (e ) 6= 0 for {0, }, therefore we need a type-I FIR.


M ATLAB implementation of DAC equalizer (dac.m)

19

% DAC Equalizer
M = 40; L = M+1; k = -M/2:M/2;
Ad = 1./sinc(k/L); % Hd over
Ad = ifftshift(Ad); % Hd over 0 2
alpha = M/2; Q = floor(alpha); % phase delay
parameters
om = linspace(0,2*pi,1001); % Frequency array
psid = -alpha*2*pi/L*[(0:Q),-(L-(Q+1:M))]; % Desired
Phase
Hd = Ad.*exp(1j*psid); % Desired Freq Resp Samples
hd = real(ifft(Hd)); % Desired Impulse response
h = hd.*rectwin(L); % Actual Impulse response
% h = hd.*hamming(L); % Alternative windowing
figure(1); stem(0:M,h); figure(2); freqz(h,1,om);
[H W]=freqz(h,1,om); % Actual frequency response
figure(3); plot(W,abs(H),linspace(0,2*pi-2*pi/L,L),abs(Hd));

M ATLAB functions for frequency sampling FIR design


Indeed M ATLAB provides the function fir2 which simplify the design...
The minimal invocation h=fir2(M,f,A) designs a M -th order linear-phase FIR with specified response
(f,A), where f=[0,...,1] are the normalized (1 corresponds to = ) sampled frequencies.
The previous DAC equalizer can be obtained by
h=fir2(40,[0:20]/20,Ad(1:21),rectwin(41));
Additional functions for specific design technique are: firls, fircls and fircls1

5
5.1

Chebyshev polynomials and minimax approximation


Definition and properties

Let us start observing the following correspondence between real numbers x [1, 1] and complex numbers on the
unit circle w = ej :
x =
=


1
w + w1
2
cos [1, 1]
Re [w] =

y
w = ej
x = Re [w] = cos()

-1

w1 = ej

x and w are uniquely related for 0 .


20

Chebyshev polynomial
The mth-order Chebyshev polynomial, denoted Tm (x), is defined by
Tm (x) , Re [wm ] =

1 m
(w + wm ) = cos(m) = cos[m cos1 (x)]
2

By means of the trigonometric identity


cos[(m + 1)] = 2 cos() cos(m) cos[(m 1)],
it is easy to prove the following recursive relationship

T0 (x) = 1
T1 (x) = x

Tm+1 (x) = 2xTm (x) Tm1 (x),

m1

m1

which allow one to generate Chebyshev polynomial of any desired order.


Low order Chebyshev polynomials
Order Polynomial Tm (x) = cos[m cos1 (x)]
0
1
2
3
4
5
6

T0 (x) = 1
T1 (x) = x
T2 (x) = 2x2 1
T3 (x) = 4x3 3x
T4 (x) = 8x4 8x2 + 1
T5 (x) = 16x5 20x3 + 5x
T6 (x) = 32x6 48x4 + 18x2 1

Properties of the Chebyshev polynomials


leading coefficient equal to 2m1 for m 1
|Tm (x)| 1 for x [1, 1]
all Chebyshev polynomials have integer coefficients
Tm (x) = (1)m
T (x)
m zeros in [1, 1] at xk = cos

2k1
m 2

, k = 1, 2, . . . , m

m + 1 extrema in [1, 1] at k = cos(k/m), k = 0, 1, . . . , m, with Tm (k ) = (1)k (equiripple property)


Tm (x)

T4 (x)
T7 (x)

1
1

21

Observe that any (freq. resp.) finite trigonometric series in cos(m) can be expressed as linear combination of
Chebyshev polynomials:
Example
P () ,

2 + cos() + cos(2) + cos(3)

[2T0 (x) + T1 (x) + T2 (x) + T3 (x)]|x=cos()



1 2x + 2x2 + 4x3

x=cos()

Trigonometric series in cos(m)


j

P (e ) =

R
X

p[k] cos k =

k=0

R
X

p[k]Tk (x) =

k=0

R
X
k=0



pk x

k

x=cos

it allows us to express the amplitude response of linear-phase FIR

5.2

Minimax approximation optimality

Theorem 4 (Chebyshev). Of all polynomials of degree m with coefficient of xm equal to 1, the Chebyshev polynomial
2(m1) Tm (x) has the least maximum amplitude on the interval [1, 1].

If we can express the error as a single Chebyshev polynomial, then we have the best minimax polynomial
approximation
Another important theorem is the following
Theorem 5 (Alternation). Suppose that f (x) is a continuous function.
Then Pm (x) is the best mth-order minimax approximating polynomial to f (x) if and only if the error e(x) =
f (x) Pm (x) has an (m + 2)-point equiripple property.
The above theorem establishes a characterization of the minimax approximations, but does not provide a solution
to find one
An effective solution is given by the iterative Remez exchange algorithm

Equiripple optimum Chebyshev FIR design

6.1

Problem formulation

Let
Ad (ej ) desired amplitude response
A(ej ) amplitude response of the implemented approximating filter
W () a nonnegative weighting function. W () enables us to control the relative size of the error in different
frequency bands.
The approximation error is defined by
E() , W ()[Ad (ej ) A(ej )]
Then, the design objective is to find the coefficients of a type IIV FIR that minimize the weighted Chebyshev error:
22

||E()|| = max |E()|


B

B, union of disjoint closed subsets (frequency bands) of [0, ].


For linear-phase FIR the amplitude response can be written as
A(ej ) = Q(ej )P (ej ),
where

1,
Type I

cos(/2), Type II
Q(ej ) =
,

sin(),
Type III

sin(/2), Type IV

P (ej ) =

R
X

p[k] cos(k)

k=0

(
M/2,
M even
with R = bM/2c =
(M 1)/2, M odd
Observe that Q(ej ) does not depend on the filter coefficient can be incorporated in weighting function
W (),...
... yielding


E() = W () Ad (ej ) P (ej )
where
W () , W ()Q(ej )
Ad (ej ) ,

Ad (ej )
Q(ej )

Now we can formulate the


Chebyshev approximation problem
Given the filter order M , determine the coefficients of P (ej ) that minimize the maximum absolute value of E()
over the frequency bands of interest, that is, choose P (ej ) so that


||E()|| = max W ()[Ad (ej ) P (ej )]
B

is minimum.
P (ej ) is a trigonometric series, hence, a Chebyshev polynomial in x = cos .

6.2

Specifying the optimum Chebyshev approximation

The solution of the minimax problem is strictly related to the alternation theorem. A more suited formulation of
theorem for the purpose of FIR design is the following:
Theorem 6 (Alternation theorem for FIR filters). A necessary and sufficient condition that P (ej ) be the unique
solution minimizing ||E()|| is that the weighted error function E() exhibit at least R + 2 alternations in B. That
is, there must exist R + 2 extremal frequencies 1 < 2 < < R+2 such that for every k = 1, 2, . . . , R + 2
E(k ) = E(k+1 ),

|E(k )| = max E() ,


B

being P (ej ) and R the entities involved in the definition of the amplitude response of a linear-phase FIR
Implications:
23

the best Chebyshev approximation have equiripple E()


given B, M and W (), there is a unique best approximation
R = bM/2c
B is the union of passband and stopband compact intervals
Ad (ej ) should be defined and be continuous only within B
W (), defined only within B, includes the approximation error parameters
once fixed the above parameters there exist a unique best solution; this solution is equiripple and the will have
a given maximum error = max E().
B

if does not match the specifications, we need to increase the filter order M and compute1 the new minimax
solution.
Ad (ej )
{f1 , f2 , f3 , f4 } = {0.2, 0.25, 0.5, 0.6}

1 + 1
1
1 1

{1 , 2 , 3 } = {0.1, 0.05, 0.15}

{1 /1 , 1 /2 , 1 /3 } = {1, 2, 2/3} W ()
B = [0, f1 ] [f2 , f3 ] [f4 , 1]

0.5 + 3
0.5
0.5 3
2
2

/
f1 f2

f3

f4

M ATLAB specification:

fo = [0, 0.2, 0.25, 0.5, 0.6, 1]; % band edges


ao = [1, 1, 0, 0, 0.5, 0.5] % amp. resp. at fk
W = [1, 2, 2/3]; % weights
M = ??? % FIR order

Observe: W () controls the rela-

tive error proportions; the absolute error is function of M .

6.3

Finding the optimum Chebyshev approximation

To design a minimax optimal FIR it is now necessary to answer two fundamental questions:
how to determine the best minimax approximation given B, W () and Ad (ej )?
how to determine the smallest value of M for a fixed absolute maximum error?
These problems have been widely studied and there exists different solutions. One of the most common solution
is the Parks and McClellan algorithm, whose theoretical formulation is here skipped. This technique is a standard
technique and is available under several professional tools (i.e. M ATLAB ).
In particular the two above questions are addressed by the following M ATLAB functions respectively:
[h, delta] = firpm(M, fo, Ao, W)
[M, fo, Ao, W] = firpmord(F, A, dev,Fs)
1 We

still have to discuss how to find the minimax

24

Observe that the default value of Fs is 2 implicit use of normalized frequencies F (Nyquist frequency Fs = 1)
where:
F: % band edges (0 and Fs/2 are implicit, always)
A: % amplitude resp.
dev:

in each band

% absolute deviations b in each band

Fs:

% sampling frequency

fo:

% normalized band edges

Ao:

% amplitude resp.

and

in each band

W: % error weights for the different bands


M: % filter order
A empirical formula to estimate the filter order M from the specifications is due to Kaiser:
p
20 log10 p s 13
M=
2.324
Observe that it depends on both transition bandwidth and ripple sizes.

6.4

Design examples using MATLAB

Filter design using firpm (Parks and McClellan)


[parks.m]
% ParksMcClellan filter design
close all; clear all;
f=[0.2,0.25,0.5,0.60]; a=[1,0,0.5];
d1=0.1; d2=0.05; d3=0.15; dev=[d1,d2,d3];
[M,fo,ao,W]=firpmord(f,a,dev); % W=[1, 2, 2/3];
[h,delta]=firpm(M,fo,ao,W);
[H w]=freqz(h); w=w/pi;
% VERIFICATION
% ...
plot(...)
Observe that firpmord may underestimate M . The obtained value is an initial guess to be refined, usually
incrementing it.
DAC equalization using firpm [DACbyParks.m]

25

% DAC equalizer designed using firpm


clear all; close all;
% DAC Specifications
fdac = 0:0.05:0.55; % fdac = 0:0.01:0.55;
adac = 1./sinc(fdac/2);
deltap = 0.01; deltas = 0.005;
ws = 0.65*pi;
% Filter Design using FIRPM function
fo = [fdac,ws/pi,1]; % Band-edge array
ao = [adac,0,0]; % Band-edge desired gain
W = ([deltap*ones(1,length(fdac)/2),deltas]/deltas).(-1);
M = 41; % M=46;
[h,delta] = firpm(M,fo,ao,W);
% PLOT and VERIFICATION ...

Design of some special FIR

7.1

Discrete-time differentiators

In the continuous-time domain the derivation is characterized as follows:


yc (t) =

dxc (t)
Hc (j) = j
dt

An ideal bandlimited differentiator is defined by


(
Hc (j) =

j, || < /T
0,
|| /T

The corresponding discrete-time differentiator with linear phase is


H(ej ) = (j)ej ,

|| <

Notice that the constant group delay is necessary to have a causal system (after windowing).
The inverse transforming of H(ej ) is given by
h[n] =

cos[(n )] sin[(n )]

,
(n )
(n )2

< n <

Observations:
we should use type III or IV for which H(ej ) = jA(ej )ejM/2
Type III has a null in , hence unsuitable for fullband differentiator:
it can be used in a restricted band, i.e. [0, 0.8]
it has an integer delay
Type IV is more suited:
more accurate and applicable to the full band [0, ]
it has a non-integer delay
26

Proceed with M ATLAB ...


Implementation of a differentiator type-IV
Experience the design of a type-IV discrete-time differentiator using the following methods and tools
Kaiser window: M = 21 and = 4.5 (plot impulse/magnitude responce and approximation error)
Parks-McClellan: M = 21 (same plots)
FDATool: Repeat the above design tasks by means of fdatool

7.2

Discrete-time Hilbert transformers

The ideal Hilbert transformer is an allpass system that introduces a 90-degree phase shift. Its discrete-time version is
defined by:
(
jsgn()ej , 1 < || < 2
j
H(e ) =
0,
otherwise
corresponding to
h[n] =

cos(1 [n ]) cos(2 [n ])
(n )

In this case, the most suited linear-phase fir is the type-III.


Design of a Hilbert transformer
Design a type-III FIR approximating a Hilbert transformer using M = 40, by means of the fixed window method. Use
in particular the Hamming window.
Repeat the design by using fdatool

7.3

Ideal raised-cosine pulse-shaping lowpass filters

Another special filter commonly used in practice is the raised-cosine pulse-shaping lowpass:

1,h
i 0 || < (1 )c

H(ej ) =

0,

1 sin

c
2 c

, (1 )c || (1 + )c
(1 + )c < ||

Design of a raised-cosine pulse-shaping filter


Experience the use of fdatool on the design a raised-cosine pulse-shaping filter.

27

You might also like