DSP Using Matlab v.4 Solution
DSP Using Matlab v.4 Solution
2m
1)
(n
2m)] ; 0 n 25.
u (n
4)
u (n
10)].
0 n 20.
S OLUTIONS M ANUAL
FOR
A PRIL 98
Homework1 : Problem 1
Sequence x1(n)
10
x1(n)
5
0
5
10
0
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
n
Sequence x3(n)
1
x3(n)
0.5
0
0.5
1
10 11 12 13 14 15 16 17 18 19 20
+ w (n) ; 0
(e) x5 (n) =
:::;
1; 2; 3; 2; 1; 2; 3; 2; 1; : : : . Plot 5 periods.
"
A PRIL 98
S OLUTIONS M ANUAL
FOR
Sequence x4(n)
10
x4(n)
5
0
5
10
0
10
20
30
40
50
60
70
80
90
100
x1(n)
20
15
10
5
0
5
10
n
Sequence x5(n)
4
x5(n)
3
2
1
0
8 7 6 5 4 3 2 1
10
11
4)
2x (n).
S OLUTIONS M ANUAL
FOR
A PRIL 98
x1(n)
30
20
10
0
n
Sequence x2(n)
x2(n)
60
40
20
0
9
1) + x(2
n)x (n).
A PRIL 98
S OLUTIONS M ANUAL
FOR
10 n 10.
S OLUTIONS M ANUAL
FOR
A PRIL 98
x3(n)
20
0
20
40
8
n
Sequence x4(n)
50
x4(n)
40
30
20
10
0
10 9 8 7 6 5 4 3 2 1
=1
) f0 N = K (an integer)
20 n 20.
Sequence x5(n)
100
x5(n)
80
60
40
20
0
3
10
A PRIL 98
S OLUTIONS M ANUAL
FOR
20 n 20.
Problem 2.3
Sequence cos(0.3*pi*n)
1
x1(n)
0.5
0
0.5
1
20
15
10
10
15
20
10
15
20
Sequence cos(0.3*n)
1
x2(n)
0.5
0
0.5
1
20
15
10
10
S OLUTIONS M ANUAL
FOR
(0:4n)
DSP
USING
M ATLAB
A PRIL 98
0 n 10.
n = 0:10; x = 10*exp(-0.4*pi*n);
[xe,xo,neo] = evenodd(x,n);
Re_xe = real(xe); Im_xe = imag(xe);
Re_xo = real(xo); Im_xo = imag(xo);
% Plots of the sequences
subplot(2,2,1); stem(neo,Re_xe);
ylabel(Re{xe(n)}); title(Real part of Even Seq.);
subplot(2,2,3); stem(neo,Im_xe);
xlabel(n); ylabel(Im{xe(n)}); title(Imag part of Even Seq.);
subplot(2,2,2); stem(neo,Re_xo);
ylabel(Re{xo(n)}); title(Real part of Odd Seq.);
subplot(2,2,4); stem(neo,Im_xo);
xlabel(n); ylabel(Im{xo(n)}); title(Imag part of Odd Seq.);
The M ATLAB verification plots are shown in Figure 2.8.
Real part of Odd Seq.
1.5
1
Re{xo(n)}
Re{xe(n)}
6
4
2
0
10
0.5
0
0.5
1
1.5
10
10
0.5
0.5
0
0.5
1
10
10
Im{xo(n)}
Im{xe(n)}
0
0.5
0
n
10
1
10
0
n
10
x1 (n) x2 (n)
x2 (n)] x3 (n)
=
=
=
=
x2 (n) x1 (n)
x1 (n) [x2 (n) x3 (n)]
x1 (n) x2 (n) + x1 (n) x3 (n)
x ( n n0 )
:
:
:
:
Commutation
Association
Distribution
Identity
(a) Commutation:
x1 (n) x2 (n)
k=
x 1 (k ) x 2 (n
k) =
| {z }
=m
m=
x 1 (n
m) x2 (m)
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
m=
USING
M ATLAB
11
m ) = x 2 (n ) x 1 ( n )
x2 (m) x1 (n
Association:
[x1 (n)
x2 (n)] x3 (n)
"
=
x1 (k) x2 (n k)
k=
x1 ( k ) x2 ( m
m= k=
x3 (n)
k=
x 1 (k )
x1 (k) [x2 (n
k=
x 2 (m
k)x (n
| {z } 3
m)5
=`
k=
m=
"
m)
x 1 (k ) 4
k ) x 3 (n
m=
x2 (`) x3 (n
k ) x 3 (n
`)
Distribution:
x1 (n) [x2 (n) + x3 (n)]
x1 (k) [x2 (n
k ) + x 3 (n
x1 (k) x2 (n
k) +
k=
k=
Identity:
x (n) (n
since (n
n0
k) = 1 for k = n
x (k) (n
k=
k=
x1 (k) x3 (n
x 1 (n ) x 2 ( n ) + x 1 (n ) x 3 ( n )
n0 ) =
k)]
n0
k) = x (n
n0 )
k)
12
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
ydiff =
6.8212e-013
ndiff = max(abs(ny1-ny2))
ndiff =
0
% Distributive Property
[y1,ny1] = sigadd(x2,n2,x3,n3);
[y1,ny1] = conv_m(x1,n1,y1,ny1);
[y2,ny2] = conv_m(x1,n1,x2,n2);
[y3,ny3] = conv_m(x1,n1,x3,n3);
[y2,ny2] = sigadd(y2,ny2,y3,ny3);
ydiff = max(abs(y1-y2))
ydiff =
1.7053e-013
ndiff = max(abs(ny1-ny2))
ndiff =
0
% Identity Property
n0 = fix(100*(rand(1,1)-0.5));
[dl,ndl] = impseq(n0,n0,n0);
[y1,ny1] = conv_m(x1,n1,dl,ndl);
[y2,ny2] = sigshift(x1,n1,n0);
ydiff = max(abs(y1-y2))
ydiff =
0
ndiff = max(abs(ny1-ny2))
ndiff =
0
6. Problem P2.13: Linear convolution as a matrix-vector multiplication. Consider the sequences
x (n) = f1; 2; 3; 4g and h (n) = f3; 2; 1g
(a) The linear convolution of the above two sequences is
y (n) = f3; 8; 14; 20; 11; 4g
(b) The vector representation of the above operation is:
2
6
6
6
6
6
6
4
3
8
14
20
11
4
7 6
7 6
7 6
7=6
7 6
7 6
5 4
| {z }
3
2
1
0
0
0
0
3
2
1
0
0
{z
0
0
3
2
1
0
0
0
0
3
2
1
3
2
7
7
76
76
74
7
5
|
}
1
2 7
7
3 5
4
{z }
x
(c) Note that the matrix H has an interesting structure. Each diagonal of H contains the same number. Such a matrix is
called a Toeplitz matrix. It is characterized by the following property
[H]i; j = [H]i j
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
13
(d) Note carefully that the first column of H contains the impulse response vector h (n) followed by number of zeros
equal to the number of x (n) values minus one. The first row contains the first element of h (n) followed by the same
number of zeros as in the first column. Using this information and the above property we can generate the whole
Topelitz matrix.
7. Problem P2.14:
(a) The M ATLAB function conv tp:
function [y,H]=conv_tp(h,x)
% Linear Convolution using Toeplitz Matrix
% ---------------------------------------% [y,H] = conv_tp(h,x)
% y = output sequence in column vector form
% H = Toeplitz matrix corresponding to sequence h so that y = Hx
% h = Impulse response sequence in column vector form
% x = input sequence in column vector form
%
Nx = length(x); Nh = length(h);
hc = [h; zeros(Nx-1, 1)];
hr = [h(1),zeros(1,Nx-1)];
H = toeplitz(hc,hr);
y = H*x;
(b) M ATLAB verification:
x = [1,2,3,4]; h = [3,2,1];
[y,H] = conv_tp(h,x); y = y, H
y =
3
8
14
20
11
H =
3
0
0
0
2
3
0
0
1
2
3
0
0
1
2
3
0
0
1
2
0
0
0
1
=
=
=
k
u (n k)
k=
k=0
n
n
k
n
k
u (n) = (0:8)n (8=8)k u (n)
(0:8) (0:8) (0:8)
k =0
k=0
n
n
(0:8) (n + 1) u (n) = (n + 1) (0:8) u (n)
x (k ) x (n
k) = (0:8)k (0:8)n
14
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
ya(n)
2
1
0
1
10
15
20
10
15
20
25
30
n
Filter output
35
40
45
50
35
40
45
50
yb(n)
2
1
0
1
25
n
30
Chapter 3
"
16
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
set(gca,XTickMode,manual,XTick,wtick)
set(gca,YTickMode,manual,YTick,phatick)
The plots are shown in Figure 3.1. The angle plot for this signal is a linear function of .
Homework1 : Problem 4
Magnitude Response
|X|
20
10
0
0
0.2
0.4
0.6
frequency in pi units
Phase Response
0.8
0.2
0.4
0.6
frequency in pi units
0.8
Degrees
180
180
0
"
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
17
set(gca,XTickMode,manual,XTick,wtick)
set(gca,YTickMode,manual,YTick,phatick)
The plots are shown in Figure 3.2. The angle plot for this signal is a linear function of .
Problem 3.2
Magnitude Response
|X|
20
10
0
0
0.2
0.4
0.6
frequency in pi units
Phase Response
0.8
0.2
0.4
0.6
frequency in pi units
0.8
Degrees
180
180
0
3 (0:9)n e
n=0
3
0:9e
jn
=3
0:9e
j n
n=0
18
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
2+4
2 (0:8)n
2) = 2 (0:8)4 (0:8)n
0:8192 (0:8)n 2 u (n
u (n
u (n
2)
2)
= 0:8192
e j2
1 0:8e
(0 5)n e
jn
n=0
0:5e
1
0:5e
, that is,
j n (0:5)n e
jn
=(
1)
n =0
1
(1
0:5e
0:5e
j)2
j)
Hence
X e j
jn
n=0
0:5e
(1
0:5e
j)2
u (n
=
=
=
=
2 + 4)( 0:7)n
0 :7 ) ( n
0 :7 ) ( n
0 :7 ) ( n
2 + 4)(
2+1
u (n
2)
0:7)n 2 u (n
2) (
0:7)n 2 u (n
2) (
0:7)n 2 u (n
2)
2) + 4 ( 0:7) ( 0:7)n
2)
2:8 (
0:7)n 2 u (n
u (n
2)
2)
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
19
0 :7 ) e
0:49e
j2 (
0:7) e
j]2
[1 + 0:7e
j3
2
[1 + 0:7e j]
2 :8
e j
1 + 0:7e
2 :8
e j
1 + 0:7e
5 ( 0:9)n
5
2
e j0:1n + e
2
0:9e j0:1
n
j0:1n
u (n) +
u (n )
5
2
0:9e
j0:1 n
u (n)
=
=
5=2
5=2
+
1 + 0:9e j0:1e j 1 + 0:9e j0:1e
1 + 0:9 cos (0:1) e j
1 + 1:8 cos (0:1) e j + 0:81e j2
20
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Problem 3.3
Magnitude Plots
Angle Plots
180
180
a.
30
0
0
0.2
0.4
0.6
0.8
0.2
0.4
0.6
0.8
0.2
0.4
0.6
0.8
0.2
0.4
0.6
0.8
0.2
0.4
0.6
0.8
180
b.
4.096
0
0
0.2
0.4
0.6
0.8
180
180
c.
0
0
0.2
0.4
0.6
0.8
180
180
180
d.
14.7778
0
0
0.2
0.4
0.6
0.8
180
e.
5.4629
0
0
0.5
180
0.5
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
21
% Super Title
suptitle(Problem 3.4);
The plots are shown in Figure 3.4. These plots show that the DTFT of a rectangular pulse is similar to a sinc function
and as N increases the function becomes narrowrer and narrower.
Problem 3.4
DTFT for N = 5
DTFT for N = 15
0.2234
1
0.2166
1
DTFT for N = 25
0
0.2074
1
0
0.005
1
frequency in pi units
frequency in pi units
TN = 1
jnj R
N
N (n )
This problem is solved using M ATLAB. It uses the function dtft described in P3.1 ans is given below.
function [X] = dtft(x,n,w)
% Computes Discrete-time Fourier Transform
% [X] = dtft(x,n,w)
%
% X = DTFT values computed at w frequencies
% x = finite duration sequence over n (row vector)
% n = sample position row vector
% W = frequency row vector
X = x*exp(-j*n*w);
The M ATLAB script for the problem is:
clear; close all;
w = [-100:100]*pi/100;
22
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
% x(n) = (1-abs(n)/N)*R_N(n);
% (a) N = 5
N = 5; n = -N:N; x = 1-abs(n)/N; X = dtft(x,n,w); X = real(X); X = X/max(X);
subplot(2,2,1); plot(w/pi,X); axis([-1,1,0,1]);
title(DTFT for N = 5,fontweight,bold); ylabel(X);
set(gca,XTickMode,manual,XTick,[-1,0,1],FontSize,10);
set(gca,YTickMode,manual,YTick,[min(X),0,1],FontSize,10); grid;
% (b) N = 15
N = 15; n = -N:N; x = 1-abs(n)/N; X = dtft(x,n,w); X = real(X); X = X/max(X);
subplot(2,2,2); plot(w/pi,X); axis([-1,1,0,1]);
title(DTFT for N = 15,fontweight,bold); ylabel(X);
set(gca,XTickMode,manual,XTick,[-1,0,1],FontSize,10);
set(gca,YTickMode,manual,YTick,[min(X),0,1],FontSize,10); grid;
% (c) N = 25
N = 25; n = -N:N; x = 1-abs(n)/N; X = dtft(x,n,w); X = real(X); X = X/max(X);
subplot(2,2,3); plot(w/pi,X); axis([-1,1,0,1]);
title(DTFT for N = 25,fontweight,bold); ylabel(X);
xlabel(frequency in pi units);
set(gca,XTickMode,manual,XTick,[-1,0,1],FontSize,10);
set(gca,YTickMode,manual,YTick,[min(X),0,1],FontSize,10); grid;
% (d) N = 100
N = 100; n = -N:N; x = 1-abs(n)/N; X = dtft(x,n,w); X = real(X); X = X/max(X);
subplot(2,2,4); plot(w/pi,X); axis([-1,1,0,1]);
title(DTFT for N = 100,fontweight,bold); ylabel(X);
xlabel(frequency in pi units);
set(gca,XTickMode,manual,XTick,[-1,0,1],FontSize,10);
set(gca,YTickMode,manual,YTick,[min(X),0,1],FontSize,10); grid;
% Super Title
suptitle(Problem 3.5);
The DTFT plots are shown in Figure 3.5. These plots show that the DTFT of a triangular pulse is similar to a (sinc)2
function and as N increases the function becomes narrowrer and narrower.
6. Problem P3.7: Consider xe (n) =
obtain
F [xe (n)]
1
2 [x (n) + x (
=
=
1
F
fx (n) + x ( n)g = 12 fF [x (n)] + F [x ( n)]g
2
1
X e j + X e j , XR e j :
2
Similarly
F [xo (n)]
=
=
1
1
F
f
x (n) x ( n)g = fF [x (n)]
2
2
1
X e j
X e j , jXI e j :
2
u (n
20)]:
F [x ( n)]g
A PRIL 98
S OLUTIONS M ANUAL
DSP
FOR
USING
M ATLAB
23
Problem 3.5
DTFT for N = 5
DTFT for N = 15
0
1
0
1
DTFT for N = 25
0
1
0
1
frequency in pi units
frequency in pi units
1) + x (n
2)
0:5y (n
1) + 0:25y (n
2)
The frequency response H (e j ) of the filter: Substituting x (n) = e jn and y (n) = H e jn e jn in the above difference equation and simplifying
1 + 2e j + e j2
H e j =
1 + 0:5e j 0:25e j2
clear; close all;
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P3.16b);
%
% Analytical calculations of Frequency Response using diff eqn
b = [1,2,1]; a = [1,0.5,0.25];
w = [0:1:500]*pi/500; kb = 0:length(b)-1; ka = 0:length(a)-1;
24
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
H = (b*exp(-j*kb*w)) ./ (a*exp(-j*ka*w));
magH = abs(H); phaH = angle(H);
%
% Magnitude Responses Plot
subplot(2,1,1); plot(w/pi,magH);grid; axis([0,1,0,3]);
xlabel(frequency in pi units); ylabel(|H|);
title(Magnitude Response);
wtick = [0:0.2:1]; magtick = [0:3];
set(gca,XTickMode,manual,XTick,wtick)
set(gca,YTickMode,manual,YTick,magtick)
%
% Phase response plot
subplot(2,1,2); plot(w/pi,phaH*180/pi);grid;
xlabel(frequency in pi units); ylabel(Degrees);
title(Phase Response); axis([0,1,-180,180])
wtick = [0:0.2:1]; phatick = [-180;0;180];
set(gca,XTickMode,manual,XTick,wtick)
set(gca,YTickMode,manual,YTick,phatick)
The magnitude and phase response plots are shown in Figure 3.6.
Homework1 : Problem 5
Magnitude Response
3
|H|
0
0
0.2
0.4
0.6
frequency in pi units
Phase Response
0.8
0.2
0.4
0.6
frequency in pi units
0.8
Degrees
180
180
0
1)
0:25y (n
1) + 0:25y (n
2)
The frequency response H (e j ) of the filter: Substituting x (n) = e jn and y (n) = H e jn e jn in the above difference equation and simplifying
2 + e j
H e j =
1 + 0:25e j 0:25e j2
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
25
Problem 3.16c
Magnitude Response
3
|H|
0
0
0.2
0.4
0.6
frequency in pi units
Phase Response
0.8
0.2
0.4
0.6
frequency in pi units
0.8
Degrees
180
180
0
26
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
(a) xa (t ) = 3 cos (20t ). Hence x (n) = xa (nTs ) = 3 cos (0:2n). Therefore, the digital frequency is 0:2 rad/sam.
(b) The steady-state response when x (n) = 3 cos (0:2n): The frequency response is
H e j
n
= F [h (n)] = F [(0:5) u (n)] =
1
:
0:5e j
1
0:5e j0:2
= 0:6969 (
0:2063c) :
Hence
yss (n) = 3 (0:6969) cos (0:2n
0:2363)
0:2363) :
(d) Aliased frequencies of F0 for the given sampling rate Fs are F0 + kFs . Now for F0 = 10 Hz and Fs = 100, the aliased
frequencies are 10 + 100k = f110; 210; : : :g. Therefore, two other xa (t )s are
3 cos (220t ) and 3 cos (420t ) .
(e) The prefilter should be a lowpass filter with the cutoff frequency of 50 Hz.
9. Problem P3.20: An analog signal xa (t ) = cos (20t ) ; 0 t
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
27
x1(n)
1
0
x2(n)
1
0
x3(n)
1
0
1
t,sec
28
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
xa(t)
1
0
xa(t)
1
0
xa(t)
1
0
1
t,sec
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
29
xa(t)
1
0
xa(t)
1
0
xa(t)
1
0
1
t,sec
Chapter 4
The z-transform
1. Problem P4.1
(a) Part (c): The given sequence is x (n) = (4=3)n u (1
X (z)
1 n
4
3
4
3z 1
3z
4
n
=
1
(3z=4)
4
3z
4
3z
3z
4
0
=
n
n
16=9
4
; jzj <
z (z 4=3)
3
M ATLAB verification: Since the sequence is a left-sided sequence, the difference equation (and hence the filter
function) should be run backward in time. This means that X (z) should be a rational function in z. Furthermore,
since x (n) begins at n = 1 and continues (backwards in time) to , we will advance x (n) by one sample and
simulate zX (z) for verification purposes where
zX (z) =
4=3
(3=4) z
0.7500
% simulation of x(n)
n = [1:-1:-6]; x = (4/3).n
x =
Columns 1 through 7
1.3333
1.0000
0.7500
Column 8
0.1780
% Difference equation
% Input sequence
0.5625
0.4219
0.3164
0.2373
0.5625
0.4219
0.3164
0.2373
30
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
M ATLAB
USING
31
(b) Part (d): The given sequence x (n) = 2 jnj + 3 jnj can be rearranged as
x (n)
2 n u (n )
2n u ( n
1)] + 3 n u (n)
3n u ( n
1)]
i. The z-transform is
X (z)
1
2 1z
jzj
{z
2 1
2 + 5z 1
5z 1 + 6z
{z
jzj
{z
jzj
<
>
1
2z
1
3 1z
jzj
{z
3 1
>
2 (5=6)z 1
(5=6)z 1 + (1=6)z
<
{z
jzj
1
3z
{z
jzj
<
(4.1)
2 1
>
ii. M ATLAB verification: Since the sequence is two-sided, it is not possible to drive the difference equation in both
directions using the filter function. However we can verify the step in (4.1) by generating positive-time
and negative-time sequences as shown below.
%(d) x(n) = (2)(-|n|)+(3)(-|n|)
R = [1;-1;1;-1];
% residues
p = [1/2;2;1/3;3];
% poles
[b,a] = residuez(R,p,[])
% Difference equation coefficients
b =
0
-4.1667
11.6667
-4.1667
a =
1.0000
-5.8333
10.3333
-5.8333
1.0000
% Forward difference equation
Rf = [1;1]; pf = [1/2;1/3];
[bf,af] = residuez(Rf,pf,[])
bf =
2.0000
-0.8333
af =
1.0000
-0.8333
0.1667
[delta,nf]= impseq(0,0,30);
xf = filter(bf,af,delta);
% Backward difference equation
Rb = [-1;-1]; pb = [2;3];
[bb,ab] = residuez(Rb,pb,[])
bb =
-2
5
ab =
1
-5
6
[delta,nb]= impseq(0,0,29);
xb = filter(fliplr(bb),fliplr(ab),delta);
% Total solution
x1 = [fliplr(xb),xf];
% simulation of x(n)
n = [-fliplr(nb+1),nf];
32
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
x2 = 2.(-abs(n)) + 3.(-abs(n));
% difference
diff = max(abs(x1-x2))
diff =
1.1102e-016
2. Problem P4.2
(a) Part (b):The given sequence x (n) can be rearranged as
n
x (n)
=
=
=
1
u (n 2) + (0:9)n 3 u (n)
3
2 n 2
1
1
u (n 2) + (0:9) 3 (0:9)n u (n)
3
3
1 1 n 2
1000
n
u (n 2) +
(0 :9 ) u (n )
9 3
729
The z-transform is
X (z)
=
=
1 2
1 n
1000
z Z
u (n ) +
Z [(0:9)n u (n)]
9
3
729
!
1 2
1
1000
1
z
+
9
729 1 0:9z 1
1 13 z 1
1000
729
1000 1
1
+ 9 z 2 0:1z 3
2187 z
37 1
1 30 z + 0:3z 2
M ATLAB verification:
%
%
%
%
%
%
%
Sequence:
x(n) = (1/3)n*u(n-2) + (0.9)(n-3)*u(n)
Analytical Expression of X(z)
X(z) = ((1000/729) - (1000/2187)*z(-1) + (1/9)*z(-2) -0.1*z(-3)
X(z) = -----------------------------------------------1 - (37/30)*z(-1) + 0.3*z(-2)
% Matlab verification
b = [1000/729, -1000/2187, 1/9, -0.1]; a = [1, -37/30, 0.3];
delta = impseq(0,0,7); format long
xb1 = filter(b,a,delta)
xb1 =
Columns 1 through 4
1.37174211248285
1.23456790123457
1.22222222222222
1.03703703703704
Columns 5 through 8
0.91234567901235
0.81411522633745
0.73037174211248
0.65655724737083
%
% check
n = 0:7;
xb2 = ((1/3).n).*stepseq(2,0,7) + ((0.9).(n-3)).*stepseq(0,0,7)
xb2 =
Columns 1 through 4
1.37174211248285
1.23456790123457
1.22222222222222
1.03703703703704
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
Columns 5 through 8
0.91234567901235
0.81411522633745
USING
M ATLAB
0.73037174211248
33
0.65655724737083
%
error = abs(max(xb1-xb2)), format short;
error =
4.440892098500626e-016
(b) Part (d):The given sequence x (n) can be rearranged as
n
x ( n)
1
2
cos
n
n 1
1
2
n
1 1
2 2
1
2
=
=
The z-transform is
cos
1
cos
45 u (n
n
=
=
1 1
1
z Z
2
2
1
1 1
z
2
1 z
1)
(n
n
X (z)
u (n
4
1)
cos
1 ) u (n
1)
n u (n )
1 1
2 z cos (=4)
1 cos (=4) + 1 z 2
4
p1 z
2
p1 z 2
4 2
1 + 0:25z 2
jzj
>
0:5
M ATLAB verification:
%
%
%
%
%
%
%
Sequence:
x(n) = (1/2)n*cos(pi*n/4-pi/4)*u(n-1)
Analytical Expression of X(z)
0.5*z(-1) - 1/(4*sq(2))*z(-2)
X(z) = -------------------------------1 - 1/sq(2)*z(-1) + 0.25*z(-2)
% Matlab verification
b = [0, 0.5, -1/(4*sqrt(2))]; a = [1, -1/sqrt(2), 0.25];
delta = impseq(0,0,7); format long
xb1 = filter(b,a,delta)
xb1 =
Columns 1 through 4
0
0.50000000000000
0.17677669529664
Columns 5 through 8
-0.04419417382416 -0.03125000000000 -0.01104854345604
%
% check
n = 0:7;
xb2 = ((1/2).n).*cos(pi*n/4-pi/4).*stepseq(1,0,7)
xb2 =
Columns 1 through 4
0
0.50000000000000
0.17677669529664
Columns 5 through 8
-0.04419417382416 -0.03125000000000 -0.01104854345604
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
34
S OLUTIONS M ANUAL
FOR
DSP
M ATLAB
USING
A PRIL 98
%
error = abs(max(xb1-xb2)), format short;
error =
6.938893903907228e-018
3. Problem P4.3
(a) Part (b): The z-transform of x (n) is X (z) = 1 + 2z
2
z 6= 0. Consider
X2 (z)
X (z) +
X (z)
z
z
1 + 2z
d
X (z)
dz
+
d
dz
z
d
X (z)
dz
d
d
d
X (z) + z
z X (z)
dz
dz dz
1
2z
+ 2z
= 1 + 6z
z 6= 0:
(b) Part (c): The z-transform of a sequence x (n) is X (z) = 1 + 2z 1 , z 6= 0. A new sequence is x3 (n) =
Using the time-shift property
Z [x (n 2)] = z 2 X (z) = z 2 + 2z 3
with no change in ROC. Now using the frequency-shift property
n
1
2
x (n
2)
X3 (z) = z
(2z)
+ 2z
+ 2 (2z)
z!z=0:5
= 0:25z
+ 0:25z
with ROC scaled by (1=2). Since the old ROC is jzj > 0; the new ROC is also jzj > 0.
4. Problem P4.5 (b): The inverse z-transform of X (z) is x (n) = 2 n u (n). Then
zX z
=
=
x(
2
(n + 1)) = x (
(
n 1)
u( n
1)
1) = 2
n+1
u( n
1)
5. Problem P4.9
(a) Part (b): The z-transform X (z) of a sequence is given as follows:
X (z) =
1
1
4z
2 + 4z 3
11 1
13
+ 8z 2
4 z
0:25z
1 n
x (n
2
2).
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
35
Therefore,
X (z)
16 +
0
2z
10
0:5z
27
0:25z
x (n) = 16 (n)
M ATLAB verification:
[delta,n] = impseq(0,0,7);
xb1 = filter(b,a,delta)
xb1 =
Columns 1 through 4
1.00000000000000
1.75000000000000 -0.81250000000000
Columns 5 through 8
-0.51953125000000 -0.28613281250000 -0.14965820312500
xb2 = -16*delta - 10*(0.5).n + 27*(0.25).n
xb2 =
Columns 1 through 4
1.00000000000000
1.75000000000000 -0.81250000000000
Columns 5 through 8
-0.51953125000000 -0.28613281250000 -0.14965820312500
error = abs(max(xb1-xb2))
error =
0
-0.82812500000000
-0.07647705078125
-0.82812500000000
-0.07647705078125
z
;
z3 + 2z2 + 1:25z + 0:25
jzj
>
4
1+z
0
1 + 0:5z
4
2
(1 + 0:5z 1)
=
=
M ATLAB verification:
36
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
[delta,n] = impseq(0,0,7);
xd1 = filter(b,a,delta)
xd1 =
0
0
1.0000
-2.0000
2.7500
xd2 = 4*(-1).n + (8*(n+1)).*((-0.5).(n+1))
xd2 =
0
0
1.0000
-2.0000
2.7500
error = abs(max(xd1-xd2))
error =
0
-3.2500
3.5625
-3.7500
-3.2500
3.5625
-3.7500
2 + 3z 1
z 1 + 0:81z
jzj
>
0:9
(a) Sequence x(n) in a form that contains no complex numbers: Compare the denominator 1 z 1 + 0:81z
denominator 1 2az 1 cos 0 + a2 z 2 of the sin(cos) transform pairs (see page 174 of the text).
1
+ 0:81z
or
a = 0:9; cos 0 =
1
1 :8
cos0 + a2z
a2 = 0:81;
2az
) 0 = 0 3125
:
cos 0 =
1
2a
=
=
2
1
1
1 + 4z 1
1 1 + (0:9)2 z 2
1 :8
jzj
>
0 :9
+ 5:3452
0:9 (0:8315) z
1
2 (0:9) z
1 1 + (0:9)2 z 2
1:8
with the
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
37
p_angl =
0.3125
-0.3125
[delta,n] = impseq(0,0,19);
xa = (p_magn(1).n).*(2*R_real(1)*cos(p_angl(1)*pi*n)-2*R_imag(1)*sin(p_angl(1)*pi*
%
% Print Response
fprintf(1,\n Hence the sequence x(n) is \n)
2
0:5z
jzj
>
0 :5 :
2
0:5z
) Y (z)
or
y (n) = 2x (n) + 0:5y (n
1) .
The pole-zero description is given by a zero at z = 0 and a pole at z = 0:5. Finally, to compute output y (n) when
x (n) = (1=4)n u (n) we use the z-transform approach (since the ROCs overlap):
Y (z)
H (z) X (z) =
4
0:5z
2
1
1
1 0:5z
1 0:25z
2
; jzj > 0:5
1 0:25z 1
Hence
y (n) = 4 (0:5)n u (n)
2 (0:25)n u (n) :
h (n) = n [u (n)
u (n
10)] = 0; 1; 2; : : : ; 9
"
+ 2z
+ : : : + 9z
kz
k =1
38
S OLUTIONS M ANUAL
FOR
DSP
M ATLAB
USING
A PRIL 98
y (n ) =
kx (n
k) :
k =1
Imaginary part
0.5
0.5
1.5
0.5
0
Real part
0.5
1.5
=
=
=
H (z) X (z) = z
z
+ 2z
+ : : : + 9z
1
0:25z
jzj
>
0:25
1 + 2z 2 + : : : + 9z 9
1 0:25z 1
3029220
3029220 757304z 1 189324z
0:25z 1
2952z 5 732z 6 176z 7 36z 8
47328z
11828z
A PRIL 98
S OLUTIONS M ANUAL
Columns 1 through 6
-3029220
-757304
Columns 7 through 9
-732
-176
Hence
y (n)
FOR
DSP
USING
-189324
M ATLAB
-47328
-11828
-2952
-36
39
757304 (n
1)
z2 =
1
1
+j ;
2
2
p1 =
j;
1
2
p2 =
1
2
It ia also known that the frequency response function H e j evaluated at = 0 is equal to 0:8, i.e.,
H e j0
= 0 :8
z2 + 1
j ) (z + j )
1
; jzj > p
=K
2
j0:5)(z + 0:5 + j0:5)
z + z + 0:5
2
(z
(z + 0:5
1+1
2
=K
1 + 1 + 0:5
2 :5
)K=1
or
H (z) =
z2 + 1
1
; jzj > p
z2 + z + 0:5
2
z2 + 1
z2 + z + 0:5
1+z 2
1 + z 1 + 0:5z
Y (z)
X (z)
1) + 0:5y (n
2) = x (n) + x (n
1)
p1
X (z) =
Hence
p1
2
sin
n
2
z
z2 + 1
u (n) :
jzj
=
=
>
1
z2 + 1
p1 z2 +z 1
2 + z + 0:5
z
2
1
z
p z2 + z + 0:5 ; jzj > p12
2
Thus the poles of Y (z) are the poles of H (z) which are inside the unit circle. Therefore, there is NO steady-state
response or yss (n) = 0.
40
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
d) Transient response ytr (n): Since yss (n) = 0, the total response y (n) = ytr (n). From the Y (z) expression from part c)
above
p1 p1 z
p
1
z
1
2 2
Y (z) = p
= 2
; jzj > p
2
2 + z + 0:5
z
2
2
1 + z 1 + p12 z 2
Hence from table lookup we have
y (n) = ytr (n) =
p1
n
1) + 0:9y (n
1)
0:81y (n
2)
pi
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
41
|H|
8
6
4
2
0
0
0.1
0.2
0.3
0.4
0.5
0.6
frequency in pi units
Phase Response
0.7
0.8
0.9
0.1
0.2
0.3
0.4
0.5
0.6
frequency in pi units
0.7
0.8
0.9
Degrees
100
0
100
0
2) + x(n); n 0
0:5z
0:25z
+ 0:25
z 2Y + (z) + y ( 2) + z 1 y ( 1)
+X
1
1 0:8z 1
1
1 0:55z 1 0:2z
=
0:8z 1
1 0:8z 1
{z
Y + (z)
=
=
=
2 0:55z 1 0:2z 2
(1 0:8z 1) (1 0:5z 1 0:25z 2 )
1 0:55z 1 0:2z 2
1 1:3z 1 + 0:15z 2 + 0:2z 3
65:8702
64
0:1298
+
+
1 0:8090z 1 1 0:8z 1 1 + 0:3090z
Hence
y (n) = 65:8702 (0:8090)n
M ATLAB verification:
b = [1]; a = [1, -0.5, -0.25];
yic = [1,2];
(z)
or
% Difference equation
% Initial conditions
2+1
42
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
2
0
2
4
6
100
110
120
130
140
150
160
n
Output sequence
110
120
130
140
170
180
190
200
170
180
190
200
10
y(n)
5
0
5
10
100
150
n
160
n = [0:20]; x = (0.8).n;
% Numerical Solution
V = filtic(b,a,yic), echo on;
V =
1.0000
0.2500
y1 = filter(b,a,x,V);
% Input sequence
% Output sequence
% Analytical solution
b1 = conv(V,[1,-0.8])+[1,0,0]
% Num of Y(z)
b1 =
2.0000
-0.5500
-0.2000
a1 = conv([1,-0.8],[1,-0.5,-0.25])
% Denom of Y(z)
a1 =
1.0000
-1.3000
0.1500
0.2000
[R,p,k] = residuez(b1,a1), echo on; % PFE
R =
65.8702
-64.0000
0.1298
p =
0.8090
0.8000
-0.3090
k =
[]
y2 = zeros(1,21); L = length(R);
%
for l = 1:L
% Assemble
A PRIL 98
S OLUTIONS M ANUAL
y2 = y2 + R(l)*(p(l).n);
end
%
error = abs(max(y1-y2))
error =
1.1169e-013
FOR
DSP
USING
%
%
M ATLAB
43
Output
Sequence
% Difference
1) + y (n
2) + x (n
1)
Y (z)
X (z)
def
=
jzj
>
r0
where r0 is the magnitude of the largest pole since the system is causal.
b) Pole-zero pot and the ROC:
H (z) =
z
z
z2
1+ 5
2
1 5
1+ 5
. Hence the ROC is jzj >
2
2
= 1:618.
H (z) =
z
1+ 5
2
!=
p1
u (n )
p1
5
Hence
h (n ) =
p1
5
1+ 5
2
1+ 5
2
!n
p1
jj
!; z
>
1+ 5
2
!n
u (n )
d) Clearly the system is not stable since h (n) % as n % . For a stable unit sample response the ROC should be
<
Then
p1
5
h (n) =
1+ 5
2
jzj
<
1+ 5
2
!n
u( n
1)
p1
5
!n
u (n )
1) + x (n) + 3x (n
1) ; n 0;
y ( 1) = 2
with the input x (n) = e jn=4 u (n). Taking one-sided z-transform of the difference equation, we obtain,
Y + (z) =
1 1 +
z Y (z) + y ( 1) +
4
1
1
e j=4 z
+3
z 1
e j=4 z
(z)
1
z
4
1
1
1 + 3z 1
+
2 1 e j=4 z
(4.2)
44
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
The second term on the right-hand side provides the zero-state response of the system. Thus
Yzs+ (z)
1 + 3z
1 14 z 1 1
4:4822e j0:8084
1 1
4z
e j=4 z
+
3:8599e j2:1447
1 e j=4 z 1
n
1
4
j0:8084
+ 3:8599e
j2:1447 jn=4
u (n ) :
The steady-state part of the total response is due to simple poles on the unit circle. The pole on the unit circle is at
z = e j=4 which is due to the input sequence. From (4.2), the total response is
Y
(z)
1
1
1 1
4z
!
1
1 + 3z 1
+
2 1 e j=4 z
1
The steady-state response is given by the second-term on the right-hand side. Thus
yss (n) = 3:6129e j2:0282 e jn=4 u (n) = 3:6129e j(n=4
2:0282)
u (n) .
Chapter 5
:::;
2 0; 2; 0; 2; 0; 2; 0; 2; 0; : : : . Now,
"
N 1
X1 (k) =
x1 (n) WNnk ; N = 4; W4 = e
j2=4
4
0
4
0
n =0
Hence,
X1 (0)
X1 (1)
X1 (2)
X1 (3)
=
=
=
=
=
=
=
M ATLAB verification:
xtilde1 = [2,0,2,0]; N = length(xtilde1);
[Xtilde1] = dft(xtilde1,N)
Xtilde1 =
4.0000
0 - 0.0000i
4.0000 + 0.0000i
0 - 0.0000i
:::;
0; 0; 1; 0; 0; 0; 0; 1; 0; 0; : : : . Now,
"
N 1
X2 (k) =
x2 (n) WNnk ; N = 5; W5 = e
j2=5
= 0:3090
j0:9511
n=0
Hence,
X2 (0)
X2 (1)
X2 (2)
X2 (3)
X2 (4)
=
=
=
=
=
0 + 0 + 1 (1) + 0 + 0
0 + 0 + 1 (0:3090 j0:9511)2 + 0 + 0
0 + 0 + 1 (0:3090 j0:9511)4 + 0 + 0
0 + 0 + 1 (0:3090 j0:9511)6 + 0 + 0
0 + 0 + 1 (0:3090 j0:9511)8 + 0 + 0
0:8090 j0:5878
0:3090 + j0:9511
0:3090 j0:9511
0:8090 + j0:5878
=
=
=
M ATLAB verification:
xtilde2 = [0,0,1,0,0]; N = length(xtilde2);
[Xtilde2] = dft(xtilde2,N)
Xtilde2 =
Columns 1 through 4
1.0000
-0.8090 - 0.5878i
0.3090 + 0.9511i
Column 5
-0.8090 + 0.5878i
45
0.3090 - 0.9511i
46
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
:::;
3; 3; 3; 3; 3; 3; 3; 3; : : : . Now,
"
N 1
X3 (k) =
x3 (n) WNnk ; N = 4; W4 = e
j2=4
n=0
Hence,
X3 (0)
X3 (1)
X3 (2)
X3 (3)
3 (1 )
3 (1 )
3 (1 )
3 (1 )
=
=
=
0
0
12
0
=
=
=
M ATLAB verification:
xtilde = [3,-3,3,-3]; N = length(xtilde);
[Xtilde] = dft(xtilde,N)
Xtilde =
0
0.0000 - 0.0000i 12.0000 + 0.0000i
(
j; j;
:::;
"
j;
j; j; j;
j;
j; : : : . Now,
N 1
X4 (k) =
0.0000 - 0.0000i
x4 (n) WNnk ; N = 4; W4 = e
j2=4
n=0
Hence,
X4 (0)
X4 (1)
X4 (2)
X4 (3)
=
=
=
=
0
2 + j2
0
2 + j2
=
=
=
M ATLAB verification:
xtilde = [j,j,-j,-j]; N = length(xtilde4);
[Xtilde4] = dft(xtilde4,N)
Xtilde4 =
0
2.0000 + 2.0000i
:::;
-2.0000 + 2.0000i
1; j; j; 1; 1; j; j; 1; j; j; : : : . Now,
"
N 1
X5 (k) =
x5 (n) WNnk ; N = 4; W4 = e
j2=4
n=0
Hence,
X5 (0)
X5 (1)
X5 (2)
X5 (3)
=
=
=
=
=
=
=
=
2+2j
2
0
2j
M ATLAB verification:
xtilde5 = [1,j,j,1]; N5 = length(xtilde5);
[Xtilde5] = dft(xtilde5,N5)
Xtilde5 =
2.0000 + 2.0000i
2.0000 + 0.0000i
0.0000 - 0.0000i
2. Problem P 5.2
0.0000 - 2.0000i
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
47
1N 1
X1 (k) WN nk ; N = 4; W4
N k
=0
=e
j2=4
Hence,
x1 (0)
x1 (1)
x1 (2)
x1 (3)
[5 (1)
[5 (1)
[5 (1)
[5 (1)
=
=
=
=
2
1:5
2
0:5
M ATLAB verification:
Xtilde1 = [5,-2*j,3,2*j]; N1 = length(Xtilde1);
[xtilde1] = real(idfs(Xtilde1,N1))
xtilde1 =
2.0000
1.5000
2.0000
-0.5000
(b) Periodic DFS sequence: X2 (k) = f4; 5; 3; 5g. Now,
x2 (n) =
1N 1
X2 (k) WNnk ; N = 4; W4 = e
N k
=0
j2=4
Hence,
x2 (0)
x2 (1)
x2 (2)
x2 (3)
[4 (1)
[4 (1)
[4 (1)
[4 (1)
0:75
0:25
4:25
0:25
=
=
=
M ATLAB verification:
Xtilde2 = [4,-5,3,-5]; N = length(Xtilde2);
[xtilde2] = real(idfs(Xtilde2,N))
xtilde2 =
-0.7500
0.2500
4.2500
0.2500
(c) Periodic DFS sequence: X3 (k) = f1; 2; 3; 4; 5g. Now,
x3 (n) =
Hence,
x3 (0)
x3 (1)
x3 (2)
x3 (3)
x3 (4)
=
=
=
=
=
1N 1
X3 (k) WNnk ; N = 5; W5 = e
N k
=0
j2=5
0
0
0
0
0
1W50 + 2W51 + 3W52 + 4W53 + 5W54 =5
1W50 + 2W52 + 3W54 + 4W56 + 5W58 =5
1W50 + 2W53 + 3W56 + 4W59 + 5W512 =5
1W50 + 2W54 + 3W58 + 4W512+ 5W5 16 =5
1W5
= 0:3090
=
=
=
=
=
j0:9511
3
0:5000 j0:6882
0:5000 j0:1625
0:5000 + j0:1625
0:5000 + j0:6882
M ATLAB verification:
Xtilde3 = [1,2,3,4,5]; N = length(Xtilde3);
[xtilde3] = idfs(Xtilde3,N)
xtilde3 =
Columns 1 through 4
3.0000
-0.5000 - 0.6882i -0.5000 - 0.1625i
Column 5
-0.5000 + 0.6882i
-0.5000 + 0.1625i
48
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
jg, N = 4. Now,
1N 1
X5 (k) WN nk ; N = 4; W4
N k
=0
=e
j2=4
Hence,
x5 (0)
x5 (1)
x5 (2)
x5 (3)
[0 (1) + j (1)
[0 (1) + j ( j)
2 j (1) j (1)] =4
2 j ( 1) j ( j)] =4
1) 2 j (1) 2 j ( 1)] =4
j) 2 j ( 1) j ( j)] =4
[0 (1) + j (
[0 (1) + j (
j0:5
0:5 + j0:5
j0:25
0:5 + j0:5
M ATLAB verification:
Xtilde5 = [0,j,-2*j,-j]; N5 = length(Xtilde5);
[xtilde5] = real(idfs(Xtilde5,N1))
xtilde1 =
0 - 0.5000i -0.5000 + 0.5000i
0.0000 - 0.2500i
0.5000 + 0.5000i
1N 1
X (k) WNnk ; N = 4; W4 = e
N k
=0
j2=4
Hence,
x (0)
x (1)
x (2)
x (3)
[0 (1) + 0 (
=
=
j) + 2 ( 1) + 0 ( j)] =4
[0 (1) + 0 ( 1) + 2 (1) + 0 ( 1)] =4
[0 (1) + 0 ( j) + 2 ( 1) + 0 ( j)] =4
=
=
0:5
0:5
0:5
0:5
M ATLAB verification:
Xtilde4 = [00,0,2,0]; N = length(Xtilde4);
[xtilde4] = real(idfs(Xtilde4,N))
xtilde4 =
0.5000
-0.5000
0.5000
-0.5000
3. Problem P 5.3
Periodic x1 (n) with fundamental period N = 50
x1 (n) =
ne
0:3n
0;
0 n 25
26 n 49 PERIODIC
x2 (n) =
ne
0:3n ;
0;
0 n 25
26 n 99
PERIODIC
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
49
xtilde1
0.5
8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Magnitude of Xtilde1(k)
|Xtilde1|
10
8
6
4
2
0
8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Phase of Xtilde1(k)
Angle in Deg
180
90
0
90
180
8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
50
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
xtilde2
1
0.5
|Xtilde2|
10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
Magnitude of Xtilde2(k)
10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
Phase of Xtilde2(k)
10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
k
10
8
6
4
2
Degrees
180
90
0
90
180
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
51
xtilde3
1
0.5
0
10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
Magnitude of Xtilde3(k)
10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
Phase of Xtilde3(k)
10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
k
|Xtilde3|
20
15
10
5
Degrees
180
90
0
90
180
xM (n) =
9
>
=
>
:|
{z
M times
}>
;
PERIODIC
52
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
then there will be (M 1) zeros between samples of XM (k). The magnitudes of non-zero samples of XM (k) will be
M times the magnitudes of the samples of X1 (k), i.e.,
XM (Mk)
XM (k)
=
=
M X1 (k)
0
;
;
k = 0; 1; : : : ; N 1
k 6= 0; 1; : : : ; MN
5. Problem P 5.5
(a) Since y1 (n) is a 3-point IDFS of three samples of X e j on the unit circle, it can be obtained as a 3-point aliasing
operation on x (n). Thus
y1 (n) = f2 + ( 4) + 0 + 2; 5 + ( 2) + ( 3) ; 3 + 6 + ( 3)g = f0; 0; 6gperiodic
M ATLAB verification:
clear; close all;
x = [2,5,3,-4,-2,6,0,-3,-3,2]; n = 0:9;
% (a) y1(n) = 3-point IDFS{X(0),X(2*pi/3),X(4*pi/3)}
N = 3; k = 0:N-1; w = 2*pi*k/N;
[Y1] = dtft(x,n,w); y1 = real(idfs(Y1,N))
y1 =
0.0000
0.0000
6.0000
(b) Since y2 (n) is a 20-point IDFS of twenty samples of X e j on the unit circle, then from the frequency sampling
theorem there will not be any aliasing of x (n) and y2 (n) will be a zero-padded version of x (n). Thus
y2 (n) = f2; 5; 3; 4; 2; 6; 0; 3; 3; 2; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0gperiodic
M ATLAB verification:
% (b) y1(n) = 20-point IDFS{X(0),X(2*pi/20),...,X(28*pi/20)}
N = 20; k = 0:N-1; w = 2*pi*k/N;
[Y2] = dtft(x,n,w); y2 = real(idfs(Y2,N))
y2 =
Columns 1 through 7
2.0000
5.0000
3.0000
-4.0000
-2.0000
6.0000
Columns 8 through 14
-3.0000
-3.0000
2.0000
0.0000
0.0000
0.0000
Columns 15 through 20
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
6. Problem P 5.6
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
53
set(gca,XTickMode,manual,XTick,[0:1:N]);
set(gca,YTickMode,manual,YTick,[0;20;40]);
xlabel(k); ylabel(magnitude); title(Magnitude plots of DFT and DTFT)
hold on
subplot(2,1,2); stem(k,pha_Xk); axis([0,N,-180,180])
set(gca,XTickMode,manual,XTick,[0:1:N]);
set(gca,YTickMode,manual,YTick,[-180;-90;0;90;180]);
xlabel(k); ylabel(Degrees); title(Phase plots of DFT and DTFT)
hold on
The stem plot of X (k) is shown in 5.4.
Plots in Problem P2.6
Magnitude plots of DFT and DTFT
magnitude
40
20
10
11
12
10
11
12
Degrees
90
0
90
180
6
k
(b) DTFT X e j :
[X,w] = freqz(xn,1,1000,whole);
mag_X = abs(X); pha_X = angle(X)*180/pi;
Dw = (2*pi)/N;
subplot(2,1,1); plot(w/Dw,mag_X); grid
hold off
subplot(2,1,2); plot(w/Dw,pha_X); grid
hold off
% DTFT of xn
% mag and phase of DTFT
% frequency resolution
N 1
k=0
X (k)
2k
N
;
where () = e
54
S OLUTIONS M ANUAL
FOR
DSP
USING
11
X (k) e
M ATLAB
j (5:5)
k=0
A PRIL 98
sin (6)
12 sin (=2)
7. Problem P 5.7
This problem is done using M ATLAB.
(a) x1 (n) = 2 cos (0:2n) [u (n)
u (n
10)].
x1
1
0
1
2
10
n
Magnitude of DTFT X1(omega)
10
|X1|
8
6
4
2
0
0
0.1
0.2
0.3
0.4
0.5
0.6
frequency in pi units
0.7
0.8
0.9
Figure 5.5: Sequence and its DTFT magnitude plots in Problem P5.7a
(b) x2 (n) = sin (0:45n) sin (0:55n) ; 0 n 50.
% (b) x2(n) = sin(0.45*pi*n)*sin(0.55*pi*n), 0 <= n <= 50
Hf_2 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_2,NumberTitle,off,Name,P5.7b);
n2 = [0:50]; x2 = sin(0.45*pi*n2).*sin(0.55*pi*n2); N2 = length(n2); N = 300;
[X2] = dft([x2, zeros(1,N-N2)],N);
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
55
x2
0.5
0
0.5
1
10
20
30
40
50
n
Magnitude of DTFT X2(omega)
25
|X2|
20
15
10
5
0
0
0.1
0.2
0.3
0.4
0.5
0.6
frequency in pi units
0.7
0.8
0.9
Figure 5.6: Sequence and its DTFT magnitude plots in Problem P5.7b
(c) x3 (n) = 3 (2)n ;
n
u (n).
56
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
2000
1500
1000
500
10
0
n
Magnitude of DTFT X3(omega)
10
6000
|X3|
4000
2000
0
0
0.1
0.2
0.3
0.4
0.5
0.6
frequency in pi units
0.7
0.8
0.9
Re H e
k
= 5k=0 (0:5) cos k.
(
= Re
Consider
)
h (k) e
jk
k =0
h (k) Re e
k =0
jk
h (k) cos k
k=0
h (n) =
5
= `=0 2` sin `
and
(0:5)
0;
R
0k5
else
H e j d = h (0) = 0
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
57
real(x5)
4
2
0
2
4
0
10
20
30
40
50
60
70
n
Magnitude of DTFT X5(omega)
0.1
0.2
0.3
0.4
0.5
0.6
frequency in pi units
80
90
100
0.8
0.9
|X5|
40
30
20
10
0
0
0.7
Im H e
h (`) e
= Im
`=
h (`) Im e
`=
h (n ) =
j`
j`
h ( ) sin
`
`=
2`; 0 ` 5
0;
else
9. Problem P 5.9
An N-point sequence x (n):
N 1
N 1
(a) The N-point DFT of x (n): X (k) = x (m) WNmk . The N-point DFT of X (k): y (n) = X (k) WNkn . Hence,
N 1
y (n)
m=0
N 1
x (m) WNmk
N 1
WNkn =
N 1
x (m) WNmkWNkn 0 n N
;
m=0
m=0
k =0
N 1
N 1
(m+n)k
x ( m)
WN
=
x ( m)
N (m + n
r=
m=0
m=0
k=0
N
x ( n + rN ) = Nx (( n))N ; 0 n N 1
r=
k=0
N 1
k=0
rN ) ; 0 n N
This means that y (n) is a circularly folded and amplified (by N) version of x (n). Continuing further, if we take
two more DFTs of x (n) then
x ( n)
N-point
DFT
N-point
DFT
N-point
DFT
N-point
DFT
! N 2 x (n)
Therefore, if a given DFT function is working correctly then four successive applications of this function on any
arbitrary signal will produce the same signal (multiplied by N 2 ). This approach can be used to verify a DFT
function.
58
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
5.0000
6.0000
1
[x (n) + x (( n))N ]
2
1
xoc (n) , [x (n) + x (( n))N ]
2
(a) Using the DFT properties of conjugation and circular folding, we obtain
DFT [xec (n)] =
=
1
fDFT [x (n)] + DFT [x (( n))N ]g
2
1
X (k) + X (( k))N ; where X (k) = DFT [x (( n))N ]
2
1
= fX (k) + X (k)g = Re [X (k)] = Re [X (( k))N ]
2
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
59
Imag{DFT[x(n)]}
5
Im{X(k)}
Re{X(k)}
6
4
2
0
0
10
15
20
DFT[xec(n)]
10
15
20
15
20
DFT[xoc(n)]
5
Xoc(k)
Xec(k)
0
0
10
k
15
20
10
k
j0:3; 0; 0:125
From this we can compute the 8-point sequence x (n). M ATLAB script:
clear, close all;
N = 8; X = [0.25,0.125-j*0.3,0,0.125-j*0.06,0.5];
X = [X,conj(X(4:-1:2))]
% Reconstruct the entire X(k)
X =
Columns 1 through 4
0.2500
0.1250 - 0.3000i
0
0.1250 - 0.0600i
Columns 5 through 8
60
S OLUTIONS M ANUAL
FOR
DSP
0.5000
0.1250 + 0.0600i
x = real(idft(X,N))
x =
Columns 1 through 7
0.1562
0.0324
0.1538
0.0324
Column 8
-0.0949
USING
M ATLAB
A PRIL 98
0.1250 + 0.3000i
0.0312
-0.0949
0.0337
(a) Consider x1 (n) = x ((2 n))8 = x (( [n 2]))8 . It is obtained by first circular folding and then circular shifting by
2 of x (n). Hence using properties of the DFT
X1 (k) = DFT [x ((2
n))8 ] = W8
2k
((
k))8 :
M ATLAB script:
% (a) x1(n) = x((2-n))_8; Circ folding followed
N = 8; WN = exp(-j*2*pi/N); k = 0:N-1; m = 2;
X1 = circfold(X,N)
X1 =
Columns 1 through 4
0.2500 + 0.0000i
0.1250 + 0.3000i
0.0000
Columns 5 through 8
0.5000 + 0.0000i
0.1250 - 0.0600i
0.0000
X1 = (WN.(m*k)).*X1
X1 =
Columns 1 through 4
0.2500 + 0.0000i
0.3000 - 0.1250i
0.0000
Columns 5 through 8
0.5000 + 0.0000i -0.0600 - 0.1250i
0.0000
% Matlab verification
x1 = circfold(x,N); x1 = cirshftt(x1,m,N);
X12 = dft(x1,N)
X12 =
Columns 1 through 4
0.2500 + 0.0000i
0.3000 - 0.1250i
0.0000
Columns 5 through 8
0.5000 + 0.0000i -0.0600 - 0.1250i
0.0000
difference = max(abs(X1-X12))
difference =
4.7692e-016
(b) Let X
by circ shifting by 2
- 0.0000i
0.1250 + 0.0600i
- 0.0000i
0.1250 - 0.3000i
+ 0.0000i
-0.0600 + 0.1250i
+ 0.0000i
0.3000 + 0.1250i
+ 0.0000i
-0.0600 + 0.1250i
- 0.0000i
0.3000 + 0.1250i
10
( )
( )
5k
DFT [x ((n + 5))10 ] = W10
X
mk
10
( )
Note that x ((n + 5))10 is 10-point circularly shifted sequence of x (n), shifted to the left by 5 samples. M ATLAB
script:
% (b) x2(n) = x((n+5))_{10}
N = 10; WN = exp(-j*2*pi/N); k = 0:N-1; m = -5;
X2 = (WN.(m*k)).*dft([x,0,0],N)
X2 =
Columns 1 through 4
0.2500
-0.2916 + 0.2848i
0.0176 - 0.0487i
Columns 5 through 8
-0.0863 - 0.1368i
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
61
0.0108 - 0.0047i
-0.0863 + 0.1368i
0.0176 - 0.0487i
-0.0863 - 0.1368i
0.0108 - 0.0047i
-0.0863 + 0.1368i
(c) Consider x3 (n) = x2 (n) = x (n) x (n). Hence the DFT of x3 (n) is given by
X3 (k) =
1
X (k) N X (k) ;
N
N = 8:
M ATLAB script:
% (c) x3(n) = x(n)*x(n);
N = 8;
X3 = circonvf(X,X,N)/N;
% Matlab verification
x3 = x.*x;
X32 = dft(x3,N);
difference = max(abs(X3-X32))
difference =
4.5103e-017
(d) Let X
10
( )
( )
= X (k) X ((
k))8
Note that x (( n))8 and X (( k))8 are the 8-point circular foldings of their respective sequences. M ATLAB script:
clear, close all;
N = 8; X = [0.25,0.125-j*0.3,0,0.125-j*0.06,0.5]; k = 0:N-1; n = k;
X = [X,conj(X(4:-1:2))]
% Reconstruct the entire X(k)
X =
Columns 1 through 4
0.2500
0.1250 - 0.3000i
0
0.1250 - 0.0600i
Columns 5 through 8
0.5000
0.1250 + 0.0600i
0
0.1250 + 0.3000i
x = real(idft(X,N))
x =
Columns 1 through 7
0.1562
0.0324
0.1538
0.0324
0.0312
-0.0949
0.0337
Column 8
-0.0949
%
% (d) x4(n) = x(n)(8)x((-n))_8
X0 = X(mod(-k,N)+1);
% DFT of x((-n))_8
62
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
X4 = X .* X0
% DFT of x(n) (8) x((-n))_8
X4 =
Columns 1 through 7
0.0625
0.1056
0
0.0192
0.2500
0.0192
Column 8
0.1056
% Verification
x4 = circonvt(x,x(mod(-n,N)+1),N); X44 = dft(x4,N);
difference = max(abs(X4-X44))
difference =
2.1330e-016
12. Problem P 5.12 (Two real DFTs using one complex DFT)
x (n) = xR (n) + jxI (n) ; xR (n) and xI (n) are real sequences
Then
xR (n)
jxI (n)
(a) Consider
XR (k)
def
=
1
fx (n) + x (n)g ;
2
1
fx (n) x (n)g
2
and
1
DFT [xR (n)] = fDFT [x (n)] + DFT [x (n)]g
2
1
def
f
X (k) + X (( k))N g = Xec
2
Similarly
jXI (k)
def
=
1
DFT [ jxI (n)] = fDFT [x (n)] DFT [x (n)]g
2
1
def
f
X (k) X (( k))N g = Xoc
2
9
>
=
>
;
) XI = Xjoc =
jXoc
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
63
We will also need the circevod function for complex sequences (see Problem P5.10). This can be obtained from
the one given in the text by two simple changes.
function [xec, xoc] = circevod(x)
% Complex signal decomposition into circular-even and circular-odd parts
% ---------------------------------------------------------------------% [xec, xoc] = circecod(x)
%
N = length(x); n = 0:(N-1);
xec = 0.5*(x + conj(x(mod(-n,N)+1)));
xoc = 0.5*(x - conj(x(mod(-n,N)+1)));
(c) M ATLAB verification:
clear; close all;
N = 64; n = 0:N-1; x1 = cos(0.25*pi*n); x2 = sin(0.75*pi*n);
[X1,X2] = real2dft(x1,x2,N);
X11 = dft(x1,N); X21 = dft(x2,N);
difference = max(abs(X1-X11))
difference =
1.4918e-013
difference = max(abs(X2-X21))
difference =
1.4914e-013
13. Problem P 5.13
Circular shifting:
(a) The M ATLAB routine cirshftf.m to implement circular shift is written using the frequency-domain property
y (n) = x ((n
m))N
= IDFT
X (k) WNmk
This routine will be used in the next problem to generate a circulant matrix and has the following features. If m is a
scaler then y (n) is circularly shifted sequence (or array). If m is a vector then y (n) is a matrix, each row of which
is a circular shift in x (n) corresponding to entries in the vector m.
function y = cirshftf(x,m,N)
% Circular shift of m samples wrt size N in sequence x: (freq domain)
% ------------------------------------------------------------------% function y=cirshift(x,m,N)
%
y : output sequence containing the circular shift
%
x : input sequence of length <= N
%
m : sample shift
%
N : size of circular buffer
%
% Method: y(n) = idft(dft(x(n))*WN(mk))
%
% If m is a scalar then y is a sequence (row vector)
% If m is a vector then y is a matrix, each row is a circular shift
% in x corresponding to entries in vecor m
% M and x should not be matrices
%
% Check whether m is scalar, vector, or matrix
[Rm,Cm] = size(m);
if Rm > Cm
m = m; % make sure that m is a row vector
64
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
end
[Rm,Cm] = size(m);
if Rm > 1
error(*** m must be a vector ***) % stop if m is a matrix
end
% Check whether x is scalar, vector, or matrix
[Rx,Cx] = size(x);
if Rx > Cx
x = x; % make sure that x is a row vector
end
[Rx,Cx] = size(x);
if Rx > 1
error(*** x must be a vector ***) % stop if x is a matrix
end
% Check for length of x
if length(x) > N
error(N must be >= the length of x)
end
x=[x zeros(1,N-length(x))];
X=dft(x,N);
X=ones(Cm,1)*X;
WN=exp(-2*j*pi/N);
k=[0:1:N-1];
Y=(WN.(m * k)).*X;
y=real(conj(dfs(conj(Y),N)))/N;
(b) M ATLAB verification:
n = [0:1:10]; x = 11*ones(1,length(n))-n;
y = cirshftf(x,10,15)
y =
Columns 1 through 7
6.0000
5.0000
4.0000
3.0000
Columns 8 through 14
0.0000
0.0000
0.0000
11.0000
Column 15
7.0000
2.0000
1.0000
0.0000
10.0000
9.0000
8.0000
jx (n)j
N 1
n=0
N 1
x (n) x (n) =
n =0
n=0
(
)
N 1
1N 1
nk
X (k)
x (n) WN
=
N k=0
n =0
1N 1
X (k) WN nk x (n)
N k
=0
1N 1
X (k )
N k
=0
N 1
N 1
n =0
k=0
M ATLAB verification:
x = [1,2,3,4,5,6,6,5,4,3,2,1]; N = length(x);
% power of x(n) in the time-domain
power_x = sum(x.*conj(x))
k=0
n=0
Therefore,
N 1
)
N 1
x (n) WNnk
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
65
power_x =
182
X = dft(x,N);
power_X = (1/N)*sum(X.*conj(X))
power_X =
182.0000
15. Problem P 5.15
M ATLAB function circonvf:
function y = circonvf(x1,x2,N)
%
%function y=circonvf(x1,x2,N)
%
% N-point circular convolution between x1 and x2: (freq domain)
% ------------------------------------------------------------%
y : output sequence containing the circular convolution
%
x1 : input sequence of length N1 <= N
%
x2 : input sequence of length N2 <= N
%
N : size of circular buffer
%
% Method: y(n) = idft(dft(x1)*dft(x2))
% Check for length of x1
if length(x1) > N
error(N must be >= the length of x1)
end
% Check for length of x2
if length(x2) > N
error(N must be >= the length of x2)
end
x1=[x1 zeros(1,N-length(x1))];
x2=[x2 zeros(1,N-length(x2))];
X1=fft(x1); X2=fft(x2);
y=real(ifft(X1.*X2));
16. Problem P 5.16. Circular convolution using circulant matrix operation.
1
6 2 7
7
x1 = 6
4 2 5;
0
1
6 2
X2 = 6
4 3
4
4
1
2
3
3
4
1
2
2
3 7
7
4 5
1
The matrix X2 has the property that its every row or column can be obtained from the previous row or column using
circular shift. Such a matrix is called a circulant matrix. It is completely described by the first column or the row.
The following M ATLAB function circulnt uses the mod function to generate a circulant matrix.
66
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
function C = circulnt(h,N)
% Circulant matrix generation using vector data values
% ---------------------------------------------------% function C = circulnt(h,N)
%
%
C : Circulant matrix
%
h : input sequence of length <= N
%
N : size of the circular buffer
%
Method: C = h((n-m) mod N); n : col vec, m : row vec
Mh
h
C
m
nm
C(:)
=
=
=
=
=
=
length(h); h = reshape(h,Mh,1);
[h; zeros(N-Mh,1)];
zeros(N,N);
0:N-1; n=m;
mod((n*ones(1,N)-ones(N,1)*m),N);
h(nm+1);
1
6 2
x 3 = X2 x 1 = 6
4 3
4
4
1
2
3
3
4
1
2
%
%
%
%
%
%
32
2
1
6 2 7 6
3 7
76
7=6
4 54 2 5 4
1
0
15
12 7
7
9 5
14
M ATLAB script:
% Chapter 5: Problem P5.16: Circular convolution using Circulant matrix
clear, close all;
N = 4; x1 = [1,2,2,0]; x2 = [1,2,3,4];
% (a) Circulant matrix
X2 = circulnt(x2,N)
X2 =
1
4
3
2
1
4
3
2
1
4
3
2
2
3
4
1
% length of x
A PRIL 98
S OLUTIONS M ANUAL
x
C
m
x
C
=
=
=
=
=
FOR
[x, zeros(N-Mx,1)];
zeros(N,N);
0:N-1;
circfold(x,N);
cirshift(x,m,N);
%
%
%
%
%
DSP
USING
M ATLAB
67
zero-pad x
establish size of C
indices n and m
Circular folding
Circular shifting
14.0000
x 3 (n ) = x 1 (n ) N x 2 (n )
n) RN (n) ; N = 10;
-2.4142
-2.4142
205.0000
210.0000
225.0000
68
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
N = 32
M ATLAB script:
% (b) x1(n) = cos(2*pi*n/N)*R16, x2(n) = cos(2*pi*n/N)*R16; N = 32;
N = 32; n = 0:15;
x1 = cos(2*pi*n/N); x2 = sin(2*pi*n/N);
x3 = circonvf(x1,x2,N)
x3 =
Columns 1 through 7
0.0000
0.1951
0.5740
1.1111
1.7678
2.4944
3.2336
Columns 8 through 14
3.9231
4.5000
4.9039
5.0813
4.9888
4.5962
3.8890
Columns 15 through 21
2.8701
1.5607
0.0000
-1.3656
-2.4874
-3.3334
-3.8891
Columns 22 through 28
-4.1573
-4.1575
-3.9231
-3.5000
-2.9424
-2.3097
-1.6629
Columns 29 through 32
-1.0607
-0.5556
-0.1913
0.0000
x4 = conv(x1,x2), x4 = [x4, zeros(1,33)]; N4 = length(x4);
x4 =
Columns 1 through 7
0
0.1951
0.5740
1.1111
1.7678
2.4944
3.2336
Columns 8 through 14
3.9231
4.5000
4.9039
5.0813
4.9888
4.5962
3.8890
Columns 15 through 21
2.8701
1.5607
0.0000
-1.3656
-2.4874
-3.3334
-3.8891
Columns 22 through 28
-4.1573
-4.1575
-3.9231
-3.5000
-2.9424
-2.3097
-1.6629
Columns 29 through 31
-1.0607
-0.5556
-0.1913
e = round(x3 - x4(1:N))
e =
Columns 1 through 12
0
0
0
0
0
0
0
0
0
0
0
0
Columns 13 through 24
0
0
0
0
0
0
0
0
0
0
0
0
Columns 25 through 32
0
0
0
0
0
0
0
0
x4(N+1:N4)
ans =
Columns 1 through 12
0
0
0
0
0
0
0
0
0
0
0
0
Columns 13 through 24
0
0
0
0
0
0
0
0
0
0
0
0
Columns 25 through 32
0
0
0
0
0
0
0
0
(e) Error between Linear and Circular Convolutions:
x1 (n) = f1; 1; 1; 1g ; x2 (n) = f1; 0; 1; 0g ;
M ATLAB script:
N =5
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
69
-2
-2
1 = 7.
Chapter 6
y ( n) =
k=0
k
1
2
x (n
k) +
`=
k
1
3
y (n
`)
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
71
y_CF = casfiltr(b0,B,A,x);
%% Parallel Form: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp(*** Parallel Form Coefficients ***)
*** Parallel Form Coefficients ***
[C,B,A] = dir2par(b,a)
C =
-7.5938
B =
3.4526
0.8052
3.3193
-0.0367
1.8219
0
A =
1.0000
0.4522
0.0745
1.0000
-0.1303
0.0843
1.0000
-0.6553
0
% Output Response
y_PF = parfiltr(C,B,A,x);
%% Filter response plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P6.1);
subplot(3,1,1); stem(n,y_DF); axis([-1,101,0,5])
set(gca,XTickMode,manual,XTick,[0:25:100],fontsize,10);
set(gca,YTickMode,manual,YTick,[0;4]);
ylabel(y_DF); title(Direct Form Filter Response)
subplot(3,1,2); stem(n,y_CF); axis([-1,101,0,5])
set(gca,XTickMode,manual,XTick,[0:25:100],fontsize,10);
set(gca,YTickMode,manual,YTick,[0;4]);
ylabel(y_DF); title(Cascade Form Filter Response)
subplot(3,1,3); stem(n,y_PF); axis([-1,101,0,5])
set(gca,XTickMode,manual,XTick,[0:25:100],fontsize,10);
set(gca,YTickMode,manual,YTick,[0;4]);
xlabel(n); ylabel(y_DF); title(Parallel Form Filter Response)
The block diagrams are shown in Figure 6.1 and the output responses are shown in Figure 6.2.
2. Problem P 6.2
The IIR filter is decribed by the system function
H (z) = 2
1 + 0z 1 + z 2
1 0:8z 1 + 0:64z
2
2
1
z 1
0:75z
1
1 + 2z 1 + z 2
1 + 0:81z 2
The given structure is a parallel form. The M ATLAB script to determine various realizations is:
clear; close all;
%% Cascade Form Coefficients %%%%%%%%%%%%%%%%%%%%%%%%%%
b0 = [2]; B = [1,0,1; 2,-1,0; 1,2,1];
A = [1,-0.8,0.64; 1,-0.75,0; 1,0,0.81];
%% Direct Forms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp(*** Direct Form Coefficients ***)
72
S OLUTIONS M ANUAL
x(n)
FOR
z
z
-1
USING
y(n)
1
-1
DSP
0.1111
-1
0.1111
-1
0.0370
z-1
0.0370
z-1 0.125
z 0.0625
0.0123
0.0123
z 0.0625
z-1 0.03125
0.0041
z-1
0.0041
z-1 0.03125
-1
x(n)
1
z
1
z
0.5
y(n)
-1
0.1303
z-1
-0.0745
0.25
b. Direct Form II
1
-1
-0.4522
0.5
-1
-1
a. Direct Form I
1
y(n)
1
z
z-1 0.125
x(n)
0.3333
0.25
0.3333
A PRIL 98
-1
0.5
-1
M ATLAB
-1
-0.5
0.6553
0.5
z-1
0.25
-0.0843
0.25
c. Cascade Form
-7.5938
3.4526
x(n)
-0.4522
-0.0745
z-1
0.1303
-1
-1
-0.0843
0.6553
-1
y(n)
-1
0.8052
3.3193
-0.0367
1.8219
d. Parallel Form
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
73
y_DF
25
50
Cascade Form Filter Response
75
100
25
50
Parallel Form Filter Response
75
100
25
50
n
75
100
y_CF
y_PF
-0.7500
14:75 12:9z
3
1 76 z 1 + 32
z
1
2
+
24:5 + 26:82z
1 z 1 + 0:5z
1
2
The given structure is a parallel form. The M ATLAB script to determine various realizations is:
clear; close all;
%% Parallel Form Coefficients %%%%%%%%%%%%%%%%%%%%%%%%%%
74
S OLUTIONS M ANUAL
x(n)
DSP
FOR
y(n)
4
z-1
z
-1
-1
z-1
z
-1
USING
6
4
2.05
-1
-1
1.7355
1.0044
z-1
0.3888
x(n)
1.55
-1
0.81
z-1
1.7355
-1
1.0044
z-1
0.3888
-1
b. Direct Form II
1
0.8
z-1
0
z-1
-1
2.05
z-1
0
y(n)
a. Direct Form I
1
A PRIL 98
x(n)
z-1
1.55
M ATLAB
0.64
z-1
2
z-1
0.75
0.5
c. Cascade Form
5.144
2.0137
x(n)
0
0.81
-1
y(n)
0.1106
z-1
10.8732
0.8
0.64
-1
-1
-1
15.0013
7.7155
0.75
d. Parallel Form
y(n)
A PRIL 98
S OLUTIONS M ANUAL
1.0000
1.0000
1.8251
-1.0834
0.3726
0
1.0000
1.0000
-1.0000
-0.8750
0.5000
0.0938
FOR
DSP
USING
M ATLAB
75
A =
y(n)
9.75
z-1 7.2325
1.8750
z-1
-15.6456
-1.4688
z-1
-3.9356
z-1
z-1
z
-0.0469
y(n)
9.75
z-1
0.5312
a. Direct Form I
x(n)
1.8750
z-1 7.2325
-1.4688
z-1
-15.6456
z-1
0.5312
-1
-0.0469
-3.9356
-1
b. Direct Form II
1
9.75
x(n)
1
y(n)
z-1
1.0
1.8251
0.8750
0.3726
-0.0938
z-1
-0.5
z-1
-1.0834
z-1
0
c. Cascade Form
-14.75
x(n)
y(n)
7/8
-3/32
-1
-12.9
z-1
24.5
1
-0.5
-1
26.82
z-1
d. Parallel Form
76
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
4. Problem P 6.5
A linear shift-invariant system has the system function
H (z) =
0:5 1 + z
1
3 1
7
+ 8z 2
2z
13 3
16 z
1 6
1 4
6z
11 5
7
+ 16 z 6
32 z
0.5
-1
-1
-1
-0.5
-1
0.5
-1
-1
z
-0.875
y(n)
-1
-1
-1
-1
5 + 11:2z
+ 5:44z 2
1 + 0:8z 1
0:384z
0:512z
3
3
2:3552z
0:4096z
4
4
1:2288z
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
77
z-1
2
x(n)
y(n)
z-1
-0.8
z-1
-0.64
2
z-1
0
z-1
0.64
Figure 6.6: Parallel Form Structure in Problem P6.6
(b) The above solution is unique because the numerator residues and the corresponding denominator poles must be
grouped into a second order section.
6. Problem P 6.7
A linear shift-invariant system has the system function
H (z) =
0:5 1 + z
1
3 1
7
+ 8z 2
2z
13 3
16 z
1 6
1 4
8z
11 5
7
+ 16 z 6
32 z
78
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
-0.9780
0.7500
A PRIL 98
S OLUTIONS M ANUAL
x(n)
FOR
DSP
USING
2.0470
M ATLAB
z-1
0.9985
0.5
z-1
2.0359
0.5
z-1
0.5050
z-1
0.5527
79
y(n)
1.5470
z-1
9.9973
2.0
0.875
z-1
y (n) =
k=0
j5 kj
1
2
x (n
k)
0.5000
80
S OLUTIONS M ANUAL
1.0000
1.0000
1.0000
1.0000
1.0000
1.7495
0.5046
-0.5506
-2.0806
2.3770
FOR
DSP
USING
M ATLAB
A PRIL 98
3.4671
0.2884
0.2646
3.7789
1.0000
A =
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0.0312
0.25
z-1
0.0625
z-1
0.1250
z-1
0.25
z-1
0.1250
0.0312
z-1
0.0625
z-1
x(n)
y(n)
a. Direct Form
z-1
x(n)
z-1
z-1
z-1
0.0312
-1
z
0.0625
-1
0.5
0.25
1
y(n)
b. Linear-Phase Form
x(n)
1
-1
z-1
1.7495
3.4671
-1
-1
0.5046
-0.5506
-2.0806
-1
1
-1
-1
z-1
-1
2.3770
-1
0.2884
0.2646
3.7789
1.0
c. Cascade Form
H (z) =
(2z)
k=0
y(n)
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
0.1250
USING
M ATLAB
0.0625
0.0312
81
0.0156
0.0010
0.00098
0.00391
0.125
z-1
0.00195
z-1
0.00781
z-1
0.25
z-1
0.5
z-1
x(n)
y(n)
a. Direct Form
x(n)
1
-1
z-1
0.9595
0.25
-1
1
-1
0.6549
0.1423
-0.4154
-0.8413
-1
-1
-1
0.25
-1
0.25
-1
0.25
b. Cascade Form
-1
0.25
y(n)
Chapter 7
n); 0 n M
1;
1
2
is not an integer
Consider,
H e
M 1
jn
h (n ) e
M
2
n=0
jn
h (n ) e
M 1
n=0
h (n) e
jn
n)
M
2
n2=0 h (n) e
! M2
jn +
M 1
2
n=0
M
2 1
n=0
M
2 1
n=0
1; M
2
h (n) e
nn
=0
j( M2 1 )
j( M2 1 )
h(n) e+ j(
j( M2 1 )
2h(n) cos
h (n ) e
j(M 1 n)
jn+ j( M2 1 )
M 1
2
n)
M 1
j(M 1 n)+ j( M2 1 )
+e
+e
j( M2 1 n)
Change of variable:
n!n)n=0!n=
M
2
and
cos
Hence,
H e
Define b(n) = 2h
H e
M
2
=e
j( M2 1 )
M
2
2h
n=1
! cos
1!n=1
M
M
; n =
2
2
M
2
n
1
2
n cos n
1
2
n . Then,
=e
j( M2 1 )
M
2
b (n) cos
n =1
82
1
2
M
2
) Hr () = b (n) cos
n=1
1
2
A PRIL 98
S OLUTIONS M ANUAL
1
2
DSP
FOR
M ATLAB
USING
83
1
2
3
cos
2
5
cos
2
1
2
b (n) cos n
Hr () =
n=1
M
2
= cos
2
where b (n) are related to b (n) through the above trignometric identities.
2. (Problem P 7.3) Type-III ) anti-symmetric h(n) and Modd, i.e.,
h(n) = h(M
n); 0 n M
1; h
= 0;
1
2
is an integer
(a) Consider,
H e j
M 1
jn
h (n) e
M 3
2
n=0
h (n ) e
M 1
jn
n=0
h (n) e
jn
M +1
2
n)
M+1
2
! M2
1 ! 0; and h (n) !
h (n)
Hence,
H e
M 3
2
h (n) e
M 3
2
h (n) e
jn
n=0
j ( M2 1 )
j(M 1 n)
n=0
M 3
2
h (n )
jn+ j( M2 1 )
j(M 1 n)+ j( M2 1 )
n=0
j ( M2 1 )
M 3
2
h (n )
e+ j(
M 1
2
n)
j( M2 1 n)
n=0
j ( M2 1 )
je
M 3
2
2h(n) sin
M
2
n!n)n=0!n=
and
sin
1
2
H e
je
j( M2 1 )
1
2
Hence,
j
M 1
2
n=1
n=0
Change of variable:
cos
M
2
fcos 0g
2
cos f2 cos 1g
2
cos
2h
n=
3
2
!n=1
! sin (n)
M
1
2
n sin (n)
1) in the
84
S OLUTIONS M ANUAL
Define c(n) = 2h
M 1
2
FOR
DSP
USING
M ATLAB
A PRIL 98
n . Then,
H e
je
M 1
2
M 1
2
n =1
n=1
j( M2 1 )
(b) Now sin (n) can be written as a linear combination of higher harmonics in cos multiplied by sin , i.e.,
sin ()
sin (2)
sin (3)
=
=
=
sin fcos0g
sin f2 cosg
sin fcos0 + 2 cos2g
etc. Note that the lowest harmonic frequency is zero and the highest harmonic frequency is (n
expansion. Hence,
Hr () =
M 1
2
M 3
2
n=1
where c (n) are related to c (n) through the above trignometric identities.
Problem P 7.5
M ATLAB function for amplitude response:
function [Hr,w,P,L] = ampl_res(h);
%
% function [Hr,w,P,L] = ampl_res(h)
%
% Computes Amplitude response Hr(w) and its polynomial P of order L,
%
given a linear-phase FIR filter impulse response h.
% The type of filter is determined automatically by the subroutine.
%
% Hr = Amplitude Response
% w = frequencies between [0 pi] over which Hr is computed
% P = Polynomial coefficients
% L = Order of P
% h = Linear Phase filter impulse response
%
M = length(h);
L = floor(M/2);
if fix(abs(h(1:1:L))*1010) = fix(abs(h(M:-1:M-L+1))*1010)
error(Not a linear-phase impulse response)
end
if 2*L = M
if fix(h(1:1:L)*1010) == fix(h(M:-1:M-L+1)*1010)
disp(*** Type-1 Linear-Phase Filter ***)
[Hr,w,P,L] = hr_type1(h);
elseif fix(h(1:1:L)*1010) == -fix(h(M:-1:M-L+1)*1010)
disp(*** Type-3 Linear-Phase Filter ***)
h(L+1) = 0;
[Hr,w,P,L] = hr_type3(h);
end
else
if fix(h(1:1:L)*1010) == fix(h(M:-1:M-L+1)*1010)
disp(*** Type-2 Linear-Phase Filter ***)
1) in the sin n
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
85
[Hr,w,P,L] = hr_type2(h);
elseif fix(h(1:1:L)*1010) == -fix(h(M:-1:M-L+1)*1010)
disp(*** Type-4 Linear-Phase Filter ***)
[Hr,w,P,L] = hr_type4(h);
end
end
M ATLAB verification:
clear; close all;
%% Matlab verification
h1 = [1 2 3 2 1]; [Hr1,w,P1,L1] = ampl_res(h1);
*** Type-1 Linear-Phase Filter ***
P1, L1,
P1 =
3
4
2
L1 =
2
%
h2 = [1 2 2 1]; [Hr2,w,P2,L2] = ampl_res(h2);
*** Type-2 Linear-Phase Filter ***
P2, L2,
P2 =
4
2
L2 =
2
%
h3 = [1 2 0 -2 -1]; [Hr3,w,P3,L3] = ampl_res(h3);
*** Type-3 Linear-Phase Filter ***
P3, L3,
P3 =
0
4
2
L3 =
2
%
h4 = [1 2 -2 -1]; [Hr4,w,P4,L4] = ampl_res(h4);
*** Type-4 Linear-Phase Filter ***
P4, L4,
P4 =
4
2
L4 =
2
%
%% Amplitude response plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P7.5);
subplot(2,2,1);plot(w/pi,Hr1);title(Type-1 FIR Filter);
ylabel(Hr(w));
set(gca,XTickMode,manual,XTick,[0:0.2:1],fontsize,10);
subplot(2,2,2);plot(w/pi,Hr2);title(Type-2 FIR Filter);
ylabel(Hr(w));
set(gca,XTickMode,manual,XTick,[0:0.2:1],fontsize,10);
86
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
10
Hr(w)
Hr(w)
6
4
2
0
2
0
0.2
0.4
0.6
0.8
0.2
Hr(w)
Hr(w)
0.6
0.8
0.8
3
2
3
2
1
1
0
0.4
0.2
0.4
0.6
0.8
frequency in pi units
0.2
0.4
0.6
frequency in pi units
1
z2 = e j ;
r
z2 = re
1
z2 = e
r
=
=
=
=
h (n) = 1; 2 cos r + r
"
r2 + r
2
+ 4 cos
2 cos r + r
;
+z 4
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
87
which is a finite-duration symmetric impulse response. This implies that the filter is a linear-phase FIR filter.
Problem P 7.7
The bandstop filter specifications are:
lower passband edge:
lower stopband edge:
upper stopband edge:
upper passband edge:
passband ripple:
stopband attenuation:
0:3
0:4
0:6
0:7
0:5 dB
40 dB
88
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Hanning Window
0.8
1
w_ham(n)
hd(n)
0.6
0.4
0.2
0
0
0.2
0
62
62
Magnitude Response in dB
0.8
Decibels
h(n)
0.6
0.4
0.2
40
0
0.2
0
62
0.3 0.4
0.6 0.7
frequency in pi units
Figure 7.2: Bandstop filter design using Hanning Window in Problem P 7.7
Problem P 7.8
The bandpass filter specifications are:
lower stopband edge:
lower passband edge:
upper passband edge:
upper stopband edge:
passband ripple:
stopband attenuation:
0:3
0:4
0:5
0:6
0:5 dB
50 dB
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
89
90
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Hamming Window
0.3
1
w_ham(n)
0.2
hd(n)
0.1
0
0.1
0.2
0
0
66
66
Magnitude Response in dB
0.3
0.2
Decibels
h(n)
0.1
0
0.1
50
0.2
0
66
frequency in pi units
Figure 7.3: Bandpass filter design using Hamming Window in Problem P 7.8
3. (Problem P 7.9) The highpass filter specifications are:
stopband edge:
passband edge:
passband ripple:
stopband attenuation:
0:4
0:6
0:5 dB
60 dB
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
91
[Rp,As] = delta2db(delta1,delta2)
end
%
tr_width = wp-ws; M = ceil((As-7.95)/(14.36*tr_width/(2*pi))+1)+1;
M = 2*floor(M/2)+3, % choose odd M, Increased order by 2 to get Asd>60
M =
41
n = [0:1:M-1]; beta = 0.1102*(As-8.7);
w_kai = (kaiser(M,beta));
% Kaiser Window
wc = (ws+wp)/2; hd = ideal_lp(pi,M)-ideal_lp(wc,M); % Ideal HP Filter
h = hd .* w_kai;
% Window design
[db,mag,pha,grd,w] = freqz_m(h,1);
delta_w = pi/500;
Asd = -floor(max(db(1:1:(ws/delta_w)+1))),
% Actual Attn
Asd =
61
Rpd = -min(db((wp/delta_w)+1:1:501)),
% Actual passband ripple
Rpd =
0.0148
%
%% Filter Response Plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P7.9);
subplot(2,2,1); stem(n,hd); title(Ideal Impulse Response);
axis([-1,M,min(hd)-0.1,max(hd)+0.1]); xlabel(n); ylabel(hd(n))
set(gca,XTickMode,manual,XTick,[0;M-1],fontsize,10)
subplot(2,2,2); stem(n,w_kai); title(Kaiser Window);
axis([-1,M,-0.1,1.1]); xlabel(n); ylabel(w_kai(n))
set(gca,XTickMode,manual,XTick,[0;M-1],fontsize,10)
set(gca,YTickMode,manual,YTick,[0;1],fontsize,10)
subplot(2,2,3); stem(n,h); title(Actual Impulse Response);
axis([-1,M,min(hd)-0.1,max(hd)+0.1]); xlabel(n); ylabel(h(n))
set(gca,XTickMode,manual,XTick,[0;M-1],fontsize,10)
subplot(2,2,4); plot(w/pi,db); title(Magnitude Response in dB);
axis([0,1,-As-30,5]); xlabel(frequency in pi units); ylabel(Decibels)
set(gca,XTickMode,manual,XTick,[0;0.4;0.6;1])
set(gca,XTickLabelMode,manual,XTickLabels,[ 0 ;0.4;0.6; 1 ],...
fontsize,10)
set(gca,YTickMode,manual,YTick,[-60;0])
set(gca,YTickLabelMode,manual,YTickLabels,[ 60; 0 ]);grid
% Super Title
suptitle(Problem P7.9: Highpass Filter Design);
92
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Kaiser Window
0.6
1
w_kai(n)
hd(n)
0.4
0.2
0
0.2
0
0.4
0
40
40
Magnitude Response in dB
0.6
Decibels
h(n)
0.4
0.2
0
60
0.2
0.4
0
40
0.4
0.6
frequency in pi units
Figure 7.4: Highpass Filter design using Kaiser window in Problem P 7.9.
The bandpass filter specifications are:
lower stopband edge:
lower passband edge:
upper passband edge:
upper stopband edge:
passband tolerance:
stopband tolerance:
0:25
0:35
0:65
0:75
0:05
0:01
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
93
94
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Homework4 : Problem 4
Ideal Impulse Response: Bandpass
Kaiser Window
1
w_ham(n)
0.4
hd(n)
0.2
0
0.2
0
0.4
48
48
Magnitude Response in dB
0
0.4
Decibels
h(n)
0.2
0
40
0.2
0.4
48
0.250.35
0.650.75
frequency in pi units
Figure 7.5: Bandpass filter design using Kaiser Window in Problem P 7.10
%
%
%
%
%
%
%
=
=
=
=
=
if ws <= 0
error(Stopband edge must be larger than 0)
end
if wp >= pi
error(Passband edge must be smaller than pi)
end
if wp <= ws
error(Passband edge must be larger than Stopband edge)
end
% Select the min(delta1,delta2) since delta1=delta2 in windodow design
tr_width = wp-ws; M = ceil((As-7.95)/(14.36*tr_width/(2*pi))+1)+1;
M = 2*floor(M/2)+1;
% choose odd M
if M > 255
error(M is larger than 255)
end
n = [0:1:M-1]; beta = 0.1102*(As-8.7);
w_kai = (kaiser(M,beta));
% Kaiser Window
wc = (ws+wp)/2; hd = ideal_lp(pi,M)-ideal_lp(wc,M); % Ideal HP Filter
h = hd .* w_kai;
% Window design
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
95
M ATLAB verification:
clear; close all;
%% Specifications:
ws = 0.4*pi; % stopband edge
wp = 0.6*pi; % passband edge
As = 60;
% stopband attenuation
%
[h,M] = kai_hpf(ws,wp,As); n = 0:M-1;
[db,mag,pha,grd,w] = freqz_m(h,1);
delta_w = pi/500;
Asd = -floor(max(db(1:1:(ws/delta_w)+1))),
% Actual Attn
Asd =
60
Rpd = -min(db((wp/delta_w)+1:1:501)),
% Actual passband ripple
Rpd =
0.0147
%
%% Filter Response Plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P7.11b);
subplot(2,1,1); stem(n,h); title(Actual Impulse Response);
axis([-1,M,min(h)-0.1,max(h)+0.1]); xlabel(n); ylabel(h(n))
set(gca,XTickMode,manual,XTick,[0;M-1],fontsize,10)
subplot(2,1,2); plot(w/pi,db); title(Magnitude Response in dB);
axis([0,1,-As-30,5]); xlabel(frequency in pi units); ylabel(Decibels)
set(gca,XTickMode,manual,XTick,[0;0.4;0.6;1])
set(gca,XTickLabelMode,manual,XTickLabels,[ 0 ;0.4;0.6; 1 ],...
fontsize,10)
set(gca,YTickMode,manual,YTick,[-60;0])
set(gca,YTickLabelMode,manual,YTickLabels,[ 60; 0 ]);grid
% Super Title
suptitle(Problem P7.11b: Highpass Filter Design);
The filter design plots are shown in Figure 7.6.
Problem P 7.12
The staircase filter specifications are:
Band-1:
Band-2:
Band-3:
0
0:4
0:8
0:3
0:7
Ideal Gain
Ideal Gain
Ideal Gain
=
=
=
1
0:5
0
1
2
3
=
=
=
0:010
0:005
0:001
96
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
h(n)
0.2
0
0.2
0.4
0
38
n
Magnitude Response in dB
Decibels
60
0.4
0.6
frequency in pi units
Figure 7.6: Highpass filter design using the ntexttt fkai hpfg function in Problem 7.11b.
As = -20*log10(delta3)
As =
60.0000
%
%% Determination of Window Parameters
tr_width = min((w2L-w1U),(w3L-w2U));
M = ceil(11*pi/tr_width); M = 2*floor(M/2)+1, % choose odd M
M =
111
n=[0:1:M-1];
w_bla = (blackman(M));
wc1 = (w1U+w2L)/2; wc2 = (w2U+w3L)/2;
hd = 0.5*ideal_lp(wc1,M) + 0.5*ideal_lp(wc2,M);
h = hd .* w_bla;
[db,mag,pha,grd,w] = freqz_m(h,1);
delta_w = pi/500;
Asd = floor(-max(db([floor(w3L/delta_w)+1:501]))),
% Actual Attn
Asd =
79
%
%% Filter Response Plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P7.12);
subplot(2,2,1); stem(n,w_bla); title(Blackman Window);
axis([-1,M,-0.1,1.1]); xlabel(n); ylabel(w_ham(n))
set(gca,XTickMode,manual,XTick,[0;M-1],fontsize,10)
set(gca,YTickMode,manual,YTick,[0;1],fontsize,10)
A PRIL 98
S OLUTIONS M ANUAL
DSP
FOR
USING
M ATLAB
97
1],
1],
% Super Title
suptitle(Problem P7.12: Staircase Filter);
The filter design plots are shown in Figure 7.7.
Problem P7.12: Staircase Filter
Blackman Window
h(n)
w_ham(n)
0.4
0.2
0
0
0
110
110
Magnitude Response
Magnitude Response in dB
0
Decibels
|H|
0.5
0.3 0.4
0.7 0.8
frequency in pi units
60
0.3 0.4
0.7 0.8
frequency in pi units
Figure 7.7: Staircase filter design using Blackman Window in Problem P 7.12
Problem P 7.18
Bandpass filter design using Parks-McClellan Algorithm
98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
(a) Since we are given M = 25, As = 50 dB, and the ideal cutoff frequencies, c1 = =3 and c2 = 2=3, we will
have to determine the band edge frequencies, s1 , p1 , p2 , and S2 , to implement the Parks-McClellan algorithm.
Using the formula due to Kaiser we can determine the transition width as follows
M
1'
As 13
14:36 (=2)
Since no additional information is given, we will assume that the transition bandwidths are equal and that the
tolerances are also equal in each band, i.e.,
s 1
and
= c1
; p
1
2
= c1 +
; p
2
2
= c2
; s
2
2
= c2 +
1 = 2 = 3
Using this we will run the remez algorithm and check for the stopband attenuation of 50 dB. If the actual attenuation is less than (more than) 50 then we will increase (decrease) until the attenuation condition is met. In the
following M ATLAB script, the conditionw were met at the above computed value.
clear; close all;
%% Specifications
wc1 = pi/3;
% lower cutoff frequency
wc2 = 2*pi/3; % upper cuoff frequency
As = 50;
% stopband attenuation
M = 25;
% filter length
%
% (a) Design
tr_width = 2*pi*(As-13)/(14.36*(M-1)), % transition width in radians
tr_width =
0.6746
ws1 = wc1-tr_width/2; wp1 = wc1+tr_width/2;
wp2 = wc2-tr_width/2; ws2 = wc2+tr_width/2;
f = [0,ws1/pi,wp1/pi,wp2/pi,ws2/pi,1];
m = [0,0,1,1,0,0];
n = 0:M-1;
h = remez(M-1,f,m);
[db,mag,pha,grd,w] = freqz_m(h,1);
delta_w = pi/500;
Asd = floor(-max(db([1:floor(ws1/delta_w)+1]))), % Actual Attn
Asd =
50
Rpd = -min(db(ceil(wp1/delta_w)+1:floor(wp2/delta_w)+1)), % Actial ripple
Rpd =
0.0518
%
%% Filter Response Plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P7.18);
subplot(2,1,1); stem(n,h); title(Impulse Response: Bandpass);
axis([-1,M,min(h)-0.1,max(h)+0.1]); xlabel(n); ylabel(h(n))
set(gca,XTickMode,manual,XTick,[0;12;24],fontsize,10)
subplot(2,1,2); plot(w/pi,db); title(Magnitude Response in dB);
axis([0,1,-80,5]); xlabel(frequency in pi units); ylabel(Decibels)
set(gca,XTickMode,manual,XTick,f,fontsize,10)
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
99
set(gca,YTickMode,manual,YTick,[-50;0])
set(gca,YTickLabelMode,manual,YTickLabels,[-50; 0 ],fontsize,10);grid
The impulse response plot is shown in Figure 7.8.
Homework4 : Problem 5a
Impulse Response: Bandpass
0.4
h(n)
0.2
0
0.2
12
24
n
Magnitude Response in dB
Decibels
50
0.226
0.4407
0.5593
0.774
frequency in pi units
100
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
0.8
Hr(w)
0.6
0.4
0.2
0
0
0.226
0.4407 0.5593
frequency in pi units
0.774
As = 40;
% stopband attenuation
%
% (a) Design
delta1 = (10(Rp/20)-1)/(10(Rp/20)+1);
delta2 = (1+delta1)*(10(-As/20));
weights = [delta2/delta1, 1, delta2/delta1];
delta_f =min((wp2-ws2)/(2*pi), (ws1-wp1)/(2*pi));
M = ceil((-20*log10(sqrt(delta1*delta2))-13)/(14.6*delta_f)+1);
M = 2*floor(M/2)+1
M =
33
f = [0, wp1/pi, ws1/pi, ws2/pi, wp2/pi, 1];
m = [1 1 0 0 1 1];
h = remez(M-1,f,m,weights);
[db,mag,pha,grd,w] = freqz_m(h,[1]);
delta_w = pi/500;
Asd = floor(-max(db([floor(ws1/delta_w)+1:floor(ws2/delta_w)]))), % Actual Attn
Asd =
40
M = M+2
M =
35
h = remez(M-1,f,m,weights);
[db,mag,pha,grd,w] = freqz_m(h,[1]);
delta_w = pi/500;
Asd = floor(-max(db([floor(ws1/delta_w)+1:floor(ws2/delta_w)]))), % Actual Attn
Asd =
40
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
101
n = 0:M-1;
%
%%Filter Response Plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P7.19a);
subplot(2,1,1); stem(n,h); title(Impulse Response: Bandpass);
axis([-1,M,min(h)-0.1,max(h)+0.1]); xlabel(n); ylabel(h(n))
set(gca,XTickMode,manual,XTick,[0;17;34],fontsize,10)
subplot(2,1,2); plot(w/pi,db); title(Magnitude Response in dB);
axis([0,1,-60,5]); xlabel(frequency in pi units); ylabel(Decibels)
set(gca,XTickMode,manual,XTick,f,fontsize,10)
set(gca,YTickMode,manual,YTick,[-40;0])
set(gca,YTickLabelMode,manual,YTickLabels,[ 40; 0 ],fontsize,10);grid
% Super Title
suptitle(Problem P7.19a: Bandstop Filter);
The filter response plots are shown in Figure 7.10.
Problem P7.19a: Bandstop Filter Design
Impulse Response: Bandpass
0.6
h(n)
0.4
0.2
0
0.2
0
17
34
n
Magnitude Response in dB
Decibels
40
0.3
0.4
0.6
0.7
frequency in pi units
102
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
axis([0,1,-0.1,1.1])
set(gca,XTickMode,manual,XTick,f)
The amplitude response plot is shown in Figure 7.11.
Amplitude Response in Problem P7.19b
0.8
Hr(w)
0.6
0.4
0.2
0
0
0.3
0.4
0.6
frequency in pi units
0.7
Band-1
0
0:4
0:4
0:05
Band-2 Band-3
0 :8
0:5
0:7
1
0:95
0:025
0:05
0:025
M ATLAB Script:
clear; close all;
%% Specifications
f = [0,0.4,0.5,0.7,0.8,1];
m = [0.4,0.4,0.95,0.95,0.025,0.025];
delta1 = 0.05; delta2 = 0.05; delta3 = 0.025;
weights = [delta3/delta2, delta3/delta2, delta3/delta3];
As = -20*log10(0.05)
As =
26.0206
%
%% Design
delta_f = 0.05; % Transition width in cycles per sample
M = ceil((-20*log10(sqrt(delta2*delta3))-13)/(14.6*delta_f)+1)
M =
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
103
23
h = remez(M-1,f,m,weights);
[db,mag,pha,grd,w] = freqz_m(h,1);
delta_w = pi/500;
Asd = floor(-max(db([(0.8*pi/delta_w)+1:501]))), % Actual Attn
Asd =
24
M = M+1
M =
24
h = remez(M-1,f,m,weights);
[db,mag,pha,grd,w] = freqz_m(h,1);
Asd = floor(-max(db([(0.8*pi/delta_w)+1:501]))), % Actual Attn
Asd =
25
M = M+1
M =
25
h = remez(M-1,f,m,weights);
[db,mag,pha,grd,w] = freqz_m(h,1);
Asd = floor(-max(db([(0.8*pi/delta_w)+1:501]))), % Actual Attn
Asd =
25
M = M+1
M =
26
h = remez(M-1,f,m,weights);
[db,mag,pha,grd,w] = freqz_m(h,1);
Asd = floor(-max(db([(0.8*pi/delta_w)+1:501]))), % Actual Attn
Asd =
25
M = M+1
M =
27
h = remez(M-1,f,m,weights);
[db,mag,pha,grd,w] = freqz_m(h,1);
Asd = floor(-max(db([(0.8*pi/delta_w)+1:501]))), % Actual Attn
Asd =
26
n = 0:M-1;
%
%% Impulse Response Plot
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P7.21a);
stem(n,h); title(Impulse Response Plot in Problem 6);
axis([-1,M,min(h)-0.1,max(h)+0.1]); xlabel(n); ylabel(h(n))
set(gca,XTickMode,manual,XTick,[0;13;26],fontsize,12)
%
%% Amplitude Response Plot
Hf_2 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_2,NumberTitle,off,Name,P7.21b);
[Hr,w,a,L] = Hr_type1(h);
104
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
0.4
0.3
h(n)
0.2
0.1
0.1
0.2
0
13
n
26
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
105
Hr(w)
0.9
0.45
0.35
0.05
0
0
0.4
0.5
frequency in pi units
0.7
0.8
(b) Output when x (n) = 3 sin (0:25n) ; 0 n 100: First determine the sign of h (n) and then appropriately convolve
with x (n).
% (b) Differentiator verification
Hf_2 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_2,NumberTitle,off,Name,P7.20b);
[Hr,w,P,L] = ampl_res(h); subplot; plot(w/(2*pi), Hr);
*** Type-3 Linear-Phase Filter ***
title(Amplitude Response); grid; axis([0,0.5,-0.5,0]);
set(gca,XTickMode,manual,XTick,[0;w1/(2*pi);w2/(2*pi);0.5],fontsize,10);
set(gca,YTickMode,manual,YTick,[-0.5;-0.45;-0.05;0],fontsize,10);
The amplitude response plot is shown in Figure 7.15. The sign of h (n) from Figure 7.15 is negative, hence we will
convolve x (n) with h (n) and then compare the input and output in the steady-state (i.e. when n > 25) with output
shifted by 12 to the left to account for the phase delay of the differentiator.
Hf_3 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_3,NumberTitle,off,Name,P7.20c);
n=[0:1:100]; x = 3*sin(0.25*pi*n); y = conv(x,-h);
m = [41:1:81];
plot(m,x(41:1:81),m,y(41+12:1:81+12));grid % add 12 sample delay to y
xlabel(n); title(Input-Output Sequences); axis([40,82,-4,4]);
set(gca,XTickMode,manual,XTick,[41;81],fontsize,10);
set(gca,YTickMode,manual,YTick,[-3;0;3],fontsize,10);
The input-output plots are shown in Figure 7.16. Since the slope is =2 sam/rad, the gain at = 0:25 is equal to
0:125. Therefore, the output (when properly shifted) should be
y (n) = 3 (0:125) cos (0:25n) = 0:375 cos (0:25n)
106
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Impulse Response
0.2
h(n)
0.1
0.1
0.2
12
n
24
Magnitude Response
0.5
0.45
0.05
0
0.05
0.45
0.5
= c
0:01; p2
= c + 0:01;
s 1
= p1
; and s2
= p2 +
We will aslo assume that the tolerances in each band are equal. Now we will begin with initial value for = 0:2
and run the remez algorithm to obtain the actual stopband attenuation. If it is smaller (larger) than the given 30 dB
then we will increase (decrease) then iterate to obtain the desired solution. The desired solution was found for
= 0:5. M ATLAB Script:
clear; close all;
%% Specifications
N = 50;
% Order of the filter
w0 = 0.5*pi;
% Center frequency
Bandwidth = 0.02*pi;
% Bandwidth
%
%
Deltaw = Transition bandwidth (iteration variable)
%
wp1 = w0-Bandwidth/2; wp2 = w0+Bandwidth/2;
% (a) Design
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
107
Amplitude Response
0
0.05
0.45
0.5
0.05
0.45
0.5
Deltaw = 0.02*pi;
% Initial guess
ws1=wp1-Deltaw; ws2=wp2+Deltaw;
F=[0, ws1, wp1, wp2, ws2, pi]/pi;
m=[0,0,1,1,0,0];
h=remez(50,F,m);
[db,mag,pha,grd,w]=freqz_m(h,1);
delta_w = pi/500;
Asd = floor(-max(db([1:floor(ws1/delta_w)]))), % Actual Attn
Asd =
13
% Next iteration
Deltaw = Deltaw+0.01*pi;
ws1=wp1-Deltaw; ws2=wp2+Deltaw;
F=[0, ws1, wp1, wp2, ws2, pi]/pi;
h=remez(50,F,m);
[db,mag,pha,grd,w]=freqz_m(h,1);
delta_w = pi/500;
Asd = floor(-max(db([1:floor(ws1/delta_w)]))), % Actual Attn
Asd =
20
% Next iteration
Deltaw = Deltaw+0.01*pi;
ws1=wp1-Deltaw; ws2=wp2+Deltaw;
F=[0, ws1, wp1, wp2, ws2, pi]/pi;
h=remez(50,F,m);
[db,mag,pha,grd,w]=freqz_m(h,1);
108
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
InputOutput Sequences
41
81
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
109
DECIBELS
30
0.44
0.56
frequency in pi units
110
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
110
120
130
140
150
160
170
180
190
200
170
180
190
200
110
120
130
140
150
160
Chapter 8
112
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
% Plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P8.1);
%
subplot(2,2,1);plot(w,mag); axis([0,Wmax,0,1.1]);
xlabel(Analog frequency in rad/sec,fontsize,10);
ylabel(Magnitude,fontsize,10); title (Magnitude Response,fontsize,10);
set(gca,XTickMode,manual,Xtick,[0;Wp;Ws;Wmax],fontsize,10);
magRp = round(10(-Rp/20)*100)/100;
set(gca,YTickMode,manual,Ytick,[0;magRp;1],fontsize,10);grid
%
subplot(2,2,2);plot(w,db); axis([0,Wmax,-100,0]);
xlabel(Analog frequency in rad/sec,fontsize,10);
ylabel(Decibels,fontsize,10); title (Log-Magnitude Response,fontsize,10);
set(gca,XTickMode,manual,Xtick,[0;Wp;Ws;Wmax],fontsize,10);
set(gca,YTickMode,manual,Ytick,[-100;-As;0],fontsize,10);grid
AS = [ ,num2str(As)];
set(gca,YTickLabelMode,manual,YTickLabels,[100;AS; 0]);
%
minpha = floor(min(pha/pi)); maxpha = ceil(max(pha/pi));
subplot(2,2,3);plot(w,pha/pi); axis([0,Wmax,minpha,maxpha]);
xlabel(Analog frequency in rad/sec,fontsize,10);
ylabel(Phase in pi units,fontsize,10); title (Phase Response,fontsize,10);
set(gca,XTickMode,manual,Xtick,[0;Wp;Ws;Wmax],fontsize,10);
phaWp = (round(pha(Wp/Wmax*500+1)/pi*100))/100;
phaWs = (round(pha(Ws/Wmax*500+1)/pi*100))/100;
set(gca,YTickMode,manual,Ytick,[phaWs;phaWp;0],fontsize,10); grid
%
subplot(2,2,4); plot(t,ha); title (Impulse Response,fontsize,10);
xlabel(t (sec), fontsize,10); ylabel(ha(t),fontsize,10);
%
suptitle(Analog Butterworth Lowpass Filter Design Plots in P 8.1)
The system function is given by
Ha (s) = 2:8199 1022
1
s2 + 61:393s
+ 984:84
1
s + 31:382
1
1
2 + 57:338s + 984:84
2 + 50:777s + 984:84
s
s
1
1
2 + 41:997s + 984:84
2 + 31:382s + 984:84
s
s
1
1
s2 + 19:395s + 984:84
s2 + 6:5606s + 984:84
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
113
LogMagnitude Response
0
1
0.89
Decibels
Magnitude
30
30 40
Analog frequency in rad/sec
100
100
30 40
Analog frequency in rad/sec
Phase Response
Impulse Response
0.1
0.05
ha(t)
Phase in pi units
100
3.47
4.89
30 40
Analog frequency in rad/sec
100
0.05
0.5
1
t (sec)
1.5
114
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
4
2
:46978s + 220:07s + 2298:5
5
4
3
s + 9:23s + 184:71s + 1129:2s2 + 7881:3s + 22985
LogMagnitude Response
0
Magnitude
logMagnitude in dB
1
0.89
10
15
Analog frequency in rad/sec
30
40
100
10
15
Analog frequency in rad/sec
Phase Response
Impulse Response
3
2
1.26
ha(t)
Phase in pi units
30
1.64
1
0
1
10
15
Analog frequency in rad/sec
30
t (sec)
10
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
115
The filter passband must include the 100 Hz component while the stopband must include the 130 Hz component. To
obtain a minimum-order filter, the transition band must be as large as possible. This means that the passaband cutoff must
be at 100 Hz while the stopband cutoff must be at 130 Hz. Hence the analog Chebyshev-I lowpass filter specifications
are: p = 2 (100) rad/s, R p = 2 dB, s = 2 (130) rad/s, As = 50 dB.
M ATLAB Script:
116
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
1
2
s + 142:
45s
+ 51926
s2 + 75:794s + 301830
1
2
s + 116:12s
+ 168860
1
1
2
s + 26:323s + 388620
s + 75:794
Magnitude
0.79
100
130
Analog frequency in Hz
200
LogMagnitude Response
Decibels
50
100
100
130
Analog frequency in Hz
200
5.9633e+013
1.9564e+034
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
117
2.3171e+040
a =
Columns 1 through 6
1.0000e+000 1.5853e+004 1.2566e+008 6.5800e+011 2.5371e+015 7.5982e+018
Columns 7 through 12
1.8208e+022 3.5290e+025 5.5639e+028 6.9823e+031 6.9204e+034 4.7962e+037
Column 13
2.3171e+040
% Frequency Response
Fmax = 500; Wmax = 2*pi*Fmax; [db,mag,pha,w] = freqs_m(b,a,Wmax); pha = unwrap(pha);
% Impulse Response
%
The impulse response of the designed filter when computed by Matlab is numerically
%
unstable due to large coefficient values. Hence we will compute the impulse respon
%
of the filter with Wp/1000 and Ws/1000 band edges to keep coefficient values small
%
The actual impulse response is time-scaled and amplitude scaled version of the
%
computed impulse response.
[b,a] = afd_chb2(Wp/1000,Ws/1000,Rp,As); [ha,x,t] = impulse(b,a);
*** Chebyshev-2 Filter Order = 12
t = t/1000; ha = ha/1000;
%
% Plots
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P8.4);
%
subplot(2,2,1);plot(w/(2*pi),mag); axis([0,Fmax,0,1]); set(gca,fontsize,10);
xlabel(Analog frequency in Hz); ylabel(Magnitude);
title (Magnitude Response,fontsize,12);
set(gca,XTickMode,manual,Xtick,[0;Fp;Fs;Fmax]);
magRp = round(10(-Rp/20)*100)/100;
set(gca,YTickMode,manual,Ytick,[0;magRp;1]);grid
%
subplot(2,2,2);plot(w/(2*pi),db); axis([0,Fmax,-100,0]); set(gca,fontsize,10);
xlabel(Analog frequency in Hz); ylabel(log-Magnitude in dB);
title (Log-Magnitude Response,fontsize,12);
set(gca,XTickMode,manual,Xtick,[0;Fp;Fs;Fmax]);
set(gca,YTickMode,manual,Ytick,[-100;-As;0]);grid
AS = [ ,num2str(As)];
set(gca,YTickLabelMode,manual,YTickLabels,[100;AS; 0]);
%
minpha = floor(min(pha/pi)); maxpha = ceil(max(pha/pi));
subplot(2,2,3);plot(w/(2*pi),pha/pi); axis([0,Fmax,minpha,maxpha]); set(gca,fontsize
xlabel(Analog frequency in Hz); ylabel(Phase in pi units);
title (Phase Response,fontsize,12);
set(gca,XTickMode,manual,Xtick,[0;Fp;Fs;Fmax]);
phaWp = (round(pha(Wp/Wmax*500+1)/pi*100))/100;
phaWs = (round(pha(Ws/Wmax*500+1)/pi*100))/100;
set(gca,YTickMode,manual,Ytick,[phaWs;phaWp;0]); grid
%
subplot(2,2,4); plot(t,ha); set(gca,fontsize,10);
title (Impulse Response,fontsize,12); xlabel(t (sec)); ylabel(ha(t));
%
suptitle(Analog Chebyshev-II Lowpass Filter Design Plots in P 8.4)
118
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Decibel
50
0
0.4
0.6
Frequency in Hz
Impulse Response
ha(t)
0.1
0.1
10
20
30
40
50
60
time in seconds
70
80
90
100
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
Digital Chebyshev-1 Lowpass Filter Design using Impulse Invariance. M ATLAB script:
clear; close all; Twopi = 2*pi;
%% Analog Filter Specifications
Fsam = 8000;
Fp = 1500;
Rp = 3;
Fs = 2000;
As = 40;
%
%% Digital Filter Specifications
wp = Twopi*Fp/Fsam;
Rp = 3;
ws = Twopi*Fs/Fsam;
As = 40;
(a) Part (a): T
= 1.
%
%
%
%
%
Sampling
Passband
Passband
Stopband
Stopband
Rate in sam/sec
edge in Hz
Ripple in dB
edge in Hz
attenuation in dB
%
%
%
%
Passband
Passband
Stopband
Stopband
edge in rad/sam
Ripple in dB
edge in rad/sam
attenuation in dB
M ATLAB script:
7
% II Transformation
% Parallel form
= 1=8000.
M ATLAB script:
7
% II Transformation
% Parallel form
119
120
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Decibel
40
60
1500
2000
Frequency in Hz
4000
Impulse Response
0.3
h(n)
0.2
0.1
0
0.1
0.2
10
20
30
40
50
n
60
70
0.4460
-1.2232
1.8869
0
1.0000
1.0000
1.0000
1.0000
-0.7767
-1.0919
-1.5217
-0.8616
80
=1
90
100
in Problem P 8.6a
A =
0.9358
0.8304
0.7645
0
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
121
Decibel
40
60
0
1500
2000
Frequency in Hz
4000
Impulse Response
3000
h(n)
2000
1000
0
1000
2000
10
20
30
40
50
n
60
70
80
= 1=8000 in
90
100
Problem P 8.6b
122
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Decibel
50
0
0.4
0.6
Frequency in Hz
Impulse Response
ha(t)
0.1
0.1
10
20
30
40
50
60
time in seconds
70
80
=2
90
100
in Problem P 8.7
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
123
edge in rad/sam
Ripple in dB
edge in rad/sam
attenuation in dB
= 2
124
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
set(gca,YTickLabelMode,manual,YtickLabels,[0.0;0.5]);
xlabel(Frequency in pi units); ylabel(|H|);
title(Magnitude Response,fontsize,12); axis;
suptitle(Digital Butterworth Filter Design Plots in P 8.8)
The filter design plots are given in Figure 8.8.
Digital Butterworth Filter Design Plots in P 8.8
LogMagnitude Response
Decibel
50
0
0.4
0.6
Frequency in pi units
Magnitude Response
|H|
0.5
0.0
0.4
0.6
Frequency in pi units
= 2.
M ATLAB script:
T = 2;
OmegaP = (2/T)*tan(wp/2);
% Analog Prototype Passband edge
OmegaS = (2/T)*tan(ws/2);
% Analog Prototype Stopband edge
[cs,ds] = afd_butt(OmegaP,OmegaS,Rp,As); % Analog Prototype Design
*** Butterworth Filter Order = 11
[b,a] = bilinear(cs,ds,1/T)
% Bilinear Transformation
b =
Columns 1 through 7
0.0004
0.0048
0.0238
0.0715
0.1429
0.2001
0.2001
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
125
Columns 8 through 12
0.1429
0.0715
0.0238
0.0048
0.0004
a =
Columns 1 through 7
1.0000
-1.5495
2.5107
-2.1798
1.7043
-0.8997
0.4005
Columns 8 through 12
-0.1258
0.0309
-0.0050
0.0005
0.0000
%
% Plots of Log-magnitude Response and Impulse Response
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P8.11a);
% Frequency response
[db,mag,pha,grd,w] = freqz_m(b,a);
subplot(2,1,1); plot(w/pi,db); axis([0,1,-60,0]); set(gca,fontsize,10);
set(gca,XTickMode,manual,Xtick,[0;wp/pi;ws/pi;1]);
set(gca,YTickMode,manual,Ytick,[-80;-As;0]);grid
AS = [num2str(As)];
set(gca,YTickLabelMode,manual,YtickLabels,[80;AS; 0]);
xlabel(Frequency in Hz); ylabel(Decibel);
title(Log-Magnitude Response,fontsize,12); axis;
% Impulse response of the prototype analog filter
Nmax = 50; t = 0:T/10:T*Nmax; [ha,x,t] = impulse(cs,ds,t);
subplot(2,1,2); plot(t,ha); axis([0,T*Nmax,-0.3,0.4]); set(gca,fontsize,10);
xlabel(time in seconds,fontsize,10); ylabel(ha(t),fontsize,10);
title(Impulse Response,fontsize,12); hold on;
% Impulse response of the digital filter
[x,n] = impseq(0,0,Nmax); h = filter(b,a,x);
stem(n*T,h); hold off;
suptitle(Digital Butterworth Filter Design Plots in P 8.11a);
The filter design plots are shown in Figure 8.9.
Comparison: If we compare filter orders from two methods then bilinear transformation gives the lower order than
the impulse invariance method. This implies that the bilinear transformation design method is a better one in all
aspects. If we compare the impulse responses then we observe from Figure 8.9 that the digital impulse response is
not a sampled version of the analog impulse response as was the case in Figure 8.7.
(b) Part (b): Use of the butter function. M ATLAB script:
[N,wn] = buttord(wp/pi,ws/pi,Rp,As);
[b,a] = butter(N,wn)
b =
Columns 1 through 7
0.0005
0.0054
0.0270
0.0810
0.1619
0.2267
0.2267
Columns 8 through 12
0.1619
0.0810
0.0270
0.0054
0.0005
a =
Columns 1 through 7
1.0000
-1.4131
2.3371
-1.9279
1.5223
-0.7770
0.3477
Columns 8 through 12
-0.1066
0.0262
-0.0042
0.0004
0.0000
%
% Plots of Log-magnitude Response and Impulse Response
Hf_2 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_2,NumberTitle,off,Name,P8.11b);
% Frequency response
[db,mag,pha,grd,w] = freqz_m(b,a);
126
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Decibel
50
0
0.4
0.6
Frequency in Hz
Impulse Response
ha(t)
0.2
0.2
0
10
20
30
40
50
60
time in seconds
70
80
=2
90
100
in Problem P 8.11a
%
%
%
%
%
Sampling
Passband
Passband
Stopband
Stopband
Rate in sam/sec
edge in Hz
Ripple in dB
edge in Hz
attenuation in dB
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
127
Decibel
50
0
0.4
0.6
Frequency in Hz
Impulse Response
h(n)
0.2
0.2
0
10
15
20
25
n
30
35
40
45
50
Figure 8.10: Butterworth filter design using the butter function in Problem P 8.11b
%%
wp
Rp
ws
As
(a) Part(a): T
= 1.
%
%
%
%
Passband
Passband
Stopband
Stopband
edge in rad/sam
Ripple in dB
edge in rad/sam
attenuation in dB
M ATLAB script:
= 1=8000.
M ATLAB script:
% Bilinear Transformation
% Cascade form
128
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Decibel
40
60
1500
2000
Frequency in Hz
4000
Impulse Response
0.3
h(n)
0.2
0.1
0
0.1
0.2
10
20
30
40
50
n
60
70
80
=1
90
100
in Problem P 8.13a
6
% II Transformation
% Cascade form
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
129
Decibel
40
60
1500
2000
Frequency in Hz
4000
Impulse Response
0.3
h(n)
0.2
0.1
0
0.1
0.2
10
20
30
40
50
n
60
70
80
90
= 1=8000 in
100
Problem P 8.13b
130
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Decibel
60
80
0.4
0.6
Frequency in Hz
Impulse Response
ha(t)
0.2
0.2
0
10
20
30
40
50
60
time in seconds
70
80
90
100
Figure 8.13: Digital elliptic lowpass filter design using the bilinear function in Problem P8.14a.
M ATLAB script using the ellip function:
%% (b) Use of the Ellip function
[N,wn] = ellipord(wp/pi,ws/pi,Rp,As);
[b,a] = ellip(N,Rp,As,wn);
%
% Plots of Log-magnitude Response and Impulse Response
Hf_2 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_2,NumberTitle,off,Name,P8.14b);
% Frequency response
[db,mag,pha,grd,w] = freqz_m(b,a);
subplot(2,1,1); plot(w/pi,db); axis([0,1,-80,0]); set(gca,fontsize,10);
set(gca,XTickMode,manual,Xtick,[0;wp/pi;ws/pi;1]);
set(gca,YTickMode,manual,Ytick,[-80;-As;0]);grid
AS = [num2str(As)];
set(gca,YTickLabelMode,manual,YtickLabels,[80;AS; 0]);
xlabel(Frequency in Hz); ylabel(Decibel);
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
131
Decibel
60
80
0.4
0.6
Frequency in Hz
Impulse Response
h(n)
0.2
0.2
0
10
15
20
25
n
30
35
40
45
50
Figure 8.14: Digital elliptic lowpass filter design using the ellip function in Problem P8.14b.
12. Digital elliptic highpass filter design using bilinear mapping.
M ATLAB function dhpfd bl.m:
function [b,a] = dhpfd_bl(type,wp,ws,Rp,As)
%
% function [b,a] = dhpfd_bl(type,wp,ws,Rp,As,T)
%
Designs digital highpass filters using bilinear
%
b = Numerator polynomial of the highpass filter
%
a = Denominator polynomial of the highpass filter
% type = butter or cheby1 or cheby2 or ellip
%
wp = passband cutoff in radians
%
ws = stopband cutoff in radians (ws < wp)
%
Rp = passband ripple in dB
%
As = stopband attenuation in dB
% Determine the digital lowpass cutoff frequecies:
wplp = 0.2*pi;
132
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
alpha = -(cos((wplp+wp)/2))/(cos((wplp-wp)/2));
wslp = angle(-(exp(-j*ws)+alpha)/(1+alpha*exp(-j*ws)));
%
% Compute Analog lowpass Prototype Specifications:
T = 1; Fs = 1/T;
OmegaP = (2/T)*tan(wplp/2);
OmegaS = (2/T)*tan(wslp/2);
% Design Analog Chebyshev Prototype Lowpass Filter:
type = lower([type, ]); type = type(1:6);
if type == butter
[cs,ds] = afd_butt(OmegaP,OmegaS,Rp,As);
elseif type == cheby1
[cs,ds] = afd_chb1(OmegaP,OmegaS,Rp,As);
elseif type == cheby2
[cs,ds] = afd_chb2(OmegaP,OmegaS,Rp,As);
elseif type == ellip
[cs,ds] = afd_elip(OmegaP,OmegaS,Rp,As);
else
error(Specify the correct type)
end
% Perform Bilinear transformation to obtain digital lowpass
[blp,alp] = bilinear(cs,ds,Fs);
% Transform digital lowpass into highpass filter
Nz = -[alpha,1]; Dz = [1,alpha];
[b,a] = zmapping(blp,alp,Nz,Dz);
(a) Design using the dhpfd bl function:
clear; close all;
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P8.17)
%% Digital Filter Specifications
type = ellip;
% Elliptic
ws = 0.4*pi;
% Passband
As = 60;
% Stopband
wp = 0.6*pi;
% Stopband
Rp = 1;
% Passband
%
%% (a) Use of the dhpfd_bl function
[b,a] = dhpfd_bl(type,wp,ws,Rp,As)
design
edge in rad/sam
attenuation in dB
edge in rad/sam
Ripple in dB
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
133
set(gca,YTickMode,manual,Ytick,[-80;-As;0]);grid
AS = [num2str(As)];
set(gca,YTickLabelMode,manual,YtickLabels,[80;AS; 0]);
xlabel(Frequency in Hz); ylabel(Decibel);
title(Design using the dhpfd_bl function,fontsize,12); axis;
The filter frequency response plot is shown in the top row of Figure 8.15.
(b) Design using the ellip function:
%% (b) Use of the Ellip function
[N,wn] = ellipord(wp/pi,ws/pi,Rp,As);
[b,a] = ellip(N,Rp,As,wn,high)
b =
0.0208
-0.0543
0.0862
-0.0862
0.0543
-0.0208
a =
1.0000
2.1266
2.9241
2.3756
1.2130
0.3123
%
% Plot of Log-magnitude Response
% Frequency response
[db,mag,pha,grd,w] = freqz_m(b,a);
subplot(2,1,2); plot(w/pi,db); axis([0,1,-80,0]); set(gca,fontsize,10);
set(gca,XTickMode,manual,Xtick,[0;ws/pi;wp/pi;1]);
set(gca,YTickMode,manual,Ytick,[-80;-As;0]);grid
AS = [num2str(As)];
set(gca,YTickLabelMode,manual,YtickLabels,[80;AS; 0]);
xlabel(Frequency in Hz); ylabel(Decibel);
title(Design using the ellip function,fontsize,12); axis;
suptitle(Digital Elliptic Filter Design Plots in P 8.17);
The filter frequency response plot is shown in the bottom row of Figure 8.15. Both M ATLAB scripts and the Figure
8.15 indicate that we designed essentially the same filter.
13. Digital Chebyshev-2 bandpass filter design using bilinear transformation. M ATLAB script:
clear; close all;
Hf_1 = figure(Units,normalized,position,[0.1,0.1,0.8,0.8],color,[0,0,0]);
set(Hf_1,NumberTitle,off,Name,P8.19)
%% Digital Filter Specifications
ws1 = 0.3*pi;
% Lower passband edge in rad/sam
ws2 = 0.6*pi;
% upper passband edge in rad/sam
As = 50;
% Stopband attenuation in dB
wp1 = 0.4*pi;
% Lower passband edge in rad/sam
wp2 = 0.5 *pi;
% Lower passband edge in rad/sam
Rp = 0.5;
% Passband Ripple in dB
%
%% Use of the cheby2 function
ws = [ws1,ws2]/pi; wp = [wp1,wp2]/pi;
[N,wn] = cheb2ord(wp,ws,Rp,As)
N =
5
wn =
0.3390
0.5661
[b,a] = cheby2(N,As,wn)
b =
Columns 1 through 7
134
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
A PRIL 98
Decibel
60
80
0.4
0.6
Frequency in Hz
Decibel
60
80
0.4
0.6
Frequency in Hz
Figure 8.15: Digital elliptic highpass filter design plots in Problem 8.17.
0.0050
-0.0050
0.0087
-0.0061
0.0060
0.0000
-0.0060
Columns 8 through 11
0.0061
-0.0087
0.0050
-0.0050
a =
Columns 1 through 7
1.0000
-1.3820
4.4930
-4.3737
7.4582
-5.1221
5.7817
Columns 8 through 11
-2.6221
2.0882
-0.4936
0.2764
%
% Plot of the filter Responses
% Impulse response
Nmax = 50; [x,n] = impseq(0,0,Nmax); h = filter(b,a,x);
subplot(2,1,1); stem(n,h); axis([0,Nmax,-0.15,0.15]); set(gca,fontsize,10);
xlabel(n,fontsize,10); ylabel(h(n),fontsize,10);
title(Impulse Response,fontsize,12);
% Frequency response
[db,mag,pha,grd,w] = freqz_m(b,a);
subplot(2,1,2); plot(w/pi,db); axis([0,1,-70,0]); set(gca,fontsize,10);
set(gca,XTickMode,manual,Xtick,[0,ws,wp,1]);
set(gca,YTickMode,manual,Ytick,[-70;-As;0]);grid
AS = [num2str(As)];
set(gca,YTickLabelMode,manual,YtickLabels,[70;AS; 0]);
xlabel(Frequency in pi units); ylabel(Decibel);
title(Log-Magnitude Response,fontsize,12); axis;
suptitle(Digital Chebyshev-2 Bandpass Filter Design Plots in P 8.19);
The filter impulse and log-magnitude response plots are shown in Figure 8.16.
A PRIL 98
S OLUTIONS M ANUAL
FOR
DSP
USING
M ATLAB
135
h(n)
0.05
0
0.05
0.1
0
10
15
20
25
n
30
35
40
45
50
LogMagnitude Response
Decibel
50
70
0.3
0.4
0.5
0.6
Frequency in pi units
Figure 8.16: Digital Chebyshev-2 bandpass filter design plots in Problem P8.19.