SPL 3
SPL 3
Experiment No.3
Signal Processing Laboratory
Third Year
Morning Study
Group (B)
➢ Introduction
A mathematical way of combining two signals to form a new signal is known as Convolution.
In MATLAB for convolution ‘conv’ statement is used. The convolution of two vectors, p, and
q given as “a = conv (p,q)” which represents that the area of overlap under the points as p
slides across q. Convolution is the most important technique in Digital Signal Processing.
n1=0:1:7; 4
01234567 2
y1=[1 2 3 1 2 3 4 5];
0
h1=[1 1 1 2 1 -1 1 1]; 0 1 2 3 4 5 6 7
X=conv(y1,h1); 2
impulse response h(n)
n2=0:length(X)-1;
1
figure(1)
subplot(3,1,1) 0
stem(n1,y1); -1
0 1 2 3 4 5 6 7
title(‘input y(n)’);
output x(n)
subplot(3,1,2) 30
stem(n1,h1); 20
title(‘output x(n)’);
close all;
Fs=100; % use higher sampling rate
t=-10:1/Fs:10;
d=(abs(t)-1)==0; % use kronecker delta function
% for discrete-time simulation
s=sign(t);
x=d.*s;
x2=1*(t>-1 & t<2);
Signal Processing Laboratory EXP.NO.3: Convolution and Correlation
sp1=conv(x,x2,’same’);
1
%plotting figures
figure;
f(x)
0.5
subplot(3,1,1);
0
plot(t,x2); -10 -8 -6 -4 -2 0 2 4 6 8 10
1
ylabel(‘f(x)’); 0.5
subplot(3,1,2)
g(x)
0
plot(t,x); -0.5
ylabel(‘g(x)’); -1
-10 -8 -6 -4 -2 0 2 4 6 8 10
subplot(3,1,3) 1
plot(t,sp1); convolution
0.5
0
xlabel(‘Time’); -0.5
ylabel(‘convolution’); -1
-10 -8 -6 -4 -2 0 2 4 6 8 10
Time
input signal
Example (3): Corss-correlation of two vectors 1
0.5
t=0:99;
x=cos(2*pi*1/10*t); 0
figure; -0.5
subplot(2,1,1);
-1
stem(t,x); 0 20 40 60 80 100
subplot(2,1,2); 0
stem(lags(21:end),xc(21:end));
-0.5
title('cross-correlated signal');
-1
0 5 10 15 20
input signal
n=0:N-1; 1
figure; 0.5
subplot(2,1,1); -0.5
plot(n,x);
-1
title('input signal'); 0 200 400 600 800 1000
[xc, lags]=xcorr(x,'coeff'); 1
auto-correlated signal
tau=lags*1/FS;
0.5
subplot(2,1,2);
0
plot(tau,xc);
-0.5
title('auto-correlated signal');
-1
-5 0 5
➢ Discussion
1. Consider an LTI system with the impulse response h(n) and input x(n). Find the
convolution sum, y(n) and sketch the output for this system using MATLAB.
x(n) signal
3
n=0:1:6; 2
x=[0,1,2,3,2,1,0]; 1
h=[1,1,1,1,1,1,0]; 0
0 1 2 3 4 5 6
c=conv(x,h); 1
h(n) signal
n1=0:length(c)-1;
0.5
subplot(3,1,1);
stem(n,x); 0
0 1 2 3 4 5 6
title('x(n) signal'); 10
convolution signal
subplot(3,1,2);
5
stem(n,h);
0
0 2 4 6 8 10 12
Signal Processing Laboratory EXP.NO.3: Convolution and Correlation
title('h(n) signal');
subplot(3,1,3);
stem(n1,c)
title('convolution signal');
2. Write a MATLAB program to compute the output for a discrete time LTI system whose
impulse response h(t) and input x(t) is given by:
x(t) signal
2
t=-5:0.001:5;
1
x=(-t+1>=0)-2*(-t>=0)+(-t-1>=0);
0
h=2*[(-t+1==0)-(-t+3==0)];
-1
c=conv(x,h,'same');
-2
subplot(3,1,1) -5 -4 -3 -2 -1 0 1 2 3 4 5
title('x(t) signal'); 2
ylim([-2 2]);
0
subplot(3,1,2)
plot(t,h) -2
title('h(t) signal'); -5 -4 -3 -2 -1 0 1 2 3 4 5
subplot(3,1,3) 2
plot(t,c)
0
title('output signal');
ylim([-3 3]); -2
-5 -4 -3 -2 -1 0 1 2 3 4 5
Signal Processing Laboratory EXP.NO.3: Convolution and Correlation
3. Write a MATLAB program to compute the cross correlation between the following
signals:
v1(t) signal)
T=1; 2
t=0:0.001:3; 1
v1=sawtooth(2*pi*t*T); 0
v2=square(2*pi*t*T); -1
[c,lags]=xcorr(v1,v2,10,'coeff'); -2
-1 0 1 2 3 4
title('v2(t) signal)');
-0.84
subplot(3,1,3);
-0.86
plot(lags(11:end),c(11:end))
title('cross correlated signal)'); -0.88
0 2 4 6 8 10
4. Write a MATLAB program to convolute the two signals below in the time domain:
t=-4:0.001:4;
0.5
x=abs(t).*[(-t+1>=0)-(-t-1>=0)];
h=t.*[(-t+2>=0)-(-t>=0)]; 0
-4 -3 -2 -1 0 1 2 3 4
c=conv(x,h,'same');
h(t) signal
subplot(3,1,1) 2
plot(t,x)
title('x(t) signal'); 1
subplot(3,1,2)
0
plot(t,h) -4 -3 -2 -1 0 1 2 3 4
subplot(3,1,3)
plot(t,c) 500
title('convolution signal');
0
-4 -3 -2 -1 0 1 2 3 4
5. Write the MATLAB code that sketch the autocorrelation of the following periodic square
wave f(t):
x(t) signal
2
T=1;
0
t=-5:0.001:5;
s=square(2*pi*(t+0.25)*(T) ); -1
subplot(2,1,1)
plot(t,s); -2
-5 -4 -3 -2 -1 0 1 2 3 4 5
ylim([-2 2]);
title('x(t) signal'); 1
auto-correlated signal
[c,lags]=xcorr(s,'coeff');
t=lags*T; 0.5
subplot(2,1,2)
0
plot(t,c);
title('auto-correlated signal'); -0.5
-1
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
4
x 10