Updated - DSP Lab Manual
Updated - DSP Lab Manual
DEPARTMENT OF TELECOMMUNICATION
ENGINEERING
In-charge Faculty
Mr. VENU K N
Mrs. Akshatha Kori
Course Objectives
1. Understand the basic concepts of discrete-time signal processing like
sampling theorem.
2. Identify the concepts behind convolution, DFT & IDFT, auto correlation
and cross correlation.
3. Developing the solution for a difference equation and implement the same
by writing the program.
4. Identify the concepts behind FIR filters and design using different windows
like rectangular, hamming, hanning and Kaiser and implement the same
using matlab
5. Identify the concepts behind IIR filters and design the same using different
windows like rectangular, hamming, hanning and Kaiser and implement
the same using matlab
Course Outcomes
1. Analyze DSP concepts like Sampling theorem, convolution, DFT & IDFT,
correlation and solution for a difference equation on Mat-lab.
2. Computation of convolution and correlation.
3. Design of FIR filter by using different window methods.
4. Design IIR filter by using different window methods.
5. Design of FIR and IIR filters and implementation through DSP Processor.
Course
List of Experiments
Outcomes
Course Code and Title : Digital Signal Processing Lab, Course Credits : 0:0:1
TCL506
CIE : 50 Marks SEE : 50 Marks
Total No of Lab Hours : 24, No. of sessions: 12
Prepared by : Umesharaddy Date :31/08/2020
Reviewd by : Dr. B K Sujatha Date :31/08/2020
Prerequisites
Course Objectives
1. Understand the basic concepts of discrete-time signal processing like
sampling theorem.
2. Identify the concepts behind convolution, DFT & IDFT, auto correlation
and cross correlation.
3. Developing the solution for a difference equation and implement the same
by writing the program.
4. Identify the concepts behind FIR filters and design using different windows
like rectangular, hamming, hanning and Kaiser and implement the same
using matlab
5. Identify the concepts behind IIR filters and design the same using different
windows like rectangular, hamming, hanning and Kaiser and implement
the same using matlab
List of experiments:
Duratio
Sl No. Topics n
Course Outcomes
1. Analyze DSP concepts like Sampling theorem, convolution, DFT & IDFT,
correlation and solution for a difference equation on Mat-lab.
2. Computation of convolution and correlation.
3. Design of FIR filter by using different window methods.
4. Design IIR filter by using different window methods.
5. Design of FIR and IIR filters and implementation through DSP Processor.
Course Delivery:
The Course will be delivered through black board teaching, exercises, self-study and
demonstrations.
course forms
ct Assessment
Methods Feedback
End of course - C01-C05
Course survey forms
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3
1. 3 3 - 1 2 - - - 2 - - 2 1 1
2. 3 3 - 1 2 - - - 2 - - 2 1 1
3. 3 2 - 2 3 - - - 2 - - 2 1 1
4. 3 3 - 1 2 - - - 2 1 1 2 2 1 2
5. 3 3 - 1 2 - - - 1 - 1 2 2 1 1
Overall 3 3 - 2 3 - - - 2 1 1 1 2 1 2
a. PO1: The realization of FIR & IIR filters require a good engineering
and science (modeling) background as well as a foundation in
mathematics.
b. PO2: Use of partial differential equations to solve IIR filter
expressions allow a deeper understanding of complex problems.
c. PO3: With the structures of FIR & IIR filters, the specified needs
with societal, environmental considerations are met.
d. PO4: In realizing both FIR & IIR filters, students will be able to
analyze complex engineering problems.
e. PO5: Modern tool such as matlab simulation tool was used.
f. PO9: CO3 gives an ability to work effectively as an individual and
members or leader in diverse and multi-disciplinary teams
PROGRAM:
%UNIT IMPULSE SIGNAL
y1=[1;zeros(999,1)];
subplot(4,3,1)
plot(y1,'r');
%axis([-1 500 -1.5 1.5 ]);------------->not required
xlabel('time(sec)')
ylabel('amplitude')
title('unit impulse signal');
%RAMP SIGNAL
t=(0:0.001:1);
y3=t;
subplot(4,3,3)
plot(y3,'b');
%axis([-200 1200 -0.5 1.5 ]);------------->not required
xlabel('time(sec)')
ylabel('amplitude')
title('ramp signal');
%EXPONENTIAL SIGNAL
t=(0:0.001:1);
y5=t.^2;
subplot(4,3,7:9)
plot(y5,'r');
%axis([-200 1200 -0.5 1.5 ]);------------->not required
xlabel('time(sec)')
ylabel('amplitude')
title('exponential signal');
OUTPUT:
WORKSPACE
COMMAND WINDOW
GRAPH
THEORITICAL CALCULATION:
1. COSINE
Input Signal = cos (2 π 150t)
Frequency = 150 Hz
INPUT WAVEFORM
EXPECTED WAVEFORM
2. SINE
Input Signal = sin (2 π 150t)
Frequency = 150 Hz
INPUT WAVEFORM
PROGRAM:
1. COSINE WAVEFORM
%UNDER SAMPLED SIGNAL
tfinal=0.05;
t=0:0.00005:0.05;
fa=input('enter analog frequency(in Hz):');
%Generate analog signal for the given frequency
xt=cos(2*pi*fa*t);
%condition considered for under sampling is fs is 1.3 times fmax
instead of 2 times of fmax
fs1=1.3*fa;
n1=0:1/fs1:tfinal;
%generate undersampled signal
xn1=cos(2*pi*fa*n1);
%desired frequency is fs is 1.3 times fmax
subplot(3,1,1);
plot(t,xt,'b',n1,xn1,'r*-');
title ('undersampled signal');
2. SINE WAVEFORM
%UNDER SAMPLED SIGNAL
tfinal=0.05;
t=0:0.00005:0.05;
fa=input('enter analog frequency(in Hz):');
%Generate analog signal for the given frequency
xt=sin(2*pi*fa*t);
%condition considered for under sampling is fs is 1.3 times fmax
instead of 2 times of fmax
fs1=1.3*fa;
n1=0:1/fs1:tfinal;
%generate undersampled signal
xn1=sin(2*pi*fa*n1);
%desired frequency is fs is 1.3 times fmax
subplot(3,1,1);
plot(t,xt,'b',n1,xn1,'r*-');
title ('undersampled signal');
OUTPUT:
COSINE WAVE
WORKSPACE
COMMAND WINDOW
GRAPH
SINE WAVE
WORKSPACE
COMMAND WINDOW
GRAPH
THEORITICAL CALCULATION:
Let x1(n)={1,1,2} ={1,1,2,0} &
x2(n)={1,2}={1,2,0,0}
N−1
X(k) = x ( 0 ) W k4 0+ x ( 1 ) W k4 1+ x ( 2 ) W k4 2+ x ( 3 ) W k4 3 ;0≤ k ≤3
X1(k)=1+1W k4 1+ 2W k4 2+ 0W k4 3 ; 0≤ k ≤3
X2(k)=1+2 W k4 1+ 0 W k4 2 +0 W k4 3 ; 0≤ k ≤3
TWIDDLE FACTOR
2π
−j
kn N
W ¿ⅇ
4
OUTPUT
K X1(k) X2(k)
0 1+1(1)+2(1)=4 1+2(1)=3
1 1+1(-j)+2(-1)=-1-j 1+2(-j)=1-2j
2 1+1(-1)+2(1)=2 1+2(-1)=-1
3 1+1(j)+2(-1)= -1+ j 1+2(j)=1+2j
X1(k)={4,-1- j,2,-1+ j}
X2(k)={3,1- 2j,-1,1+2j}
3
1
y(n)= ∑ X ( k ) W −kn
4 ; 0≤ n ≤N-1
4 k=0
1
y(n) = 4 {Y ( 0 ) W −0 n −1 n −2 n −3 n
4 +Y ( 1 ) W 4 +Y ( 2 ) W 4 +Y ( 3 ) W 4 } ; 0≤ n ≤N-1
TWIDDLE FACTOR
2π
j
W −kn
4 ¿ⅇ
N
W 04=1; 4 = j;
W −1 4 =−1;
W −2 W −3
4 =− j
OUTPUT
n y(n)
1
0 {12+(-3+1i)(1)+(-2)(1)+( -3-1i)(1)}=1
4
1
1 {12+(-3+1i)(j)+(-2)(-1)+( -3-1i)(-j)}=3
4
1
2 {12+(-3+1i)(-1)+(-2)(1)+( -3-1i)(-1)}=4
4
1
3 {12+(-3+1i)(-j)+(-2)(-1)+( -3-1i)(j)}=1
4
y(n)={1,3,4,4}
PROGRAM:
%LINEAR CONVOLUTION OF TWO SEQUENCES USING DFT
AND IDFT
OUTPUT:
WORKSPACE
COMMAND WINDOW
GRAPH
THEORITICAL CALCULATION:
Let the two single sided sequences be x1(n) is {2, 3, 4} & x2(n) is {1, 2, 1}
i.e. x1(n) is {2,3,4} &
x2(n) is {1,2,1}
* 2 3 4
1 2 3 4
2 4 6 8
1 2 3 4
y(n) = x(n)*h(n)
PROGRAM:
%SINGLE SIDED LINEAR CONVOLUTION
X1=input('Enter the first sequence =');
X2=input('Enter the second sequence =');
y=conv(X1,X2);
disp('Linear convolution of X1 and X2 is y =');
disp(y);
subplot(2,1,1);
stem(y);
xlabel('Time index n');
ylabel('Amplitude');
title('Convolution output');
subplot(2,2,3);
stem(X1);
xlabel('Time index n');
ylabel('Amplitude');
title('Plot of X1');
subplot(2,2,4);
stem(X2);
xlabel('Time index n');
ylabel('Amplitude');
title('Plot of X2');
OUTPUT:
WORKSPACE
COMMAND WINDOW
GRAPH
THEORITICAL CALCULATION:
* 1 2 3 2 1 3 4
2 2 4 6 4 2 6 8
-3 -3 -6 -9 -6 -3 -9 -12
4 4 8 12 8 4 12 16
-1 -1 -2 -3 -2 -1 -3 -4
0 0 0 0 0 0 0 0
1 1 2 3 2 1 3 4
Therefore the required result after double sided linear convolution is
y(n)={2,1,4,2,6,9,3,2,15,-3,3,4}
PROGRAM:
%DOUBLE SIDED LINEAR CONVOLUTION
X1 =input('Enter the first sequence =');
x1=input('Enter position of zero in the first sequnce = ');
%enter position considering first term as zero not 1.
disp('sequence 1');
disp(X1);
n1 =(length(X1)-(length(X1)+x1)):(length(X1)-(x1+1));
%the above statement helps in determining the range of values
that is taken up by n1
X2 =input('Enter the second sequence =');
x2=input('Enter position of zero in the second sequnce = ');
n2 = (length(X2)-(length(X2)+x2)):(length(X2)-(x2+1));
%the above statement helps in determining the range of values
that is taken up by n2
disp('sequence 2');
disp(X2);
ybegin = n1(1)+n2(1);
yend = n1(length(X1))+n2(length(X2));
ny=[ybegin:yend];
y=conv(X1,X2);
disp('Linear convolution of the two sequence');
disp(y);
subplot(3,2,1:2);
stem(ny,y);
xlabel('Amplitude');
ylabel('Time index n');
title('Convolution output');
subplot(3,2,3:4);
stem(n1,X1);
xlabel('Amplitude');
ylabel('Time index n');
title('Plot of input X1');
subplot(3,2,5:6);
stem(n2,X2);
xlabel('amplitude');
ylabel('time index n');
title('Plot of input X2');
OUTPUT:
WORKSPACE
COMMAND WINDOW
GRAPH
1. COSINE INPUT
2. IMUPLSE INPUT
3. UNIT STEP INPUT
THEORITICAL CALCULATION:
A difference equation with constant coefficient describes a LTI system.
Difference equation containing past samples of output leads to recursive
system, whose impulse response is of infinite duration(IIR).for such systems
the impulse response is computed for large value of ‘n’ (say 100).
The function ‘filter’ evaluates the convolution of an infinite sequence (IIR)
and x[n], which isn’t possible with ‘conv’ function as it requires both the
sequence to be finite.
The difference equation having only y[n] and present and past samples of
input, represents a system whose impulse response is finite in nature hence
both functions ‘filter’ and ‘conv’ can be used.
y(n)-1.5y(n-1)+y(n-2)=2x(n-2
Therefore,
Y (z)
H(z) =
X ( z)
Y (z) 2z
−2
H(z) = =
X ( z ) 1−1.5 z−1+ 1 z−2
Y(z)=X(z)H(z)
1. COSINE INPUT
−1
1−z cos ( 0.05 Π )
X(z) = −1 −2 ROC ∈ ∀ |z|>1
1−2 z cos ( 0.05 Π ) + z
2. IMPULSE INPUT
X(z) = 1 ROC ∈ ∀ z
−2
2z
Y(z) = 1 x −1 −2
1−1.5 z + 1 z
1
X(z) = ROC ∈ ∀ |z|>1
1−z−1
1 2 z−2
Y(z) = −1 x
1−z 1−1.5 z−1+ 1 z−2
PROGRAM:
%DIFFERENCE EQUATION-COSINE INPUT
N=input('The length of the impulse response required: ');
b=input('The co-efficients of numerator: ');
a=input('The co-efficients of denominator: ');
n=0:N-1;
x=cos(0.05*pi*n);
h=filter(b,a,x);
subplot(211);
stem(n,x);
xlabel('Time index n');
ylabel('Amplitude');
title('cosine input');
subplot(212);
stem(n,h);
xlabel('Time index n');
ylabel('Amplitude');
title('Cosine input response');
n=0:N-1;
x=ones(1,N);
h=filter(b,a,x);
subplot(211);
stem(n,x);
xlabel('Time index n');
ylabel('Amplitude');
title('Unit input');
subplot(212);
stem(n,h);
xlabel('Time index n');
ylabel('Amplitude');
title('Unit input response');
OUTPUT:
COSINE INPUT
WORKSPACE
COMMAND WINDOW
GRAPH
IMPULSE INPUT
WORKSPACE
COMMAND WINDOW
GRAPH
COMMAND WINDOW
GRAPH
THEORITICAL CALCULATION:
Discrete Fourier Transform (DFT) is used for performing frequency analysis
of discrete time signals.
DFT gives a discrete frequency domain representation whereas the other
transformations are continuous in frequency domain.
N point DFT of discrete time signals x (n) is given by
N−1
DFT of x (n) = X (k) = ∑ x ( n ) W knN ; 0≤ k ≤N-1
n =0
2π
k −j
, where W N is called as Twiddle factor given by W kn ¿ ⅇ N
4
Determine DFT of {1, 1, 0, and 0} and showcase its magnitude and phase
spectrum.
X (k) = x ( 0 ) W k4 0+ x ( 1 ) W k4 1+ x ( 2 ) W k4 2+ x ( 3 ) W k4 3 ; 0≤ k ≤3
X (k) = 1+1W k4 1+ 0 W k4 2 +0 W k4 3 ; 0≤ k ≤3
TWIDDLE-FACTOR
2 πn
−j
W kn
4 ¿ⅇ
N
OUTPUT
K X(k)
0 1+1(1)=2
1 1+1(-j)=-1-j
2 1+1(-1)=0
3 1+1(j)= 1+ j
X(k) | X (k )|
2 2
1-j √2
0 0
1+j √2
Phase = tan ¿ ¿ real part)
−1
X(k) phase
2 0
1-j −π /2
0 0
1+j π/2
PROGRAM:
%DFT OF A GIVEN SEQUENCE
subplot(2,1,1);
p=abs(X);
disp('amplitude of each term in DFT of the given sequence is ');
disp(p);
%magnitude of each term in DFT
stem(n,p);
xlabel('length');
ylabel('magnitude of X(k)');
title('magnitude spectrum');
q=angle(sym(X));
%if q=angle(X);is used instead of q=angle(sym(X));the phase angle
retrned
%is term of absolute value not in terms of 'PI'
OUTPUT:
WORKSPACE
COMMAND WINDOW
GRAPH
THEORITICAL CALCULATION:
According to Cross-correlation
−∞
rxy(l)= ∑ x ( n ) y (n−l) ; l =± 1, ± 2 ,± 3 , ± 4 , ±5 , ± 6 , ±7 … .
n=∞
OR
−∞
Similarly,
−∞
ryx(l)= ∑ y ( n ) x (n−l) ; l =± 1, ± 2 ,± 3 , ± 4 , ±5 , ± 6 , ±7 … .
n=∞
OR
−∞
Therefore,
rxy(l)=ryx(-l)
We know that,
−∞
X(n)*H(n)= ∑ x ( n ) y (n−m)
m=∞
−∞ −∞
Therefore, rxy(l)= ∑ x ( n ) y (n−l)= ∑ x ( n ) y (−(l−n)) ;
n=∞ n=∞
l =± 1, ± 2 ,± 3 , ± 4 , ±5 , ± 6 , ±7 … .
ryx(l)= x(l)*y(-l)
1.CROSS-CORRELATION
x(l)= [2 -1 3 7 1 2 -3]
y(-l)= [5 -2 1 4 -2 2 -1 1]
ryx(l)= x(l)*y(-l)
2 -1 3 7 1 2 -3
5 -2 1 4 -2 2 -1 1
2 -1 3 7 1 2 -3
-2 1 -3 -7 -1 -2 3 x
4 -2 6 14 2 4 -6 x x
4 -2 6 14 2 4 -6 x x x
8 -4 12 28 4 8 -12 x x x x
2 -1 3 7 1 2 -3 x x x x x
4 -2 6 14 2 4 -6 x x x x x x
10 -5 15 35 5 10 -15 x x x x x x x
10 -9 19 36 -14 33 0 7 13 -18 16 -7 5 -3
ANSWER:
2.AUTOCORRELATION
x(l)= [1 2 3 4]
x(-l)= [4 3 2 1]
ryx(l)= x(l)*x(-l)
1 2 3 4
4 3 2 1
1 2 3 4
2 4 6 8 x
3 6 9 12 x x
4 8 12 16 x x x
4 11 20 30 20 11 4
ANSWER:
PROGRAM:
%GENERAL PROGRAM FOR CROSS CORRELATION
X1 = input('Enter the first sequence = ');
x1=input('Enter position of zero in the first sequnce = ');
%enter position assuming that the leftmost element is beginings as zero and
%the position of zero in the sequence is with respect to this leftmost element
n1 =(length(X1)-(length(X1)+x1)):(length(X1)-(x1+1));
%(x1+1)is subtracted instead of x1 as position numbering begins from with a
%zero
if length(X2)==length(X1)
yend =ybegin+(length(X2)+length(X1))-2;
%this involves subtraction with 2 as yend is found with respect to
%ybegin(which is by itself a position)i.e
%[((length(X2)+length(X1))-1)-1]
else
yend =ybegin+(length(X2)+length(X1))-2+abs((length(X1)-length(X2)));
%although yend=ybegin+(length(X2)+length(X1))-(length(X1)-length(X2))equals
to
%one extra term than required ,its used to accomodate one extra zero
%seen at right most end of plot
end
ny=[ybegin:yend];
if length(X1)>length(X2)
X2=[zeros(1,abs((length(X1)-length(X2)))) X2];
end
%concatenation of 2 matrices
[r,lag]=xcorr(X1,X2);
OUTPUT:
CROSS-CORRELATION
WORKSPACE
COMMAND WINDOW
GRAPH
AUTOCORRELATION
WORKSPACE
COMMAND WINDOW
GRAPH
1. EQUAL IN LENGTH
2. UNEQUAL IN LENGTH
THEORITICAL CALCULATION:
1. EQUAL IN LENGTH
2. UNEQUAL IN LENGTH
ANSWER : (18,16,19,12,15)
PROGRAM:
%GENERAL CIRCULAR CONVOLUTION PROGRAM
X1=input('Enter the first sequence:');
X2=input('Enter the second sequence:');
if length(X1)>length(X2)
b=length(X1);
else
b=length(X2);
end
%b is the length of the largest sequence of the two
y = cconv(X1,X2,b);
OUTPUT:
EQUAL LENGTH
WORKSPACE
COMMAND WINDOW
GRAPH
UNEQUAL LENGTH
WORKSPACE
COMMAND WINDOW
GRAPH
1. EQUAL IN LENGTH
2. UNEQUAL IN LENGTH
THEORITICAL CALCULATION:
1. EQUAL
X (k) = x ( 0 ) W k4 0+ x ( 1 ) W k4 1+ x ( 2 ) W k4 2+ x ( 3 ) W k4 3 ;0≤ k ≤3
X2(k) =1+2 W k4 1+ 3W k4 2+ 4 W k4 3 ; 0≤ k ≤3
ANSWER : (13,14,11,12)
TWIDDLE FACTOR
2π
−j
kn N
W ¿ⅇ
4
2. UNEQUAL
X (k) = x ( 0 ) W k4 0+ x ( 1 ) W k4 1+ x ( 2 ) W k4 2+ x ( 3 ) W k4 3 ;0≤ k ≤3
X1(k) =1+1W k4 1+ 0 W k4 2 +0 W k4 3 ; 0≤ k ≤3
X2(k) =1+2 W k4 1+ 0 W k4 2 +0 W k4 3 ; 0≤ k ≤3
ANSWER : (1,3,2,0)
TWIDDLE FACTOR
2π
−j
kn N
W 4 ¿ⅇ
PROGRAM:
%CIRCULAR CONVOLUTION OF TWO SEQUENCES USING DFT AND IDFT
METHOD.
OUTPUT:
EQUAL
WORKSPACE
COMMAND WINDOW
GRAPH
UNEQUAL
WORKSPACE
COMMAND WINDOW
G
RAPH
THEORITICAL CALCULATION:
2 π ×300
ωs=Ωsx T = 2πfsT = 2000
= 0.3 π rad
2 0.2 π
Ωp ’ = 1
× tan
2
= 0.6498 rad / sec
2 0.3 π
Ωs ' = 1
× tan
2
= 1.0191 rad / sec
[ { }]
-Kp
[ { }]
10 0.1
10 -1 10 -1
log 10 -Ks
log 10 0.4
10 -1
N = 10 10
-1 = = 1.96 ≈ 2
[ 2 log ( ΩΩ ps )] [ (
2 log
0.6498
1.0191 )]
Ωp 0.6498
Ωc = ¿¿¿
= ¿¿¿
= 0.9109 rad /sec
Ωs 1.0191
Ωc = ¿¿¿
= ¿¿¿
= 0.9190 rad /sec
[{ } { } ]
2
s s
+√2 +1
0.919 0.919
0.844561
Ha(s) = 2 1
s + 1.2996 s +0.844561
H(z) = H a (s) s → T
×
[
2 1−z−1
1+ z−1 ]
0.844561
{ [ ]} { [ ]}
2 1
H(z) = 2 1−z−1 2 1−z −1
× + 1.2996 × + 0.844561
1 1+ z −1 1 1+ z −1
2 π ×500
ω s = Ωs x T = 2πfsT = 4000
= 0.25 π rad
2 0.05 π
Ωp‘= 1
× tan
2
= 0.1574 rad / sec
2 0.25 π
Ωs‘= 1
× tan
2
= 0.8284 rad / sec
1
δp = 1− = 0.20567
√ 1+ ∈2
1
Ks = 20 log δs = -2 ⇒ δs=
10
=0.1
'
Ω p 0.1574
K = '
= =0.19
Ω s 0.8284
D=
√ (1−δp)2−1
2
δs −1
=0.0768
−1 1
cosh ( )
d
N≥ 1
⇒ N ≥ 1.39
cosh−1 ( )
k
N = 2
1 1+ √1+ ɛ N 1 1+ √ 1+ ɛ N
2 1 2 −1
A = { } − { } = 0.568
2 ɛ 2 ɛ
1 1+ √1+ ɛ N 1 1+ √ 1+ɛ N
2 1 2 −1
B = { } + { } = 1.15
2 ɛ 2 ɛ
π
σ k =−a sin ((2 k −1) )
2N
(
Ωk =−a cos ( 2 k−1 )
π
2N ) ; Where k=1,2,3 …. 2N
K σk Ωk
1 -0.401 0.813
2 -0.401 -0.813
b0 = 0.82177
0.82177
KN ¿ √ 1+ ϵ 2 = 0.653
0.653
H2(s) = 2
s + 0.802 s+ 0.82177
Ha(s)=H2(s)s->s/(0.1574)
0.653
Ha(s)= ( s
2
) +0.644 (
s
)+ 0.7074
0.1574 0.1574
0.01618
Ha(s)= s 2+ 0.126 s +0.0204
[ ]
−1
2 1−z
H(z) = H a (s) s → T × −1
1+ z
0.01618
{ [ ]} { [ ]}
2 1
−1 −1
H(z) = 2 1−z
×
2 1−z
+ 0.126 × +0.0204
1 1+ z −1 1 1+ z
−1
PROGRAM:
%DIGITAL BUTTERWORTH LOWPASS FILTER USING
BILINEAR TRANSFORM.
% Digital Butterworth Lowpass Filter using Bilinear
Transformation
% inputs : 200, 1, 300, 4, 2000
fp = input('Enter pass band edge frequency in Hz = ');
Ap = input('Enter pass band ripple in dB = ');
fs = input('Enter stop band edge frequency in Hz = ');
As = input('Enter stop band ripple in dB = ');
Fs = input('Enter the sampling frequency in Hz = ');
W1 = 2*pi*fp/Fs;
W2 = 2*pi*fs/Fs;
% Prewarping
Wp = 2*tan(W1/2);
Ws = 2*tan(W2/2);
W1 = 2*pi*fp/Fs;
W2 = 2*pi*fs/Fs;
% Prewarping
Wp = 2*tan(W1/2);
Ws = 2*tan(W2/2);
OUTPUT:
BLT -BUTTERWORTH LOWPASS
WORKSPACE
COMMAND WINDOW
GRAPH
BLT-CHEBYSHEV LOWPASS
WORKSPACE
COMMAND WINDOW
GRAPH
1. LOWPASS FILTER
2. HIGH PASS FILTER
3. BANDPASS FILTER
THEORITICAL CALCULATION:
For a Chebyshev filter the magnitude response is given by
Then,
Where,
Where,
1. LOWPASS FILTER
N ≥ 1.12
N = 2
1 1+ √1+ ɛ N 1 1+ √ 1+ ɛ N
2 1 2 −1
A = { } − { } = 0.4560239
2 ɛ 2 ɛ
1 1+ √1+ ɛ 2 N 1 1+ √ 1+ɛ 2 N
1 −1
B = { } + { } = 1.09907
2 ɛ 2 ɛ
π
σ k =−a sin ((2 k −1) )
2N
(
Ωk =−a cos ( 2 k−1 )
π
2N ) ; Where k=1,2,3 …. 2N
K σk Ωk
1 -0.322 0.777
2 -0.322 -0.777
0.5008
H2(s)= s 2+ 0.644 s +0.7074
0.7074
KN ¿ √ 1+ϵ 2
Ha(s)=H2(s)s->s/(4π)
0.5008
Ha(s)= ( s ) +0.644 ( s )+0.7074
2
4π 4π
79.083
Ha(s)= s 2+ 8.093 s+ 111.708
2. HIGHPASS FILTER
No theory required
No Theory required
PROGRAM:
%DESIGN OF ANALOG CHEBYSHEV LOW PASS FILTER
fp = input('Enter pass band edge frequency in Hz = ');
rp = input('Enter pass band ripple in dB = ');
fs = input('Enter stop band edge frequency in Hz = ');
rs = input('Enter stop band ripple in dB = ');
Wp = 2*pi*fp;
disp(Wp);
Ws = 2*pi*fs;
disp(Ws);
[n, Wp] = cheb1ord(Wp, Ws, rp, rs, 's');
disp('The order of the filter is N = ');
disp(n);
Wp is the Chebyshev natural frequency to use with
CHEBY1 to achieve the required specifications.
disp('The cutoff frequency is = ');
disp(Wp);
disp('The numerator and denominator coefficients of the analog
filter are ');
% for low pass
[b, a] = cheby1(n, rp, Wp,'low','s');
disp('Numerator co-efficients :');
disp(b);
disp('Denominator co-efficients :');
disp(a);
[h, w] = freqs(b, a);
m = 20*log10(abs(h));
an = (angle(h)*180/pi);
subplot(211);
plot(w, m);
title('Frequency response of analog Chebyshev filter');
xlabel('Frequency in rad/s');
ylabel('Magnitude in dB');
grid on;
subplot(212);
plot(w, an);
title('Phase response of analog Chebyshev filter');
xlabel('Frequency in rad/sec');
ylabel('Phase(degrees)');
grid on;
OUTPUT:
CHEBYSHEV LOWPASS
WORKSPACE
COMMAND WINDOW
GRAPH
1. LOWPASS FILTER
2. HIGH PASS FILTER
3. BANDPASS FILTER
THEORITICAL CALCULATION:
For a butterworth filter
1. LOWPASS FILTER
Pass band edge frequency (fp) = 2Hz i.e. Ωp = 4 Π rad/sec
10 π
Ωs = 4π
= 2.5 rad/sec (backward transformation for low pass filter)
New normalized Ω p = 1
[ { }]
-Kp
[ { }]
10 0.3
10 -1 10 -1
log 10 -Ks
log 10 1
10 -1
N = 10 10
-1 = = 1.2015 ≈ 2
[ ( )]
2 log
Ωp
Ωs
[ ( )]
2 log
1
2.5
Ωp 1
Ωc = ¿¿¿
= ¿¿¿
= 1 rad /sec
Ωs 2.5
Ωc = ¿¿¿
= ¿¿¿
= 1.4434 rad /sec
(In order to get a curve which satisfy cutoff frequency closely we use Ωc based on cutoff parameter
instead of one based on pass band based cutoff)
1
H a ( s )=
[{ } { } ]
2
s s
+√ 2 +1
18.138 18.138
328.9868
Ha(s) = 2 1
s + 25.6510 s +328.9868
2. HIGHPASS FILTER
Pass band edge frequency (fp) = 5Hz i.e. Ωp = 10 Π rad/sec
Normalized filter
10 π
Ωs = 4π
= 2.5 rad/sec (backward transformation for high pass filter)
New normalized Ω p = 1
[ { }]
-Kp
[ { }]
10 0.3
10 -1 10 -1
log 10 -Ks
log 10 1
10 -1
N = 10 10 -1 = = 1.2015 ≈ 2
[ ( )]
2 log
Ωp
Ωs
[ ( )]
2 log
1
2.5
Ωp 1
Ωc = ¿¿¿
= ¿¿¿
= 1 rad/sec
Ωs 2.5
Ωc = ¿¿¿
= ¿¿¿
= 1.4434 rad/sec
(In order to get a curve which satisfy cutoff frequency closely we use Ωc based on cutoff parameter
instead of one based on pass band based cutoff)
1
H a ( s )=
[{ }{ } ]
2
21.765 21.765
+ +1
s s
s2
Ha(s) =
s 2+ 30.781 s1 +473.73
3. BANDPASS FILTER
Lower stop band edge frequency (f1) = 20Hz i.e. Ω1 =40π rad/sec
Lower stop band edge frequency (f2) = 45kHz i.e. Ω2 =90000π rad/sec
OR
2
−Ω 1 +Ω l Ωu
A = = 2.51
Ω1 (Ω ¿ ¿ l−Ω u) ¿
2
Ω2 −Ωl Ωu
B = = 2.25
Ω2 (Ω ¿ ¿ u−Ωl )¿
Ωs = Min{|A|,|B|} = 2.25
[ { }]
-Kp
N =
log 10
10 10 -1
10
-Ks
10
-1 =
[ {log 10
100.30103 -1
2
10 -1 }] = 2.83 ≈
[ ( )]
2 log
Ωp
Ωs
[ ( )]
2 log
1
2.25
Ωp 1
Ωc = ¿¿¿
= ¿¿¿
= 0.99979 rad/sec
Ωs 2.25
Ωc = ¿¿¿
= ¿¿¿
= 1.0461 rad/sec
(In order to get a curve which satisfy cutoff frequency closely we use Ωc based on cutoff parameter
instead of one based on pass band based cutoff)
1.9695 x 1015 s3
Ha(s) = s 6 +2.51 x 105 s 5+ 3.154 x 1010 s 4 +1.989 x 1015 s 3
+ 1.2453 x 1018 s2 +3.9073 x 1020 s +6.1529 x 1022
PROGRAM:
%ANALOG BUTTERWORTH LOW PASS FILTER
fp = input('Enter pass band edge frequency in Hz = ');
rp = input('Enter pass band ripple in dB = ');
fs = input('Enter stop band edge frequency in Hz = ');
rs = input('Enter stop band ripple in dB = ');
Wp = 2*pi*fp;
disp('pass band edge frequency in rad/sec:');
disp(Wp);
Ws = 2*pi*fs;
disp('stop band edge frequency in rad/sec:');
disp(Ws);
[n, Wn] = buttord(Wp, Ws, rp, rs,'s');
disp('The order of the filter is N = ');
disp(n);
disp('The cutoff frequency is = ');
disp(Wn);
disp('The numerator and denominator coefficients of the analog
filter are ');
[b, a] = butter(n,Wn,'low','s');
disp('Numerator co-efficients :');
disp(b);
disp('Denominator co-efficients :');
disp(a);
[h, w] = freqs(b, a);
m = 20*log10(abs(h));
an = (angle(h)*180/pi);
subplot(211);
plot(w, m);
title('Frequency response of analog butterworth filter');
xlabel('Frequency in rad/s');
ylabel('Magnitude in dB');
grid on;
subplot(212);
plot(w, an);
title('Phase response of analog Butterworth filter');
xlabel('Frequency in rad/sec');
ylabel('Phase(degrees)');
grid on;
OUTPUT:
BUTTERWORTH LOWPASS
WORKSPACE
COMMAND WINDOW
GRAPH
BUTTERWORTH HIGHPASS
WORKSPACE
COMMAND WINDOW
GRAPH
BUTTER
WORTH BANDPASS
WORKSPACE
COMMAND WINDOW
GRAPH
THEORY:
The window function that satisfies the pass band or stop band specifications is
selected (refer table). The number of coefficients is determined using the
appropriate relationship between the filter length and transition bandwidth.
Filter coefficients are given by
h(n) = hd(n) w(n)
where hd(n) is the ideal desired impulse response of the filter which is obtained by
inverse Fourier transform of the desired frequency response HD(ω) and w(n) is the
window function.
The desired filter can be a low pass, high pass, band pass or band reject filter.
Routine fir1 in its various forms is used to design filters based on various windows.
if you need help use the matlab help or happy googling
PROGRAM:
%PLOT THE MAGNITUDE RESPONSE AND CHECK THE
SPECIFICATION OF THE OBTAINED FILTER
clc
clear
closeall
% Pass band and Stop band edge frequencies
fp =2000;
fs=4000;
Fs=20000; % sampling frequency
fn=Fs/2; %normalizing to half the sampling frequency
fc=(fs+fp)/2/fn; %cut off frequency (normalized to fn)
%normalized transition width
tw=(fs-fp)/Fs;
N=ceil(4/tw);
% as stop band attenuation is = 40db
wn=harming(N);% window coefficients
%calculate truncated ideal impulse response coeffs
hd =fir1(N-1,fc,boxcar(N));
% to obtain Windowed coefficients
hn=fir1(N-1,fc,wn);
% Frequency response
[H,f] = freqz(hn,1,512,Fs);
mag = 20*log10(abs(H));
plot(f,mag)
grid on
xlabel(‘Frequency(Hz)’);
ylabel(‘Magnitude response (dB)’);
title('Frequency response of BPF’);
clc
clear
closeall
%Passband and transition width
fp1=150;
fp2=250;
tw=50;
Fs=1000; % sampling frequency
fn=Fs/2; % normalised to half the sampling frequency
fc1=(fp1-(tw/2))/fn; % cutoff frequency(normalised to fn)
fc2=(fp2+(tw/2))/fn;
tw=tw/Fs; %Normailsed transition width
% stopband attenuation is 60db so we choose blackman window
N=ceil(6/tw);
wn=blackman(N);
%calculate truncated ideal impulse response coeffs
hn=fir1(N-1,[fcl,fc2),boxcar(N));
% obtain windowed coefficients
hn=firl(N-1,[fcl,fc2],wn);
[H,f]=freqz(hn,1,512,Fs);
mag =20*log10(abs(H));
plot(f,mag)
grid on;
xlabel(‘Frequency(Hz)’);
ylabel('Magnitude response (dB)’);
title(‘Frequency response of BPF’);