SlideShare a Scribd company logo
MATLAB Programs
Chapter 16
16.1   INTRODUCTION
MATLAB stands for MATrix LABoratory. It is a technical computing environment
for high performance numeric computation and visualisation. It integrates numerical
analysis, matrix computation, signal processing and graphics in an easy-to-use
environment, where problems and solutions are expressed just as they are written
mathematically, without traditional programming. MATLAB allows us to express
the entire algorithm in a few dozen lines, to compute the solution with great accuracy
in a few minutes on a computer, and to readily manipulate a three-dimensional
display of the result in colour.
MATLAB is an interactive system whose basic data element is a matrix that
does not require dimensioning. It enables us to solve many numerical problems in a
fraction of the time that it would take to write a program and execute in a language
such as FORTRAN, BASIC, or C. It also features a family of application specific
solutions, called toolboxes. Areas in which toolboxes are available include signal
processing, image processing, control systems design, dynamic systems simulation,
systems identification, neural networks, wavelength communication and others.
It can handle linear, non-linear, continuous-time, discrete-time, multivariable and
multirate systems. This chapter gives simple programs to solve specific problems
that are included in the previous chapters. All these MATLAB programs have been
tested under version 7.1 of MATLAB and version 6.12 of the signal processing
toolbox.
16.2   REPRESENTATION OF BASIC SIGNALS
MATLAB programs for the generation of unit impulse, unit step, ramp, exponential,
sinusoidal and cosine sequences are as follows.
% Program for the generation of unit impulse signal
clc;clear all;close all;
t522:1:2;
y5[zeros(1,2),ones(1,1),zeros(1,2)];subplot(2,2,1);stem(t,y);
816   Digital Signal Processing
ylabel(‘Amplitude --.’);
xlabel(‘(a) n --.’);
% Program for the generation of unit step sequence [u(n)2 u(n 2 N]
n5input(‘enter the N value’);
t50:1:n21;
y15ones(1,n);subplot(2,2,2);
stem(t,y1);ylabel(‘Amplitude --.’);
xlabel(‘(b) n --.’);
% Program for the generation of ramp sequence
n15input(‘enter the length of ramp sequence’);
t50:n1;
subplot(2,2,3);stem(t,t);ylabel(‘Amplitude --.’);
xlabel(‘(c) n --.’);
% Program for the generation of exponential sequence
n25input(‘enter the length of exponential sequence’);
t50:n2;
a5input(‘Enter the ‘a’ value’);
y25exp(a*t);subplot(2,2,4);
stem(t,y2);ylabel(‘Amplitude --.’);
xlabel(‘(d) n --.’);
% Program for the generation of sine sequence
t50:.01:pi;
y5sin(2*pi*t);figure(2);
subplot(2,1,1);plot(t,y);ylabel(‘Amplitude --.’);
xlabel(‘(a) n --.’);
% Program for the generation of cosine sequence
t50:.01:pi;
y5cos(2*pi*t);
subplot(2,1,2);plot(t,y);ylabel(‘Amplitude --.’);
xlabel(‘(b) n --.’);
As an example,
enter the N value 7
enter the length of ramp sequence 7
enter the length of exponential sequence 7
enter the a value 1
Using the above MATLAB programs, we can obtain the waveforms of the unit
impulse signal, unit step signal, ramp signal, exponential signal, sine wave signal and
cosine wave signal as shown in Fig. 16.1.
MATLAB Programs  817
Fig. 16.1  Representation of Basic Signals (a) Unit Impulse Signal (b) Unit-step
Signal (c) Ramp Signal (d) Exponential Signal (e) Sinewave Signal ( f )Cosine Wave Signal
−2 −1 0 1 2 0
0
2
2
4
4
6
6 8
nn
nn
0.2
0.2
1
0.2
0.4
0.4
2
0.4
0.6
0.6
3
4
5
6
7
0.6
0.8
0.8
0.8
1
1
1
0
00
0 2 4 6 8
0
(a)
Amplitude
Amplitude
AmplitudeAmplitude
(b)
(d)(c)
1
1
0.5
0.5
0.5
0.5
0
0
1
1
1.5
(e)
(f)
1.5
2.5
2.5
3
3
3.5
3.5
2
2
0
0
−0.5
−0.5
−1
−1
n
n
AmplitudeAmplitude
818   Digital Signal Processing
16.3   DISCRETE CONVOLUTION
16.3.1  Linear Convolution
Algorithm
1.	 Get two signals x(m)and h(p)in matrix form
2.	 The convolved signal is denoted as y(n)
3.	 y(n)is given by the formula
	 y(n) 5 [ ( ) ( )]x k h n k
k
−
=−∞
∞
∑   where n50 to m 1 p 2 1
4.	 Stop
% Program for linear convolution of the sequence x5[1, 2] and h5[1, 2, 4]
clc;
clear all;
close all;
x5input(‘enter the 1st sequence’);
h5input(‘enter the 2nd sequence’);
y5conv(x,h);
figure;subplot(3,1,1);
stem(x);ylabel(‘Amplitude --.’);
xlabel(‘(a) n --.’);
subplot(3,1,2);
stem(h);ylabel(‘Amplitude --.’);
xlabel(‘(b) n --.’);
subplot(3,1,3);
stem(y);ylabel(‘Amplitude --.’);
xlabel(‘(c) n --.’);
disp(‘The resultant signal is’);y
As an example,
enter the 1st sequence [1 2]
enter the 2nd sequence [1 2 4]
The resultant signal is
	 y51 4 8 8
Figure 16.2 shows the discrete input signals x(n)and h(n)and the convolved output
signal y(n).
Fig. 16.2  (Contd.)
AmplitudeAmplitude
n
n
0
0
0.5
1
1
1
1.1
1.2
1.2
1.4
1.3
1.6
1.4
1.8
1.5
2
(a)
(b)
1.6
2.2
1.7
2.4
1.8
2.6
1.9
2.8
2
3
1
2
1.5
3
2
4
MATLAB Programs  819
16.3.2  Circular Convolution
% Program for Computing Circular Convolution
clc;
clear;
	 a = input(‘enter the sequence x(n) = ’);
	 b = input(‘enter the sequence h(n) = ’);
n1=length(a);
n2=length(b);
N=max(n1,n2);
x = [a zeros(1,(N-n1))];
for i = 1:N
	 k = i;
	 for j = 1:n2
		 H(i,j)=x(k)* b(j);
		 k = k-1;
		 if (k == 0)
			 k = N;
		 end
	 end
end
y=zeros(1,N);
M=H’;
for j = 1:N
	 for i = 1:n2
		 y(j)=M(i,j)+y(j);
	 end
end
disp(‘The output sequence is y(n)= ‘);
disp(y);
Fig. 16.2  Discrete Linear Convolution
AmplitudeAmplitudeAmplitude
n
n
n
0
0
0
0.5
1
2
1
1
1
1.1
1.2
1.2
1.4
1.5
1.3
1.6
1.4
1.8
1.5
2
2
(a)
(b)
(c)
1.6
2.2
2.5
1.7
2.4
3
1.8
2.6
3.5
1.9
2.8
2
3
4
1
2
4
1.5
3
6
2
4
8
AmplitudeAmplitudeAmplitude
n
n
n
0
0
0
0.5
1
2
1
1
1
1.1
1.2
1.2
1.4
1.5
1.3
1.6
1.4
1.8
1.5
2
2
(a)
(b)
(c)
1.6
2.2
2.5
1.7
2.4
3
1.8
2.6
3.5
1.9
2.8
2
3
4
1
2
4
1.5
3
6
2
4
8
820   Digital Signal Processing
stem(y);
title(‘Circular Convolution’);
xlabel(‘n’);
ylabel(‚y(n)‘);
As an Example,
enter the sequence x(n) = [1 2 4]
enter the sequence h(n) = [1 2]
The output sequence is y(n)= 9 4 8
% Program for Computing Circular Convolution with zero padding
clc;
close all;
clear all;
g5input(‘enter the first sequence’);
h5input(‘enter the 2nd sequence’);
N15length(g);
N25length(h);
N5max(N1,N2);
N35N12N2;
%Loop for getting equal length sequence
if(N350)
	 h5[h,zeros(1,N3)];
else
	 g5[g,zeros(1,2N3)];
end
%computation of circular convolved sequence
for n51:N,
	 y(n)50;
	 for i51:N,
		 j5n2i11;
		 if(j550)
			 j5N1j;
		 end
		 y(n)5y(n)1g(i)*h(j);
	 end
end
disp(‘The resultant signal is’);y
As an example,
enter the first sequence [1 2 4]
enter the 2nd sequence [1 2]
The resultant signal is y51 4 8 8
16.3.3  Overlap Save Method and Overlap Add method
% Program for computing Block Convolution using Overlap Save
Method
Overlap Save Method
x=input(‘Enter the sequence x(n) = ’);
MATLAB Programs  821
h=input(‘Enter the sequence h(n) = ’);
n1=length(x);
n2=length(h);
N=n1+n2-1;
h1=[h zeros(1,N-n1)];
n3=length(h1);
y=zeros(1,N);
x1=[zeros(1,n3-n2) x zeros(1,n3)];
H=fft(h1);
for i=1:n2:N
	 y1=x1(i:i+(2*(n3-n2)));
	 y2=fft(y1);
	 y3=y2.*H;
	 y4=round(ifft(y3));
	 y(i:(i+n3-n2))=y4(n2:n3);
end
disp(‘The output sequence y(n)=’);
disp(y(1:N));
stem(y(1:N));
title(‘Overlap Save Method’);
xlabel(‘n’);
ylabel(‘y(n)’);
Enter the sequence x(n) = [1 2 -1 2 3 -2 -3 -1 1 1 2 -1]
Enter the sequence h(n) = [1 2 3 -1]
The output sequence y(n) = 1 4 6 5 2 11 0 -16 -8 3 8 5 3 -5 1
%Program for computing Block Convolution using Overlap Add
Method
x=input(‘Enter the sequence x(n) = ’);
h=input(‘Enter the sequence h(n) = ’);
n1=length(x);
n2=length(h);
N=n1+n2-1;
y=zeros(1,N);
h1=[h zeros(1,n2-1)];
n3=length(h1);
y=zeros(1,N+n3-n2);
H=fft(h1);
for i=1:n2:n1
	 if i<=(n1+n2-1)
		 x1=[x(i:i+n3-n2) zeros(1,n3-n2)];
	 else
		 x1=[x(i:n1) zeros(1,n3-n2)];
	 end
	 x2=fft(x1);
	 x3=x2.*H;
	 x4=round(ifft(x3));
	 if (i==1)
822   Digital Signal Processing
		 y(1:n3)=x4(1:n3);
	 else
		 y(i:i+n3-1)=y(i:i+n3-1)+x4(1:n3);
	 end
end
disp(‘The output sequence y(n)=’);
disp(y(1:N));
stem((y(1:N));
title(‘Overlap Add Method’);
xlabel(‘n’);
ylabel(‘y(n)’);
As an Example,
Enter the sequence x(n) = [1 2 -1 2 3 -2 -3 -1 1 1 2 -1]
Enter the sequence h(n) = [1 2 3 -1]
The output sequence
	 y(n) = 1 4 6 5 2 11 0 -16 -8 3 8 5 3 -5 1
16.4   DISCRETE CORRELATION
16.4.1  Crosscorrelation
Algorithm
1.	 Get two signals x(m)and h(p)in matrix form
2.	 The correlated signal is denoted as y(n)
3.	 y(n)is given by the formula
y(n) 5 [ ( ) ( )]x k h k n
k
−
=−∞
∞
∑
where n52 [max (m, p)2 1] to [max (m, p)2 1]
4.	 Stop
% Program for computing cross-correlation of the sequences
x5[1, 2, 3, 4] and h5[4, 3, 2, 1]
clc;
clear all;
close all;
x5input(‘enter the 1st sequence’);
h5input(‘enter the 2nd sequence’);
y5xcorr(x,h);
figure;subplot(3,1,1);
stem(x);ylabel(‘Amplitude --.’);
xlabel(‘(a) n --.’);
subplot(3,1,2);
stem(h);ylabel(‘Amplitude --.’);
xlabel(‘(b) n --.’);
subplot(3,1,3);
stem(fliplr(y));ylabel(‘Amplitude --.’);
MATLAB Programs  823
xlabel(‘(c) n --.’);
disp(‘The resultant signal is’);fliplr(y)
As an example,
enter the 1st sequence [1 2 3 4]
enter the 2nd sequence [4 3 2 1]
The resultant signal is
	 y51.0000 4.0000 10.0000 20.
↑
0000 25.0000 24.0000 16.0000
Figure 16.3 shows the discrete input signals x(n)and h(n)and the cross-correlated
output signal y(n).
AmplitudeAmplitudeAmplitude
n
n
n
0
0
1
1
1
1
1
1.5
1.5
2
2
2
3
3
3
5
2.5
2.5
4
(a)
(b)
(c)
3.5
3.5
6
4
4
7
2
2
3
3
4
4
30
20
10
0
Fig. 16.3  Discrete Cross-correlation
16.4.2  Autocorrelation
Algorithm
1.	 Get the signal x(n)of length N in matrix form
2.	 The correlated signal is denoted as y(n)
3.	 y(n)is given by the formula
	 y(n) 5 [ ( ) ( )]x k x k n
k
−
=−∞
∞
∑
where n52(N 2 1) to (N 2 1)
824   Digital Signal Processing
% Program for computing autocorrelation function
x5input(‘enter the sequence’);
y5xcorr(x,x);
figure;subplot(2,1,1);
stem(x);ylabel(‘Amplitude --.’);
xlabel(‘(a) n --.’);
subplot(2,1,2);
stem(fliplr(y));ylabel(‘Amplitude --.’);
xlabel(‘(a) n --.’);
disp(‘The resultant signal is’);fliplr(y)
As an example,
enter the sequence [1 2 3 4]
The resultant signal is
	 y54 11 20
↑
30 20 11 4
Figure 16.4 shows the discrete input signal x(n)and its auto-correlated output
signal y(n).
( )
AmplitudeAmplitude
n
n
0
1
1
1
1.5
2
2
3
3
(a)
5
2.5
4
(b) y (n)
3.5
6
4
7
2
3
4
0
5
10
15
20
25
30
Fig. 16.4  Discrete Auto-correlation
16.5   STABILITY TEST
% Program for stability test
clc;clear all;close all;
b5input(‘enter the denominator coefficients of the
­filter’);
k5poly2rc(b);
knew5fliplr(k);
s5all(abs(knew)1);
if(s55 1)
	 disp(‘“Stable system”’);
MATLAB Programs  825
else
	 disp(‘“Non-stable system”’);
end
As an example,
enter the denominator coefficients of the filter [1 21 .5]
“Stable system”
16.6   SAMPLING THEOREM
The sampling theorem can be understood well with the following example.
Example 16.1  Frequency analysis of the amplitude modulated discrete-time
signal
	 x(n)5cos 2 pf1
n 1 cos 2pf2
n
where f1
1
128
=  and f2
5
128
=  modulates the amplitude-modulated signal is
	 xc
(n)5cos 2p fc
n
where fc
550/128. The resulting amplitude-modulated signal is
	 xam
(n)5x(n) cos 2p fc
n
Using MATLAB program,
(a)  sketch the signals x(n), xc
(n) and xam
(n), 0 # n # 255
(b)  compute and sketch the 128-point DFT of the signal xam
(n), 0 # n # 127
(c)  compute and sketch the 128-point DFT of the signal xam
(n), 0 # n # 99
Solution
% Program
Solution for Section (a)
clc;close all;clear all;
f151/128;f255/128;n50:255;fc550/128;
x5cos(2*pi*f1*n)1cos(2*pi*f2*n);
xa5cos(2*pi*fc*n);
xamp5x.*xa;
subplot(2,2,1);plot(n,x);title(‘x(n)’);
xlabel(‘n --.’);ylabel(‘amplitude’);
subplot(2,2,2);plot(n,xc);title(‘xa(n)’);
xlabel(‘n --.’);ylabel(‘amplitude’);
subplot(2,2,3);plot(n,xamp);
xlabel(‘n --.’);ylabel(‘amplitude’);
%128 point DFT computation2solution for Section (b)
n50:127;figure;n15128;
f151/128;f255/128;fc550/128;
x5cos(2*pi*f1*n)1cos(2*pi*f2*n);
xc5cos(2*pi*fc*n);
xa5cos(2*pi*fc*n);
(Contd.)
826   Digital Signal Processing
−2
−1
0
1
2
Amplitude
0 100 200 300
(iii) n
Fig. 16.5(a)  (iii) Amplitude Modulated Signal
Fig. 16.5(a)  (ii) Carrier Signal and
−1
−0.5
0
0.5
1
Amplitude
0 100 200 300
(ii) n
0
−2
−1
0
1
2
100
Amplitude
200
(i)
300
n
Fig. 16.5(a)  (i) Modulating Signal x (n)
(Contd.)
MATLAB Programs  827
20
Amplitude
40 60 80 100 120
n
1400
−10
−5
0
5
10
15
20
25
Fig. 16.5(b)  128-point DFT of the Signal xam
(n), 0 # n # 127
20 40 60 80 100 120 140
n
0
−5
0
5
10
15
20
25
30
35
Amplitude
Fig. 16.5(c)  128-point DFT of the Signal xam
(n), 0 # n # 99
828   Digital Signal Processing
xamp5x.*xa;xam5fft(xamp,n1);
stem(n,xam);title(‘xamp(n)’);xlabel(‘n --.’);
ylabel(‘amplitude’);
%128 point DFT computation2solution for Section (c)
n50:99;figure;n250:n121;
f151/128;f255/128;fc550/128;
x5cos(2*pi*f1*n)1cos(2*pi*f2*n);
xc5cos(2*pi*fc*n);
xa5cos(2*pi*fc*n);
xamp5x.*xa;
for i51:100,
xamp1(i)5xamp(i);
end
xam5fft(xamp1,n1);
s t e m ( n 2 , x a m ) ; t i t l e ( ‘ x a m p ( n ) ’ ) ; x l a b e l ( ‘ n
--.’);ylabel(‘amplitude’);
(a)Modulated signal x(n), carrier signal xa
(n) and amplitude modulated signal
xam
(n) are shown in Fig. 16.5(a). Fig. 16.5 (b) shows the 128-point DFT of the
signal xam
(n) for 0 # n # 127 and Fig. 16.5 (c) shows the 128-point DFT of the
signal xam
(n), 0 # n # 99.
16.7   FAST FOURIER TRANSFORM
Algorithm
1.	 Get the signal x(n)of length N in matrix form
2.	 Get the N value
3.	 The transformed signal is denoted as
x k x n e k N
j
N
nk
n
N
( ) ( ) for= ≤ ≤ −
−
=
−
∑
2
0
1
0 1
p

% Program for computing discrete Fourier transform
clc;close all;clear all;
x5input(‘enter the sequence’);
n5input(‘enter the length of fft’);
X(k)5fft(x,n);
stem(y);ylabel(‘Imaginary axis --.’);
xlabel(‘Real axis --.’);
X(k)
As an example,
enter the sequence [0 1 2 3 4 5 6 7]
enter the length of fft 8
X(k)5
	 Columns 1 through 4
	 28.0000 24.000019.6569i 24.0000 14.0000i 24.0000
1 1.6569i
	 Columns 5 through 8
	 24.0000 24.0000 21.6569i 24.0000 24.0000i 24.0000
29.6569i
MATLAB Programs  829
The eight-point decimation-in-time fast Fourier transform of the sequence x(n)is
computed using MATLAB program and the resultant output is plotted in Fig. 16.6.
0−5
−10
−8
−6
−4
−2
0
2
4
6
8
10
5 10 15 20 25 30
Real axis
Imaginaryaxis
Fig. 16.6  Fast Fourier Transform
16.8   BUTTERWORTH ANALOG FILTERS
16.8.1  Low-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.46
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Butterworth analog low pass filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
wp5input(‘enter the passband freq’);
ws5input(‘enter the stopband freq’);
fs5input(‘enter the sampling freq’);
830   Digital Signal Processing
w152*wp/fs;w252*ws/fs;
[n,wn]5buttord(w1,w2,rp,rs,’s’);
[z,p,k]5butter(n,wn);
[b,a]5zp2tf(z,p,k);
[b,a]5butter(n,wn,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple	 0.15
enter the stopband ripple	 60
enter the passband freq		 1500
enter the stopband freq		 3000
enter the stopband freq		 7000
The amplitude and phase responses of the Butterworth low-pass analog filter are
shown in Fig. 16.7.
Fig. 16.7  Butterworth Low-pass Analog Filter
(a) Amplitude Response and (b) Phase Response
0.1
0.1
−250
−200
−150
−4
−2
2
4
0
−100
−50
50
GainindB
0
0.2
0.2
0.3
0.3
0.4
0.4
(a)
(b)
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
Phaseinradians
0.1
0.1
−250
−200
−150
−4
−2
2
4
0
−100
−50
50
GainindB
0
0.2
0.2
0.3
0.3
0.4
0.4
(a)
(b)
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
Phaseinradians
MATLAB Programs  831
16.8.2  High-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.46
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Butterworth analog high—pass filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
wp5input(‘enter the passband freq’);
ws5input(‘enter the stopband freq’);
fs5input(‘enter the sampling freq’);
w152*wp/fs;w252*ws/fs;
[n,wn]5buttord(w1,w2,rp,rs,’s’);
[b,a]5butter(n,wn,’high’,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple	 0.2
enter the stopband ripple	 40
enter the passband freq	 2000
enter the stopband freq	 3500
enter the sampling freq	 8000
The amplitude and phase responses of Butterworth high-pass analog filter are
shown in Fig. 16.8.
832   Digital Signal Processing
16.8.3  Bandpass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.46
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Butterworth analog Bandpass filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
Fig. 16.8  Butterworth High-pass Analog Filter (a) Amplitude Response and
(b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−400
−4
4
−2
2
0
−300
−200
−100
0
100
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−400
−4
4
−2
2
0
−300
−200
−100
0
100
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
MATLAB Programs  833
[n]5buttord(w1,w2,rp,rs);
wn5[w1 w2];
[b,a]5butter(n,wn,’bandpass’,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.36
enter the stopband ripple...	 36
enter the passband freq...	 1500
enter the stopband freq...	 2000
enter the sampling freq...	 6000
The amplitude and phase responses of Butterworth bandpass analog filter are
shown in Fig. 16.9.
Fig. 16.9  Butterworth Bandpass Analog Filter (a) Amplitude Response and
(b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−1000
−4
4
−2
2
0
−800
−600
−200
−400
0
200
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
834   Digital Signal Processing
16.8.4  Bandstop Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.46
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Butterworth analog Bandstop filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n]5buttord(w1,w2,rp,rs,’s’);
wn5[w1 w2];
[b,a]5butter(n,wn,’stop’,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.28
enter the stopband ripple...	 28
enter the passband freq...	 1000
enter the stopband freq...	 1400
enter the sampling freq...	 5000
The amplitude and phase responses of Butterworth bandstop analog filter are
shown in Fig. 16.10.
MATLAB Programs  835
16.9   CHEBYSHEV TYPE-1 ANALOG FILTERS
16.9.1  Low-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.57
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-1 low-pass filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−150
−50
−100
−200
0
50
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
Fig. 16.10  Butterworth Bandstop Analog Filter (a) Amplitude Response and
(b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−150
−50
−100
−200
0
50
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
836   Digital Signal Processing
w152*wp/fs;w252*ws/fs;
[n,wn]5cheb1ord(w1,w2,rp,rs,’s’);
[b,a]5cheby1(n,rp,wn,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.23
enter the stopband ripple...	 47
enter the passband freq...	 1300
enter the stopband freq...	 1550
enter the sampling freq...	 7800
The amplitude and phase responses of Chebyshev type - 1 low-pass analog filter
are shown in Fig. 16.11.
GainindBPhaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−80
−40
−20
−60
−100
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
Fig. 16.11  Chebyshev Type-I Low-pass Analog Filter (a) Amplitude Response
and (b) Phase Response
MATLAB Programs  837
16.9.2  High-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.57
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
%Program for the design of Chebyshev Type-1 high-pass filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n,wn]5cheb1ord(w1,w2,rp,rs,’s’);
[b,a]5cheby1(n,rp,wn,’high’,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
Fig. 16.12  Chebyshev Type - 1 High-pass Analog Filter (a) Amplitude Response
and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−100
−50
−150
−200
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−100
−50
−150
−200
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
838   Digital Signal Processing
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.29
enter the stopband ripple...	 29
enter the passband freq...	 900
enter the stopband freq...	 1300
enter the sampling freq...	 7500
The amplitude and phase responses of Chebyshev type - 1 high-pass analog filter
are shown in Fig. 16.12.
16.9.3  Bandpass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.57
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-1 Bandpass filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n]5cheb1ord(w1,w2,rp,rs,’s’);
wn5[w1 w2];
[b,a]5cheby1(n,rp,wn,’bandpass’,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.3
enter the stopband ripple...	 40
enter the passband freq...	 1400
MATLAB Programs  839
enter the stopband freq...	 2000
enter the sampling freq...	 5000
The amplitude and phase responses of Chebyshev type - 1 bandpass analog filter
are shown in Fig. 16.13.
16.9.4  Bandstop Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequency
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.57
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-1 Bandstop filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
Fig. 16.13  Chebyshev Type-1 Bandpass Analog Filter
(a) Amplitude Response and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−3
3
−2
−1
1
2
0
−200
−100
−300
−400
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−3
3
−2
−1
1
2
0
−200
−100
−300
−400
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
840   Digital Signal Processing
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n]5cheb1ord(w1,w2,rp,rs,’s’);
wn5[w1 w2];
[b,a]5cheby1(n,rp,wn,’stop’,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.15
enter the stopband ripple...	 30
enter the passband freq...	 2000
enter the stopband freq...	 2400
enter the sampling freq...	 7000
The amplitude and phase responses of Chebyshev type - 1 bandstop analog filter
are shown in Fig. 16.14.
Fig. 16.14  Chebyshev Type - 1 Bandstop Analog Filter
(a) Amplitude Response and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−150
−50
−100
−200
−250
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−150
−50
−100
−200
−250
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
MATLAB Programs  841
16.10   CHEBYSHEV TYPE-2 ANALOG FILTERS
16.10.1  Low-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.67
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-2 low pass analog filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n,wn]5cheb2ord(w1,w2,rp,rs,’s’);
[b,a]5cheby2(n,rs,wn,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.4
enter the stopband ripple...	 50
enter the passband freq...	 2000
enter the stopband freq...	 2400
enter the sampling freq...	 10000
The amplitude and phase responses of Chebyshev type - 2 low-pass analog filter
are shown in Fig. 16.15.
842   Digital Signal Processing
Fig. 16.15  Chebyshev Type - 2 Low-pass Analog Filter (a) Amplitude Response
and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−60
−20
−40
−80
−100
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
16.10.2  High-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.67
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-2 High pass analog filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n,wn]5cheb2ord(w1,w2,rp,rs,’s’);
[b,a]5cheby2(n,rs,wn,’high’,’s’);
w50:.01:pi;
MATLAB Programs  843
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.34
enter the stopband ripple...	 34
enter the passband freq...	 1400
enter the stopband freq...	 1600
enter the sampling freq...	 10000
The amplitude and phase responses of Chebyshev type - 2 high-pass analog filter
are shown in Fig. 16.16.
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−60
−20
−40
−80
0
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
Fig. 16.16  Chebyshev Type - 2 High-pass Analog Filter
(a) Amplitude Response and (b) Phase Response
16.10.3  Bandpass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
844   Digital Signal Processing
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.67
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-2 Bandpass analog filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n]5cheb2ord(w1,w2,rp,rs,’s’);
wn5[w1 w2];
[b,a]5cheby2(n,rs,wn,’bandpass’,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.37
enter the stopband ripple...	 37
enter the passband freq...	 3000
enter the stopband freq...	 4000
enter the sampling freq...	 9000
The amplitude and phase responses of Chebyshev type - 2 bandpass analog filter
are shown in Fig. 16.17.
Fig. 16.17  (Contd.)
GainindB
aseinradians
(a)
0.1
4
−2
2
0
−80
−60
−20
0
−40
−100
20
0.2 0.3 0.4
Normalised frequency
0.5 0.6 0.7 0.8 0.9 10
MATLAB Programs  845
16.10.4   Bandstop Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.67
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-2 Bandstop analog filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n]5cheb2ord(w1,w2,rp,rs,’s’);
wn5[w1 w2];
[b,a]5cheby2(n,rs,wn,’stop’,’s’);
w50:.01:pi;
[h,om]5freqs(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
Fig. 16.17  Chebyshev Type - 2 Bandstop Analog Filter (a) Amplitude Response
and (b) Phase Response
Gainind
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−80
−60
−40
−100
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
846   Digital Signal Processing
As an example,
enter the passband ripple...	 0.25
enter the stopband ripple...	 30
enter the passband freq...	 1300
enter the stopband freq...	 2000
enter the sampling freq...	 8000
The amplitude and phase responses of Chebyshev type - 2 bandstop analog filter
are shown in Fig. 16.18.
Fig. 16.18  Chebyshev Type - 2 Bandstop Analog Filter
(a) Amplitude Response and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−60
−40
0
20
−20
−80
40
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
16.11   BUTTERWORTH DIGITAL IIR FILTERS
16.11.1 Low-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.46
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
MATLAB Programs  847
% Program for the design of Butterworth low pass digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
wp5input(‘enter the passband freq’);
ws5input(‘enter the stopband freq’);
fs5input(‘enter the sampling freq’);
w152*wp/fs;w252*ws/fs;
[n,wn]5buttord(w1,w2,rp,rs);
[b,a]5butter(n,wn);
w50:.01:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple	 0.5
enter the stopband ripple	 50
enter the passband freq		 1200
enter the stopband freq		 2400
enter the sampling freq		 10000
The amplitude and phase responses of Butterworth low-pass digital filter are
shown in Fig. 16.19.
GainindB
Phaseinradians
(a)
0.1
0.1
−4
4
−2
2
0
−300
−200
0
−100
−400
100
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
Fig. 16.19  (Contd.)
848   Digital Signal Processing
16.11.2  High-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.46
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Butterworth highpass digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
wp5input(‘enter the passband freq’);
ws5input(‘enter the stopband freq’);
fs5input(‘enter the sampling freq’);
w152*wp/fs;w252*ws/fs;
[n,wn]5buttord(w1,w2,rp,rs);
[b,a]5butter(n,wn,’high’);
w50:.01:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple	 0.5
enter the stopband ripple	 50
enter the passband freq	 1200
Fig. 16.19  Butterworth Low-pass Digital Filter (a) Amplitude Response and
(b) Phase Response
Gain
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−300
−200
−400
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
MATLAB Programs  849
enter the stopband freq	 2400
enter the sampling freq	 10000
The amplitude and phase responses of Butterworth high-pass digital filter are
shown in Fig. 16.20.
16.11.3 Band-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.46
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Butterworth Bandpass digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
wp5input(‘enter the passband freq’);
Fig. 16.20  Butterworth High-pass Digital Filter
(a) Amplitude Response and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−250
−200
−150
0
−100
−50
−300
50
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
−250
−200
−150
0
−100
−50
−300
50
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
850   Digital Signal Processing
ws5input(‘enter the stopband freq’);
fs5input(‘enter the sampling freq’);
w152*wp/fs;w252*ws/fs;
[n]5buttord(w1,w2,rp,rs);
wn5[w1 w2];
[b,a]5butter(n,wn,’bandpass’);
w50:.01:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple	 0.3
enter the stopband ripple	 40
enter the passband freq	 1500
enter the stopband freq	 2000
enter the sampling freq	 9000
The amplitude and phase responses of Butterworth band-pass digital filter are
shown in Fig. 16.21.
Fig. 16.21  Butterworth Bandstop Digital Filter (a) Amplitude Response and
(b) Phase Response
GainindB
(a)
(b)
Phaseinradians
0.1
0.1
0
− 200
− 100
− 400
− 500
− 600
− 700
4
2
0
− 2
− 4
− 300
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
MATLAB Programs  851
16.11.4  Bandstop Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.46
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Butterworth Band stop digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
wp5input(‘enter the passband freq’);
ws5input(‘enter the stopband freq’);
fs5input(‘enter the sampling freq’);
w152*wp/fs;w252*ws/fs;
[n]5buttord(w1,w2,rp,rs);
wn5[w1 w2];
[b,a]5butter(n,wn,’stop’);
w50:.01:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple	 0.4
enter the stopband ripple	 46
enter the passband freq	 1100
enter the stopband freq	 2200
enter the sampling freq	 6000
The amplitude and phase responses of the Butterworth bandstop digital filter are
shown in Fig. 16.22.
852   Digital Signal Processing
Fig. 16.22  Butterworth Bandstop Digital Filter
(a) Amplitude Response and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
100
0
−200
−100
−400
−300
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
100
0
−200
−100
−400
−300
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
16.12   CHEBYSHEV TYPE-1 DIGITAL FILTERS
16.12.1  Low-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.57
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-1 lowpass digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
MATLAB Programs  853
w152*wp/fs;w252*ws/fs;
[n,wn]5cheb1ord(w1,w2,rp,rs);
[b,a]5cheby1(n,rp,wn);
w50:.01:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
­frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.2
enter the stopband ripple...	 45
enter the passband freq...	 1300
enter the stopband freq...	 1500
enter the sampling freq...	 10000
The amplitude and phase responses of Chebyshev type - 1 low-pass digital filter
are shown in Fig. 16.23.
Fig. 16.23  Chebyshev Type - 1 Low-pass Digital Filter (a) Amplitude Response
and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
0
−200
−100
−500
−400
−300
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
854   Digital Signal Processing
16.12.2  High-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.57
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-1 highpass digital
filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n,wn]5cheb1ord(w1,w2,rp,rs);
[b,a]5cheby1(n,rp,wn,’high’);
w50:.01/pi:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency
--.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.3
enter the stopband ripple...	 60
enter the passband freq...	 1500
enter the stopband freq...	 2000
enter the sampling freq...	 9000
The amplitude and phase responses of Chebyshev type - 1 high-pass digital filter
are shown in Fig. 16.24.
MATLAB Programs  855
16.12.3  Bandpass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.57
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-1 Bandpass digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
Fig. 16.24  Chebyshev Type - 1 High-pass Digital Filter
(a) Amplitude Response and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
0
−250
−200
−150
−100
−50
−350
−300
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
0
−250
−200
−150
−100
−50
−350
−300
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
856   Digital Signal Processing
[n]5cheb1ord(w1,w2,rp,rs);
wn5[w1 w2];
[b,a]5cheby1(n,rp,wn,’bandpass’);
w50:.01:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.4
enter the stopband ripple...	 35
enter the passband freq...	 2000
enter the stopband freq...	 2500
enter the sampling freq...	 10000
The amplitude and phase responses of Chebyshev type - 1 bandpass digital filter
are shown in Fig. 16.25.
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
4
−2
2
0
0
−300
−200
−100
−500
−400
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
Fig. 16.25  Chebyshev Type - 1 Bandpass Digital Filter (a) Amplitude
Response and (b) Phase Response
MATLAB Programs  857
16.12.4  Bandstop Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.57
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-1 Bandstop digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n]5cheb1ord(w1,w2,rp,rs);
wn5[w1 w2];
[b,a]5cheby1(n,rp,wn,’stop’);
w50:.1/pi:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.25
enter the stopband ripple...	 40
enter the passband freq...	 2500
enter the stopband freq...	 2750
enter the sampling freq...	 7000
The amplitude and phase responses of Chebyshev type - 1 bandstop digital filter
are shown in Fig. 16.26.
858   Digital Signal Processing
Fig. 16.26  Chebyshev Type - 1 Bandstop Digital Filter
(a) Amplitude Response and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−3
−2
−1
1
2
3
4
0
0
− 200
− 150
− 100
−50
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−3
−2
−1
1
2
3
4
0
0
− 200
− 150
− 100
−50
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
16.13   CHEBYSHEV TYPE-2 DIGITAL FILTERS
16.13.1  Low-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.67
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-2 lowpass digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n,wn]5cheb2ord(w1,w2,rp,rs);
[b,a]5cheby2(n,rs,wn);
MATLAB Programs  859
w50:.01:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.35
enter the stopband ripple...	 35
enter the passband freq...	 1500
enter the stopband freq...	 2000
enter the sampling freq...	 8000
The amplitude and phase responses of Chebyshev type - 2 low-pass digital filter
are shown in Fig. 16.27.
Fig. 16.27  Chebyshev Type - 2 Low-pass Digital Filter (a) Amplitude Response
and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
−2
2
4
0
−100
−80
−60
−40
−20
0
20
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
−2
2
4
0
−100
−80
−60
−40
−20
0
20
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
16.13.2  High-pass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
860   Digital Signal Processing
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.67
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-2 high pass digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n,wn]5cheb2ord(w1,w2,rp,rs);
[b,a]5cheby2(n,rs,wn,’high’);
w50:.01/pi:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.25
enter the stopband ripple...	 40
enter the passband freq...	 1400
enter the stopband freq...	 1800
enter the sampling freq...	 7000
The amplitude and phase responses of Chebyshev type - 2 high-pass digital filter
are shown in Fig. 16.28.
GainindB
aseinradians
(a)
0.1
−2
2
4
0
−120
−100
−80
−60
−40
−20
0
0.2 0.3 0.4
Normalised frequency
0.5 0.6 0.7 0.8 0.9 10
Fig. 16.28  (Contd.)
MATLAB Programs  861
16.13.3  Bandpass Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequency
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.67
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of Chebyshev Type-2 Bandpass digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n]5cheb2ord(w1,w2,rp,rs);
wn5[w1 w2];
[b,a]5cheby2(n,rs,wn,’bandpass’);
w50:.01/pi:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
Gainin
Phaseinradians
(a)
(b)
0.1
0.1
−4
−2
2
4
0
−120
−100
−80
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
Fig. 16.28  Chebyshev Type - 2 High-pass Digital Filter (a) Amplitude Response
and (b) Phase Response
862   Digital Signal Processing
As an example,
enter the passband ripple...	 0.4
enter the stopband ripple...	 40
enter the passband freq...	 1400
enter the stopband freq...	 2000
enter the sampling freq...	 9000
The amplitude and phase responses of Chebyshev type - 2 bandpass digital filter
are shown in Fig. 16.29.
Fig. 16.29  Chebyshev Type - 2 Bandpass Digital Filter (a) Amplitude Response
and (b) Phase Response
GainindB
Phaseinradians
(a)
(b)
0.1
0.1
−4
−2
4
0
2
−400
−300
−200
−100
0
100
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
16.13.4  Bandstop Filter
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter using Eq. 8.67
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
MATLAB Programs  863
% Program for the design of Chebyshev Type-2 Bandstop digital filter
clc;
close all;clear all;
format long
rp5input(‘enter the passband ripple...’);
rs5input(‘enter the stopband ripple...’);
wp5input(‘enter the passband freq...’);
ws5input(‘enter the stopband freq...’);
fs5input(‘enter the sampling freq...’);
w152*wp/fs;w252*ws/fs;
[n]5cheb2ord(w1,w2,rp,rs);
wn5[w1 w2];
[b,a]5cheby2(n,rs,wn,’stop’);
w50:.1/pi:pi;
[h,om]5freqz(b,a,w);
m520*log10(abs(h));
an5angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised
frequency --.’);
subplot(2,1,2);plot(om/pi,an);
xlabel(‘(b) Normalised frequency --.’);
ylabel(‘Phase in radians --.’);
As an example,
enter the passband ripple...	 0.3
enter the stopband ripple...	 46
enter the passband freq...	 1400
enter the stopband freq...	 2000
enter the sampling freq...	 8000
The amplitude and phase responses of Chebyshev type - 2 bandstop digital filter
are shown in Fig. 16.30.
GainindB
Phaseinradians
(a)
0.1
-4
-2
-1
-3
3
0
1
2
−80
−60
−40
−20
0
20
0.2 0.3 0.4
Normalised frequency
0.5 0.6 0.7 0.8 0.9 10
Fig. 16.30  (Contd.)
864   Digital Signal Processing
16.14
 FIR FILTER DESIGN USING WINDOW
TECHNIQUES
In the design of FIR filters using any window technique, the order can be calculated
using the formula given by
N
p s
f f Fs p s
=
− −
−
20 13
14 6
log( )
. ( ) /
d d
where dp is the passband ripple, ds is the stopband ripple, fp
is the passband frequency,
fs
is the stopband frequency and Fs
is the sampling frequency.
16.14.1  Rectangular Window
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter
5.	 Find the window coefficients using Eq. 7.37
6.	 Draw the magnitude and phase responses.
% Program for the design of FIR Low pass, High pass, Band pass
and Bandstop filters using rectangular window
clc;clear all;close all;
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
fp5input(‘enter the passband freq’);
fs5input(‘enter the stopband freq’);
f5input(‘enter the sampling freq’);
wp52*fp/f;ws52*fs/f;
num5220*log10(sqrt(rp*rs))213;
Fig. 16.30  Chebyshev Type - 2 Bandstop Digital Filter (a) Amplitude Response
and (b) Phase Response
Gaini
Phaseinradians
(a)
(b)
0.1
0.1
-4
-2
-1
-3
3
0
1
2
−80
−60
−40
0.2
0.2
0.3
0.3
0.4
0.4
Normalised frequency
Normalised frequency
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
1
1
0
0
MATLAB Programs  865
dem514.6*(fs2fp)/f;
n5ceil(num/dem);
n15n11;
if (rem(n,2)˜50)
n15n;
n5n21;
end
y5boxcar(n1);
% low-pass filter
b5fir1(n,wp,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(a) Normalised frequency --.’);
% high-pass filter
b5fir1(n,wp,’high’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(b) Normalised frequency --.’);
% band pass filter
wn5[wp ws];
b5fir1(n,wn,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --’);
xlabel(‘(c) Normalised frequency --’);
% band stop filter
b5fir1(n,wn,’stop’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --’);
xlabel(‘(d) Normalised frequency --’);
As an example,
enter the passband ripple	 0.05
enter the stopband ripple	 0.04
enter the passband freq		 1500
enter the stopband freq		 2000
enter the sampling freq		 9000
The gain responses of low-pass, high-pass, bandpass and bandstop filters using
rectangular window are shown in Fig. 16.31.
866   Digital Signal Processing
16.14.2  Bartlett Window
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of FIR Low pass, High pass, Band pass
and Bandstop filters using Bartlett window
clc;clear all;close all;
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
fp5input(‘enter the passband freq’);
fs5input(‘enter the stopband freq’);
f5input(‘enter the sampling freq’);
Fig. 16.31  Filters Using Rectangular Window (a) Low-pass (b) High-pass
(c) Bandpass and (d) Bandstop
0.2
0.2
0.2
0.2
−80
−80 −20
−80
−60
−60 −15
−60
−40
−40 −10
−40
−20
−20 −5
−20
0
0 0
0
20
20 5
20
0.4
0.4
0.4
0.4
(a)
(c)
(b)
(d)
Normalised frequency
Normalised frequency
Normalised frequency
Normalised frequency
0.6
0.6
0.6
0.6
0.8
0.8
0.8
0.8
1
1
1
1
0
0
0
0
GainindBGainindB
GainindBGainindB
MATLAB Programs  867
wp52*fp/f;ws52*fs/f;
num5220*log10(sqrt(rp*rs))213;
dem514.6*(fs2fp)/f;
n5ceil(num/dem);
n15n11;
if (rem(n,2)˜50)
n15n;
n5n21;
end
y5bartlett(n1);
% low-pass filter
b5fir1(n,wp,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(a) Normalised frequency --.’);
% high-pass filter
b5fir1(n,wp,’high’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(b) Normalised frequency --.’);
% band pass filter
wn5[wp ws];
b5fir1(n,wn,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(c) Normalised frequency --.’);
% band stop filter
b5fir1(n,wn,’stop’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(d) Normalised frequency --.’);
As an example,
enter the passband ripple	 0.04
enter the stopband ripple	 0.02
enter the passband freq		 1500
enter the stopband freq		 2000
enter the sampling freq		 8000
The gain responses of low-pass, high-pass, bandpass and bandstop filters using
Bartlett window are shown in Fig. 16.32.
868   Digital Signal Processing
16.14.3  Blackman window
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter
5.	 Find the window coefficients using Eq. 7.45
6.	 Draw the magnitude and phase responses.
% Program for the design of FIR Low pass, High pass, Band pass
and Band stop digital filters using Blackman window
clc;clear all;close all;
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
fp5input(‘enter the passband freq’);
fs5input(‘enter the stopband freq’);
f5input(‘enter the sampling freq’);
Fig. 16.32  Filters using Bartlett Window (a) Low-pass (b) High-pass
(c) Bandpass and (d) Bandstop
− 35
−40 −8
− 30
−30
−6
− 30
−25
−25
−20
−20
−4
− 20
−15
− 15
−10
−10
−2
−0
− 10
−5
−5
0
20
5
0
0.2
0.2
0.2
0.2
0.4
0.4
0.4
0.4
(a)
(c)
(b)
(d)
Normalised frequency
Normalised frequency
Normalised frequency
Normalised frequency
0.6
0.6
0.6
0.6
0.8
0.8
0.8
0.8
1
1
1
1
0
0
0
0
GainindB
GainindBGainindB
GainindB
MATLAB Programs  869
wp52*fp/f;ws52*fs/f;
num5220*log10(sqrt(rp*rs))213;
dem514.6*(fs2fp)/f;
n5ceil(num/dem);
n15n11;
if (rem(n,2)˜50)
	 n15n;
	 n5n21;
end
y5blackman(n1);
% low-pass filter
b5fir1(n,wp,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(a) Normalised frequency --.’);
% high-pass filter
b5fir1(n,wp,’high’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(b) Normalised frequency --.’);
% band pass filter
wn5[wp ws];
b5fir1(n,wn,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(c) Normalised frequency --.’);
% band stop filter
b5fir1(n,wn,’stop’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,4);plot(o/pi,m);;ylabel(‘Gain in dB --.’);
xlabel(‘(d) Normalised frequency --.’);
As an example,
enter the passband ripple	 0.03
enter the stopband ripple	 0.01
enter the passband freq		 2000
enter the stopband freq		 2500
enter the sampling freq		 7000
The gain responses of low-pass, high-pass, bandpass and bandstop filters using
Blackman window are shown in Fig. 16.33.
870   Digital Signal Processing
16.14.4  Chebyshev Window
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter
5.	 Find the filter coefficients
6.	 Draw the magnitude and phase responses.
% Program for the design of FIR Lowpass, High pass, Band pass
and Bandstop filters using Chebyshev window
clc;clear all;close all;
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
fp5input(‘enter the passband freq’);
fs5input(‘enter the stopband freq’);
f5input(‘enter the sampling freq’);
Fig. 16.33  Filters using Blackman Window (a) Low-pass (b) High-pass
(c) Bandpass and (d) Bandstop
−120
−120 −8
−150
−100
−100
−100 −6
−80
−60
− 80
−4
−40
−20
− 60
− 40
− 20
−2
0
−50
0
0
20
20
50
0.2
0.2
0.2
0.2
0.4
0.4
0.4
0.4
(a)
(c)
(b)
(d)
Normalised frequency
Normalised frequency
Normalised frequency
Normalised frequency
0.6
0.6
0.6
0.6
0.8
0.8
0.8
0.8
1
1
1
1
0
0
0
0
GainindB
GainindB
GainindB
GainindB
MATLAB Programs  871
r5input(‘enter the ripple value(in dBs)’);
wp52*fp/f;ws52*fs/f;
num5220*log10(sqrt(rp*rs))213;
dem514.6*(fs2fp)/f;
n5ceil(num/dem);
if(rem(n,2)˜50)
	 n5n11;
end
y5chebwin(n,r);
% low-pass filter
b5fir1(n-1,wp,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(a) Normalised frequency --.’);
% high-pass filter
b5fir1(n21,wp,’high’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(b) Normalised frequency --.’);
% band-pass filter
wn5[wp ws];
b5fir1(n21,wn,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(c) Normalised frequency --.’);
% band-stop filter
b5fir1(n21,wn,’stop’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(d) Normalised frequency --.’);
As an example,
enter the passband ripple	 0.03
enter the stopband ripple	 0.02
enter the passband freq		 1800
enter the stopband freq		 2400
enter the sampling freq		 10000
enter the ripple value(in dBs)40
The gain responses of low-pass, high-pass, bandpass and bandstop filters using
Chebyshev window are shown in Fig. 16.34.
872   Digital Signal Processing
16.14.5  Hamming Window
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter
5.	 Find the window coefficients using Eq. 7.40
6.	 Draw the magnitude and phase responses.
% Program for the design of FIR Low pass, High pass, Band pass
and Bandstop filters using Hamming window
clc;clear all;close all;
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
fp5input(‘enter the passband freq’);
fs5input(‘enter the stopband freq’);
f5input(‘enter the sampling freq’);
Fig. 16.34  Filters using Chebyshev Window (a) Low-pass (b) High-pass
(c) Bandpass and (d) Bandstop
−100
−120 −12
−100
−40
−20
−60
−80− 80
−100 −10
−60
− 40
− 80
−8
− 20
− 60
− 40
− 20
−6
−4
−2
0
0
20
0
20
20
0.2
0.2
0.2
0.2
0.4
0.4
0.4
0.4
(a)
(c)
(b)
(d)
Normalised frequency
Normalised frequency
Normalised frequency
Normalised frequency
0.6
0.6
0.6
0.6
0.8
0.8
0.8
0.8
1
1
1
1
0
0
0
0
GainindB
GainindBGainindB
GainindB
MATLAB Programs  873
wp52*fp/f;ws52*fs/f;
num5220*log10(sqrt(rp*rs))213;
dem514.6*(fs2fp)/f;
n5ceil(num/dem);
n15n11;
if (rem(n,2)˜50)
	 n15n;
	 n5n21;
end
y5hamming(n1);
% low-pass filter
b5fir1(n,wp,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(a) Normalised frequency --.’);
% high-pass filter
b5fir1(n,wp,’high’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(b) Normalised frequency --.’);
% band pass filter
wn5[wp ws];
b5fir1(n,wn,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(c) Normalised frequency --.’);
% band stop filter
b5fir1(n,wn,’stop’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(d) Normalised frequency --.’);
As an example,
enter the passband ripple	 0.02
enter the stopband ripple	 0.01
enter the passband freq		 1200
enter the stopband freq		 1700
enter the sampling freq		 9000
The gain responses of low-pass, high-pass, bandpass and bandstop filters using
Hamming window are shown in Fig. 16.35.
874   Digital Signal Processing
16.14.6 Hanning Window
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter
5.	 Find the window coefficients using Eq. 7.44
6.	 Draw the magnitude and phase responses.
% Program for the design of FIR Low pass, High pass, Band pass
and Band stop filters using Hanning window
clc;clear all;close all;
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
fp5input(‘enter the passband freq’);
fs5input(‘enter the stopband freq’);
f5input(‘enter the sampling freq’);
Fig. 16.35  Filters using Hamming Window (a) Low-pass (b) High-pass
(c) Bandpass and (d) Bandstop
−120
−120 −15
−100
−40
−20
−60
−80−100
−100
−10
−5
−80
− 60
− 80
− 40
− 20
− 60
− 40
− 20
0
0
20
0
20
20
0.2
0.2
0.2
0.2
0.4
0.4
0.4
0.4
(a)
(c)
(b)
(d)
Normalised frequency
Normalised frequency
Normalised frequency
Normalised frequency
0.6
0.6
0.6
0.6
0.8
0.8
0.8
0.8
1
1
1
1
0
0
0
0
GainindB
GainindBGainindB
GainindB
MATLAB Programs  875
wp52*fp/f;ws52*fs/f;
num5220*log10(sqrt(rp*rs))213;
dem514.6*(fs2fp)/f;
n5ceil(num/dem);
n15n11;
if (rem(n,2)˜50)
n15n;
n5n21;
end
y5hamming(n1);
% low-pass filter
b5fir1(n,wp,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(a) Normalised frequency --.’);
% high-pass filter
b5fir1(n,wp,’high’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(b) Normalised frequency --.’);
% band pass filter
wn5[wp ws];
b5fir1(n,wn,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(c) Normalised frequency --.’);
% band stop filter
b5fir1(n,wn,’stop’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --.’);
xlabel(‘(d) Normalised frequency --.’);
As an example,
enter the passband ripple	 0.03
enter the stopband ripple	 0.01
enter the passband freq		 1400
enter the stopband freq		 2000
enter the sampling freq		 8000
The gain responses of low-pass, high-pass, bandpass and bandstop filters using
Hanning window are shown in Fig. 16.36.
876   Digital Signal Processing
16.14.7  Kaiser Window
Algorithm
1.	 Get the passband and stopband ripples
2.	 Get the passband and stopband edge frequencies
3.	 Get the sampling frequency
4.	 Calculate the order of the filter
5.	 Find the window coefficients using Eqs 7.46 and 7.47
6.	 Draw the magnitude and phase responses.
% Program for the design of FIR Low pass, High pass, Band pass
and Bandstop filters using Kaiser window
clc;clear all;close all;
rp5input(‘enter the passband ripple’);
rs5input(‘enter the stopband ripple’);
fp5input(‘enter the passband freq’);
fs5input(‘enter the stopband freq’);
f5input(‘enter the sampling freq’);
beta5input(‘enter the beta value’);
Fig. 16.36   Filters using Hanning Window (a) Low-pass (b) High-pass
(c) Bandpass and (d) Bandstop
− 120
−120 −12
−100
−40
−20
−60
−80− 100
−100 −10
−8
−6
−4
−2
0
−80
−60
−80
−40
−20
−60
−40
−20
0
20
0
0 2
20
0.2
0.2
0.2
0.2
0.4
0.4
0.4
0.4
(a)
(c)
(b)
(d)
Normalised frequency
Normalised frequency
Normalised frequency
Normalised frequency
0.6
0.6
0.6
0.6
0.8
0.8
0.8
0.8
1
1
1
1
0
0
0
0
GainindB
GainindBGainindB
GainindB
MATLAB Programs  877
wp52*fp/f;ws52*fs/f;
num5220*log10(sqrt(rp*rs))213;
dem514.6*(fs2fp)/f;
n5ceil(num/dem);
n15n11;
if (rem(n,2)˜50)
	 n15n;
	 n5n21;
end
y5kaiser(n1,beta);
% low-pass filter
b5fir1(n,wp,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --’);
xlabel(‘(a) Normalised frequency --’);
% high-pass filter
b5fir1(n,wp,’high’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --’);
xlabel(‘(b) Normalised frequency --’);
% band pass filter
wn5[wp ws];
b5fir1(n,wn,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --’);
xlabel(‘(c) Normalised frequency --’);
% band stop filter
b5fir1(n,wn,’stop’,y);
[h,o]5freqz(b,1,256);
m520*log10(abs(h));
subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --’);
xlabel(‘(d) Normalised frequency --’);
As an example,
enter the passband ripple	 0.02
enter the stopband ripple	 0.01
enter the passband freq	 1000
enter the stopband freq	 1500
enter the sampling freq	 10000
enter the beta value	 5.8
The gain responses of low-pass, high-pass, bandpass and bandstop filters using
Kaiser window are shown in Fig. 16.37.
878   Digital Signal Processing
16.15   UPSAMPLING A SINUSOIDAL SIGNAL
% Program for upsampling a sinusoidal signal by factor L
N5input(‘Input length of the sinusoidal sequence5’);
L5input(‘Up Samping factor5’);
fi5input(‘Input signal frequency5’);
% Generate the sinusoidal sequence for the specified length N
n50:N21;
x5sin(2*pi*fi*n);
% Generate the upsampled signal
y5zeros (1,L*length(x));
y([1:L:length(y)])5x;
%Plot the input sequence
subplot (2,1,1);
stem (n,x);
title(‘Input Sequence’);
xlabel(‘Time n’);
ylabel(‘Amplitude’);
Fig. 16.37  Filters using Kaiser Window (a) Low-pass (b) High-pass
(c) Bandpass and (d) Bandstop
−120
−120 −15
−40
−20
−60
−80
−100
−100
−10
−5
0
−80
− 60
− 80
− 40
− 20
− 60
− 40
− 20
0
20
0
0 5
20
0.2
0.2
0.2
0.2
0.4
0.4
0.4
0.4
(a)
(c)
(b)
(d)
Normalised frequency
Normalised frequency
Normalised frequency
Normalised frequency
0.6
0.6
0.6
0.6
0.8
0.8
0.8
0.8
1
1
1
1
0
0
0
0
GainindB
GainindB
GainindB
GainindB
MATLAB Programs  879
%Plot the output sequence
subplot (2,1,2);
stem (n,y(1:length(x)));
title(‘[output sequence,upsampling factor5‘,num2str(L)]);
xlabel(‘Time n’);
ylabel(‘Amplitude’);
16.16
 UPSAMPLING AN EXPONENTIAL
SEQUENCE
% Program for upsampling an exponential sequence by a factor M
n5input(‘enter length of input sequence …’);
l5input(‘enter up sampling factor …’);
% Generate the exponential sequence
m50:n21;
a5input(‘enter the value of a …’);
x5a.^m;
% Generate the upsampled signal
y5zeros(1,l*length(x));
y([1:l:length(y)])5x;
figure(1)
stem(m,x);
xlabel({‘Time n’;’(a)’});
ylabel(‘Amplitude’);
figure(2)
stem(m,y(1:length(x)));
xlabel({‘Time n’;’(b)’});
ylabel(‘Amplitude’);
As an example,
enter length of input sentence …	 25
enter upsampling factor …	 3
enter the value of a …	 0.95
The input and output sequences of upsampling an exponential sequence an
are shown
in Fig. 16.38.
Fig. 16.38  (Contd.)
880   Digital Signal Processing
16.17
 DOWN SAMPLING A SINUSOIDAL
SEQUENCE
% Program for down sampling a sinusoidal sequence by a factor M
N5input(‘Input length of the sinusoidal signal5’);
M5input(‘Down samping factor5’);
fi5input(‘Input signal frequency5’);
%Generate the sinusoidal sequence
n50:N21;
m50:N*M21;
x5sin(2*pi*fi*m);
%Generate the down sampled signal
y5x([1:M:length(x)]);
%Plot the input sequence
subplot (2,1,1);
stem(n,x(1:N));
title(‘Input Sequence’);
xlabel(‘Time n’);
ylabel(‘Amplitude’);
%Plot the down sampled signal sequence
subplot(2,1,2);
stem(n,y);
title([‘Outputsequencedownsamplingfactor’,num2str(M)]);
xlabel(‘Time n’);
ylabel(‘Amplitude’);
16.18
 DOWN SAMPLING AN EXPONENTIAL
SEQUENCE
% Program for downsampling an exponential sequence by a factor M
N5input(‘enter the length of the output sequence …’);
M5input(‘enter the down sampling factor …’);
Fig. 16.38  (a) Input Exponential Sequence
(b) Output Sequence Upsampled by a Factor of 3
MATLAB Programs  881
% Generate the exponential sequence
n50:N21;
m50:N*M21;
a5input(‘enter the value of a …’);
x5a.^m;
% Generate the downsampled signal
y5x([1:M:length(x)]);
figure(1)
stem(n,x(1:N));
xlabel({‘Time n’;’(a)’});
ylabel(‘Amplitude’);
figure(2)
stem(n,y);
xlabel({‘Time n’;’(b)’});
ylabel(‘Amplitude’);
As an example,
enter the length of the output sentence …	 25
enter the downsampling factor …	 3
enter the value of a …	 0.95
The input and output sequences of downsampling an exponential sequence an
are
shown in Fig. 16.39.
Fig. 16.39  (a) Input Exponential Sequence
(b) Output Sequence Downsampled by a Factor of 3
882   Digital Signal Processing
16.19   DECIMATOR
% Program for downsampling the sum of two sinusoids using
MATLAB’s inbuilt decimation function by a factor M
N5input(‘Length of the input signal5’);
M5input(‘Down samping factor5’);
f15input(‘Frequency of first sinusoid5’);
f25input(‘Frequency of second sinusoid5’);
n50:N21;
% Generate the input sequence
x52*sin(2*pi*f1*n)13*sin(2*pi*f2*n);
%Generate the decimated signal
% FIR low pass decimation is used
y5decimate(x,M,‘fir’);
%Plot the input sequence
subplot (2,1,1);
stem (n,x(1:N));
title(‘Input Sequence’);
xlabel(‘Time n’);
ylabel(‘Amplitude’);
%Plot the output sequence
subplot (2,1,2);
m50:N/M21;
stem (m,y(1:N/M));
title([‘Outputsequencedownsamplingfactor’,num2str(M)]);
xlabel(‘Time n’);
ylabel(‘Amplitude’);
16.20   DECIMATOR AND INTERPOLATOR
% Program for downsampling and upsampling the sum of two
sinusoids using MATLAB’s inbuilt decimation and interpolation
function by a factor of 20.
%Generate the input sequence for Fs
5200Hz, f1
550Hz and
f2
5100 Hz
t50:1/200:10;
y53.*cos(2*pi*50.*t/200)11.*cos(2*pi*100.*t/200);
figure(1)
stem(y);
xlabel({‘Times in Seconds’;’(a)});
ylabel(‘Amplitude’);
MATLAB Programs  883
%Generate the decimated and interpolated signals
figure(2)
stem(decimate(y,20));
xlabel({‘Times in Seconds’;’(b)});
ylabel(‘Amplitude’);
figure(3)
stem(interp(decimate(y,20),2));
xlabel({‘Times in Seconds’;’(c)});
ylabel(‘Amplitude’);
Fig. 16.40  (a) Input Sequence, (b) Decimated Sequence and
(c) Interpolated sequence
5
0
AmplitudeAmplitude
0 500 1000
Time in Seconds
(a)
1500 2000 2500
−5
5
0
0 20 40
Time in Seconds
(b)
60 80 100 120
−5
Amplitude
5
0
0 50 100 150 200 250
Time in Seconds
(c)
−5
16.21
 ESTIMATION OF POWER SPECTRAL
DENSITY (PSD)
% Program for estimating PSD of two sinusoids plus noise
% Algorithm;
% 1:Get the frequencies of the two sinusoidal waves
% 2:Get the sampling frequency
% 3:Get the length of the sequence to be considered
884   Digital Signal Processing
% 4:Get the two FFT lengths for comparing the corre-
sponding power spectral densities
clc; close all; clear all;
f15input(‘Enter the frequency of first sinusoid’);
f25input(‘Enter the frequency of second sinusoid’);
fs5input(‘Enter the sampling frequency’);
N5input(“Enter the length of the input sequence’);
N15input(“Enter the input FFT length 1’);
N25input(“Enter the input FFT length 2’);
%Generation of input sequence
t50:1/fs:1;
x52*sin(2*pi*f1*1)13*sin(2*pi*f2*t)2randn(size(t));
%Generation of psd for two different FFT lengths
Pxx15abs(fft(x,N1)).^2/(N11);
Pxx25abs(fft(x,N2)).^2/(N11);
%Plot the psd;
subplot(2,1,1);
plot ((0:(N121))/N1*fs,10*log10(Pxx1));
xlabel(‘Frequency in Hz’);
ylabel(‘Power spectrum in dB’);
title(‘[PSD with FFT length,num2str(N1)]’);
subplot (2,1,2);
plot ((0:(N221))/N2*fs,10*log10(Pxx2));
xlabel(‘Frequency in Hz’);
ylabel(‘Power spectrum in dB’);
title(‘[PSD with FFT length,num2str(N2)]’);
16.22   PSD ESTIMATOR
% Program for estimating PSD of a two sinusoids plus noise using
%(i)non-overlapping sections
%(ii)overlapping sections and averaging the periodograms
clc; close all; clear all;
f15input(‘Enter the frequency of first sinusoid’);
f25input(‘Enter the frequency of second sinusoid’);
fs5input(‘Enter the sampling frequency’);
N5input(“Enter the length of the input sequence’);
N15input(“Enter the input FFT length 1’);
N25input(“Enter the input FFT length 2’);
%Generation of input sequence
t50:1/fs:1;
x52*sin(2*pi*f1*1)13*sin(2*pi*f2*t)2randn(size(t));
MATLAB Programs  885
%Generation of psd for two different FFT lengths
Pxx15(abs(fft(x(1:256))).^21abs(fft(x(257:512))).^21
abs(fft(x(513:768))).^2/(256*3); %using nonoverlapping
sections
Pxx25(abs(fft(x(1:256))).^21abs(fft(x(129:384))).^21ab
s(fft(x(257:512))).^21abs(fft(x(385:640))).^21abs(fft(
x(513:768))).^21abs(fft(x(641:896))).^2/(256*6); %using
overlapping sections
% Plot the psd;
subplot (2,1,1);
plot ((0:255)/256*fs,10*log10(Pxx1));
xlabel(‘Frequency in Hz’);
ylabel(‘Power spectrum in dB’);
title(‘[PSD with FFT length,num2str(N1)]’);
subplot (2,1,2);
plot ((0:255)/256*fs,10*log10(Pxx2));
xlabel(‘Frequency in Hz’);
ylabel(‘Power spectrum in dB’);
title(‘[PSD with FFT length,num2str(N2)]’);
16.23   PERIODOGRAM ESTIMATION
% Periodogram estimate cold be done by applying a non-
rectangular data windows to the sections prior to com-
puting the periodogram
% This program estimates PSD for the input signal of two
sinusoids plus noise using Hanning window
f15input(‘Enter the frequency of first sinusoid’);
f25input(‘Enter the frequency of second sinusoid’);
fs5input(‘Enter the sampling frequency’);
t50:1/fs:1;
w5hanning(256);
x52*sin(2*pi*f1*t)13*sin(2*pi*f2*t)2randn(size(t));
Pxx5(abs(fft(w.*x(1:256))).^21abs(fft(w.*x(129:384))).^
21abs(fft(w.*x(257:512))).^21abs(fft(w.*x(385:640))).^2
1abs(fft(w.*x(513:768))).^21abs(fft(w.*x(641:896))).^2/
(norm(w)^2*6);
Plot((0:255)/256*fs,10*log10(Pxx));
16.24   WELCH PSD ESTIMATOR
% Program for estimating the PSD of sum of two sinusoids plus
noise using Welch method
n50.01:0.001:.1;
x5sin(.25*pi*n)13*sin(.45*pi*n)1rand(size(n));
pwelch(x)
886   Digital Signal Processing
xlabel(‘Normalised Frequency (x pi rad/sample)’);
ylabel(‘Power Spectrum Density (dB/rad/sample)’)
16.25  WELCH PSD ESTIMATOR USING WINDOWS
% Program for estimating the PSD of sum of two sinusoids using
Welch method with an overlap of 50 percent and with Hanning,
Hamming, Bartlett, Blackman and rectangular windows.
fs51000;
t50:1/fs:3;
x5sin(2*pi*200*t)1sin(2*pi*400*t);
figure(1)
subplot(211)
pwelch(x,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’)
subplot(212)
pwelch(x,hanning(512),0,512,fs)
title(‘N5512 Overlap550% Hanning’)
figure(2)
subplot(211)
pwelch(x,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’)
subplot(212)
pwelch(x,hamming(512),0,512,fs)
title(‘N5512 Overlap550% Hamming’)
figure(3)
subplot(211)
pwelch(x,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’)
subplot(212)
pwelch(x,bartlett(512),0,512,fs)
title(‘N5512 Overlap550% Bartlett’)
figure(4)
subplot(211)
pwelch(x,[],[],[],fs);
Fig. 16.41  Welch PSD Estimate
5
0
−5
−10
−15
−20
0 0.1 0.2 0.3 0.4 0.5
Welch PSD Estimate
PowerSpectrumDensity(dB/rad/sample)
Normalised Frequency (x pi rad/sample)
0.6 0.7 0.8 0.9 1
MATLAB Programs  887
Fig. 16.42  (b) Welch Estimate with N5512, 50% Overlap Hamming
0
−20
−40
−60
0 50 100 150 200
Frequency (Hz)
Overlay plot of 50 Welch estimates
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−80
0
−20
−40
−60
0 50 100 150 200
Frequency (Hz)
N=512 Overlap = 50% Hamming
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−80
0
−20
−40
−60
0 50 100 150 200
Frequency (Hz)
N=512 Overlap = 50% Hanning
Overlay plot of 50 Welch estimates
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
0 50 100 150 200
Frequency (Hz)
250 300 350 400 450 500
−80
0
−50
−100
PowerSpectralDensity(dB/Hz)
−150
Fig. 16.42  (a) Welch Estimate with N5512, 50% Overlap Hanning
title(‘Overlay plot of 50 Welch estimates’)
subplot(212)
pwelch(x,blackman(512),0,512,fs)
title(‘N5512 Overlap550% Blackman’)
figure(5)
subplot(211)
pwelch(x,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’)
subplot(212)
pwelch(x,boxcar(512),0,512,fs)
title(‘N5512 Overlap550% Rectangular’)
888   Digital Signal Processing
Fig. 16.42  (d) Welch ­Estimate with N5512, 50% Overlap Blackman
0
−20
−40
−60
0 50 100 150 200
Frequency (Hz)
Frequency (Hz)
Overlay plot of 50 Welch estimates
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−80
0
−50
−100
−150
0 50 100 150 200
N=512 Overlap = 50% Blackman
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−200
0
−20
−40
−60
0 50 100 150 200
Frequency (Hz)
Overlay plot of 50 Welch estimates
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−80
0
−20
−40
−60
0 50 100 150 200
Frequency (Hz)
N=512 Overlap = 50% Bartlett
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−80
Fig. 16.42  (c) Welch Estimate with N5512, 50% Overlap Bartlett
MATLAB Programs  889
Fig. 16.42  (e) Welch Estimate with N5512, 50% Overlap Rectangular
0
−20
−40
−60
0 50 100 150 200
Frequency (Hz)
Overlay plot of 50 Welch estimates
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−80
0
−10
−20
−30
−40
−50
−60
0 50 100 150 200
Frequency (Hz)
N = 512 Overlap = 50% Rectangular
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
16.26   WELCH PSD ESTIMATOR USING WINDOWS
% Program for estimating the PSD of sum of two sinusoids plus
noise using Welch method with an overlap of 50 percent and with
Hanning, Hamming, Bartlett, Blackman and rectangular windows
fs51000;
t50:1/fs:3;
x52*sin(2*pi*200*t)15*sin(2*pi*400*t);
y5x1randn(size(t));
figure(1)
subplot(211);
pwelch(y,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’);
subplot(212);
pwelch(y,hanning(512),0,512,fs);
title(‘N5512 Overlap550% Hanning’);
figure(2)
subplot(211);
pwelch(y,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’);
subplot(212);
pwelch(y,hamming(512),0,512,fs);
title(‘N5512 Overlap550% Hamming’);
890   Digital Signal Processing
10
0
−10
−20
−30
0 50 100 150 200
Overlay plot of 50 Welch estimates
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
10
0
−10
−20
−30
0 50 100 150 200
N=512 Overlap = 50% Hanning
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
Fig. 16.43  (a) Welch Estimate with N5512, 50% Overlap Hanning
figure(3)
subplot(211);
pwelch(y,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’);
subplot(212);
pwelch(y,bartlett(512),0,512,fs);
title(‘N5512 Overlap550% Bartlett’);
figure(4)
subplot(211);
pwelch(y,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’);
subplot(212);
pwelch(y,blackman(512),0,512,fs);
title(‘N5512 Overlap550% Blackman’);
figure(5)
subplot(211);
pwelch(y,[],[],[],fs);
title(‘Overlay plot of 50 Welch estimates’);
subplot(212);
pwelch(y,boxcar(512),0,512,fs);
title(‘N5512 Overlap550% Rectangular’);
MATLAB Programs  891
Fig. 16.43  (b) Welch Estimate with N5512, 50% Overlap Hamming
10
0
−10
−20
−30
0 50 100 150 200
Overlay plot of 50 Welch estimates
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
10
0
−10
−20
−30
0 50 100 150 200
N=512 Overlap = 50% Hanning
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
Fig. 16.43  (c) Welch Estimate with N5512, 50% Overlap Bartlett
10
0
−10
−20
−30
0 50 100 150 200
Overlay plot of 50 Welch estimates
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
10
0
−10
−20
−30
0 50 100 150 200
N=512 Overlap = 50% Bartlett
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
892   Digital Signal Processing
10
0
−10
−20
−30
0 50 100 150 200
Overlay plot of 50 Welch estimates
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
10
0
−10
−20
−30
0 50 100 150 200
N=512 Overlap = 50% Hanning
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
Fig. 16.43  (e) Welch Estimate with N5512, 50% Overlap Rectangular
Fig. 16.43  (d) Welch Estimate with N5512, 50% Overlap Blackman
10
0
−10
−20
−30
0 50 100 150 200
Overlay plot of 50 Welch estimates
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
10
0
−10
−20
−30
0 50 100 150 200
N=512 Overlap = 50% Blackman
Frequency (Hz)
PowerSpectralDensity(dB/Hz)
250 300 350 400 450 500
−40
MATLAB Programs  893
16.27   STATE-SPACE REPRESENTATION
% Program for computing the state-space matrices from the given
transfer function
function [A,B,C,D]5tf2ss(b,a);
a5input (‘enter the denominator polynomials5’);
b5input (‘enter the numerator polynomials5’);
p5length(a)21;q5length(b)21;N5max(p,q);
if(Np),a5[a,zeros(1,N2p)];end
if(Nq),b5[b,zeros(1,N2q)];end
A5[2a(2:N11);[eye(N21),zeros(N21,1)]];
B5[1;zeros(N21,1)];
C5b(2:N11)2b(1)*(2:N11);
D5b(1);
16.28   PARTIAL FRACTION DECOMPOSITION
% Program for partial fraction decomposition of a rational transfer
function
function[c,A,alpha]5tf2pf(b,a);
a5input (‘enter the denominator polynomials5’);
b5input (‘enter the numerator polynomials5’);
p5length(a)21;
q5length(b)21;
a5(1/a(1))*reshape(a,1,p11);
b5(1/a(1))*reshape(b,1,q11);
if(q5p),%case of nonempty c(z)
	 temp5toeplitz([a,zeros(1,q2p)]’,[a(1),zeros(1,q2p)]);
	 temp5[temp,[eye(p);zeros(q2p11,p)]);
	 temp5temp/b’;
	 c5temp(1:;q2p11);
	 d5temp(q2p12:q11)’;
else
	 c5[];
	 d5[b,zeros(1,p2q21)];
end
alpha5cplxpair (roots(a));’;
A5zeros(1,p);
for k51 :p
	 temp5prod(alpha(k)2alpha(find(1:p5k)));
	 if(temp550),error(‘repeated roots in TF2PF’);
	 else,A(k)5polyval(d,alpha(k))/temp;
	 end
end
894   Digital Signal Processing
16.29   INVERSE z-TRANSFORM
% Program for computing inverse z-transform of a rational transfer
function
function x5invz(b,a,N);
b5input (‘enter the numerator polynomials5’);
a5input (‘enter the denominator polynomials5’);
N5input (‘enter the number of points to be computed5’);
[c,A,alpha]5tf2pf(b,a);
x5zeros (1,N);
x(1:length(c))5c;
for k51:length(A),
	 x5x1A(k)*(alpha(k)).^(0:N21);
end
x5real(x);
16.30   GROUP DELAY
% Program for computing group delay of a rational transfer
function on a given frequency interval
function D5grpdly(b,a,K,theta);
b5input (‘enter the numerator polynomials5’);
a5input (‘enter the denominator polynomials5’);
K5input (‘enter the number of frequency response
points5’);
theta5input (‘enter the theta value5’);
a5reshape(a,1,length(a));
b5reshape(b,1,length(b));
if (length(a)551)%case of FIR
	 bd52j*(0:length(b)21).*b;
	 if(nargin553),
		 B5frqresp(b,1,K);
		 Bd5frqresp(bd,1,K);
	 else,
		 B5frqresp(b,1,K,theta);
		 Bd5frqresp(bd,1,K,theta);
	 end
	 D5(real(Bd).*imag(B)2real(B).*imag(Bd))./abs(B).^2;
else %case of IIR
	 if(nargin553),
		 D5grpdly (b,1,K)2grpdly(a,1,K);
	 else,
		 D5grpdly(b,1,K,theta)2grpdly(a,1,K,theta);
	 end
end
MATLAB Programs  895
16.31
 IIR FILTER DESIGN-IMPULSE INVARIANT
METHOD
% Program for transforming an analog filter into a digital filter using
impulse invariant technique
function [bout,aout]5impinv(bin,ain,T);
bin5input(‘enter the numerator polynomials5’);
ain5input(‘enter the denominator polynomials5’);
T5input(‘enter the sampling interval5’);
if(length(bin)5length(ain)),
	 error(‘Anlog filter in IMPINV is not strictly proper’);
end
[r,p,k]5residue(bin,ain);
[bout,aout]5pf2tf([],T*r,exp(T*p));
16.32
 IIR FILTER DESIGN-BILINEAR
TRANSFORMATION
% Program for transforming an analog filter into a digial filter using
bilinear transformation
function [b,a,vout,uout,Cout]5bilin(vin,uin,Cin,T);
pin5input(‘enter the poles5’);
zin5input(‘enter the zero5’);
T5input(‘enter the sampling interval5’);
Cin5input(‘enter the gain of the analog filter5’);
p5length(pin);
q5length(zin);
Cout5Cin*(0.5*T)^(p2q)*prod(120.5*T*zin)/
prod(120.5*T*pin);
zout5[(110.5*T*zin)./(120.5*T*pin),2ones(1,p2q)];
pout5(110.5*T*pin)./(120.5*T*pin);
a51;
b51;
for k51 :length(pout),a5conv(a,[1,2pout(k)]); end
for k51 :length(zout),b5conv(b,[1,2zout(k)]); end
a5real(a);
b5real(Cout*b);
Cout5real(Cout);
16.33
 DIRECT REALISATION OF IIR DIGITAL
FILTERS
% Program for computing direct realisation values of IIR digital filter
function y5direct(typ,b,a,x);
x5input(‘enter the input sequence5’);
896   Digital Signal Processing
b5input(‘enter the numerator polynomials5’);
a5input(‘enter the denominator polynomials5’);
typ5input(‘type of realisation5’);
p5length(a)21;
q5length(b)21;
pq5max(p,q);
a5a(2:p11);u5zeros(1,pq);%u is the internal state;
if(typ551)
	 for i51:length(x),
		 unew5x(i)2sum(u(1:p).*a);
		 u5[unew,u];
		 y(i)5sum(u(1:q11).*b);
		 u5u(1:pq);
	 end
elseif(typ552)
	 for i51:length(x)
		 y(i)5b(1)*x(i)1u(1);
		 u5u[(2:pq),0];
		 u(1:q)5u(1:q)1b(2:q11)*x(i);
		 u(1:p)5u(1:p)2a*y(i);
	 end
end
16.34
 PARALLEL REALISATION OF IIR DIGITAL
FILTERS
% Program for computing parallel realisation values of IIR digital
filter
function y5parallel(c,nsec,dsec,x);
x5input(‘enter the input sequence5’);
b5input(‘enter the numerator polynomials5’);
a5input(‘enter the denominator polynomials5’);
c5input(‘enter the gain of the filter5’);
[n,m]5size(a);a5a(:,2:3);
u5zeros(n,2);
for i51:length(x),
	 y(i)5c*x(i);
	 for k51:n,
		 unew5x(i)2sum(u(k,:).*a(k,:));u(k,:)5[unew,u(k,1)];
		 y(i)5y(i)1sum(u(k,:).*b(k,:));
	 end
end
MATLAB Programs  897
16.35
 CASCADE REALISATION OF IIR DIGITAL
FILTERS
% Program for computing cascade realisation values of digital IIR filter
function y5cascade(c,nsec,dsec,x);
x5input(‘enter the input sequence5’);
b5input(‘enter the numerator polynomials5’);
a5input(‘enter the denomiator polynomials5’);
c5input(‘enter the gain of the filter5’);
[n,m]5size(b);
a5a(:,2:3);b5b(:,2,:3);
u5zeros(n,2);
for i51 :length(x),
	 for k51 :n,
		 unew5x(i)2sum(u(k,:).*a(k,:));
		 x(i)52unew1sum(u(k,:).*b(k,:))
		 u(k,:)5[unew,u(k,1)];
	 end
		 y(i)5c*x(i);
	 end
16.36
 DECIMATION BY POLYPHASE
DECOMPOSITION
% Program for computing convolution and m-fold decimation by
polyphase decomposition
function y5ppdec(x,h,M);
x5input(‘enter the input sequence5’);
h5input(‘enter the FIR filter coefficients5’);
M5input(‘enter the decimation factor5’);
1h5length(h); 1p5floor((1h21)/M)11;
p5reshape([reshape(h,1,1h),zeros(1,1p*M21h)],M,1p);
lx5length(x); ly5floor ((1x11h22)/M)11;
1u5foor((1x1M22)/M)11; %length of decimated sequences
u5[zeros(1,M21),reshape(x,1,1x),zeros(1,M*lu2lx2M11)];
y5zeros(1,1u11p21);
for m51:M,y5y1conv(u(m,: ),p(m,: )); end
y5y(1:1y);
16.37   MULTIBAND FIR FILTER DESIGN
% Program for the design of multiband FIR filters
function h5firdes(N,spec,win);
N5input(‘enter the length of the filter5’);
898   Digital Signal Processing
spec5input(‘enter the low,high cutoff frequencies and 	
	 gain5’);
win5input(‘enter the window length5’);
flag5rem(N,2);
[K,m]5size(spec);
n5(0:N)2N/2;
if (˜flag),n(N/211)51;
end,h5zeros(1,N11);
for k51:K
	temp5(spec (k,3)/pi)*(sin(spec(k,2)*n)2sin(spec(k,1)
*n))./n;
	if (˜flag);temp(N/211)5spec(k,3)*(spec(k,2)2
spec(k,1))/pi;
	 end
	 h5h1temp;
end
if (nargin553),
	 h5h.*reshape(win,1,N11);
end
16.38   ANALYSIS FILTER BANK
% Program for maximally decimated uniform DFT analysis filter
bank
function u5dftanal(x,g,M);
g5input(‘enter the filter coefficient5’);
x5input(‘enter the input sequence5’);
M5input(‘enter the decimation factor5’);
1g5length(g); 1p5floor((1g21)/M)11;
p5reshape([reshape(g,1,1g),zeros(1,1p*M21g)],M,1p);
lx5length(x); lu5floor ((1x1M22)/M)11;
x5[zeros(1,M21),reshape(x,1,1x),zeros(1,M*lu2lx2M11)];
x5flipud(reshape(x,M,1u)); %the decimated sequences
u5[];
for m51:M,u5[u; cov(x(m,:),p(m,:))]; end
u5ifft(u);
16.39   SYNTHESIS FILTER BANK
% Program for maximally decimated uniform DFT synthesis filter
bank
function y5udftsynth(v,h,M);
1h5length(h); ‘1q5floor((1h21)/M)11;
q5flipud(reshape([reshape(h,1,1h),zeros(1,1q*M21h)],M,1q));
MATLAB Programs  899
v5fft(v);
y5[ ];
for m51:M,y5[conv(v(m,:),q(m,:));y]; end
y5y(:).’;
16.40   LEVINSON-DURBIN ALGORITHM
% Program for the solution of normal equations using Levinson-
Durbin algorithm
function [a,rho,s]5levdur(kappa);
% Input;
% kappa: covariance sequence values from 0 to p
% Output parameters:
% a: AR polynomial,with leading entry 1
% rho set of p reflection coefficients
% s: innovation variance
kappa5input(‘enter the covariance sequence5’);
p5length(kappa)21;
kappa5reshape(kappa,p11,1);
a51; s5kappa(1); rho5[];
for i51:p,
rhoi5(a*kappa(i11:21:2))/s; rho5[rho,rhoi];
s5s*(12rhoi^2);
a5[a,0]; a5a2rhoi*fliplr(a);
end
16.41   WIENER EQUATION’S SOLUTION
% Program
function b5wiener(kappax,kappayx);
kappax5input(‘enter the covariance sequence5’);
kappyx5input(‘enter the joint covariance sequence5’);
q5length(kappax)21;
kappax5reshape(kappax,q11,1);
kappayx5reshape(kappayx,q11,1);
b5(toeplitz(kappax)/(kappayx)’;
16.42   SHORT-TIME SPECTRAL ANALYSIS
% Program
function X5stsa(x,N,K,L,w,opt,M,theta0,dtheta);
x5input(‘enter the input signal5’); L5input(‘enter the
number consecutive DFTs to average5’);
N5input(‘enter the segment length5’); K5input(‘enter
the number of overlapping points5’); w5input(‘enter
900   Digital Signal Processing
the window coefficients5’); opt5input(‘opt5’);
M5input(‘enter the length of DFT5’);
theta05input(‘theta05’); dtheta5input(‘dtheta5’);
1x5length(x); nsec5ceil((1x2N)/(N2K)11;
x5[reshape(x,1,1x),zeros(1,N1(nsec21)*(N2K))2lx)];
nout5N; if (nargin 5),nout5M; else,opt5‘n’; end
X5zeros(nsec,nout);
for n51: nsec,
temp5w.*x((n21)*(N2K)11:(n21)*(N2K)1N);
if (opt(1) 55 ‘z’),temp5[temp,zeros(1,M2N)]; end
if (opt(1)55‘c’),temp5chirpf (temp,theta0,dtheta,M);
else,temp5fftshift(fft(temp)); end
X(n,: )5abs(temp).^2;
end
if(L1);
nsecL5floor(nsec/L);
for n51:nsecL,X(n,:)5mean (X((n21)*L11:n*L,:)); end
if (nsec55nsecL*L11),
X(nsecL11,:)5X(nsecL*L11,:); X5X(1:nsecL11),: );
elseif(nsec nsecL*L),
X(nsecL11,:)5mean(x(nsecL*L11:nsec,:));
X5X(1:nsecL11,:);
else,X5X(1:nsecL,:); end
end
LKh
16.43
 CANCELLATION OF ECHO PRODUCED ON
THE TELEPHONE-BASE BAND CHANNEL
Echo
Canceler
Echo
path
Estimated
sequence
Desired
sequence
Base band
transmit
filter
Fig. 16.44  Baseband Channel Echo Canceler
% Simulation program for baseband echo cancellation
shown in Fig. 16.44 using LMS algorithm
clc; close all; clear all;
format short
T5input(‘Enter the symbol interval’);
br5input(‘Enter the bit rate value’);
rf5input(‘Enter the roll off factor’);
n5[210 10];
y55000*rcosfir(rf,n,br,T); %Transmit filter pulse shape
is assumed as raised cosine
Matlab programs
Matlab programs
Matlab programs
Matlab programs
Matlab programs
Matlab programs
Matlab programs
Matlab programs
Matlab programs
Matlab programs

More Related Content

What's hot

Basic simulation lab manual1
Basic simulation lab manual1Basic simulation lab manual1
Basic simulation lab manual1Janardhana Raju M
 
Decimation and Interpolation
Decimation and InterpolationDecimation and Interpolation
Decimation and Interpolation
Fernando Ojeda
 
digital signal-processing-lab-manual
digital signal-processing-lab-manualdigital signal-processing-lab-manual
digital signal-processing-lab-manual
Ahmed Alshomi
 
EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf
EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf
EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf
Loren Schwappach
 
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions ManualApplied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
towojixi
 
Fourier series 1
Fourier series 1Fourier series 1
Fourier series 1
Faiza Saher
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Kurmendra Singh
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2bilawalali74
 
M.TECH, ECE 2nd SEM LAB RECORD
M.TECH, ECE 2nd SEM LAB RECORD M.TECH, ECE 2nd SEM LAB RECORD
M.TECH, ECE 2nd SEM LAB RECORD
Arif Ahmed
 
Matlab implementation of fast fourier transform
Matlab implementation of  fast fourier transformMatlab implementation of  fast fourier transform
Matlab implementation of fast fourier transform
Rakesh kumar jha
 
DFT and IDFT Matlab Code
DFT and IDFT Matlab CodeDFT and IDFT Matlab Code
DFT and IDFT Matlab Code
Bharti Airtel Ltd.
 
DSP lab manual
DSP lab manualDSP lab manual
DSP lab manual
tamil arasan
 
Dsp Lab Record
Dsp Lab RecordDsp Lab Record
Dsp Lab Record
Aleena Varghese
 
The method of comparing two audio files
The method of comparing two audio filesThe method of comparing two audio files
The method of comparing two audio files
Minh Anh Nguyen
 
Dsp U Lec04 Discrete Time Signals & Systems
Dsp U   Lec04 Discrete Time Signals & SystemsDsp U   Lec04 Discrete Time Signals & Systems
Dsp U Lec04 Discrete Time Signals & Systems
taha25
 
Effects of varying number of samples in an image
Effects of varying number of samples in an imageEffects of varying number of samples in an image
Effects of varying number of samples in an image
lakshmymk
 
fft using labview
fft using labviewfft using labview
fft using labview
kiranrockz
 

What's hot (20)

Basic simulation lab manual1
Basic simulation lab manual1Basic simulation lab manual1
Basic simulation lab manual1
 
Dsp manual print
Dsp manual printDsp manual print
Dsp manual print
 
Decimation and Interpolation
Decimation and InterpolationDecimation and Interpolation
Decimation and Interpolation
 
digital signal-processing-lab-manual
digital signal-processing-lab-manualdigital signal-processing-lab-manual
digital signal-processing-lab-manual
 
EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf
EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf
EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf
 
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions ManualApplied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
 
Fourier series 1
Fourier series 1Fourier series 1
Fourier series 1
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2
 
M.TECH, ECE 2nd SEM LAB RECORD
M.TECH, ECE 2nd SEM LAB RECORD M.TECH, ECE 2nd SEM LAB RECORD
M.TECH, ECE 2nd SEM LAB RECORD
 
Matlab implementation of fast fourier transform
Matlab implementation of  fast fourier transformMatlab implementation of  fast fourier transform
Matlab implementation of fast fourier transform
 
DSP Mat Lab
DSP Mat LabDSP Mat Lab
DSP Mat Lab
 
DFT and IDFT Matlab Code
DFT and IDFT Matlab CodeDFT and IDFT Matlab Code
DFT and IDFT Matlab Code
 
DSP lab manual
DSP lab manualDSP lab manual
DSP lab manual
 
Dsp lab pdf
Dsp lab pdfDsp lab pdf
Dsp lab pdf
 
Dsp Lab Record
Dsp Lab RecordDsp Lab Record
Dsp Lab Record
 
The method of comparing two audio files
The method of comparing two audio filesThe method of comparing two audio files
The method of comparing two audio files
 
Dsp U Lec04 Discrete Time Signals & Systems
Dsp U   Lec04 Discrete Time Signals & SystemsDsp U   Lec04 Discrete Time Signals & Systems
Dsp U Lec04 Discrete Time Signals & Systems
 
Effects of varying number of samples in an image
Effects of varying number of samples in an imageEffects of varying number of samples in an image
Effects of varying number of samples in an image
 
fft using labview
fft using labviewfft using labview
fft using labview
 

Viewers also liked

MATLAB Programs For Beginners. | Abhi Sharma
MATLAB Programs For Beginners. | Abhi SharmaMATLAB Programs For Beginners. | Abhi Sharma
MATLAB Programs For Beginners. | Abhi Sharma
Abee Sharma
 
Review of fourier series
Review of fourier seriesReview of fourier series
Review of fourier series
Max Powers
 
Matlab code for comparing two microphone files
Matlab code for comparing two microphone filesMatlab code for comparing two microphone files
Matlab code for comparing two microphone files
Minh Anh Nguyen
 
Fourier Specturm via MATLAB
Fourier Specturm via MATLABFourier Specturm via MATLAB
Fourier Specturm via MATLABZunAib Ali
 
Solvedproblems 120406031331-phpapp01
Solvedproblems 120406031331-phpapp01Solvedproblems 120406031331-phpapp01
Solvedproblems 120406031331-phpapp01
Rimple Mahey
 
Kernel Methods on Manifolds
Kernel Methods on ManifoldsKernel Methods on Manifolds
Kernel Methods on Manifolds
Sadeep Jayasumana
 
Factorization from Gaussian Elimination
  Factorization from Gaussian Elimination  Factorization from Gaussian Elimination
Factorization from Gaussian Elimination
Mudassir Parvi
 
DSP 05 _ Sheet Five
DSP 05 _ Sheet FiveDSP 05 _ Sheet Five
DSP 05 _ Sheet Five
Amr E. Mohamed
 
Gauss Elimination Method With Partial Pivoting
Gauss Elimination Method With Partial PivotingGauss Elimination Method With Partial Pivoting
Gauss Elimination Method With Partial Pivoting
SM. Aurnob
 
SE2_Lec 18_ Coding
SE2_Lec 18_ CodingSE2_Lec 18_ Coding
SE2_Lec 18_ Coding
Amr E. Mohamed
 
Manifold learning
Manifold learningManifold learning
Manifold learning
Wei Yang
 
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab sessionDr. Krishna Mohbey
 
numerical differentiation&integration
numerical differentiation&integrationnumerical differentiation&integration
numerical differentiation&integration8laddu8
 
How to work on Matlab.......
How to work on Matlab.......How to work on Matlab.......
How to work on Matlab.......biinoida
 
AEM Fourier series
 AEM Fourier series AEM Fourier series
AEM Fourier series
Siddhi Viradiya
 
Fourier series example
Fourier series exampleFourier series example
Fourier series exampleAbi finni
 
Manifold Learning
Manifold LearningManifold Learning
Manifold Learning
Phong Vo
 
Gauss y gauss jordan
Gauss y gauss jordanGauss y gauss jordan
Gauss y gauss jordanjonathann89
 
fourier series
fourier seriesfourier series
fourier series8laddu8
 
Manifold learning with application to object recognition
Manifold learning with application to object recognitionManifold learning with application to object recognition
Manifold learning with application to object recognitionzukun
 

Viewers also liked (20)

MATLAB Programs For Beginners. | Abhi Sharma
MATLAB Programs For Beginners. | Abhi SharmaMATLAB Programs For Beginners. | Abhi Sharma
MATLAB Programs For Beginners. | Abhi Sharma
 
Review of fourier series
Review of fourier seriesReview of fourier series
Review of fourier series
 
Matlab code for comparing two microphone files
Matlab code for comparing two microphone filesMatlab code for comparing two microphone files
Matlab code for comparing two microphone files
 
Fourier Specturm via MATLAB
Fourier Specturm via MATLABFourier Specturm via MATLAB
Fourier Specturm via MATLAB
 
Solvedproblems 120406031331-phpapp01
Solvedproblems 120406031331-phpapp01Solvedproblems 120406031331-phpapp01
Solvedproblems 120406031331-phpapp01
 
Kernel Methods on Manifolds
Kernel Methods on ManifoldsKernel Methods on Manifolds
Kernel Methods on Manifolds
 
Factorization from Gaussian Elimination
  Factorization from Gaussian Elimination  Factorization from Gaussian Elimination
Factorization from Gaussian Elimination
 
DSP 05 _ Sheet Five
DSP 05 _ Sheet FiveDSP 05 _ Sheet Five
DSP 05 _ Sheet Five
 
Gauss Elimination Method With Partial Pivoting
Gauss Elimination Method With Partial PivotingGauss Elimination Method With Partial Pivoting
Gauss Elimination Method With Partial Pivoting
 
SE2_Lec 18_ Coding
SE2_Lec 18_ CodingSE2_Lec 18_ Coding
SE2_Lec 18_ Coding
 
Manifold learning
Manifold learningManifold learning
Manifold learning
 
Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab session
 
numerical differentiation&integration
numerical differentiation&integrationnumerical differentiation&integration
numerical differentiation&integration
 
How to work on Matlab.......
How to work on Matlab.......How to work on Matlab.......
How to work on Matlab.......
 
AEM Fourier series
 AEM Fourier series AEM Fourier series
AEM Fourier series
 
Fourier series example
Fourier series exampleFourier series example
Fourier series example
 
Manifold Learning
Manifold LearningManifold Learning
Manifold Learning
 
Gauss y gauss jordan
Gauss y gauss jordanGauss y gauss jordan
Gauss y gauss jordan
 
fourier series
fourier seriesfourier series
fourier series
 
Manifold learning with application to object recognition
Manifold learning with application to object recognitionManifold learning with application to object recognition
Manifold learning with application to object recognition
 

Similar to Matlab programs

DSP_EXP.pptx
DSP_EXP.pptxDSP_EXP.pptx
DSP_EXP.pptx
Praveen156918
 
Dsp manual
Dsp manualDsp manual
Dsp manual
pramod naik
 
Dsp iit workshop
Dsp iit workshopDsp iit workshop
Dsp GA (1).pptx
Dsp GA (1).pptxDsp GA (1).pptx
Dsp GA (1).pptx
bhavanags5
 
DSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docxDSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docx
MUMAR57
 
Matlab fair-record-model
Matlab fair-record-modelMatlab fair-record-model
Matlab fair-record-modelajaydev1111
 
Dsp lab task1 ganesh
Dsp lab task1 ganeshDsp lab task1 ganesh
Dsp lab task1 ganesh
ChetanShahukari
 
Informe laboratorio n°1
Informe laboratorio n°1Informe laboratorio n°1
Informe laboratorio n°1
luisescobedo38
 
Modulation techniques matlab_code
Modulation techniques matlab_codeModulation techniques matlab_code
Modulation techniques matlab_code
Вахидреза Мохсени
 
BS LAB Manual (1).pdf
BS LAB Manual  (1).pdfBS LAB Manual  (1).pdf
BS LAB Manual (1).pdf
ssuser476810
 
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfIncorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
aartechindia
 
Digital Signal Processing
Digital Signal ProcessingDigital Signal Processing
Digital Signal Processing
aj ahmed
 
Dsp file
Dsp fileDsp file
Dsp file
Rakesh Thakur
 
Csci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingCsci101 lect06 advanced_looping
Csci101 lect06 advanced_looping
Elsayed Hemayed
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
KokilaK25
 
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and SystemsDSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
Amr E. Mohamed
 
Mcqmc talk
Mcqmc talkMcqmc talk
Dsp 1recordprophess-140720055832-phpapp01
Dsp 1recordprophess-140720055832-phpapp01Dsp 1recordprophess-140720055832-phpapp01
Dsp 1recordprophess-140720055832-phpapp01
Sagar Gore
 

Similar to Matlab programs (20)

DSP_EXP.pptx
DSP_EXP.pptxDSP_EXP.pptx
DSP_EXP.pptx
 
Mat lab
Mat labMat lab
Mat lab
 
Dsp manual
Dsp manualDsp manual
Dsp manual
 
Dsp iit workshop
Dsp iit workshopDsp iit workshop
Dsp iit workshop
 
Dsp GA (1).pptx
Dsp GA (1).pptxDsp GA (1).pptx
Dsp GA (1).pptx
 
DSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docxDSP LAB COMPLETE CODES.docx
DSP LAB COMPLETE CODES.docx
 
Matlab fair-record-model
Matlab fair-record-modelMatlab fair-record-model
Matlab fair-record-model
 
Dsp lab task1 ganesh
Dsp lab task1 ganeshDsp lab task1 ganesh
Dsp lab task1 ganesh
 
Informe laboratorio n°1
Informe laboratorio n°1Informe laboratorio n°1
Informe laboratorio n°1
 
Modulation techniques matlab_code
Modulation techniques matlab_codeModulation techniques matlab_code
Modulation techniques matlab_code
 
BS LAB Manual (1).pdf
BS LAB Manual  (1).pdfBS LAB Manual  (1).pdf
BS LAB Manual (1).pdf
 
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdfIncorporate the SOR method in the multigridTest-m and apply the multig.pdf
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf
 
Digital Signal Processing
Digital Signal ProcessingDigital Signal Processing
Digital Signal Processing
 
Dsp file
Dsp fileDsp file
Dsp file
 
Csci101 lect06 advanced_looping
Csci101 lect06 advanced_loopingCsci101 lect06 advanced_looping
Csci101 lect06 advanced_looping
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and SystemsDSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
 
Mcqmc talk
Mcqmc talkMcqmc talk
Mcqmc talk
 
Adaptive signal processing simon haykins
Adaptive signal processing simon haykinsAdaptive signal processing simon haykins
Adaptive signal processing simon haykins
 
Dsp 1recordprophess-140720055832-phpapp01
Dsp 1recordprophess-140720055832-phpapp01Dsp 1recordprophess-140720055832-phpapp01
Dsp 1recordprophess-140720055832-phpapp01
 

Recently uploaded

Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 

Recently uploaded (20)

Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 

Matlab programs

  • 1. MATLAB Programs Chapter 16 16.1   INTRODUCTION MATLAB stands for MATrix LABoratory. It is a technical computing environment for high performance numeric computation and visualisation. It integrates numerical analysis, matrix computation, signal processing and graphics in an easy-to-use environment, where problems and solutions are expressed just as they are written mathematically, without traditional programming. MATLAB allows us to express the entire algorithm in a few dozen lines, to compute the solution with great accuracy in a few minutes on a computer, and to readily manipulate a three-dimensional display of the result in colour. MATLAB is an interactive system whose basic data element is a matrix that does not require dimensioning. It enables us to solve many numerical problems in a fraction of the time that it would take to write a program and execute in a language such as FORTRAN, BASIC, or C. It also features a family of application specific solutions, called toolboxes. Areas in which toolboxes are available include signal processing, image processing, control systems design, dynamic systems simulation, systems identification, neural networks, wavelength communication and others. It can handle linear, non-linear, continuous-time, discrete-time, multivariable and multirate systems. This chapter gives simple programs to solve specific problems that are included in the previous chapters. All these MATLAB programs have been tested under version 7.1 of MATLAB and version 6.12 of the signal processing toolbox. 16.2   REPRESENTATION OF BASIC SIGNALS MATLAB programs for the generation of unit impulse, unit step, ramp, exponential, sinusoidal and cosine sequences are as follows. % Program for the generation of unit impulse signal clc;clear all;close all; t522:1:2; y5[zeros(1,2),ones(1,1),zeros(1,2)];subplot(2,2,1);stem(t,y);
  • 2. 816   Digital Signal Processing ylabel(‘Amplitude --.’); xlabel(‘(a) n --.’); % Program for the generation of unit step sequence [u(n)2 u(n 2 N] n5input(‘enter the N value’); t50:1:n21; y15ones(1,n);subplot(2,2,2); stem(t,y1);ylabel(‘Amplitude --.’); xlabel(‘(b) n --.’); % Program for the generation of ramp sequence n15input(‘enter the length of ramp sequence’); t50:n1; subplot(2,2,3);stem(t,t);ylabel(‘Amplitude --.’); xlabel(‘(c) n --.’); % Program for the generation of exponential sequence n25input(‘enter the length of exponential sequence’); t50:n2; a5input(‘Enter the ‘a’ value’); y25exp(a*t);subplot(2,2,4); stem(t,y2);ylabel(‘Amplitude --.’); xlabel(‘(d) n --.’); % Program for the generation of sine sequence t50:.01:pi; y5sin(2*pi*t);figure(2); subplot(2,1,1);plot(t,y);ylabel(‘Amplitude --.’); xlabel(‘(a) n --.’); % Program for the generation of cosine sequence t50:.01:pi; y5cos(2*pi*t); subplot(2,1,2);plot(t,y);ylabel(‘Amplitude --.’); xlabel(‘(b) n --.’); As an example, enter the N value 7 enter the length of ramp sequence 7 enter the length of exponential sequence 7 enter the a value 1 Using the above MATLAB programs, we can obtain the waveforms of the unit impulse signal, unit step signal, ramp signal, exponential signal, sine wave signal and cosine wave signal as shown in Fig. 16.1.
  • 3. MATLAB Programs  817 Fig. 16.1  Representation of Basic Signals (a) Unit Impulse Signal (b) Unit-step Signal (c) Ramp Signal (d) Exponential Signal (e) Sinewave Signal ( f )Cosine Wave Signal −2 −1 0 1 2 0 0 2 2 4 4 6 6 8 nn nn 0.2 0.2 1 0.2 0.4 0.4 2 0.4 0.6 0.6 3 4 5 6 7 0.6 0.8 0.8 0.8 1 1 1 0 00 0 2 4 6 8 0 (a) Amplitude Amplitude AmplitudeAmplitude (b) (d)(c) 1 1 0.5 0.5 0.5 0.5 0 0 1 1 1.5 (e) (f) 1.5 2.5 2.5 3 3 3.5 3.5 2 2 0 0 −0.5 −0.5 −1 −1 n n AmplitudeAmplitude
  • 4. 818   Digital Signal Processing 16.3   DISCRETE CONVOLUTION 16.3.1  Linear Convolution Algorithm 1. Get two signals x(m)and h(p)in matrix form 2. The convolved signal is denoted as y(n) 3. y(n)is given by the formula y(n) 5 [ ( ) ( )]x k h n k k − =−∞ ∞ ∑   where n50 to m 1 p 2 1 4. Stop % Program for linear convolution of the sequence x5[1, 2] and h5[1, 2, 4] clc; clear all; close all; x5input(‘enter the 1st sequence’); h5input(‘enter the 2nd sequence’); y5conv(x,h); figure;subplot(3,1,1); stem(x);ylabel(‘Amplitude --.’); xlabel(‘(a) n --.’); subplot(3,1,2); stem(h);ylabel(‘Amplitude --.’); xlabel(‘(b) n --.’); subplot(3,1,3); stem(y);ylabel(‘Amplitude --.’); xlabel(‘(c) n --.’); disp(‘The resultant signal is’);y As an example, enter the 1st sequence [1 2] enter the 2nd sequence [1 2 4] The resultant signal is y51 4 8 8 Figure 16.2 shows the discrete input signals x(n)and h(n)and the convolved output signal y(n). Fig. 16.2  (Contd.) AmplitudeAmplitude n n 0 0 0.5 1 1 1 1.1 1.2 1.2 1.4 1.3 1.6 1.4 1.8 1.5 2 (a) (b) 1.6 2.2 1.7 2.4 1.8 2.6 1.9 2.8 2 3 1 2 1.5 3 2 4
  • 5. MATLAB Programs  819 16.3.2  Circular Convolution % Program for Computing Circular Convolution clc; clear; a = input(‘enter the sequence x(n) = ’); b = input(‘enter the sequence h(n) = ’); n1=length(a); n2=length(b); N=max(n1,n2); x = [a zeros(1,(N-n1))]; for i = 1:N k = i; for j = 1:n2 H(i,j)=x(k)* b(j); k = k-1; if (k == 0) k = N; end end end y=zeros(1,N); M=H’; for j = 1:N for i = 1:n2 y(j)=M(i,j)+y(j); end end disp(‘The output sequence is y(n)= ‘); disp(y); Fig. 16.2  Discrete Linear Convolution AmplitudeAmplitudeAmplitude n n n 0 0 0 0.5 1 2 1 1 1 1.1 1.2 1.2 1.4 1.5 1.3 1.6 1.4 1.8 1.5 2 2 (a) (b) (c) 1.6 2.2 2.5 1.7 2.4 3 1.8 2.6 3.5 1.9 2.8 2 3 4 1 2 4 1.5 3 6 2 4 8 AmplitudeAmplitudeAmplitude n n n 0 0 0 0.5 1 2 1 1 1 1.1 1.2 1.2 1.4 1.5 1.3 1.6 1.4 1.8 1.5 2 2 (a) (b) (c) 1.6 2.2 2.5 1.7 2.4 3 1.8 2.6 3.5 1.9 2.8 2 3 4 1 2 4 1.5 3 6 2 4 8
  • 6. 820   Digital Signal Processing stem(y); title(‘Circular Convolution’); xlabel(‘n’); ylabel(‚y(n)‘); As an Example, enter the sequence x(n) = [1 2 4] enter the sequence h(n) = [1 2] The output sequence is y(n)= 9 4 8 % Program for Computing Circular Convolution with zero padding clc; close all; clear all; g5input(‘enter the first sequence’); h5input(‘enter the 2nd sequence’); N15length(g); N25length(h); N5max(N1,N2); N35N12N2; %Loop for getting equal length sequence if(N350) h5[h,zeros(1,N3)]; else g5[g,zeros(1,2N3)]; end %computation of circular convolved sequence for n51:N, y(n)50; for i51:N, j5n2i11; if(j550) j5N1j; end y(n)5y(n)1g(i)*h(j); end end disp(‘The resultant signal is’);y As an example, enter the first sequence [1 2 4] enter the 2nd sequence [1 2] The resultant signal is y51 4 8 8 16.3.3  Overlap Save Method and Overlap Add method % Program for computing Block Convolution using Overlap Save Method Overlap Save Method x=input(‘Enter the sequence x(n) = ’);
  • 7. MATLAB Programs  821 h=input(‘Enter the sequence h(n) = ’); n1=length(x); n2=length(h); N=n1+n2-1; h1=[h zeros(1,N-n1)]; n3=length(h1); y=zeros(1,N); x1=[zeros(1,n3-n2) x zeros(1,n3)]; H=fft(h1); for i=1:n2:N y1=x1(i:i+(2*(n3-n2))); y2=fft(y1); y3=y2.*H; y4=round(ifft(y3)); y(i:(i+n3-n2))=y4(n2:n3); end disp(‘The output sequence y(n)=’); disp(y(1:N)); stem(y(1:N)); title(‘Overlap Save Method’); xlabel(‘n’); ylabel(‘y(n)’); Enter the sequence x(n) = [1 2 -1 2 3 -2 -3 -1 1 1 2 -1] Enter the sequence h(n) = [1 2 3 -1] The output sequence y(n) = 1 4 6 5 2 11 0 -16 -8 3 8 5 3 -5 1 %Program for computing Block Convolution using Overlap Add Method x=input(‘Enter the sequence x(n) = ’); h=input(‘Enter the sequence h(n) = ’); n1=length(x); n2=length(h); N=n1+n2-1; y=zeros(1,N); h1=[h zeros(1,n2-1)]; n3=length(h1); y=zeros(1,N+n3-n2); H=fft(h1); for i=1:n2:n1 if i<=(n1+n2-1) x1=[x(i:i+n3-n2) zeros(1,n3-n2)]; else x1=[x(i:n1) zeros(1,n3-n2)]; end x2=fft(x1); x3=x2.*H; x4=round(ifft(x3)); if (i==1)
  • 8. 822   Digital Signal Processing y(1:n3)=x4(1:n3); else y(i:i+n3-1)=y(i:i+n3-1)+x4(1:n3); end end disp(‘The output sequence y(n)=’); disp(y(1:N)); stem((y(1:N)); title(‘Overlap Add Method’); xlabel(‘n’); ylabel(‘y(n)’); As an Example, Enter the sequence x(n) = [1 2 -1 2 3 -2 -3 -1 1 1 2 -1] Enter the sequence h(n) = [1 2 3 -1] The output sequence y(n) = 1 4 6 5 2 11 0 -16 -8 3 8 5 3 -5 1 16.4   DISCRETE CORRELATION 16.4.1  Crosscorrelation Algorithm 1. Get two signals x(m)and h(p)in matrix form 2. The correlated signal is denoted as y(n) 3. y(n)is given by the formula y(n) 5 [ ( ) ( )]x k h k n k − =−∞ ∞ ∑ where n52 [max (m, p)2 1] to [max (m, p)2 1] 4. Stop % Program for computing cross-correlation of the sequences x5[1, 2, 3, 4] and h5[4, 3, 2, 1] clc; clear all; close all; x5input(‘enter the 1st sequence’); h5input(‘enter the 2nd sequence’); y5xcorr(x,h); figure;subplot(3,1,1); stem(x);ylabel(‘Amplitude --.’); xlabel(‘(a) n --.’); subplot(3,1,2); stem(h);ylabel(‘Amplitude --.’); xlabel(‘(b) n --.’); subplot(3,1,3); stem(fliplr(y));ylabel(‘Amplitude --.’);
  • 9. MATLAB Programs  823 xlabel(‘(c) n --.’); disp(‘The resultant signal is’);fliplr(y) As an example, enter the 1st sequence [1 2 3 4] enter the 2nd sequence [4 3 2 1] The resultant signal is y51.0000 4.0000 10.0000 20. ↑ 0000 25.0000 24.0000 16.0000 Figure 16.3 shows the discrete input signals x(n)and h(n)and the cross-correlated output signal y(n). AmplitudeAmplitudeAmplitude n n n 0 0 1 1 1 1 1 1.5 1.5 2 2 2 3 3 3 5 2.5 2.5 4 (a) (b) (c) 3.5 3.5 6 4 4 7 2 2 3 3 4 4 30 20 10 0 Fig. 16.3  Discrete Cross-correlation 16.4.2  Autocorrelation Algorithm 1. Get the signal x(n)of length N in matrix form 2. The correlated signal is denoted as y(n) 3. y(n)is given by the formula y(n) 5 [ ( ) ( )]x k x k n k − =−∞ ∞ ∑ where n52(N 2 1) to (N 2 1)
  • 10. 824   Digital Signal Processing % Program for computing autocorrelation function x5input(‘enter the sequence’); y5xcorr(x,x); figure;subplot(2,1,1); stem(x);ylabel(‘Amplitude --.’); xlabel(‘(a) n --.’); subplot(2,1,2); stem(fliplr(y));ylabel(‘Amplitude --.’); xlabel(‘(a) n --.’); disp(‘The resultant signal is’);fliplr(y) As an example, enter the sequence [1 2 3 4] The resultant signal is y54 11 20 ↑ 30 20 11 4 Figure 16.4 shows the discrete input signal x(n)and its auto-correlated output signal y(n). ( ) AmplitudeAmplitude n n 0 1 1 1 1.5 2 2 3 3 (a) 5 2.5 4 (b) y (n) 3.5 6 4 7 2 3 4 0 5 10 15 20 25 30 Fig. 16.4  Discrete Auto-correlation 16.5   STABILITY TEST % Program for stability test clc;clear all;close all; b5input(‘enter the denominator coefficients of the ­filter’); k5poly2rc(b); knew5fliplr(k); s5all(abs(knew)1); if(s55 1) disp(‘“Stable system”’);
  • 11. MATLAB Programs  825 else disp(‘“Non-stable system”’); end As an example, enter the denominator coefficients of the filter [1 21 .5] “Stable system” 16.6   SAMPLING THEOREM The sampling theorem can be understood well with the following example. Example 16.1  Frequency analysis of the amplitude modulated discrete-time signal x(n)5cos 2 pf1 n 1 cos 2pf2 n where f1 1 128 =  and f2 5 128 =  modulates the amplitude-modulated signal is xc (n)5cos 2p fc n where fc 550/128. The resulting amplitude-modulated signal is xam (n)5x(n) cos 2p fc n Using MATLAB program, (a)  sketch the signals x(n), xc (n) and xam (n), 0 # n # 255 (b)  compute and sketch the 128-point DFT of the signal xam (n), 0 # n # 127 (c)  compute and sketch the 128-point DFT of the signal xam (n), 0 # n # 99 Solution % Program Solution for Section (a) clc;close all;clear all; f151/128;f255/128;n50:255;fc550/128; x5cos(2*pi*f1*n)1cos(2*pi*f2*n); xa5cos(2*pi*fc*n); xamp5x.*xa; subplot(2,2,1);plot(n,x);title(‘x(n)’); xlabel(‘n --.’);ylabel(‘amplitude’); subplot(2,2,2);plot(n,xc);title(‘xa(n)’); xlabel(‘n --.’);ylabel(‘amplitude’); subplot(2,2,3);plot(n,xamp); xlabel(‘n --.’);ylabel(‘amplitude’); %128 point DFT computation2solution for Section (b) n50:127;figure;n15128; f151/128;f255/128;fc550/128; x5cos(2*pi*f1*n)1cos(2*pi*f2*n); xc5cos(2*pi*fc*n); xa5cos(2*pi*fc*n); (Contd.)
  • 12. 826   Digital Signal Processing −2 −1 0 1 2 Amplitude 0 100 200 300 (iii) n Fig. 16.5(a)  (iii) Amplitude Modulated Signal Fig. 16.5(a)  (ii) Carrier Signal and −1 −0.5 0 0.5 1 Amplitude 0 100 200 300 (ii) n 0 −2 −1 0 1 2 100 Amplitude 200 (i) 300 n Fig. 16.5(a)  (i) Modulating Signal x (n) (Contd.)
  • 13. MATLAB Programs  827 20 Amplitude 40 60 80 100 120 n 1400 −10 −5 0 5 10 15 20 25 Fig. 16.5(b)  128-point DFT of the Signal xam (n), 0 # n # 127 20 40 60 80 100 120 140 n 0 −5 0 5 10 15 20 25 30 35 Amplitude Fig. 16.5(c)  128-point DFT of the Signal xam (n), 0 # n # 99
  • 14. 828   Digital Signal Processing xamp5x.*xa;xam5fft(xamp,n1); stem(n,xam);title(‘xamp(n)’);xlabel(‘n --.’); ylabel(‘amplitude’); %128 point DFT computation2solution for Section (c) n50:99;figure;n250:n121; f151/128;f255/128;fc550/128; x5cos(2*pi*f1*n)1cos(2*pi*f2*n); xc5cos(2*pi*fc*n); xa5cos(2*pi*fc*n); xamp5x.*xa; for i51:100, xamp1(i)5xamp(i); end xam5fft(xamp1,n1); s t e m ( n 2 , x a m ) ; t i t l e ( ‘ x a m p ( n ) ’ ) ; x l a b e l ( ‘ n --.’);ylabel(‘amplitude’); (a)Modulated signal x(n), carrier signal xa (n) and amplitude modulated signal xam (n) are shown in Fig. 16.5(a). Fig. 16.5 (b) shows the 128-point DFT of the signal xam (n) for 0 # n # 127 and Fig. 16.5 (c) shows the 128-point DFT of the signal xam (n), 0 # n # 99. 16.7   FAST FOURIER TRANSFORM Algorithm 1. Get the signal x(n)of length N in matrix form 2. Get the N value 3. The transformed signal is denoted as x k x n e k N j N nk n N ( ) ( ) for= ≤ ≤ − − = − ∑ 2 0 1 0 1 p % Program for computing discrete Fourier transform clc;close all;clear all; x5input(‘enter the sequence’); n5input(‘enter the length of fft’); X(k)5fft(x,n); stem(y);ylabel(‘Imaginary axis --.’); xlabel(‘Real axis --.’); X(k) As an example, enter the sequence [0 1 2 3 4 5 6 7] enter the length of fft 8 X(k)5 Columns 1 through 4 28.0000 24.000019.6569i 24.0000 14.0000i 24.0000 1 1.6569i Columns 5 through 8 24.0000 24.0000 21.6569i 24.0000 24.0000i 24.0000 29.6569i
  • 15. MATLAB Programs  829 The eight-point decimation-in-time fast Fourier transform of the sequence x(n)is computed using MATLAB program and the resultant output is plotted in Fig. 16.6. 0−5 −10 −8 −6 −4 −2 0 2 4 6 8 10 5 10 15 20 25 30 Real axis Imaginaryaxis Fig. 16.6  Fast Fourier Transform 16.8   BUTTERWORTH ANALOG FILTERS 16.8.1  Low-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.46 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Butterworth analog low pass filter clc; close all;clear all; format long rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); wp5input(‘enter the passband freq’); ws5input(‘enter the stopband freq’); fs5input(‘enter the sampling freq’);
  • 16. 830   Digital Signal Processing w152*wp/fs;w252*ws/fs; [n,wn]5buttord(w1,w2,rp,rs,’s’); [z,p,k]5butter(n,wn); [b,a]5zp2tf(z,p,k); [b,a]5butter(n,wn,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple 0.15 enter the stopband ripple 60 enter the passband freq 1500 enter the stopband freq 3000 enter the stopband freq 7000 The amplitude and phase responses of the Butterworth low-pass analog filter are shown in Fig. 16.7. Fig. 16.7  Butterworth Low-pass Analog Filter (a) Amplitude Response and (b) Phase Response 0.1 0.1 −250 −200 −150 −4 −2 2 4 0 −100 −50 50 GainindB 0 0.2 0.2 0.3 0.3 0.4 0.4 (a) (b) Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 Phaseinradians 0.1 0.1 −250 −200 −150 −4 −2 2 4 0 −100 −50 50 GainindB 0 0.2 0.2 0.3 0.3 0.4 0.4 (a) (b) Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 Phaseinradians
  • 17. MATLAB Programs  831 16.8.2  High-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.46 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Butterworth analog high—pass filter clc; close all;clear all; format long rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); wp5input(‘enter the passband freq’); ws5input(‘enter the stopband freq’); fs5input(‘enter the sampling freq’); w152*wp/fs;w252*ws/fs; [n,wn]5buttord(w1,w2,rp,rs,’s’); [b,a]5butter(n,wn,’high’,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple 0.2 enter the stopband ripple 40 enter the passband freq 2000 enter the stopband freq 3500 enter the sampling freq 8000 The amplitude and phase responses of Butterworth high-pass analog filter are shown in Fig. 16.8.
  • 18. 832   Digital Signal Processing 16.8.3  Bandpass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.46 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Butterworth analog Bandpass filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; Fig. 16.8  Butterworth High-pass Analog Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −400 −4 4 −2 2 0 −300 −200 −100 0 100 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −400 −4 4 −2 2 0 −300 −200 −100 0 100 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 19. MATLAB Programs  833 [n]5buttord(w1,w2,rp,rs); wn5[w1 w2]; [b,a]5butter(n,wn,’bandpass’,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.36 enter the stopband ripple... 36 enter the passband freq... 1500 enter the stopband freq... 2000 enter the sampling freq... 6000 The amplitude and phase responses of Butterworth bandpass analog filter are shown in Fig. 16.9. Fig. 16.9  Butterworth Bandpass Analog Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −1000 −4 4 −2 2 0 −800 −600 −200 −400 0 200 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 20. 834   Digital Signal Processing 16.8.4  Bandstop Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.46 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Butterworth analog Bandstop filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n]5buttord(w1,w2,rp,rs,’s’); wn5[w1 w2]; [b,a]5butter(n,wn,’stop’,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.28 enter the stopband ripple... 28 enter the passband freq... 1000 enter the stopband freq... 1400 enter the sampling freq... 5000 The amplitude and phase responses of Butterworth bandstop analog filter are shown in Fig. 16.10.
  • 21. MATLAB Programs  835 16.9   CHEBYSHEV TYPE-1 ANALOG FILTERS 16.9.1  Low-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.57 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-1 low-pass filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −150 −50 −100 −200 0 50 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 Fig. 16.10  Butterworth Bandstop Analog Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −150 −50 −100 −200 0 50 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 22. 836   Digital Signal Processing w152*wp/fs;w252*ws/fs; [n,wn]5cheb1ord(w1,w2,rp,rs,’s’); [b,a]5cheby1(n,rp,wn,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.23 enter the stopband ripple... 47 enter the passband freq... 1300 enter the stopband freq... 1550 enter the sampling freq... 7800 The amplitude and phase responses of Chebyshev type - 1 low-pass analog filter are shown in Fig. 16.11. GainindBPhaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −80 −40 −20 −60 −100 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 Fig. 16.11  Chebyshev Type-I Low-pass Analog Filter (a) Amplitude Response and (b) Phase Response
  • 23. MATLAB Programs  837 16.9.2  High-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.57 5. Find the filter coefficients 6. Draw the magnitude and phase responses. %Program for the design of Chebyshev Type-1 high-pass filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n,wn]5cheb1ord(w1,w2,rp,rs,’s’); [b,a]5cheby1(n,rp,wn,’high’,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); Fig. 16.12  Chebyshev Type - 1 High-pass Analog Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −100 −50 −150 −200 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −100 −50 −150 −200 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 24. 838   Digital Signal Processing ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.29 enter the stopband ripple... 29 enter the passband freq... 900 enter the stopband freq... 1300 enter the sampling freq... 7500 The amplitude and phase responses of Chebyshev type - 1 high-pass analog filter are shown in Fig. 16.12. 16.9.3  Bandpass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.57 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-1 Bandpass filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n]5cheb1ord(w1,w2,rp,rs,’s’); wn5[w1 w2]; [b,a]5cheby1(n,rp,wn,’bandpass’,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.3 enter the stopband ripple... 40 enter the passband freq... 1400
  • 25. MATLAB Programs  839 enter the stopband freq... 2000 enter the sampling freq... 5000 The amplitude and phase responses of Chebyshev type - 1 bandpass analog filter are shown in Fig. 16.13. 16.9.4  Bandstop Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequency 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.57 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-1 Bandstop filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); Fig. 16.13  Chebyshev Type-1 Bandpass Analog Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −3 3 −2 −1 1 2 0 −200 −100 −300 −400 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −3 3 −2 −1 1 2 0 −200 −100 −300 −400 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 26. 840   Digital Signal Processing fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n]5cheb1ord(w1,w2,rp,rs,’s’); wn5[w1 w2]; [b,a]5cheby1(n,rp,wn,’stop’,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.15 enter the stopband ripple... 30 enter the passband freq... 2000 enter the stopband freq... 2400 enter the sampling freq... 7000 The amplitude and phase responses of Chebyshev type - 1 bandstop analog filter are shown in Fig. 16.14. Fig. 16.14  Chebyshev Type - 1 Bandstop Analog Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −150 −50 −100 −200 −250 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −150 −50 −100 −200 −250 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 27. MATLAB Programs  841 16.10   CHEBYSHEV TYPE-2 ANALOG FILTERS 16.10.1  Low-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.67 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-2 low pass analog filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n,wn]5cheb2ord(w1,w2,rp,rs,’s’); [b,a]5cheby2(n,rs,wn,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.4 enter the stopband ripple... 50 enter the passband freq... 2000 enter the stopband freq... 2400 enter the sampling freq... 10000 The amplitude and phase responses of Chebyshev type - 2 low-pass analog filter are shown in Fig. 16.15.
  • 28. 842   Digital Signal Processing Fig. 16.15  Chebyshev Type - 2 Low-pass Analog Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −60 −20 −40 −80 −100 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 16.10.2  High-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.67 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-2 High pass analog filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n,wn]5cheb2ord(w1,w2,rp,rs,’s’); [b,a]5cheby2(n,rs,wn,’high’,’s’); w50:.01:pi;
  • 29. MATLAB Programs  843 [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.34 enter the stopband ripple... 34 enter the passband freq... 1400 enter the stopband freq... 1600 enter the sampling freq... 10000 The amplitude and phase responses of Chebyshev type - 2 high-pass analog filter are shown in Fig. 16.16. GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −60 −20 −40 −80 0 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 Fig. 16.16  Chebyshev Type - 2 High-pass Analog Filter (a) Amplitude Response and (b) Phase Response 16.10.3  Bandpass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies
  • 30. 844   Digital Signal Processing 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.67 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-2 Bandpass analog filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n]5cheb2ord(w1,w2,rp,rs,’s’); wn5[w1 w2]; [b,a]5cheby2(n,rs,wn,’bandpass’,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.37 enter the stopband ripple... 37 enter the passband freq... 3000 enter the stopband freq... 4000 enter the sampling freq... 9000 The amplitude and phase responses of Chebyshev type - 2 bandpass analog filter are shown in Fig. 16.17. Fig. 16.17  (Contd.) GainindB aseinradians (a) 0.1 4 −2 2 0 −80 −60 −20 0 −40 −100 20 0.2 0.3 0.4 Normalised frequency 0.5 0.6 0.7 0.8 0.9 10
  • 31. MATLAB Programs  845 16.10.4   Bandstop Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.67 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-2 Bandstop analog filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n]5cheb2ord(w1,w2,rp,rs,’s’); wn5[w1 w2]; [b,a]5cheby2(n,rs,wn,’stop’,’s’); w50:.01:pi; [h,om]5freqs(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); Fig. 16.17  Chebyshev Type - 2 Bandstop Analog Filter (a) Amplitude Response and (b) Phase Response Gainind Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −80 −60 −40 −100 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 32. 846   Digital Signal Processing As an example, enter the passband ripple... 0.25 enter the stopband ripple... 30 enter the passband freq... 1300 enter the stopband freq... 2000 enter the sampling freq... 8000 The amplitude and phase responses of Chebyshev type - 2 bandstop analog filter are shown in Fig. 16.18. Fig. 16.18  Chebyshev Type - 2 Bandstop Analog Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −60 −40 0 20 −20 −80 40 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 16.11   BUTTERWORTH DIGITAL IIR FILTERS 16.11.1 Low-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.46 5. Find the filter coefficients 6. Draw the magnitude and phase responses.
  • 33. MATLAB Programs  847 % Program for the design of Butterworth low pass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); wp5input(‘enter the passband freq’); ws5input(‘enter the stopband freq’); fs5input(‘enter the sampling freq’); w152*wp/fs;w252*ws/fs; [n,wn]5buttord(w1,w2,rp,rs); [b,a]5butter(n,wn); w50:.01:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple 0.5 enter the stopband ripple 50 enter the passband freq 1200 enter the stopband freq 2400 enter the sampling freq 10000 The amplitude and phase responses of Butterworth low-pass digital filter are shown in Fig. 16.19. GainindB Phaseinradians (a) 0.1 0.1 −4 4 −2 2 0 −300 −200 0 −100 −400 100 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 Fig. 16.19  (Contd.)
  • 34. 848   Digital Signal Processing 16.11.2  High-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.46 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Butterworth highpass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); wp5input(‘enter the passband freq’); ws5input(‘enter the stopband freq’); fs5input(‘enter the sampling freq’); w152*wp/fs;w252*ws/fs; [n,wn]5buttord(w1,w2,rp,rs); [b,a]5butter(n,wn,’high’); w50:.01:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple 0.5 enter the stopband ripple 50 enter the passband freq 1200 Fig. 16.19  Butterworth Low-pass Digital Filter (a) Amplitude Response and (b) Phase Response Gain Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −300 −200 −400 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 35. MATLAB Programs  849 enter the stopband freq 2400 enter the sampling freq 10000 The amplitude and phase responses of Butterworth high-pass digital filter are shown in Fig. 16.20. 16.11.3 Band-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.46 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Butterworth Bandpass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); wp5input(‘enter the passband freq’); Fig. 16.20  Butterworth High-pass Digital Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −250 −200 −150 0 −100 −50 −300 50 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 −250 −200 −150 0 −100 −50 −300 50 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 36. 850   Digital Signal Processing ws5input(‘enter the stopband freq’); fs5input(‘enter the sampling freq’); w152*wp/fs;w252*ws/fs; [n]5buttord(w1,w2,rp,rs); wn5[w1 w2]; [b,a]5butter(n,wn,’bandpass’); w50:.01:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple 0.3 enter the stopband ripple 40 enter the passband freq 1500 enter the stopband freq 2000 enter the sampling freq 9000 The amplitude and phase responses of Butterworth band-pass digital filter are shown in Fig. 16.21. Fig. 16.21  Butterworth Bandstop Digital Filter (a) Amplitude Response and (b) Phase Response GainindB (a) (b) Phaseinradians 0.1 0.1 0 − 200 − 100 − 400 − 500 − 600 − 700 4 2 0 − 2 − 4 − 300 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 37. MATLAB Programs  851 16.11.4  Bandstop Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.46 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Butterworth Band stop digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); wp5input(‘enter the passband freq’); ws5input(‘enter the stopband freq’); fs5input(‘enter the sampling freq’); w152*wp/fs;w252*ws/fs; [n]5buttord(w1,w2,rp,rs); wn5[w1 w2]; [b,a]5butter(n,wn,’stop’); w50:.01:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple 0.4 enter the stopband ripple 46 enter the passband freq 1100 enter the stopband freq 2200 enter the sampling freq 6000 The amplitude and phase responses of the Butterworth bandstop digital filter are shown in Fig. 16.22.
  • 38. 852   Digital Signal Processing Fig. 16.22  Butterworth Bandstop Digital Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 100 0 −200 −100 −400 −300 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 100 0 −200 −100 −400 −300 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 16.12   CHEBYSHEV TYPE-1 DIGITAL FILTERS 16.12.1  Low-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.57 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-1 lowpass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’);
  • 39. MATLAB Programs  853 w152*wp/fs;w252*ws/fs; [n,wn]5cheb1ord(w1,w2,rp,rs); [b,a]5cheby1(n,rp,wn); w50:.01:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised ­frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.2 enter the stopband ripple... 45 enter the passband freq... 1300 enter the stopband freq... 1500 enter the sampling freq... 10000 The amplitude and phase responses of Chebyshev type - 1 low-pass digital filter are shown in Fig. 16.23. Fig. 16.23  Chebyshev Type - 1 Low-pass Digital Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 0 −200 −100 −500 −400 −300 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 40. 854   Digital Signal Processing 16.12.2  High-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.57 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-1 highpass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n,wn]5cheb1ord(w1,w2,rp,rs); [b,a]5cheby1(n,rp,wn,’high’); w50:.01/pi:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.3 enter the stopband ripple... 60 enter the passband freq... 1500 enter the stopband freq... 2000 enter the sampling freq... 9000 The amplitude and phase responses of Chebyshev type - 1 high-pass digital filter are shown in Fig. 16.24.
  • 41. MATLAB Programs  855 16.12.3  Bandpass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.57 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-1 Bandpass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; Fig. 16.24  Chebyshev Type - 1 High-pass Digital Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 0 −250 −200 −150 −100 −50 −350 −300 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 0 −250 −200 −150 −100 −50 −350 −300 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 42. 856   Digital Signal Processing [n]5cheb1ord(w1,w2,rp,rs); wn5[w1 w2]; [b,a]5cheby1(n,rp,wn,’bandpass’); w50:.01:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.4 enter the stopband ripple... 35 enter the passband freq... 2000 enter the stopband freq... 2500 enter the sampling freq... 10000 The amplitude and phase responses of Chebyshev type - 1 bandpass digital filter are shown in Fig. 16.25. GainindB Phaseinradians (a) (b) 0.1 0.1 −4 4 −2 2 0 0 −300 −200 −100 −500 −400 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 Fig. 16.25  Chebyshev Type - 1 Bandpass Digital Filter (a) Amplitude Response and (b) Phase Response
  • 43. MATLAB Programs  857 16.12.4  Bandstop Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.57 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-1 Bandstop digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n]5cheb1ord(w1,w2,rp,rs); wn5[w1 w2]; [b,a]5cheby1(n,rp,wn,’stop’); w50:.1/pi:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.25 enter the stopband ripple... 40 enter the passband freq... 2500 enter the stopband freq... 2750 enter the sampling freq... 7000 The amplitude and phase responses of Chebyshev type - 1 bandstop digital filter are shown in Fig. 16.26.
  • 44. 858   Digital Signal Processing Fig. 16.26  Chebyshev Type - 1 Bandstop Digital Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −3 −2 −1 1 2 3 4 0 0 − 200 − 150 − 100 −50 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −3 −2 −1 1 2 3 4 0 0 − 200 − 150 − 100 −50 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 16.13   CHEBYSHEV TYPE-2 DIGITAL FILTERS 16.13.1  Low-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.67 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-2 lowpass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n,wn]5cheb2ord(w1,w2,rp,rs); [b,a]5cheby2(n,rs,wn);
  • 45. MATLAB Programs  859 w50:.01:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.35 enter the stopband ripple... 35 enter the passband freq... 1500 enter the stopband freq... 2000 enter the sampling freq... 8000 The amplitude and phase responses of Chebyshev type - 2 low-pass digital filter are shown in Fig. 16.27. Fig. 16.27  Chebyshev Type - 2 Low-pass Digital Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 −2 2 4 0 −100 −80 −60 −40 −20 0 20 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 GainindB Phaseinradians (a) (b) 0.1 0.1 −4 −2 2 4 0 −100 −80 −60 −40 −20 0 20 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 16.13.2  High-pass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies
  • 46. 860   Digital Signal Processing 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.67 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-2 high pass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n,wn]5cheb2ord(w1,w2,rp,rs); [b,a]5cheby2(n,rs,wn,’high’); w50:.01/pi:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.25 enter the stopband ripple... 40 enter the passband freq... 1400 enter the stopband freq... 1800 enter the sampling freq... 7000 The amplitude and phase responses of Chebyshev type - 2 high-pass digital filter are shown in Fig. 16.28. GainindB aseinradians (a) 0.1 −2 2 4 0 −120 −100 −80 −60 −40 −20 0 0.2 0.3 0.4 Normalised frequency 0.5 0.6 0.7 0.8 0.9 10 Fig. 16.28  (Contd.)
  • 47. MATLAB Programs  861 16.13.3  Bandpass Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequency 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.67 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of Chebyshev Type-2 Bandpass digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n]5cheb2ord(w1,w2,rp,rs); wn5[w1 w2]; [b,a]5cheby2(n,rs,wn,’bandpass’); w50:.01/pi:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); Gainin Phaseinradians (a) (b) 0.1 0.1 −4 −2 2 4 0 −120 −100 −80 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 Fig. 16.28  Chebyshev Type - 2 High-pass Digital Filter (a) Amplitude Response and (b) Phase Response
  • 48. 862   Digital Signal Processing As an example, enter the passband ripple... 0.4 enter the stopband ripple... 40 enter the passband freq... 1400 enter the stopband freq... 2000 enter the sampling freq... 9000 The amplitude and phase responses of Chebyshev type - 2 bandpass digital filter are shown in Fig. 16.29. Fig. 16.29  Chebyshev Type - 2 Bandpass Digital Filter (a) Amplitude Response and (b) Phase Response GainindB Phaseinradians (a) (b) 0.1 0.1 −4 −2 4 0 2 −400 −300 −200 −100 0 100 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0 16.13.4  Bandstop Filter Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter using Eq. 8.67 5. Find the filter coefficients 6. Draw the magnitude and phase responses.
  • 49. MATLAB Programs  863 % Program for the design of Chebyshev Type-2 Bandstop digital filter clc; close all;clear all; format long rp5input(‘enter the passband ripple...’); rs5input(‘enter the stopband ripple...’); wp5input(‘enter the passband freq...’); ws5input(‘enter the stopband freq...’); fs5input(‘enter the sampling freq...’); w152*wp/fs;w252*ws/fs; [n]5cheb2ord(w1,w2,rp,rs); wn5[w1 w2]; [b,a]5cheby2(n,rs,wn,’stop’); w50:.1/pi:pi; [h,om]5freqz(b,a,w); m520*log10(abs(h)); an5angle(h); subplot(2,1,1);plot(om/pi,m); ylabel(‘Gain in dB --.’);xlabel(‘(a) Normalised frequency --.’); subplot(2,1,2);plot(om/pi,an); xlabel(‘(b) Normalised frequency --.’); ylabel(‘Phase in radians --.’); As an example, enter the passband ripple... 0.3 enter the stopband ripple... 46 enter the passband freq... 1400 enter the stopband freq... 2000 enter the sampling freq... 8000 The amplitude and phase responses of Chebyshev type - 2 bandstop digital filter are shown in Fig. 16.30. GainindB Phaseinradians (a) 0.1 -4 -2 -1 -3 3 0 1 2 −80 −60 −40 −20 0 20 0.2 0.3 0.4 Normalised frequency 0.5 0.6 0.7 0.8 0.9 10 Fig. 16.30  (Contd.)
  • 50. 864   Digital Signal Processing 16.14  FIR FILTER DESIGN USING WINDOW TECHNIQUES In the design of FIR filters using any window technique, the order can be calculated using the formula given by N p s f f Fs p s = − − − 20 13 14 6 log( ) . ( ) / d d where dp is the passband ripple, ds is the stopband ripple, fp is the passband frequency, fs is the stopband frequency and Fs is the sampling frequency. 16.14.1  Rectangular Window Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter 5. Find the window coefficients using Eq. 7.37 6. Draw the magnitude and phase responses. % Program for the design of FIR Low pass, High pass, Band pass and Bandstop filters using rectangular window clc;clear all;close all; rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); fp5input(‘enter the passband freq’); fs5input(‘enter the stopband freq’); f5input(‘enter the sampling freq’); wp52*fp/f;ws52*fs/f; num5220*log10(sqrt(rp*rs))213; Fig. 16.30  Chebyshev Type - 2 Bandstop Digital Filter (a) Amplitude Response and (b) Phase Response Gaini Phaseinradians (a) (b) 0.1 0.1 -4 -2 -1 -3 3 0 1 2 −80 −60 −40 0.2 0.2 0.3 0.3 0.4 0.4 Normalised frequency Normalised frequency 0.5 0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1 1 0 0
  • 51. MATLAB Programs  865 dem514.6*(fs2fp)/f; n5ceil(num/dem); n15n11; if (rem(n,2)˜50) n15n; n5n21; end y5boxcar(n1); % low-pass filter b5fir1(n,wp,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(a) Normalised frequency --.’); % high-pass filter b5fir1(n,wp,’high’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(b) Normalised frequency --.’); % band pass filter wn5[wp ws]; b5fir1(n,wn,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --’); xlabel(‘(c) Normalised frequency --’); % band stop filter b5fir1(n,wn,’stop’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --’); xlabel(‘(d) Normalised frequency --’); As an example, enter the passband ripple 0.05 enter the stopband ripple 0.04 enter the passband freq 1500 enter the stopband freq 2000 enter the sampling freq 9000 The gain responses of low-pass, high-pass, bandpass and bandstop filters using rectangular window are shown in Fig. 16.31.
  • 52. 866   Digital Signal Processing 16.14.2  Bartlett Window Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of FIR Low pass, High pass, Band pass and Bandstop filters using Bartlett window clc;clear all;close all; rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); fp5input(‘enter the passband freq’); fs5input(‘enter the stopband freq’); f5input(‘enter the sampling freq’); Fig. 16.31  Filters Using Rectangular Window (a) Low-pass (b) High-pass (c) Bandpass and (d) Bandstop 0.2 0.2 0.2 0.2 −80 −80 −20 −80 −60 −60 −15 −60 −40 −40 −10 −40 −20 −20 −5 −20 0 0 0 0 20 20 5 20 0.4 0.4 0.4 0.4 (a) (c) (b) (d) Normalised frequency Normalised frequency Normalised frequency Normalised frequency 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 1 1 1 1 0 0 0 0 GainindBGainindB GainindBGainindB
  • 53. MATLAB Programs  867 wp52*fp/f;ws52*fs/f; num5220*log10(sqrt(rp*rs))213; dem514.6*(fs2fp)/f; n5ceil(num/dem); n15n11; if (rem(n,2)˜50) n15n; n5n21; end y5bartlett(n1); % low-pass filter b5fir1(n,wp,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(a) Normalised frequency --.’); % high-pass filter b5fir1(n,wp,’high’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(b) Normalised frequency --.’); % band pass filter wn5[wp ws]; b5fir1(n,wn,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(c) Normalised frequency --.’); % band stop filter b5fir1(n,wn,’stop’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(d) Normalised frequency --.’); As an example, enter the passband ripple 0.04 enter the stopband ripple 0.02 enter the passband freq 1500 enter the stopband freq 2000 enter the sampling freq 8000 The gain responses of low-pass, high-pass, bandpass and bandstop filters using Bartlett window are shown in Fig. 16.32.
  • 54. 868   Digital Signal Processing 16.14.3  Blackman window Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter 5. Find the window coefficients using Eq. 7.45 6. Draw the magnitude and phase responses. % Program for the design of FIR Low pass, High pass, Band pass and Band stop digital filters using Blackman window clc;clear all;close all; rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); fp5input(‘enter the passband freq’); fs5input(‘enter the stopband freq’); f5input(‘enter the sampling freq’); Fig. 16.32  Filters using Bartlett Window (a) Low-pass (b) High-pass (c) Bandpass and (d) Bandstop − 35 −40 −8 − 30 −30 −6 − 30 −25 −25 −20 −20 −4 − 20 −15 − 15 −10 −10 −2 −0 − 10 −5 −5 0 20 5 0 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 (a) (c) (b) (d) Normalised frequency Normalised frequency Normalised frequency Normalised frequency 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 1 1 1 1 0 0 0 0 GainindB GainindBGainindB GainindB
  • 55. MATLAB Programs  869 wp52*fp/f;ws52*fs/f; num5220*log10(sqrt(rp*rs))213; dem514.6*(fs2fp)/f; n5ceil(num/dem); n15n11; if (rem(n,2)˜50) n15n; n5n21; end y5blackman(n1); % low-pass filter b5fir1(n,wp,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(a) Normalised frequency --.’); % high-pass filter b5fir1(n,wp,’high’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(b) Normalised frequency --.’); % band pass filter wn5[wp ws]; b5fir1(n,wn,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(c) Normalised frequency --.’); % band stop filter b5fir1(n,wn,’stop’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,4);plot(o/pi,m);;ylabel(‘Gain in dB --.’); xlabel(‘(d) Normalised frequency --.’); As an example, enter the passband ripple 0.03 enter the stopband ripple 0.01 enter the passband freq 2000 enter the stopband freq 2500 enter the sampling freq 7000 The gain responses of low-pass, high-pass, bandpass and bandstop filters using Blackman window are shown in Fig. 16.33.
  • 56. 870   Digital Signal Processing 16.14.4  Chebyshev Window Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter 5. Find the filter coefficients 6. Draw the magnitude and phase responses. % Program for the design of FIR Lowpass, High pass, Band pass and Bandstop filters using Chebyshev window clc;clear all;close all; rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); fp5input(‘enter the passband freq’); fs5input(‘enter the stopband freq’); f5input(‘enter the sampling freq’); Fig. 16.33  Filters using Blackman Window (a) Low-pass (b) High-pass (c) Bandpass and (d) Bandstop −120 −120 −8 −150 −100 −100 −100 −6 −80 −60 − 80 −4 −40 −20 − 60 − 40 − 20 −2 0 −50 0 0 20 20 50 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 (a) (c) (b) (d) Normalised frequency Normalised frequency Normalised frequency Normalised frequency 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 1 1 1 1 0 0 0 0 GainindB GainindB GainindB GainindB
  • 57. MATLAB Programs  871 r5input(‘enter the ripple value(in dBs)’); wp52*fp/f;ws52*fs/f; num5220*log10(sqrt(rp*rs))213; dem514.6*(fs2fp)/f; n5ceil(num/dem); if(rem(n,2)˜50) n5n11; end y5chebwin(n,r); % low-pass filter b5fir1(n-1,wp,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(a) Normalised frequency --.’); % high-pass filter b5fir1(n21,wp,’high’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(b) Normalised frequency --.’); % band-pass filter wn5[wp ws]; b5fir1(n21,wn,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(c) Normalised frequency --.’); % band-stop filter b5fir1(n21,wn,’stop’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(d) Normalised frequency --.’); As an example, enter the passband ripple 0.03 enter the stopband ripple 0.02 enter the passband freq 1800 enter the stopband freq 2400 enter the sampling freq 10000 enter the ripple value(in dBs)40 The gain responses of low-pass, high-pass, bandpass and bandstop filters using Chebyshev window are shown in Fig. 16.34.
  • 58. 872   Digital Signal Processing 16.14.5  Hamming Window Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter 5. Find the window coefficients using Eq. 7.40 6. Draw the magnitude and phase responses. % Program for the design of FIR Low pass, High pass, Band pass and Bandstop filters using Hamming window clc;clear all;close all; rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); fp5input(‘enter the passband freq’); fs5input(‘enter the stopband freq’); f5input(‘enter the sampling freq’); Fig. 16.34  Filters using Chebyshev Window (a) Low-pass (b) High-pass (c) Bandpass and (d) Bandstop −100 −120 −12 −100 −40 −20 −60 −80− 80 −100 −10 −60 − 40 − 80 −8 − 20 − 60 − 40 − 20 −6 −4 −2 0 0 20 0 20 20 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 (a) (c) (b) (d) Normalised frequency Normalised frequency Normalised frequency Normalised frequency 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 1 1 1 1 0 0 0 0 GainindB GainindBGainindB GainindB
  • 59. MATLAB Programs  873 wp52*fp/f;ws52*fs/f; num5220*log10(sqrt(rp*rs))213; dem514.6*(fs2fp)/f; n5ceil(num/dem); n15n11; if (rem(n,2)˜50) n15n; n5n21; end y5hamming(n1); % low-pass filter b5fir1(n,wp,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(a) Normalised frequency --.’); % high-pass filter b5fir1(n,wp,’high’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(b) Normalised frequency --.’); % band pass filter wn5[wp ws]; b5fir1(n,wn,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(c) Normalised frequency --.’); % band stop filter b5fir1(n,wn,’stop’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(d) Normalised frequency --.’); As an example, enter the passband ripple 0.02 enter the stopband ripple 0.01 enter the passband freq 1200 enter the stopband freq 1700 enter the sampling freq 9000 The gain responses of low-pass, high-pass, bandpass and bandstop filters using Hamming window are shown in Fig. 16.35.
  • 60. 874   Digital Signal Processing 16.14.6 Hanning Window Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter 5. Find the window coefficients using Eq. 7.44 6. Draw the magnitude and phase responses. % Program for the design of FIR Low pass, High pass, Band pass and Band stop filters using Hanning window clc;clear all;close all; rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); fp5input(‘enter the passband freq’); fs5input(‘enter the stopband freq’); f5input(‘enter the sampling freq’); Fig. 16.35  Filters using Hamming Window (a) Low-pass (b) High-pass (c) Bandpass and (d) Bandstop −120 −120 −15 −100 −40 −20 −60 −80−100 −100 −10 −5 −80 − 60 − 80 − 40 − 20 − 60 − 40 − 20 0 0 20 0 20 20 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 (a) (c) (b) (d) Normalised frequency Normalised frequency Normalised frequency Normalised frequency 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 1 1 1 1 0 0 0 0 GainindB GainindBGainindB GainindB
  • 61. MATLAB Programs  875 wp52*fp/f;ws52*fs/f; num5220*log10(sqrt(rp*rs))213; dem514.6*(fs2fp)/f; n5ceil(num/dem); n15n11; if (rem(n,2)˜50) n15n; n5n21; end y5hamming(n1); % low-pass filter b5fir1(n,wp,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(a) Normalised frequency --.’); % high-pass filter b5fir1(n,wp,’high’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(b) Normalised frequency --.’); % band pass filter wn5[wp ws]; b5fir1(n,wn,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(c) Normalised frequency --.’); % band stop filter b5fir1(n,wn,’stop’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --.’); xlabel(‘(d) Normalised frequency --.’); As an example, enter the passband ripple 0.03 enter the stopband ripple 0.01 enter the passband freq 1400 enter the stopband freq 2000 enter the sampling freq 8000 The gain responses of low-pass, high-pass, bandpass and bandstop filters using Hanning window are shown in Fig. 16.36.
  • 62. 876   Digital Signal Processing 16.14.7  Kaiser Window Algorithm 1. Get the passband and stopband ripples 2. Get the passband and stopband edge frequencies 3. Get the sampling frequency 4. Calculate the order of the filter 5. Find the window coefficients using Eqs 7.46 and 7.47 6. Draw the magnitude and phase responses. % Program for the design of FIR Low pass, High pass, Band pass and Bandstop filters using Kaiser window clc;clear all;close all; rp5input(‘enter the passband ripple’); rs5input(‘enter the stopband ripple’); fp5input(‘enter the passband freq’); fs5input(‘enter the stopband freq’); f5input(‘enter the sampling freq’); beta5input(‘enter the beta value’); Fig. 16.36   Filters using Hanning Window (a) Low-pass (b) High-pass (c) Bandpass and (d) Bandstop − 120 −120 −12 −100 −40 −20 −60 −80− 100 −100 −10 −8 −6 −4 −2 0 −80 −60 −80 −40 −20 −60 −40 −20 0 20 0 0 2 20 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 (a) (c) (b) (d) Normalised frequency Normalised frequency Normalised frequency Normalised frequency 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 1 1 1 1 0 0 0 0 GainindB GainindBGainindB GainindB
  • 63. MATLAB Programs  877 wp52*fp/f;ws52*fs/f; num5220*log10(sqrt(rp*rs))213; dem514.6*(fs2fp)/f; n5ceil(num/dem); n15n11; if (rem(n,2)˜50) n15n; n5n21; end y5kaiser(n1,beta); % low-pass filter b5fir1(n,wp,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,1);plot(o/pi,m);ylabel(‘Gain in dB --’); xlabel(‘(a) Normalised frequency --’); % high-pass filter b5fir1(n,wp,’high’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,2);plot(o/pi,m);ylabel(‘Gain in dB --’); xlabel(‘(b) Normalised frequency --’); % band pass filter wn5[wp ws]; b5fir1(n,wn,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,3);plot(o/pi,m);ylabel(‘Gain in dB --’); xlabel(‘(c) Normalised frequency --’); % band stop filter b5fir1(n,wn,’stop’,y); [h,o]5freqz(b,1,256); m520*log10(abs(h)); subplot(2,2,4);plot(o/pi,m);ylabel(‘Gain in dB --’); xlabel(‘(d) Normalised frequency --’); As an example, enter the passband ripple 0.02 enter the stopband ripple 0.01 enter the passband freq 1000 enter the stopband freq 1500 enter the sampling freq 10000 enter the beta value 5.8 The gain responses of low-pass, high-pass, bandpass and bandstop filters using Kaiser window are shown in Fig. 16.37.
  • 64. 878   Digital Signal Processing 16.15   UPSAMPLING A SINUSOIDAL SIGNAL % Program for upsampling a sinusoidal signal by factor L N5input(‘Input length of the sinusoidal sequence5’); L5input(‘Up Samping factor5’); fi5input(‘Input signal frequency5’); % Generate the sinusoidal sequence for the specified length N n50:N21; x5sin(2*pi*fi*n); % Generate the upsampled signal y5zeros (1,L*length(x)); y([1:L:length(y)])5x; %Plot the input sequence subplot (2,1,1); stem (n,x); title(‘Input Sequence’); xlabel(‘Time n’); ylabel(‘Amplitude’); Fig. 16.37  Filters using Kaiser Window (a) Low-pass (b) High-pass (c) Bandpass and (d) Bandstop −120 −120 −15 −40 −20 −60 −80 −100 −100 −10 −5 0 −80 − 60 − 80 − 40 − 20 − 60 − 40 − 20 0 20 0 0 5 20 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 (a) (c) (b) (d) Normalised frequency Normalised frequency Normalised frequency Normalised frequency 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 1 1 1 1 0 0 0 0 GainindB GainindB GainindB GainindB
  • 65. MATLAB Programs  879 %Plot the output sequence subplot (2,1,2); stem (n,y(1:length(x))); title(‘[output sequence,upsampling factor5‘,num2str(L)]); xlabel(‘Time n’); ylabel(‘Amplitude’); 16.16  UPSAMPLING AN EXPONENTIAL SEQUENCE % Program for upsampling an exponential sequence by a factor M n5input(‘enter length of input sequence …’); l5input(‘enter up sampling factor …’); % Generate the exponential sequence m50:n21; a5input(‘enter the value of a …’); x5a.^m; % Generate the upsampled signal y5zeros(1,l*length(x)); y([1:l:length(y)])5x; figure(1) stem(m,x); xlabel({‘Time n’;’(a)’}); ylabel(‘Amplitude’); figure(2) stem(m,y(1:length(x))); xlabel({‘Time n’;’(b)’}); ylabel(‘Amplitude’); As an example, enter length of input sentence … 25 enter upsampling factor … 3 enter the value of a … 0.95 The input and output sequences of upsampling an exponential sequence an are shown in Fig. 16.38. Fig. 16.38  (Contd.)
  • 66. 880   Digital Signal Processing 16.17  DOWN SAMPLING A SINUSOIDAL SEQUENCE % Program for down sampling a sinusoidal sequence by a factor M N5input(‘Input length of the sinusoidal signal5’); M5input(‘Down samping factor5’); fi5input(‘Input signal frequency5’); %Generate the sinusoidal sequence n50:N21; m50:N*M21; x5sin(2*pi*fi*m); %Generate the down sampled signal y5x([1:M:length(x)]); %Plot the input sequence subplot (2,1,1); stem(n,x(1:N)); title(‘Input Sequence’); xlabel(‘Time n’); ylabel(‘Amplitude’); %Plot the down sampled signal sequence subplot(2,1,2); stem(n,y); title([‘Outputsequencedownsamplingfactor’,num2str(M)]); xlabel(‘Time n’); ylabel(‘Amplitude’); 16.18  DOWN SAMPLING AN EXPONENTIAL SEQUENCE % Program for downsampling an exponential sequence by a factor M N5input(‘enter the length of the output sequence …’); M5input(‘enter the down sampling factor …’); Fig. 16.38  (a) Input Exponential Sequence (b) Output Sequence Upsampled by a Factor of 3
  • 67. MATLAB Programs  881 % Generate the exponential sequence n50:N21; m50:N*M21; a5input(‘enter the value of a …’); x5a.^m; % Generate the downsampled signal y5x([1:M:length(x)]); figure(1) stem(n,x(1:N)); xlabel({‘Time n’;’(a)’}); ylabel(‘Amplitude’); figure(2) stem(n,y); xlabel({‘Time n’;’(b)’}); ylabel(‘Amplitude’); As an example, enter the length of the output sentence … 25 enter the downsampling factor … 3 enter the value of a … 0.95 The input and output sequences of downsampling an exponential sequence an are shown in Fig. 16.39. Fig. 16.39  (a) Input Exponential Sequence (b) Output Sequence Downsampled by a Factor of 3
  • 68. 882   Digital Signal Processing 16.19   DECIMATOR % Program for downsampling the sum of two sinusoids using MATLAB’s inbuilt decimation function by a factor M N5input(‘Length of the input signal5’); M5input(‘Down samping factor5’); f15input(‘Frequency of first sinusoid5’); f25input(‘Frequency of second sinusoid5’); n50:N21; % Generate the input sequence x52*sin(2*pi*f1*n)13*sin(2*pi*f2*n); %Generate the decimated signal % FIR low pass decimation is used y5decimate(x,M,‘fir’); %Plot the input sequence subplot (2,1,1); stem (n,x(1:N)); title(‘Input Sequence’); xlabel(‘Time n’); ylabel(‘Amplitude’); %Plot the output sequence subplot (2,1,2); m50:N/M21; stem (m,y(1:N/M)); title([‘Outputsequencedownsamplingfactor’,num2str(M)]); xlabel(‘Time n’); ylabel(‘Amplitude’); 16.20   DECIMATOR AND INTERPOLATOR % Program for downsampling and upsampling the sum of two sinusoids using MATLAB’s inbuilt decimation and interpolation function by a factor of 20. %Generate the input sequence for Fs 5200Hz, f1 550Hz and f2 5100 Hz t50:1/200:10; y53.*cos(2*pi*50.*t/200)11.*cos(2*pi*100.*t/200); figure(1) stem(y); xlabel({‘Times in Seconds’;’(a)}); ylabel(‘Amplitude’);
  • 69. MATLAB Programs  883 %Generate the decimated and interpolated signals figure(2) stem(decimate(y,20)); xlabel({‘Times in Seconds’;’(b)}); ylabel(‘Amplitude’); figure(3) stem(interp(decimate(y,20),2)); xlabel({‘Times in Seconds’;’(c)}); ylabel(‘Amplitude’); Fig. 16.40  (a) Input Sequence, (b) Decimated Sequence and (c) Interpolated sequence 5 0 AmplitudeAmplitude 0 500 1000 Time in Seconds (a) 1500 2000 2500 −5 5 0 0 20 40 Time in Seconds (b) 60 80 100 120 −5 Amplitude 5 0 0 50 100 150 200 250 Time in Seconds (c) −5 16.21  ESTIMATION OF POWER SPECTRAL DENSITY (PSD) % Program for estimating PSD of two sinusoids plus noise % Algorithm; % 1:Get the frequencies of the two sinusoidal waves % 2:Get the sampling frequency % 3:Get the length of the sequence to be considered
  • 70. 884   Digital Signal Processing % 4:Get the two FFT lengths for comparing the corre- sponding power spectral densities clc; close all; clear all; f15input(‘Enter the frequency of first sinusoid’); f25input(‘Enter the frequency of second sinusoid’); fs5input(‘Enter the sampling frequency’); N5input(“Enter the length of the input sequence’); N15input(“Enter the input FFT length 1’); N25input(“Enter the input FFT length 2’); %Generation of input sequence t50:1/fs:1; x52*sin(2*pi*f1*1)13*sin(2*pi*f2*t)2randn(size(t)); %Generation of psd for two different FFT lengths Pxx15abs(fft(x,N1)).^2/(N11); Pxx25abs(fft(x,N2)).^2/(N11); %Plot the psd; subplot(2,1,1); plot ((0:(N121))/N1*fs,10*log10(Pxx1)); xlabel(‘Frequency in Hz’); ylabel(‘Power spectrum in dB’); title(‘[PSD with FFT length,num2str(N1)]’); subplot (2,1,2); plot ((0:(N221))/N2*fs,10*log10(Pxx2)); xlabel(‘Frequency in Hz’); ylabel(‘Power spectrum in dB’); title(‘[PSD with FFT length,num2str(N2)]’); 16.22   PSD ESTIMATOR % Program for estimating PSD of a two sinusoids plus noise using %(i)non-overlapping sections %(ii)overlapping sections and averaging the periodograms clc; close all; clear all; f15input(‘Enter the frequency of first sinusoid’); f25input(‘Enter the frequency of second sinusoid’); fs5input(‘Enter the sampling frequency’); N5input(“Enter the length of the input sequence’); N15input(“Enter the input FFT length 1’); N25input(“Enter the input FFT length 2’); %Generation of input sequence t50:1/fs:1; x52*sin(2*pi*f1*1)13*sin(2*pi*f2*t)2randn(size(t));
  • 71. MATLAB Programs  885 %Generation of psd for two different FFT lengths Pxx15(abs(fft(x(1:256))).^21abs(fft(x(257:512))).^21 abs(fft(x(513:768))).^2/(256*3); %using nonoverlapping sections Pxx25(abs(fft(x(1:256))).^21abs(fft(x(129:384))).^21ab s(fft(x(257:512))).^21abs(fft(x(385:640))).^21abs(fft( x(513:768))).^21abs(fft(x(641:896))).^2/(256*6); %using overlapping sections % Plot the psd; subplot (2,1,1); plot ((0:255)/256*fs,10*log10(Pxx1)); xlabel(‘Frequency in Hz’); ylabel(‘Power spectrum in dB’); title(‘[PSD with FFT length,num2str(N1)]’); subplot (2,1,2); plot ((0:255)/256*fs,10*log10(Pxx2)); xlabel(‘Frequency in Hz’); ylabel(‘Power spectrum in dB’); title(‘[PSD with FFT length,num2str(N2)]’); 16.23   PERIODOGRAM ESTIMATION % Periodogram estimate cold be done by applying a non- rectangular data windows to the sections prior to com- puting the periodogram % This program estimates PSD for the input signal of two sinusoids plus noise using Hanning window f15input(‘Enter the frequency of first sinusoid’); f25input(‘Enter the frequency of second sinusoid’); fs5input(‘Enter the sampling frequency’); t50:1/fs:1; w5hanning(256); x52*sin(2*pi*f1*t)13*sin(2*pi*f2*t)2randn(size(t)); Pxx5(abs(fft(w.*x(1:256))).^21abs(fft(w.*x(129:384))).^ 21abs(fft(w.*x(257:512))).^21abs(fft(w.*x(385:640))).^2 1abs(fft(w.*x(513:768))).^21abs(fft(w.*x(641:896))).^2/ (norm(w)^2*6); Plot((0:255)/256*fs,10*log10(Pxx)); 16.24   WELCH PSD ESTIMATOR % Program for estimating the PSD of sum of two sinusoids plus noise using Welch method n50.01:0.001:.1; x5sin(.25*pi*n)13*sin(.45*pi*n)1rand(size(n)); pwelch(x)
  • 72. 886   Digital Signal Processing xlabel(‘Normalised Frequency (x pi rad/sample)’); ylabel(‘Power Spectrum Density (dB/rad/sample)’) 16.25  WELCH PSD ESTIMATOR USING WINDOWS % Program for estimating the PSD of sum of two sinusoids using Welch method with an overlap of 50 percent and with Hanning, Hamming, Bartlett, Blackman and rectangular windows. fs51000; t50:1/fs:3; x5sin(2*pi*200*t)1sin(2*pi*400*t); figure(1) subplot(211) pwelch(x,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’) subplot(212) pwelch(x,hanning(512),0,512,fs) title(‘N5512 Overlap550% Hanning’) figure(2) subplot(211) pwelch(x,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’) subplot(212) pwelch(x,hamming(512),0,512,fs) title(‘N5512 Overlap550% Hamming’) figure(3) subplot(211) pwelch(x,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’) subplot(212) pwelch(x,bartlett(512),0,512,fs) title(‘N5512 Overlap550% Bartlett’) figure(4) subplot(211) pwelch(x,[],[],[],fs); Fig. 16.41  Welch PSD Estimate 5 0 −5 −10 −15 −20 0 0.1 0.2 0.3 0.4 0.5 Welch PSD Estimate PowerSpectrumDensity(dB/rad/sample) Normalised Frequency (x pi rad/sample) 0.6 0.7 0.8 0.9 1
  • 73. MATLAB Programs  887 Fig. 16.42  (b) Welch Estimate with N5512, 50% Overlap Hamming 0 −20 −40 −60 0 50 100 150 200 Frequency (Hz) Overlay plot of 50 Welch estimates PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −80 0 −20 −40 −60 0 50 100 150 200 Frequency (Hz) N=512 Overlap = 50% Hamming PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −80 0 −20 −40 −60 0 50 100 150 200 Frequency (Hz) N=512 Overlap = 50% Hanning Overlay plot of 50 Welch estimates PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 0 50 100 150 200 Frequency (Hz) 250 300 350 400 450 500 −80 0 −50 −100 PowerSpectralDensity(dB/Hz) −150 Fig. 16.42  (a) Welch Estimate with N5512, 50% Overlap Hanning title(‘Overlay plot of 50 Welch estimates’) subplot(212) pwelch(x,blackman(512),0,512,fs) title(‘N5512 Overlap550% Blackman’) figure(5) subplot(211) pwelch(x,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’) subplot(212) pwelch(x,boxcar(512),0,512,fs) title(‘N5512 Overlap550% Rectangular’)
  • 74. 888   Digital Signal Processing Fig. 16.42  (d) Welch ­Estimate with N5512, 50% Overlap Blackman 0 −20 −40 −60 0 50 100 150 200 Frequency (Hz) Frequency (Hz) Overlay plot of 50 Welch estimates PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −80 0 −50 −100 −150 0 50 100 150 200 N=512 Overlap = 50% Blackman PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −200 0 −20 −40 −60 0 50 100 150 200 Frequency (Hz) Overlay plot of 50 Welch estimates PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −80 0 −20 −40 −60 0 50 100 150 200 Frequency (Hz) N=512 Overlap = 50% Bartlett PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −80 Fig. 16.42  (c) Welch Estimate with N5512, 50% Overlap Bartlett
  • 75. MATLAB Programs  889 Fig. 16.42  (e) Welch Estimate with N5512, 50% Overlap Rectangular 0 −20 −40 −60 0 50 100 150 200 Frequency (Hz) Overlay plot of 50 Welch estimates PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −80 0 −10 −20 −30 −40 −50 −60 0 50 100 150 200 Frequency (Hz) N = 512 Overlap = 50% Rectangular PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 16.26   WELCH PSD ESTIMATOR USING WINDOWS % Program for estimating the PSD of sum of two sinusoids plus noise using Welch method with an overlap of 50 percent and with Hanning, Hamming, Bartlett, Blackman and rectangular windows fs51000; t50:1/fs:3; x52*sin(2*pi*200*t)15*sin(2*pi*400*t); y5x1randn(size(t)); figure(1) subplot(211); pwelch(y,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’); subplot(212); pwelch(y,hanning(512),0,512,fs); title(‘N5512 Overlap550% Hanning’); figure(2) subplot(211); pwelch(y,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’); subplot(212); pwelch(y,hamming(512),0,512,fs); title(‘N5512 Overlap550% Hamming’);
  • 76. 890   Digital Signal Processing 10 0 −10 −20 −30 0 50 100 150 200 Overlay plot of 50 Welch estimates Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40 10 0 −10 −20 −30 0 50 100 150 200 N=512 Overlap = 50% Hanning Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40 Fig. 16.43  (a) Welch Estimate with N5512, 50% Overlap Hanning figure(3) subplot(211); pwelch(y,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’); subplot(212); pwelch(y,bartlett(512),0,512,fs); title(‘N5512 Overlap550% Bartlett’); figure(4) subplot(211); pwelch(y,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’); subplot(212); pwelch(y,blackman(512),0,512,fs); title(‘N5512 Overlap550% Blackman’); figure(5) subplot(211); pwelch(y,[],[],[],fs); title(‘Overlay plot of 50 Welch estimates’); subplot(212); pwelch(y,boxcar(512),0,512,fs); title(‘N5512 Overlap550% Rectangular’);
  • 77. MATLAB Programs  891 Fig. 16.43  (b) Welch Estimate with N5512, 50% Overlap Hamming 10 0 −10 −20 −30 0 50 100 150 200 Overlay plot of 50 Welch estimates Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40 10 0 −10 −20 −30 0 50 100 150 200 N=512 Overlap = 50% Hanning Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40 Fig. 16.43  (c) Welch Estimate with N5512, 50% Overlap Bartlett 10 0 −10 −20 −30 0 50 100 150 200 Overlay plot of 50 Welch estimates Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40 10 0 −10 −20 −30 0 50 100 150 200 N=512 Overlap = 50% Bartlett Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40
  • 78. 892   Digital Signal Processing 10 0 −10 −20 −30 0 50 100 150 200 Overlay plot of 50 Welch estimates Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40 10 0 −10 −20 −30 0 50 100 150 200 N=512 Overlap = 50% Hanning Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40 Fig. 16.43  (e) Welch Estimate with N5512, 50% Overlap Rectangular Fig. 16.43  (d) Welch Estimate with N5512, 50% Overlap Blackman 10 0 −10 −20 −30 0 50 100 150 200 Overlay plot of 50 Welch estimates Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40 10 0 −10 −20 −30 0 50 100 150 200 N=512 Overlap = 50% Blackman Frequency (Hz) PowerSpectralDensity(dB/Hz) 250 300 350 400 450 500 −40
  • 79. MATLAB Programs  893 16.27   STATE-SPACE REPRESENTATION % Program for computing the state-space matrices from the given transfer function function [A,B,C,D]5tf2ss(b,a); a5input (‘enter the denominator polynomials5’); b5input (‘enter the numerator polynomials5’); p5length(a)21;q5length(b)21;N5max(p,q); if(Np),a5[a,zeros(1,N2p)];end if(Nq),b5[b,zeros(1,N2q)];end A5[2a(2:N11);[eye(N21),zeros(N21,1)]]; B5[1;zeros(N21,1)]; C5b(2:N11)2b(1)*(2:N11); D5b(1); 16.28   PARTIAL FRACTION DECOMPOSITION % Program for partial fraction decomposition of a rational transfer function function[c,A,alpha]5tf2pf(b,a); a5input (‘enter the denominator polynomials5’); b5input (‘enter the numerator polynomials5’); p5length(a)21; q5length(b)21; a5(1/a(1))*reshape(a,1,p11); b5(1/a(1))*reshape(b,1,q11); if(q5p),%case of nonempty c(z) temp5toeplitz([a,zeros(1,q2p)]’,[a(1),zeros(1,q2p)]); temp5[temp,[eye(p);zeros(q2p11,p)]); temp5temp/b’; c5temp(1:;q2p11); d5temp(q2p12:q11)’; else c5[]; d5[b,zeros(1,p2q21)]; end alpha5cplxpair (roots(a));’; A5zeros(1,p); for k51 :p temp5prod(alpha(k)2alpha(find(1:p5k))); if(temp550),error(‘repeated roots in TF2PF’); else,A(k)5polyval(d,alpha(k))/temp; end end
  • 80. 894   Digital Signal Processing 16.29   INVERSE z-TRANSFORM % Program for computing inverse z-transform of a rational transfer function function x5invz(b,a,N); b5input (‘enter the numerator polynomials5’); a5input (‘enter the denominator polynomials5’); N5input (‘enter the number of points to be computed5’); [c,A,alpha]5tf2pf(b,a); x5zeros (1,N); x(1:length(c))5c; for k51:length(A), x5x1A(k)*(alpha(k)).^(0:N21); end x5real(x); 16.30   GROUP DELAY % Program for computing group delay of a rational transfer function on a given frequency interval function D5grpdly(b,a,K,theta); b5input (‘enter the numerator polynomials5’); a5input (‘enter the denominator polynomials5’); K5input (‘enter the number of frequency response points5’); theta5input (‘enter the theta value5’); a5reshape(a,1,length(a)); b5reshape(b,1,length(b)); if (length(a)551)%case of FIR bd52j*(0:length(b)21).*b; if(nargin553), B5frqresp(b,1,K); Bd5frqresp(bd,1,K); else, B5frqresp(b,1,K,theta); Bd5frqresp(bd,1,K,theta); end D5(real(Bd).*imag(B)2real(B).*imag(Bd))./abs(B).^2; else %case of IIR if(nargin553), D5grpdly (b,1,K)2grpdly(a,1,K); else, D5grpdly(b,1,K,theta)2grpdly(a,1,K,theta); end end
  • 81. MATLAB Programs  895 16.31  IIR FILTER DESIGN-IMPULSE INVARIANT METHOD % Program for transforming an analog filter into a digital filter using impulse invariant technique function [bout,aout]5impinv(bin,ain,T); bin5input(‘enter the numerator polynomials5’); ain5input(‘enter the denominator polynomials5’); T5input(‘enter the sampling interval5’); if(length(bin)5length(ain)), error(‘Anlog filter in IMPINV is not strictly proper’); end [r,p,k]5residue(bin,ain); [bout,aout]5pf2tf([],T*r,exp(T*p)); 16.32  IIR FILTER DESIGN-BILINEAR TRANSFORMATION % Program for transforming an analog filter into a digial filter using bilinear transformation function [b,a,vout,uout,Cout]5bilin(vin,uin,Cin,T); pin5input(‘enter the poles5’); zin5input(‘enter the zero5’); T5input(‘enter the sampling interval5’); Cin5input(‘enter the gain of the analog filter5’); p5length(pin); q5length(zin); Cout5Cin*(0.5*T)^(p2q)*prod(120.5*T*zin)/ prod(120.5*T*pin); zout5[(110.5*T*zin)./(120.5*T*pin),2ones(1,p2q)]; pout5(110.5*T*pin)./(120.5*T*pin); a51; b51; for k51 :length(pout),a5conv(a,[1,2pout(k)]); end for k51 :length(zout),b5conv(b,[1,2zout(k)]); end a5real(a); b5real(Cout*b); Cout5real(Cout); 16.33  DIRECT REALISATION OF IIR DIGITAL FILTERS % Program for computing direct realisation values of IIR digital filter function y5direct(typ,b,a,x); x5input(‘enter the input sequence5’);
  • 82. 896   Digital Signal Processing b5input(‘enter the numerator polynomials5’); a5input(‘enter the denominator polynomials5’); typ5input(‘type of realisation5’); p5length(a)21; q5length(b)21; pq5max(p,q); a5a(2:p11);u5zeros(1,pq);%u is the internal state; if(typ551) for i51:length(x), unew5x(i)2sum(u(1:p).*a); u5[unew,u]; y(i)5sum(u(1:q11).*b); u5u(1:pq); end elseif(typ552) for i51:length(x) y(i)5b(1)*x(i)1u(1); u5u[(2:pq),0]; u(1:q)5u(1:q)1b(2:q11)*x(i); u(1:p)5u(1:p)2a*y(i); end end 16.34  PARALLEL REALISATION OF IIR DIGITAL FILTERS % Program for computing parallel realisation values of IIR digital filter function y5parallel(c,nsec,dsec,x); x5input(‘enter the input sequence5’); b5input(‘enter the numerator polynomials5’); a5input(‘enter the denominator polynomials5’); c5input(‘enter the gain of the filter5’); [n,m]5size(a);a5a(:,2:3); u5zeros(n,2); for i51:length(x), y(i)5c*x(i); for k51:n, unew5x(i)2sum(u(k,:).*a(k,:));u(k,:)5[unew,u(k,1)]; y(i)5y(i)1sum(u(k,:).*b(k,:)); end end
  • 83. MATLAB Programs  897 16.35  CASCADE REALISATION OF IIR DIGITAL FILTERS % Program for computing cascade realisation values of digital IIR filter function y5cascade(c,nsec,dsec,x); x5input(‘enter the input sequence5’); b5input(‘enter the numerator polynomials5’); a5input(‘enter the denomiator polynomials5’); c5input(‘enter the gain of the filter5’); [n,m]5size(b); a5a(:,2:3);b5b(:,2,:3); u5zeros(n,2); for i51 :length(x), for k51 :n, unew5x(i)2sum(u(k,:).*a(k,:)); x(i)52unew1sum(u(k,:).*b(k,:)) u(k,:)5[unew,u(k,1)]; end y(i)5c*x(i); end 16.36  DECIMATION BY POLYPHASE DECOMPOSITION % Program for computing convolution and m-fold decimation by polyphase decomposition function y5ppdec(x,h,M); x5input(‘enter the input sequence5’); h5input(‘enter the FIR filter coefficients5’); M5input(‘enter the decimation factor5’); 1h5length(h); 1p5floor((1h21)/M)11; p5reshape([reshape(h,1,1h),zeros(1,1p*M21h)],M,1p); lx5length(x); ly5floor ((1x11h22)/M)11; 1u5foor((1x1M22)/M)11; %length of decimated sequences u5[zeros(1,M21),reshape(x,1,1x),zeros(1,M*lu2lx2M11)]; y5zeros(1,1u11p21); for m51:M,y5y1conv(u(m,: ),p(m,: )); end y5y(1:1y); 16.37   MULTIBAND FIR FILTER DESIGN % Program for the design of multiband FIR filters function h5firdes(N,spec,win); N5input(‘enter the length of the filter5’);
  • 84. 898   Digital Signal Processing spec5input(‘enter the low,high cutoff frequencies and gain5’); win5input(‘enter the window length5’); flag5rem(N,2); [K,m]5size(spec); n5(0:N)2N/2; if (˜flag),n(N/211)51; end,h5zeros(1,N11); for k51:K temp5(spec (k,3)/pi)*(sin(spec(k,2)*n)2sin(spec(k,1) *n))./n; if (˜flag);temp(N/211)5spec(k,3)*(spec(k,2)2 spec(k,1))/pi; end h5h1temp; end if (nargin553), h5h.*reshape(win,1,N11); end 16.38   ANALYSIS FILTER BANK % Program for maximally decimated uniform DFT analysis filter bank function u5dftanal(x,g,M); g5input(‘enter the filter coefficient5’); x5input(‘enter the input sequence5’); M5input(‘enter the decimation factor5’); 1g5length(g); 1p5floor((1g21)/M)11; p5reshape([reshape(g,1,1g),zeros(1,1p*M21g)],M,1p); lx5length(x); lu5floor ((1x1M22)/M)11; x5[zeros(1,M21),reshape(x,1,1x),zeros(1,M*lu2lx2M11)]; x5flipud(reshape(x,M,1u)); %the decimated sequences u5[]; for m51:M,u5[u; cov(x(m,:),p(m,:))]; end u5ifft(u); 16.39   SYNTHESIS FILTER BANK % Program for maximally decimated uniform DFT synthesis filter bank function y5udftsynth(v,h,M); 1h5length(h); ‘1q5floor((1h21)/M)11; q5flipud(reshape([reshape(h,1,1h),zeros(1,1q*M21h)],M,1q));
  • 85. MATLAB Programs  899 v5fft(v); y5[ ]; for m51:M,y5[conv(v(m,:),q(m,:));y]; end y5y(:).’; 16.40   LEVINSON-DURBIN ALGORITHM % Program for the solution of normal equations using Levinson- Durbin algorithm function [a,rho,s]5levdur(kappa); % Input; % kappa: covariance sequence values from 0 to p % Output parameters: % a: AR polynomial,with leading entry 1 % rho set of p reflection coefficients % s: innovation variance kappa5input(‘enter the covariance sequence5’); p5length(kappa)21; kappa5reshape(kappa,p11,1); a51; s5kappa(1); rho5[]; for i51:p, rhoi5(a*kappa(i11:21:2))/s; rho5[rho,rhoi]; s5s*(12rhoi^2); a5[a,0]; a5a2rhoi*fliplr(a); end 16.41   WIENER EQUATION’S SOLUTION % Program function b5wiener(kappax,kappayx); kappax5input(‘enter the covariance sequence5’); kappyx5input(‘enter the joint covariance sequence5’); q5length(kappax)21; kappax5reshape(kappax,q11,1); kappayx5reshape(kappayx,q11,1); b5(toeplitz(kappax)/(kappayx)’; 16.42   SHORT-TIME SPECTRAL ANALYSIS % Program function X5stsa(x,N,K,L,w,opt,M,theta0,dtheta); x5input(‘enter the input signal5’); L5input(‘enter the number consecutive DFTs to average5’); N5input(‘enter the segment length5’); K5input(‘enter the number of overlapping points5’); w5input(‘enter
  • 86. 900   Digital Signal Processing the window coefficients5’); opt5input(‘opt5’); M5input(‘enter the length of DFT5’); theta05input(‘theta05’); dtheta5input(‘dtheta5’); 1x5length(x); nsec5ceil((1x2N)/(N2K)11; x5[reshape(x,1,1x),zeros(1,N1(nsec21)*(N2K))2lx)]; nout5N; if (nargin 5),nout5M; else,opt5‘n’; end X5zeros(nsec,nout); for n51: nsec, temp5w.*x((n21)*(N2K)11:(n21)*(N2K)1N); if (opt(1) 55 ‘z’),temp5[temp,zeros(1,M2N)]; end if (opt(1)55‘c’),temp5chirpf (temp,theta0,dtheta,M); else,temp5fftshift(fft(temp)); end X(n,: )5abs(temp).^2; end if(L1); nsecL5floor(nsec/L); for n51:nsecL,X(n,:)5mean (X((n21)*L11:n*L,:)); end if (nsec55nsecL*L11), X(nsecL11,:)5X(nsecL*L11,:); X5X(1:nsecL11),: ); elseif(nsec nsecL*L), X(nsecL11,:)5mean(x(nsecL*L11:nsec,:)); X5X(1:nsecL11,:); else,X5X(1:nsecL,:); end end LKh 16.43  CANCELLATION OF ECHO PRODUCED ON THE TELEPHONE-BASE BAND CHANNEL Echo Canceler Echo path Estimated sequence Desired sequence Base band transmit filter Fig. 16.44  Baseband Channel Echo Canceler % Simulation program for baseband echo cancellation shown in Fig. 16.44 using LMS algorithm clc; close all; clear all; format short T5input(‘Enter the symbol interval’); br5input(‘Enter the bit rate value’); rf5input(‘Enter the roll off factor’); n5[210 10]; y55000*rcosfir(rf,n,br,T); %Transmit filter pulse shape is assumed as raised cosine