DSP Lab Sheet 2 PDF
DSP Lab Sheet 2 PDF
DSP Lab Sheet 2 PDF
FOR
ELECTRICAL AND ELECTRONIC
ENGINEERING
1
REFERENCE BOOK:
2
CONTENTS
7 Computation of the factored form and to generate the pole-zero plot of the
Z-transform.
3
EXPERIMENT NO-1
Sampling: Is the process of converting a continuous time signal into a discrete time
signal. It is the first step in conversion from analog signal to digital signal.
Nyquist Rate Sampling: The Nyquist rate is the minimum sampling rate required to
avoid aliasing, equal to the highest modulating frequency contained within the signal.
In other words, Nyquist rate is equal to two sided bandwidth of the signal (Upper and
lower sidebands)
To avoid aliasing, the sampling rate must exceed the Nyquist rate. i.e. fs > fN
Program:
% Nyquist Rate Sampling.
clc; % Clear screen
fs = 1400; % Sampling frequency, fs = 1400Hz
t = 0:1/fs:13/fs; % Number of samples
x = cos(2*pi*400*t)+cos(2*pi*700*t); % Input signal
xm = abs (fft(x)); % Determine the absolute FFT of the input signal
disp(‘xm’); % Displays xm on command window
disp (xm); % Displays values of xm on command window
k = 0:length(xm)-1; % Number of samples to be plot on x-axis
subplot (2,2,1); % Divide the figure window to plot the output
stem (100*k, xm); % Plot the output
xlabel ('Hz'); % Name x-axis as “Hz”
ylabel ('Magnitude'); % Name y-axis as “Magnitude”
title ('NR sampling'); % Title is “NR Sampling”
%UNDER Sampling.
fs = 1000; % Sampling frequency, fs = 1000Hz
t = 0:1/fs:9/fs; % Number of samples
x = cos(2*pi*400*t)+cos(2*pi*700*t); % Input signal
xm1 = abs(fft(x)); % Determine the absolute FFT of the input signal
disp(‘xm1’); % Displays xm1 on command window
disp (xm1); % Displays values of xm1 on command window
k = 0:length(xm1)-1; % Number of samples to be plot on x-axis
subplot(2,2,2); % Divide the figure window to plot the output
stem(100*k, xm1); % Plot the output
xlabel('Hz'); % Name x-axis as “Hz”
ylabel('Magnitude'); % Name y-axis as “Magnitude”
4
title('UNDER sampling'); % Title is “UNDER Sampling”
%OVER Sampling.
fs = 2000; % Sampling frequency, fs = 2000Hz
t = 0:1/fs:20/fs; % Number of samples
x = cos(2*pi*400*t)+cos(2*pi*700*t); % Input signal
xm2 = abs(fft(x)); % Determine the absolute FFT of the input signal
disp(‘xm2’); % Displays xm2 on command window
disp (xm2); % Displays values of xm2 on command window
k = 0:length(xm2)-1; % Number of samples to be plot on x-axis
subplot(2,2,3); % Divide the figure window to plot the output
stem(100*k,xm2); % Plot the output
xlabel('Hz'); % Name x-axis as “Hz”
ylabel('Magnitude'); % Name y-axis as “Magnitude”
title('OVER sampling'); % Title is “OVER Sampling”
Output :
xm
Columns 1 through 7
0.0000 0.0000 0.0000 0.0000 7.0000 0.0000 0.0000
Columns 8 through 14
14.0000 0.0000 0.0000 7.0000 0.0000 0.0000 0.0000
xm1
Columns 1 through 7
0.0000 0.0000 0.0000 5.0000 5.0000 0.0000 5.0000
Columns 8 through 10
5.0000 0.0000 0.0000
xm2
Columns 1 through 7
2.0000 2.0741 2.3496 3.1607 11.5127 0.4052 1.8998
Columns 8 through 14
8.6165 4.2355 1.2552 0.3357 0.3357 1.2552 4.2355
Columns 15 through 21
8.6165 1.8998 0.4052 11.5127 3.1607 2.3496 2.0741
5
Graph:
Questions:
1. What is sampling?
2. What is the effect of under sampling?
3. X(t)=5cos100πt+90sin700πt, if this signal is sampled at 500samples/sec then after reconstruction
which signal will be found?
6
EXPERIMENT NO-2
If the input to the system is unit impulse i.e. x(n) = δ(n) then the output of the system is
known as impulse response denoted by h(n) where,
h(n) = T[δ(n)]
we know that any arbitrary sequence x(n) can be represented as a weighted sum of
discrete impulses. Now the system response is given by,
∞
y(n) = T[x(n)] = T[∑x(k) δ(n-k)] …(1)
k=-∞
For linear system (1) reduces to
∞
y(n) = ∑x(k) T[δ(n-k)] …(2)
k=-∞
The response to the shifted impulse sequence can be denoted by h(n, k) is denoted by,
7
For a linear time-invariant system if the input sequence is x(n) and impulse response
h(n) is given, we can fine output y(n) by using eqn (6).
Which is known as convolution sum and can be represented by y(n) = x(n) * h(n)
M M
y(n) = ∑ak y(n-k) + ∑bk x(n-k) …(7)
k=1 k=0
For input x(n) = δ(n)
M
y(n) = ∑bk x(n-k) = 0 for n > M
k=0
Now (7) can be written as,
N
y(n) = ∑ak y(n-k) = 0 a0 = 1 …(8)
k=0
The solution of eqn (8) is known as homogeneous solution. The particular solution is
zero since x(n) = 0 for n > 0, that is
yp(n) = 0
Therefore we can obtain the impulse response by solving the homogeneous equation and
imposing the initial conditions to determine the arbitrary constants.
Example:
Let’s take a second order difference equation
8
The roots of the characteristic equation are,
λ1 = 1/2, λ2 = -1/3
n n
The solution yh(n) = C1 (1/2) + C2 (-1/3) …(13)
For impulse x(n) = δ(n); x(n) = 0 for n > 0 and x(0) = 1
For n = 0,
For n = 1,
9
Compute y(n) for various values of n
Substitute n = 0, 1, 2, 3 in eqn (18) we get,
y(0) = 1.0000
y(1) = 0.1667
y(2) = 0.1944
y(3) = 0.0602
So, the impulse response for the given difference equation using eqn (19) is
h(n) = {1.0000, 0.1667, 0.1944, 0.0602}
Program:
clc; % Clear screen
nr = [1]; % Numeartor co-efficients
dr = [1,-1/6,-1/6]; % Denominator co-efficients
h = impz(nr,dr,4); % Computes the impulse response of a given
% difference equation, returns the co-efficients
disp('Impulse response h(n):');% Displays impulse response
disp(h); % Displays impulse response on command window
subplot(2,1,1); % Divide the figure window to plot the output
stem(h); % Displays the impulse response
title('Impulse response h(n):');% Title as impulse response
x = [1]; % Input co-efficients
y = conv (h, x); % Convolution of input and impulse co-efficients to get the
% output
disp('Output y(n):') % Displays Output y(n)
disp(y); % Displays the output on command window
subplot(2,1,2); % Divide the figure window to plot the output
stem(y); % Plots the output
title('Output y(n) for given x(n)'); % Title as “Output y(n) for given x(n)”
Output:
Impulse
response:
1.0000 0.1667
0.1944 0.0602
Output y(n):
1.0000
0.1667
0.1944
0.0602
10
Graph:
Questions:
11
EXPERIMENT NO-3
Mathematic Formula:
The linear convolution of two continuous time signals x(t) and h(t) is defined by
Where x(n) is the input signal and h(n) is the impulse response of the system.
Example:
x(n) = {1, 2, 3, 1}
h(n) = {1, 1, 1}
12
x(k) h(k)
3
2
1 1 1 1 1
-4 -3 -2 -1 0 1 2 3 4 5 6 7 0 1 2
∞
n=0; h(-k ) y(0) = ∑ x(k) h(-k) = 1
1 k=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
∞
n=1; h(1-k) y(1) = ∑ x(k) h(1-k) = 1+2=3
1 k=- ∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
∞
n=2; h(2-k) y(2) =∑ x(k) h(2-k) = 1+2+3=6
1 k=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
∞
n=3; h(3-k) y(3) =∑ x(k) h(3-k) = 2+3+1=6
1 k=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
∞
n=4; h(4-k) y(4) = ∑ x(k) h(4-k) = 3+1=4
1 k=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
∞
n=5; h(5-k) y(5) = ∑ x(k) h(5-k) = 1
1 k=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
13
6 6
4
3
y(n) = {1, 3, 6, 6, 4, 1}
1 1
-4 -3 -2 -1 0 1 2 3 4 5 6 7
Program:
Output:
14
Graph:
Questions:
15
EXPERIMENT NO-4
Circular convolution:
Let x1(n) and x2(n) are finite duration sequences both of length N with DFT’s X1(k)
and X2(k). Convolution of two given sequences x1(n) and x2(n) is given by
the equation,
x3(n) = IDFT[X3(k)]
N-1
x3(n) = ∑ x1(m) x2((n-m))N
m=0
Example:
x1(m) x2(m)
1 2
x1(1) x2(1)
x1(3) x2(3)
1 4
16
To get x2(-m), rotate x2(m) by 4 samples in clockwise direction.
x2(-m)
4
x2(3)
x2(2) x2(0)
3 1
x2(1)
2
x3(0) = x1(m) x2(-m)
= x1(0) x2(0) + x1(1) x2(3) + x1(2) x2(2) + x1(3) x2(1)
= 1 + 4 + 6 +2
x3(0) = 13
Keep x1(m) constant and rotate x2(-m) once to compute further values.
x2(1-m)
1
x2(0)
x2(3) x2(1)
4 2
x2(2)
3
17
To get x3(2) rotate x2(1-m) by one sample in anti-clockwise direction
x2(2-m)
2
x2(1)
1 3
x2(0) x2(2)
x2(3)
4
x3(2) = x1(m) x2(2-m)
= x1(0) x2(2) + x1(1) x2(1) + x1(2) x2(0) + x1(3) x2(3)
= 3 + 2 + 2+ 4
x3(2) = 11
To get x3(3) rotate x2(2-m) by one sample in anti-clockwise direction
x2(3-m)
3
x2(2)
2 4
x2(1) x2(3)
x2(0)
1
x3(3) = x1(m) x2(3-m)
= x1(0) x2(3) + x1(1) x2(2) + x1(2) x2(1) + x1(3) x2(0)
=4+3+4+1
x3(3) = 12
The convoluted signal is,
x3(n) = {13, 14, 11, 12}
18
Program:
clc; % Clear screen
x1 = input('Enter the first sequence:'); % Get the first sequence
x2 = input('Enter the second sequence:'); % Get the second sequence
N1 = length(x1); % Returns the length of first sequence
N2 = length(x2); % Returns the length of second sequence
N = max(N1,N2); % Get the Maximum length out of two signals
N3 = N1-N2;
% Get equal length sequence
if(N3>0)
x1 = [x1,zeros(1, N3)]; % Pad zeros to the first sequence if N2>N1
else
x2 = [x2,zeros(1, -N3)]; % Pad zeros to the second sequence is N1>N2
end
% Computation of circular convolution
for m=1:N
y(m) = 0;
for n=1:N
i=m-n+1;
if(i<=0)
i=N+i;
end
y(m) = y(m)+x1(n)*h(i);
end
end
disp('The circular convolution of two given sequences is:');
disp(y); % Displays the result on command window
n=0:N-1; % Get the length for x-axis
stem(n, y); % Plot the result
xlabel('Time'); % Name the x-axis as “Time”
ylabel('Magnitude'); % Name the y-axis as “Magnitude”
title('Circular convolution'); % Title as Circular convolution
Output:
Enter the first sequence: [1 1 2 1]
Enter the second sequence: [1 2 3 4]
The circular convolution of two given sequences
is: 13 14 11 12
19
Graph:
Questions:
20
EXPERIMENT NO-5
Correlation: Correlation determines the degree of similarity between two signals. If the
signals are identical, then the correlation coefficient is 1; if they are totally different, the
correlation coefficient is 0, and if they are identical except that the phase is shifted by
exactly 1800(i.e. mirrored), then the correlation coefficient is -1.
Autocorrelation: The Autocorrelation of a sequence is correlation of a sequence with
itself. The autocorrelation of a sequence x(n) is defined by,
Or equivalently
Example:
Let’s take a sequence x(n) = {1, 2, 3, 4}
x(n) 4
3
2
1
-4 -3 -2 -1 0 1 2 3 4 5 6 7
4
3 ∞
k=0; x(n) 2 Rxx(0) = ∑ x(n) x(n) = 4
1 n=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
21
4
3 ∞
k=1; x(n+1) 2 Rxx(1) = ∑ x(n) x(n+1) = 8+3=11
1 n=- ∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
4
3 ∞
k=2; x(n+2) 2 Rxx(2) =∑ x(n) x(n+2) = 12+6+2=20
1 n=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
4
3 ∞
k=3; x(n+3) 2 Rxx(3) =∑ x(n) x(n+3) = 16+9+4+1=30
1 n=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
4
3 ∞
k=4; x(n+4) 2 Rxx(4) = ∑ x(n) x(n+4) = 12+6+2=20
1 n=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
∞
k=5; x(n+5) 4 Rxx(5) = ∑ x(n) x(n+5) = 8+3=11
3 n=-∞
2
1
-4 -3 -2 -1 0 1 2 3 4 5 6 7
∞
k=6; x(n+6) 4 Rxx(6) = ∑ x(n) x(n+6) = 4
3 n=-∞
2
1
-4 -3 -2 -1 0 1 2 3 4 5 6 7
22
30
20 20
11 11
-4 -3 -2 -1 0 1 2 3 4 5 6 7
Program:
clc; % Clear screen
x = input('Enter the input sequence:'); % Get the input sequence
Rxx = xcorr(x); % Returns auto-correlated sequence
disp('Auto correlated sequence, Rxx:'); % Display the result
disp(Rxx); % Display the result on command window
t = 0:length(Rxx)-1; % Length to plot the graph
stem(t,Rxx); % plots the result on figure window
xlabel('Time'); % xlabel
ylabel('Magnitude'); % ylabel
title('Auto-correlation of the given sequence:'); % Title
Output:
Enter the input sequence: [1 2 3 4]
Auto correlated sequence, Rxx:
4.0000 11.0000 20.0000 30.0000 20.0000 11.0000 4.0000
23
Graph:
Properties of Autocorrelation:
Questions:
1. What is correlation?
2. Why autocorrelation is necessary?
3. Perform autocorrelation of the sequence [1 5 2 9 7]?
24
EXPERIMENT NO-6
Correlation: Correlation determines the degree of similarity between two signals. If the
signals are identical, then the correlation coefficient is 1; if they are totally different, the
correlation coefficient is 0, and if they are identical except that the phase is shifted by
exactly 1800(i.e. mirrored), then the correlation coefficient is -1.
Cross-correlation: When two independent signals are compared, the procedure is known
as cross-correlation. It is given by,
Or equivalently
∞
Ryx (k) = ∑ y(n+k) x(n)
n=-∞
So, we can write Cross correlation of the two sequences is given by,
Equation (1) shows that cross correlation is the essentially the convolution of two
sequences in which one of the sequences has been reversed.
25
Example:
Let’s take x(n) = {1, 1, 1, 1} and y(n) = {1, 2, 3, 4}
x(n)
1 1 1 1
-4 -3 -2 -1 0 1 2 3 4 5 6 7
4
3 ∞
k=0; y(-n) 2 Rxy(0) = ∑ x(n) y(-n) = 4
1 n=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
4
3 ∞
k=1; y(1-n) 2 Rxy(1) = ∑ x(n) y(1-n) = 4+3=7
1 n=- ∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
4
3 ∞
k=2; y(2-n) 2 Rxy(2) =∑ x(n) y(2-n) = 4+3+2=9
1 n=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
4
3 ∞
k=3; y(3-n) 2 Rxy(3) =∑ x(n) y(3-n) = 4+3+2+1=10
1 n=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
k=4; y(4-n)
4
3 ∞
2 Rxy(4) = ∑ x(n) y(4-n) = 3+2+1=6
1 n=-∞
-4 -3 -2 -1 0 1 2 3 4 5 6 7
26
k=5; y(5-n)
∞
4 Rxy(5) = ∑ x(n) y(5-n) = 2+1 = 3
3 n=-∞
2
1
-4 -3 -2 -1 0 1 2 3 4 5 6 7
k=6; y(6-n)
∞
4 Rxy(6) = ∑ x(n) y(6-n) = 1
3 n=-∞
2
1
-4 -3 -2 -1 0 1 2 3 4 5 6 7
10
4
3 Rxy(k) = {4, 7, 9, 10, 6, 3,
1}
1
-4 -3 -2 -1 0 1 2 3 4 5 6 7
27
Program:
clc; % Clear screen
x = input('Enter the first sequence:'); % Get the first sequence
y = input('Enter the second sequence:'); % Get the second sequence
Rxy = xcorr(x,y); % Returns cross correlated sequence
disp('Cross correlated sequence, Rxy:'); % Display the result
disp(Rxy); % Display the result on command window
t = 0:length(Rxy)-1; % Length to plot the graph
stem(t, Rxy); % plots the result on figure window
xlabel('Time'); % xlabel
ylabel('Magnitude'); % ylabel
title('Cross correlation of the given two sequences:'); % Title
Output:
Enter the first sequence: [1 1 1 1]
Enter the second sequence: [1 2 3 4]
Cross correlated sequence, Rxy:
4.0000 7.0000 9.0000 10.0000 6.0000 3.0000 1.0000
Graph:
28
Properties of cross correlation:
Questions:
29
EXPERIMENT NO-7
The z-Transform:
Where "z" is a complex variable. The set "R" of values of "z" for which the z-transform "X(z)"
converges is called its region of convergence (ROC). In the case of Linear Time Invariant LTI
discrete time system, all pertinent z-transforms are rational functions of z-1, that is, they are
ratios of two polyomials in z-1:
The roots of "N(z)", i.e. the values of "z" for which N(z) = 0, are known as the zeros of the
systems while the roots of "D(z)", i.e.e the values of "z" for D(z) = 0 are know as the poles of the
system. The pole-zero plot of a rational z-transform "X(z)" can be readily obtained using the
function "zplane". There are two versions of this function. If the z-transfrom is given in the form
of a rational function, the command to use is "zplane(num, den)", where "num" and "den" are the
row vectors containing the coefficients of the numerator and denominator polynomials of "X(z)"
in ascending power of z-1. On the other hand, if the zeros and poles of "X(z)" are given, the
command to use is "zplane(zeros, poles)", where "zeros" and "poles" are column vectors. In the
pole-zero plot generated by MATLAB , the location of a pole is indicated by symbol "X" and the
location of a zeros is indicated by the symbol "O". The function "tf2zp" can be used to determine
the zeros and poles of a rational z-transform "X(z)". The program statement to use is "[z, p, k] =
tf2zp(num, den)", where "num" and "den" are the row vectors which
containing the coefficients of the numerator and denominator polynomials of "X(z)" in ascending
powers of z-1 and the output file contains the gain constant "k" and the computed zeros and poles
given as column vectors "z" and "p", respectively. The factored form of the z-transform can be
obtained from the zero-pole description using the function "sos = zp2sos(z, p, k)". The function
computes the coefficients of each second-order factor given as "L X 6" matrix "sos", where:
30
The MATLAB program, given below computes and display the Poles & Zeros, the factored form
and generates the pole-zero plot of the z-transform given by:
Program:
%This program computes and display the Poles & Zeros, the factored
form
%and generates the pole-zero plot of the z-transform. clf;
%Clear all figures
clear all; %Clear all variables close
all; %Close all figures
num = [32 56 4 16 2]; %Numerator coefficients
den = [-12 18 -15 3 3]; %Denominator coefficients %Compute and
display the Poles, Zeros and Gain constant [z,p,k] =
tf2zp(num,den);
disp('Zeros are at'); disp(z);
disp('poles are at'); disp(p);
disp('gain constant'); disp(k);
%Compute and display the factored form of the z-Transform sos=
zp2sos(z,p,k);
disp('Second order sections');
disp(real(sos));
%Pole-Zero Plot
zplane(num,den);
title('Pole-Zero Plot');
grid;
Output:
Zeros are at
-1.8217
0.0972 + 0.5197i
31
0.0972 -
0.5197i -0.1227
poles are at
0.5000 + 0.8660i
0.5000 - 0.8660i
0.8090
-
0.3090
gain constant
-2.6667
Graph:
Questions:
32
EXPERIMENT NO-8
The sequence of N complex numbers x0,..., xN−1 is transformed into the sequence of
N complex numbers X0, ..., XN−1 by the DFT according to the formula:
N-1
X(k) = ∑x(n)e-j2πnk/N k = 0,1, …. N-1
n=0
Example:
Lets assume the input sequence x[n] = [1 1 0 0]
We have,
N-1
X(k) = ∑x(n)e-j2πnk/N k = 0,1, …. N-1 n=0
For k = 0,
3
X(0) = ∑x(n) = x(0) + x(1) + x(2) + x(3)
n=0
X(0) = 1+1+0+0 = 2
For k = 1,
3
X(1) = ∑x(n)e-jπn/2 = x(0) + x(1) e-jπ/2 + x(2) e-jπ + x(3) e-
j3π/2
n=0
= 1 + cos(π/2) - jsin(π/2)
X(1) = 1 – j
33
For k = 2,
3
X(2) = ∑x(n)e-jπn = x(0) + x(1) e-jπ + x(2) e-j2π + x(3) e-
j3π
n=0
= 1 + cos π – jsin π
X(2) = 1-1 = 0
For k = 3,
3
X(3) = ∑x(n)e-j3nπ/2 = x(0) + x(1) e-j3π/2 + x(2) e-j3π + x(3) e-
j9π/2
n=0
= 1 + cos(3π/2) - jsin(3π/2)
X(3) = 1 + j
Program:
clc; % Clear screen
x1 = input('Enter the sequence:'); % Get the input sequence
n = input('Enter the length:'); % Get the value of N
m = abs(fft(x1,n)); % Computes the DFT using FFT algorithm
disp('N-point DFT of a given sequence:'); % Display the results
disp(m); % Displays the result on command window
N = 0:1:n-1; % Decides the length to plot the results
34
xlabel('Length'); % Name x-axis as “Length”
ylabel('Phase of X(k)'); % Name y-axis as “Phase of X(k)”
title('Phase spectrum:'); % Title as “Phase spectrum”
Output:
Phase of X(k)
0 -0.7854 0 0.7854
Graph:
1.5 0.5
Phase of X(k)
1 0
0.5 -0.5
00 1 2 3 -10 1 2 3
Length Length
Questions:
1. What is DFT?
2. Perform Discrete Fourier Transform of the sequence [1 5 2 9 7 3 9 2].
3. Plot Magnitude and phase spectrum.
35
EXPERIMENT NO-9
Mathematic Formula:
The linear convolution of two continuous time signals x(t) and h(t) is defined by
Where x(n) is the input signal and h(n) is the impulse response of the system.
Example:
x1(n) = {1, 1, 2}
x2(n) = {1, 2}
For linear convolution,
Length N = Length(x1) + Length(x2) - 1
N=3+2–1=4
Convolution of two sequences x1(n) and x2(n) is,
x3(n) = IDFT[X3(k)]
x3(n) = IDFT[X1(k) X2(k)]
Where,
X1(k) = DFT [x1(n)]
36
N-1
X1(k) = ∑ x1(n)e-j2πkn/N k= 0, 1, 2, … , N-1
n=0
3
X1(0) = ∑ x1(n) = 1 + 1 + 2 = 4
n=0
5
X1(1) = ∑ x1(n)e-jπn/2 = 1 – j – 2 = -1 - j
n=0
5
X1(2) = ∑ x1(n)e-jπn = 1 – 1 + 2 = 2
n=0
5
X1(3) = ∑ x1(n)e-j3πn/2 = 1 + j – 2 = -1 + j
n=0
Now,
N-1
X2(k) = ∑ x2(n)e-j2πkn/N k= 0, 1, 2, … , N-1
n=0
3
X2(0) = ∑ x2(n) = 1 + 2 = 3
n=0
37
3
X2(1) = ∑ x2(n) e-jπn/2 = 1 + 2(-j) = 1 - j2
n=0
3
X2(2) = ∑ x2(n) e-jπn = 1 + 2(-1) = -1
n=0
3
X2(3) = ∑ x2(n) e-j3πn/2 = 1 + 2(j) = 1 + j2
n=0
We know that,
X3(k) = X1(k) X2(k)
X3(k) = {12, -3+j, -2, -3-j}
Convolution of two given sequences
is, x3(n) = IDFT[X3(k)]
N-1
x3(n) = (1/N) ∑ X3(k)ej2πkn/N n = 0, 1, 2, …. , N-1
k=0
3
x3(0) = (1/4) ∑ X3(k) = (1/4) [12 – 3 +j -2 -3 –j] = 1
k=0
3
x3(1) = (1/4) ∑ X3(k)ejπk/2
k=0
x3(1) = (1/4) [12 + (-3 + j) j + (-2) (-1) + (-3 - j) (-j)] = 3
3
x3(2) = (1/4) ∑ X3(k)ejπk
k=0
38
3
x3(3) = (1/4) ∑ X3(k)ej3πk/2
k=0
Program:
clc; % Clear screen
x1 = input('Enter the 1st seq:'); % Get the first sequence
x2 = input('Enter the 2nd seq:'); % Get the second sequence
n = length(x1) + length(x2)-1; % Get the length of the sequence
x1 = fft(x1,n); % Compute the DFT of x1 using FFT algorithm
x2 = fft(x2,n); % Compute the DFT of x2 using FFT algorithm
y = x1.*x2; % Multiply two DFT’s
yc = ifft(y,n); % Compute IDFT using IFFT algorithm
disp('Linear convolution using DFT and IDFT:'); % Display Linear convolution
disp(yc); % Displays the result on command window
N = 0:1:n-1; % Defines the length of x-axis to plot the result
subplot(1,1,1); % Divide the window to plot the result
stem(N,yc); % Plots the result
xlabel('Time'); % Name the x-axis as Time
ylabel('Magnitude'); % Name the y-axis as Magnitude
title('Linear convolution using DFT and IDFT:'); % Title
Output:
Enter the 1st seq: [1 1 2]
Enter the 2nd seq: [1 2]
Linear convolution using DFT and IDFT:
1344
39
Graph:
Questions:
40
EXPERIMENT NO-10
Circular convolution:
Let x1(n) and x2(n) are finite duration sequences both of length N with DFT’s
X1(k) and X2(k). Convolution of two given sequences x1(n) and x2(n) is given by,
x3(n) = IDFT[X3(k)]
x3(n) = IDFT[X1(k) X2(k)]
Where,
X1(k) = DFT [x1(n)]
X2(k) = DFT [x2(n)]
Example:
x1(n) = {1, 1, 2, 1}
x2(n) = {1, 2, 3, 4}
N-1
X1(k) = ∑ x1(n)e-j2πkn/N k= 0, 1, 2, … , N-1
n=0
3
X1(0) = ∑ x1(n) = 1 + 1 + 2 + 1 = 5
n=0
3
X1(1) = ∑ x1(n)e-jπn/2 = 1 – j – 2 + j = -1
n=0
3
X1(2) = ∑ x1(n)e-jπn = 1 – 1 + 2 - 1 = 1
n=0
3
X1(3) = ∑ x1(n)e-j3πn/2 = 1 + j - 2 - j = -1
n=0
41
X1(k) = {5, -1, 1, -1}
Now,
N-1
X2(k) = ∑ x2(n)e-j2πkn/N k= 0, 1, 2, … , N-1
n=0
3
X2(0) = ∑ x2(n) = 1 + 2 + 3 + 4 = 10
n=0
3
X2(1) = ∑ x2(n) e-jπn/2 = 1 + 2(-j) + 3(-1) + 4(j) = -2 + j2
n=0
3
X2(2) = ∑ x2(n) e-jπn = 1 + 2(-1) + 3(1) + 4(-1) = -2
n=0
3
X2(3) = ∑ x2(n) e-j3πn/2 = 1 + 2(j) + 3(-1) + 4(-j) = -2 – j2
n=0
We know that,
X3(k) = X1(k) X2(k)
X3(k) = {50, 2 – j2, -2, 2 + j2}
N-1
x3(n) = (1/N) ∑ X3(k)ej2πkn/N n = 0, 1, 2, …. , N-1
k=0
42
3
x3(0) = (1/4) ∑ X3(k) = (1/4) [50 + 2 - j2 – 2 + 2 + j2] = 13
k=0
3
x3(1) = (1/4) ∑ X3(k)ejπk/2
k=0
x3(1) = (1/4) [50 + (2 - j2) j + (-2) (-1) + (2 + j2) (-j)] = 14
3
x3(2) = (1/4) ∑ X3(k)ejπk
k=0
3
x3(3) = (1/4) ∑ X3(k)ej3πk/2
k=0
x3(3) = (1/4) [50 + (2 - j2) (-j) + (-2) (-1) + (2 + j2) (j)] = 12
Convoluted sequence of two given sequences is,
x3(n) = {13, 14, 11, 12}
Program:
clc; % Clear screen
x1 = input('Enter 1st sequence:'); % Get the first sequence x2
= input('Enter 2nd sequence:'); % Get the second sequence
n = max(length(x1), length(x2)); % Get the maximum length of the two sequences
x1 = fft(x1,n); % Compute the DFT of the x1 using FFT algorithm
x2 = fft(x2,n); % Compute the DFT of the x2 using FFT algorithm
y = x1.*x2; % Multiply two DFT’s
yc = ifft(y,n); % Compute the IDFT of mutliplied sequence to get the convoluted sequence
43
Output:
Graph:
Questions:
44
EXPERIMENT NO-11
Finite Impulse Response (FIR) Filter: The FIR filters are of non-recursive type,
whereby the present output sample is depending on the present input sample and previous
input samples.
The transfer function of a FIR causal filter is given
by, N-1
H(z) = ∑ h(n)z -n
n=0
In the design of FIR filters most commonly used approach is using windows.
jw
The desired frequency response Hd(e ) of a filter is periodic in frequency and can
be expanded in Fourier series. The resultant series is given by,
π
hd(n) = (1/2π) ∫ H(ejw)ejwn dw
-π
And known as Fourier coefficients having infinite length. One possible way of obtaining
FIR filter is to truncate the infinite Fourier series at n = ± [(N-1)/2]
Where N is the length of the desired sequence.
The Fourier coefficients of the filter are modified by multiplying the infinite
impulse response with a finite weighing sequence w(n) called a window.
45
jw
The frequency response H(e ) of the filter can be obtained by convolution of
Hd(ejw) and W(ejw) is given by,
π
H(ejw) = (1/2π) ∫ Hd(ejθ) W(ej(w-θ) dθ
-π
Example:
Here we design a lowpass filter using hamming window.
Hamming window function is given by,
=0 otherwise
WH(ejw ) = 0.54[(sin(wN/2))/(sin(w/2))]
+ 0.23[sin (wN/2 – πN/N – 1)/sin (w/2 – π/N -1)]
+ 0.23[sin (wN/2 + πN/N – 1)/sin (w/2 + π/N – 1)]
Program:
clc; % Clear screen
wp = input('Pass band edge freq:'); % Get Passband edge frequency
ws = input('Stop band edge freq:'); % Get Stopband edge frequency
tw = ws-wp; % Subtract PB frequency from SB frequency
46
Output:
Columns 8 through 14
Columns 15 through 20
Graph:
Questions:
47
EXPERIMENT NO-12
Example:
Let’s design an analog Butterworth lowpass filter.
Steps to design an analog Butterworth lowpass filter.
1. From the given specifications find the order of the filter N.
2. Round off it to the next higher integer.
3. Find the transfer function H(s) for Ωc = 1rad/sec for the value of N.
4. calculate the value of cutoff frequency Ωc
5. find the transfer function Ha(s) for the above value of Ωc by substituting
s→ (s/ Ωc) in H(s).
Program:
48
[b,a] = butter(N,wn); % Get Numerator and denominator coefficients of
% the filter
disp('b='); disp(b); % Display the numerator coefficients on command
% window
disp('a='); disp(a); % Display the denominator coefficients on
% command window
w = 0:0.01:pi; % Defines length for x-axis to plot the result
[h,om] = freqz(b,a,w,'whole'); % Frequency response of the filter
m = 20*log10(abs(h)); % Absolute value of the frequency response vector
an = angle(h); % Get the phase of the frequency response vector
Output:
Cutoff frequency:
0.4914
b=
0.1600 0.4800 0.4800 0.1600
a=
1.0000 -0.0494 0.3340 -0.0045
49
Graph:
Questions:
1. Plot gain and phase response with respect to frequency for an IIR filter having
passband attenuation of 3dB ,stopband attenuation of 40dB,sampling frequency is 3000Hz,
passband edge frequency is 300Hz and stopband edge frequency is 900Hz.
50