UNIVERSITY INSTITUTE OF ENGINEERING AND
TECHNOLOGY
KURUKSHETRA UNIVERSITY KURUKSHETRA
DEPT. OF ELECTRONICS & COMMUNICATION ENGINEERING
PRACTICAL FILE
Digital Signal Processing
Submitted To : Submitted By:
Ms. Rajni Pawan Garg
2507061
Ece(B) 4th year
INDEX
Sr. Experiment Name Date Remark
No.
1. Write a program to implement matrix .
algebra.
2. Write a program to plot following functions
a)impulse function b)Unit Step c) Ramp
function
3. Write a program to find out the convolution of
two sequences using in built convolution
function.
4. Write a program to plot Exponential function.
5. Write a program to implement loops.
6. To Implement FIR/Digital Filter.
7. Study different window functions available in
signal processing toolbox and their controlling
parameters.
8. Study of plots, subplots including functioning
of hold on and off.
9. Write a program to implement autocorrelation
function
10. Write a program to implement crosscorrelation
function
Experiment: 1
Program: Write a program to implement Matrix Algebra.
Software Used: MATLAB 7.6
>> a=[1 2 3
456
7 8 9]
a=
1 2 3
4 5 6
7 8 9
>> b=[4 6 7
793
3 5 7]
b=
4 6 7
7 9 3
3 5 7
>> %addition
>> a+b
ans
5 8 10
11 14 9
10 13 16
>> %subtraction
>> a-b
ans =
-3 -4 -4
-3 -4 3
4 3 2
>> %multiplication
>> a*b
ans =
27 39 34
69 99 85
111 159 136
>> %display a row
>> e=b(2,:)
e=
7 9 3
>> %display a column
>> f=b(:,2)
f=
>> z=[1 2 3 4]
z=
1 2 3 4
>> z=10:-3:1
z=
10 7 4 1
>> z=0:3:10
z=
0 3 6 9
>> x=zeros(1,3)
x=
0 0 0
>> x=[zeros(1,3); 4 5 6; ones(1,3)]
x=
0 0 0
4 5 6
1 1 1
>> y=rand(2,2)
y=
0.8147 0.1270
0.9058 0.9134
>> who
Your variables are:
a ans b e f x y z
>> whos
Name Size Bytes Class Attributes
a 3x3 72 double
ans 3x3 72 double
b 3x3 72 double
e 1x3 24 double
f 3x1 24 double
x 3x3 72 double
y 2x2 32 double
z 1x4 32 double
Experiment: 2
Program: Write a program to plot following functions
a) impulse function b)Unit Step c) Ramp function d) sin and cos
function
Software Used: MATLAB 7.6
%--Program to Generate IMPULSE function--%
m = 20
for i = -m:m
if(i==0)
y=1
stem(i,y,'r+')
hold on
else
continue;
end
end
hold off
%--Program for UNIT Step function--%
n=10
for t= -n:1:n
if(t>0)
z=1
plot(t,z,'+');
hold on
else
continue;
end
end
hold off
%--Program to Plot RAMP function--%
t = 0:1:10
y=3*t
plot(t,y,'r')
%--Program to plot Sine and Cosine function --%
x = 0:.0001:1
f = cos(2*pi*x)
g = sin(2*pi*x)
plot(x,f,'b+');
hold on;
plot(x,g,'r');
hold off;
OUTPUTS:-
impulse function:
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
unit step:
1.8
1.6
1.4
1.2
0.8
0.6
0.4
0.2
0
1 2 3 4 5 6 7 8 9 10
ramp function:
30
25
20
15
10
0
0 1 2 3 4 5 6 7 8 9 10
sin and cos function:
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Experiment: 3
Program: Write a program to find out the convolution of two
sequences using in built convolution function.
Software Used: MATLAB 7.6
clc;
clear all;
close all;
disp('linear convolution program');
x=input('enter i/p x(n):');
m=length(x);
h=input('enter i/p h(n):');
n=length(h);
x=[x,zeros(1,n)];
subplot(2,2,1), stem(x);
title('i/p sequencce x(n)is:');
xlabel('---->n');
ylabel('---->x(n)');grid;
h=[h,zeros(1,m)];
subplot(2,2,2), stem(h);
title('i/p sequencce h(n)is:');
xlabel('---->n');
ylabel('---->h(n)');grid;
disp('convolution of x(n) & h(n) is y(n):');
y=zeros(1,m+n-1);
for i=1:m+n-1
y(i)=0;
for j=1:m+n-1
if(j<i+1)
y(i)=y(i)+x(j)*h(i-j+1);
end
end
end
subplot(2,2,[3,4]),stem(y);
title('convolution of x(n) & h(n) is :');
xlabel('---->n');
ylabel('---->y(n)');grid;
OUTPUT:
i/p sequencce x(n)is: i/p sequencce h(n)is:
6 2
1.5
4
---->x(n)
---->h(n)
1
2
0.5
0 0
0 2 4 6 8 0 2 4 6 8
---->n ---->n
convolution of x(n) & h(n) is :
15
10
---->y(n)
0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
---->n
Experiment: 4
Program: Write a program to plot Exponential function.
Software Used: MATLAB 7.6
%--Program to Plot Exponential Function--%
a= input('enter the value of a')
b = input('enter the value of b')
c = a+i*b
k=10
n=1:10
x= k*exp(c*n)
y = abs(x)
subplot(2,2,1:2)
stem(n,y)
xlabel('time')
ylabel('Mag.')
title('Magnitude Response')
z = angle(x)
subplot(2,2,3:4)
stem(n,z)
xlabel('time')
ylabel('Phase')
title('Phase Response')
OUTPUT:
enter the value of a10
a=
10
enter the value of b10
b=
10
44
x 10 Magnitude Response
3
2
Mag.
0
1 2 3 4 5 6 7 8 9 10
time
Phase Response
4
2
Phase
-2
-4
1 2 3 4 5 6 7 8 9 10
time
EXPERIMENT NO.5
Program: Write a program to implement loops.
Software Used: MATLAB 7.6
%--while loop--%
i=1
while i<5
disp ['hello']
i=i+1;
end
%--if loop--%
a=10;
b=20;
if(a<b)
'yes'
if(a>=b)
'no'
end
end
%--if else if--%
x=5
y=10
z=15
if((x>y)&(x>z))
'x is greatest of the three'
elseif(y>z)
'y is greatest'
else
'z is greatest'
End
%--switch--%
method = 'Bilinear';
switch lower(method)
case {'linear','bilinear'}
disp('Method is linear')
case 'cubic'
disp('Method is cubic')
otherwise
disp('hello! method is not there')
end
OUTPUT:
i=
['hello']
['hello']
['hello']
['hello']
ans =
yes
x=
y=
10
z=
15
ans =
z is greatest
Method is linear
EXPERIMENT NO.6
Program: write a program to implement fir filter
Software used: matlab 7.6
f = [0 0.6 0.6 1]; m = [1 1 0 0];
b = fir2(30,f,m);
[h,w] = freqz(b);
plot(f,m,w/pi,abs(h))
legend('Ideal','fir2 Designed')
title('Frequency Response Magnitudes')
OUTPUT:
Frequency Response Magnitudes
1.4
Ideal
fir2 Designed
1.2
0.8
0.6
0.4
0.2
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
EXPERIMENT NO.7
Program: Study different window functions available in signal
processing toolbox and their controlling parameters.
Software Used: MATLAB 7.6
N = 65;
w = window(@blackmanharris,N);
w1 = window(@hamming,N);
w2 = window(@gausswin,N,2.5);
wvtool(w,w1,w2)
OUTPUT:
Time domain Frequency domain
50
1
0.8 0
Magnitude (dB)
Amplitude
0.6
-50
0.4
-100
0.2
0 -150
10 20 30 40 50 60 0 0.2 0.4 0.6 0.8
Samples Normalized Frequency ( rad/sample)
Experiment: 8
Program: Study of plots, subplots including functioning of hold
on and off.
Software Used: MATLAB 7.6
%--subplot1--%
t = 0:.0001:1;
y = sin(2*pi*t)
subplot(2,2,1);
plot(t,y)
z = cos(2*pi*t)
subplot(2,2,2);
plot(t,z);
%--subplot2--%
q = 0:.001:1
a = sin(2*pi*q)
subplot(2,2,1:2)
plot(q,a)
xlabel('time')
ylabel('amplitude')
title('sine1')
b = cos(2*pi*q)
subplot(2,2,3)
plot(q,b)
xlabel('time')
ylabel('amplitude')
title('cos')
c = sin(pi*q)
subplot(2,2,4)
plot(q,c)
xlabel('time')
ylabel('amplitude')
title('sine2')
OUTPUT1:
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
0 0.5 1 0 0.5 1
OUTPUT2:
sine1
1
0.5
am plitude
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
time
cos sine2
1 1
0.5
am plitude
am plitude
0 0.5
-0.5
-1 0
0 0.5 1 0 0.5 1
time time
EXPERIMENT 9:
Program: Write a program to implement autocorrelation
function.
Software Used: MATLAB 7.6
N=1024; % Number of samples
f1=1; % Frequency of the sinewave
FS=200; % Sampling Frequency
n=0:N-1; % Sample index numbers
x=sin(2*pi*f1*n/FS); % Generate the signal, x(n)
t=[1:N]*(1/FS); % Prepare a time axis
subplot(2,1,1); % Prepare the figure
plot(t,x); % Plot x(n)
title('Sinwave of frequency 1000Hz [FS=8000Hz]');
xlabel('Time, [s]');
ylabel('Amplitude');
grid;
Rxx=xcorr(x); % Estimate its autocorrelation
subplot(2,1,2); % Prepare the figure
plot(Rxx); % Plot the autocorrelation
grid;
title('Autocorrelation function of the sinewave');
xlabel('lags');
ylabel('Autocorrelation');
OUTPUT:
Sinwave of frequency 1000Hz [FS=8000Hz]
1
0.5
Amplitude
-0.5
-1
0 1 2 3 4 5 6
Time, [s]
Autocorrelation function of the sinewave
1000
500
Autocorrelation
-500
0 500 1000 1500 2000 2500
lags
EXPERIMENT 10:
Program: Write a program to implement crosscorrelation
function.
Software Used: MATLAB 7.6
N=1024; % Number of samples to generate
f=1; % Frequency of the sinewave
FS=200; % Sampling frequency
n=0:N-1; % Sampling index
x=sin(2*pi*f*n/FS); % Generate x(n)
y=x+10*randn(1,N); % Generate y(n)
subplot(3,1,1);
plot(x);
title('Pure Sinewave');
grid;
Rxy=xcorr(x,y); % Estimate the cross correlation
subplot(3,1,2);
plot(Rxy);
title('Cross correlation Rxy');
grid;
OUTPUT:
Pure Sinewave
1
-1
0 200 400 600 800 1000 1200
Cross correlation Rxy
500
-500
0 500 1000 1500 2000 2500