Digital Signal Processing Digital Signal Processing: DSP Lab Manual DSP Lab Manual
Digital Signal Processing Digital Signal Processing: DSP Lab Manual DSP Lab Manual
DSP Lab
B.E., V Semester, EC/TC
[As per Choice Based Credit System (CBCS) scheme]
Page | 2
Subject Code:15ECL57
Code:15ECL57 IA Marks 20
DSP Lab
B.E., V Semester, EC/TC
[As per Choice Based Credit System (CBCS) scheme]
Page | 2
Subject Code:15ECL57
Code:15ECL57 IA Marks 20
Page | 3
I - CYCLE EXPERIMENTS
Page | 4
PART - A
MATLAB® PROGRAMS
If the highest frequency contained in an analog signal xa(t) is Fmax = B Hz and the
signal is sampled at a rate Fs> 2 Fmax = 2B, then xa(t) can be exactly recovered from
its sample values using the interpolation function
() ()
() … (1.1)
The sampling rate FN = 2Fmax = 2BHz is called the Nyquist rate.
If Fs is the sampling frequency then F s= 1/T samples per second. T is the sampling
Page | 6
interval in seconds.
xa[nT]=Acos[ΩnT] … (1.5)
= Acos[2πFnT] … (1.6)
= Acos[2πfn] … (1.8)
Comparing eqn1.7 and eqn1.8 it can be seen that f = [ ], and fis called discrete time
frequency with units cycles/sample. 2πf = ω is called discrete-time angular
frequency with units radian/sample; ω = ΩT describes the relationship between the
D.T. angular frequency and C.T angular frequency(rad/sec)
ALGORIHM:
Begin
Read Amplitude ‘A’ and Frequency ‘F’, t 0,Δt, t 1
x = x 1 + x 2
PROGRAM: samp_thrm.m
Delta_t= input('Resolution = ); ’
x1 = A1 * cos( 2 * pi * F1 * t );
x2 = A2 * cos( 2 * pi * F2 * t );
xa = x1 + x2;
A = A1 + A2;
subplot( 3, 1, 3 )
plot( t, xa, 'b' )
axis( [ t0 t1 -A A] )
ylabel( 'x3(t)' )
Page | 8
% Choose the Sampling Frequency satisfying Nyquist s Sampling Theorem
’
Ts = 1/Fs;
n0 = t0/Ts
n1 = t1/Ts;
n = n0:n1;
nT = n * Ts;
xn1 = A1 * cos( 2 * pi * F1 * nT );
xn2 = A2 * cos( 2 * pi * F2 * nT );
xn = xn1 + xn2;
figure(2)
subplot( 3, 1, 1 )
stem( n, xn1, 'r' )
axis( [ t0/Ts, t1/Ts, -A1, A1 ] )
ylabel( 'x1[n]' )
title ( ' Discrete Time Signals ' )
subplot( 3, 1, 2 )
stem( n, xn2, 'g' )
axis( [ t0/Ts, t1/Ts, -A2, A2 ] )
ylabel( 'x2[n]' )
subplot( 3, 1, 3 )
stem( n, xn, 'b' )
axis( [ t0/Ts, t1/Ts, -A, A ] )
ylabel( 'x [n]' )
Fmax = max( [ F1 F2 ] );
xr = xn * Interpolate;
Page | 9
figure(3)
subplot( 2, 1, 1 )
plot( t, xa, 'b' )
axis( [ t0, t1, -A, A ] )
xlabel( ' t--> ' )
ylabel(' xa(t) ' )
title ( ' Original Continous Time Signals ' )
subplot( 2, 1, 2 )
plot( t, xr, 'b' )
xlabel( ' t--> ' )
ylabel(' xr(t) ' )
title ( ' Reconstructed Continous Time Signals ' )
>>
Amplitude of signal x1(t) = 1.2
Amplitude of signal x2(t) = 0.8
Frequency in Hz of signal x1(t) = 4
Frequency in Hz of signal x2(t) = 8
Beginning Time = -1
End Time = 1
Resolution =0.0001
Enter the Sampling Frequency in Hz = 64
Figure 1.2Screenshot of the output of Nyquist Sampling Theorem Verification program for
perfect reconstruction.
THEORY:
For discrete time systems with input signal x(n) and finite duration unit impulse
response h(n), linear convolution summation is defined by
Eqn 3.1 represents the response of a causal system. For a non-causal system the
response is given by the convolution summation defined by the eqn3.2
If Nx is the length of x[n] and Nh is the length of h[n] then the length of y[n] is N y
given by
N y = Nx + Nh - 1 … (3.3)
Example:
Page | 11
Page | 12
y[n] = { 1, 3, 6, 6, 4, 1 }
Begin
Read x n and hn
Compute
yn = ∑ x k hn,k , ∀n
length of x n, hn, yn
display x n, hn, yn
End
PROGRAM:(i)conv_causal.m
clc
Page | 13 clear all
close all
% Enter the samples of Input Sequence x[n] and Unit Impulse Response h[n]
x = input('Enter the Discrete Input Signal Sequence x[n] = ');
h = input('Enter the Impulse Response Sequence h[n] = ');
Nx = length( x );
Nh = length( h );
nx = 0 : 1 : Nx-1;
nh = 0 : 1 : Nh-1;
figure( 1 )
subplot( 3, 1, 1 )
stem(nx, x, 'r', 'filled' )
xlabel(' n--> ' )
ylabel(' x[n] ' )
title(' Input Sequence x[n] ' )
subplot( 3, 1, 2 )
stem(nh, h, 'r', 'filled' )
xlabel(' n--> ' )
ylabel(' h[n] ' )
title(' Impulse Response h[n] ' )
subplot( 3, 1, 3 )
stem(ny, y, 'r', 'filled' )
xlabel(' n--> ' )
ylabel(' y[n] ' )
Page | 14 >>
Enter the Discrete Input Signal Sequence x[n] = [ 1, 2, 3, 4 ]
Enter the Impulse Response Sequence h[n] = [ 3 1 2 1 ]
The Convolution of x[n] and h[n] is y[n] =
3 7 13 20 12 11 4
Begin
Read x n,n x, hn andnh
Compute
yn = ∑ x khn,k , ∀n
Min( ny)= Min( n x)+Min( nh)
Max( ny)= Max( n x)+Max( nh)
ny = Min( ny):Max( ny)
displayx n, hn, yn
End
PROGRAM: (ii)conv_noncuasal.m
clc
clear all
close all
Page | 15
% Enter the values of x[n] and h[n]
x = input(' Enter the input signal x[n] = ' );
nx = input( ' Enter the index values of x[n] = ' );
h = input(' Enter the Unit Impulse Response h[n] = ' );
nh = input( ' Enter the index values of h[n] = ' );
% Convolution %
y = conv( h, x );
% Index Sequence of y[n] %
ny = ( min(nx)+min(nh) ) : 1 : ( max(nx)+max(nh) );
disp(' The Output Sequence = ' )
disp( y )
disp(ny )
figure(1)
subplot( 3, 1, 1 )
stem ( nx, x, 'filled' )
axis( [ min(nx) max(nx) min(x) max(x) ] )
xlabel(' n--> ')
ylabel(' x[n] ')
title(' Input Signal x[n] ' )
subplot( 3, 1, 2 )
stem ( nh, h, 'filled' )
axis( [ min(nh) max(nh) min(h) max(h) ] )
xlabel(' n--> ')
ylabel(' h[n] ')
title(' Input Signal h[n] ' )
subplot( 3, 1, 3 )
stem ( ny, y, 'filled' )
axis( [ min(ny) max(ny) min(y) max(y) ] )
xlabel(' n--> ')
ylabel(' y[n] ')
title(' Input Signal y[n] ' )
>>
Enter the input signal x[n] = [ 3, 4, 5, 2, 1 ]
Enter the index values of x[n] = -2:2
Enter the Unit Impulse Response h[n] = [ 1, 3, 2, 1]
Enter the index values of h[n] = -1:2
The Output Sequence =
3 13 23 28 21 12 4 1
-3 -2 -1 0 1 2 3 4
Page | 16
clc
clear all
close all
x = [3, 1, 2, 1];
h1 = [ 1, 2, 2, 1 ];
h2 = [ 2, 4, 2, 4 ];
%Commutative Property
y1 = conv(x, h1)
y2 = conv(h1, x)
if y1 == y2
disp('Commutative Property of convolution is proved');
else
disp('error in the computation')
end
% Associative Property
% Distributive Property
y5 = conv(x, h1+h2)
y6 = conv(x, h1) + conv(x, h2)
if y5 == y6
disp('Distributive Property of convolution is proved');
else
disp('error in the computation')
end
>>
y1 =
3 7 10 10 7 4 1
y2 =
3 7 10 10 7 4 1
y3 =
6 26 54 86 102 96 72 40 18 4
y4 =
6 26 54 86 102 96 72 40 18 4
y5 =
9 21 24 34 19 14 5
y6 =
9 21 24 34 19 14 5
THEORY:
Circular convolution of two finite duration discrete time sequences x[n] and h[n] is
given by
IfNx is the length of x[n] and Nh is the length of h[n] then the length of y[n] is N y
given by N y = Max(Nx,Nh ) … (4.2)
The computation of y[n] is based on the equalization of the lengths of x[n] and h[n]
by appending appropriate zeros with the following logic:
if Nx>Nh
hn = [ hn, 0, 0, 0, …, 0 ]
(Nx -Nh) zeros
else
xn = [ xn, 0, 0, 0, …, 0]
(Nh- Nx ) zeros
end
Example:
Given x1(n) = {1, 1, 2, 1} and x2(n) = {1, 2, 3, 4}. Arrange x1(n) and x2(n) in circular
fashion as shown below
Page | 19
x3(0) = 13
Keep x1(m) constant and rotate x2(-m) once to compute further values.
To get x3(1) rotate x2(-m) by one sample in anti-clockwise direction x2(1-m)
Page | 20 To get x3(2) rotate x2(1-m) by one sample in anti-clockwise direction x2(2-m)
Begin
PROGRAM:circ_conv.m
clc
clear all
close all
if ( Nx>Nh )
h = [ h zeros( Nx - Nh ) ];
else
x = [ x zeros( Nh - Nx ) ];
end
N = max(Nx, Nh );
for k = 2 : N
temp = circshift( temp', k-1 )';
y( k ) = sum( x .* temp );
end
X1 = fft(x);
H1 = fft(h);
Y1 = X1 .* H1;
y1 = ifft(Y1);
disp(' Circular convolution of x[n] and h[n] in frequency domain is y1[n] ' )
disp( y1 )
X2 = fft(x, Nx+Nh-1);
H2 = fft(h, Nx+Nh-1);
disp(' Linear convolution of x[n] and h[n] in frequency domain is y2[n] ' )
disp( y2 )
Page | 22
figure( 1 )
subplot( 3, 1, 1 )
stem( 0 : Nx-1, x, 'r', 'filled' )
title(' Discrete Time Sequence x[n] ' )
xlabel(' n--> ' )
ylabel(' x[n] ' )
subplot( 3, 1, 2 )
stem( 0 : Nh-1, h, 'r', 'filled' )
title(' Discrete Time Sequence h[n] ' )
xlabel(' n--> ' )
ylabel(' h[n] ' )
subplot( 3, 1, 3 )
stem( 0 : N-1, y, 'r', 'filled' )
title(' Discrete Time Sequence y[n] ' )
xlabel(' n--> ' )
ylabel(' y[n] ' )
Page | 23
clc
clear all
close all
x = [3, 1, 2, 1];
h1 = [ 1, 2, 2, 1 ];
h2 = [ 2, 4, 2, 4 ];
%Commutative Property
X1 = fft(x);
H1 = fft(h1);
Y1x = X1 .* H1;
Y1h = H1 .* X1;
y1x = ifft(Y1x)
y1h = ifft(Y1h)
if y1x == y1h
disp('Commutative Property proved');
else
disp('Error in compuatation');
end
H2 = fft(h2);
Yxh = X1 .* ( H1 .* H2 );
Yhx = (X1 .* H1) .* H2;
Page | 24 yxh = ifft( Yxh )
yhx = ifft(Yhx)
if yxh == yhx
disp('Associative Property proved');
else
disp('Error in compuatation');
end
%Distributive Property
Y11 = X1 .* ( H1 + H2);
Y12 = X1 .* H1 + X1 .* H2;
y11 = ifft(Y11)
y12 = ifft(Y12)
if y11 == y12
disp('Distributive Property proved');
else
disp('Error in compuatation');
end
>>y1x =
10 11 11 10
y1h =
10 11 11 10
Commutative Property proved
yxh =
126 126 126 126
yhx =
126 126 126 126
y12 =
28 35 29 34
Where the integer parameter l is called the lag indicator. Eqn. 5.1 indicates that the
sequence x[n] is shifted by l samples with respect to itself. The operation defined by
eqn. 5.1 is identical to convolution except for the time folding. It may be recalled that
in convolution one of the sequence is time reversed and then the operations of
shifting multiplications and additions are carried out. However in correlation except
for the folding he rest of the three operations are carried out.
Example: x[n] = { 1, 2, 3, 4 }
1 2 3 4 1 2 3 4
________________________ _______________________
3 + 8 = 11 4
Page | 26
___________________________ _______________________
1 2 3 4 1 2 3 4
__________________________ ________________________
2 + 6 + 12 = 20 3 + 8 = 11
1 2 3 4
_________________________
4
_________________________
ALGORIHM: Autocorrelation
Begin
Read x n
Compute
length of x n
r xx = ∑ x n x (n,l) , ∀
E x = ∑ x n, x n
E = r xx (0)
displayx n, r xx
End
PROGRAM:Auto_Corr.m
clc
clear all
close all
% Compute Auto-Correlation
rxx = xcorr( x );
N = length( x );
nx = N - 1;
Page | 27
l = -nx : 1 : nx;
L = length( l );
lx = L - 1;
figure( 1 )
subplot ( 2, 1, 1 )
stem ( 0 : nx , x , 'r', 'filled' )
xlabel(' n-- > ' )
ylabel(' x[n] ' )
title ( ' Input D.T. Sequeence x[n] ' );
subplot( 2, 1, 2 )
stem( 0 : lx, rxx, 'r', 'filled' )
xlabel(' l --> ' )
ylabel(' rxx[l] ' )
title(' Autocorrelation Sequence ' )
Page | 28
Page | 29 EXAMPLE:
ALGORIHM: Cross-correlation
Begin
Read x n, yn
Compute
length of x n , yn
r xy = ∑ x n y (n,l) , ∀
ryx = ∑ yn x (n,l) , ∀
displayx n,yn,r xy, ryx
End
PROGRAM:Cross_Corr.m
clc
clear all
close all
rxy = xcorr( x, y );
ryx = xcorr( y, x );
figure( 1 )
subplot( 2, 1, 1 )
stem ( x, 'filled')
xlabel(' n--> ' )
ylabel(' x[ n ] ' )
title(' First Sequence x[n] ' )
subplot( 2, 1, 2 )
stem ( y, 'filled')
xlabel(' n--> ' )
ylabel(' y[ n ] ' )
title(' Second Sequence y[n] ' )
figure(2 )
subplot( 2, 1, 1 )
stem ( rxy, 'filled')
xlabel(' l--> ' )
ylabel(' rxy[ l ] ' )
title(' The Cross-Correlation Sequence rxy[ l ] ' )
subplot( 2, 1, 2 )
stem ( ryx, 'filled' )
xlabel(' l--> ' )
ylabel(' ryx[ l ] ' )
title(' The Cross-Correlation Sequence ryx[ l ] ' )
>>
>>
Page | 31
Rearranging eqn(7.2)
() ∑
[] () +∑
(7.3)
H(z) is called transfer function of the system.
Linear Constant Coefficient Difference Equations(LCCDEs) model LTI systems.
Equation (7.1) governs a recursive or Infinite Impulse Response(IIR) filter. If ≡
∀ except for ,& =1, then
[] ∑ − −
(7.4)
= …
EXAMPLE:
Begin
Read ak, bk
Compute ,, and h fromrational H(z) using partial fraction expansion.
n
PROGRAM: LCCDE1.m
[ r, p, k ] = residuez( b, a );
[ h, n ] = impz( b, a );
% Impulse Response of the System
figure( 1 )
subplot( 2, 1, 1 )
stem( n, h )
xlabel(' n--> ' )
ylabel(' h[n] ' )
title(' Impulse Response of the System ' )
subplot( 2, 1, 2 )
stem( 0:length( y ) - 1, y )
xlabel(' n--> ' )
ylabel(' y[n] ' )
title(' Response of the System ' )
>>
Page | 34
Enter the Numerator Coefficients = [ 1, 1/4, 1/8 ]
Enter the Denomenator Coefficients = [ 1, 1/3, 1/6 ]
Residues =
0.1250 + 0.0559i
0.1250 - 0.0559i
Poles =
-0.1667 + 0.3727i
-0.1667 - 0.3727i
Constants =
0.7500
Columns 8 through 11
Columns 8 through 12
Columns 8 through 12
Page | 35
Begin
Read ak, bk
Compute
hn= IZT { H z }
define input signal to be unit step input
x n = un
solv
e ∑ ∑
for
display h, yn
End
PROGRAM: LCCDE2.m
clc
clear all
close all
[ h n ] = impz( b, a );
x = ones( 1, 12 );
zi = filtic( b, a, ic );
y = filter( b, a, x, zi );
figure( 1 )
subplot( 2, 1, 1 )
stem( n, h )
xlabel(' n--> ' )
ylabel(' h[n] ' )
title(' Impulse Responnse ' )
N = length( y );
ny = 0:N - 1;
subplot( 2, 1, 2 )
stem(ny, y )
xlabel(' n--> ' )
ylabel(' y[n] ' )
title(' Response of the System ' )
Columns 8 through 9
-0.0001 0.0001