BSP Lab #04
BSP Lab #04
LAB OBJECTIVE:
CONVOLUTION:
The response y(n) of the LTI system as a function of the input signal x(n) and the unit
sample (impulse) response h(n) is a convolution sum between x(n) & h(n). The input x(n)
is convolved with the impulse response h(n) to yield the output y(n).
MATHEMATICAL MEANS:
�
y ( n) = �x(k )h(n - k )
k =-�
y ( n) = h( n) �x( n)
SYNTAX
w = conv(u,v)
w = conv(u,v,shape)
shape — Subsection of convolution
'full' (default) | 'same' | 'valid'
1
INSTITUTE OF BIOMEDICAL ENGINEERING & TECHNOLOGY LIAQUAT
UNIVERSITY OF MEDICAL & HEALTH SCIENCES JAMSHORO
1 'full' Full convolution (default).
2 'same' Central part of the convolution of the same size as u.
3 'valid' Only those parts of the convolution that are computed without the zero-padded
edges. Using this option, length(w) is max(length(u)-length(v)+1,0), except
when length(v) is zero. If length(v) = 0, then length(w) = length(u).
IN LAB TASK:
TASK#1:
Write an Example of Convolution in MATLAB:
CODE:
%% Performs convolution between two signals
h=[5 4 3 2 1]; % First Sigal
x=[1 1 1 1 1]; % Second Signal
c=conv(h,x); % Convolution result stored in variable ‘c’
m=length(c)-1; %Find number of convolution output terms
n=0:1:m; %Generate time vector
stem(n,c); %Plot the output
title('Convolution between x and h');
grid on; %Show grid on the plot
OUTPUT:
10
0
0 1 2 3 4 5 6 7 8
2
INSTITUTE OF BIOMEDICAL ENGINEERING & TECHNOLOGY LIAQUAT
UNIVERSITY OF MEDICAL & HEALTH SCIENCES JAMSHORO
CORRELATION:
SYNTAX
TASK#2:
Write an Example of Correlation in MATLAB:
CODE:
Instructions:
Please complete your lab reports neatly.
%% Cross Correlation
All code must be commented appropriately and included in the file submitted.
f=1;
Handouts are required
%Frequency to be submitted by the next lab.
of signal
Fs=20*f;
Make sure%Sampling Frequency
your handout contains of the required
all the signal information and that the file is
Ts=1/Fs; %Determine sampling time
named correctly.
nTs=0:Ts:1; %Indices of samples in the signal
sig1=sin(2*pi*f*nTs); %Compute sample values of the signal
sig2=sin(2*pi*f*nTs+pi); %Compute sample values of the signal
corsig=xcorr(sig1,sig2); %Compute the correlation between the
signals
subplot(311);plot(nTs,sig1); %Plot the signals
title('Correlation between two sinusoids'); %Give title to plot
xlabel('Time'); %Give label to x-axis
ylabel('Amplitude'); %Give label to y-axis
grid on; %display grid
subplot(312);plot(nTs,sig2); %Plot the signals
xlabel('Time'); %Give label to x-axis
ylabel('Amplitude'); %Give label to y-axis
grid on; %display grid
subplot(313);stem(-20:20,corsig); %Plot the signals
xlabel('Time Lag'); %Give label to x-axis
ylabel('Amplitude'); %Give label to y-axis
grid on; %display grid
3
INSTITUTE OF BIOMEDICAL ENGINEERING & TECHNOLOGY LIAQUAT
UNIVERSITY OF MEDICAL & HEALTH SCIENCES JAMSHORO
OUTPUT:
Correlation between two sinusoids
A m p litu de
1
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time
A m p litu de
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time
A m p litu de
10
-10
-20 -15 -10 -5 0 5 10 15 20
Time Lag
BONUS TASK:
Convolute an exponential signal with a unit step function, and plot three different
graph in one window using subplot command.
Instructions:
Please complete your lab reports neatly.
All code must be commented appropriately and included in the file submitted.
Handouts are required to be submitted by the next lab.
Make sure your handout contains all the required information and that the file is
named correctly.
4
INSTITUTE OF BIOMEDICAL ENGINEERING & TECHNOLOGY LIAQUAT
UNIVERSITY OF MEDICAL & HEALTH SCIENCES JAMSHORO