Lab Report 4 DSP
Lab Report 4 DSP
DATED: 25-03-2023.
1
Lab # 04:
Convolution of Discrete-time Sequences and its implementation on
DSP kits
Objective:
By the end of this lab students will learn about decomposition of signals into even and odd
components, how to take output from LTI system by convolving input signal x(n) with impulse
response h(n)? This will add to their understanding of LTI system and its operation.
In Lab Tasks
Task 1:
Write a MATLAB code to decompose a sequence into its even and odd components.
CODE:
x=[1 2 3];
n=[0 1 2];
subplot(3,1,1)
stem(n,x)
xlabel('n')
ylabel('Amplitude')
title('Original Signal');
[xa,na]=sigfold(x,n);
[xb,nb]=sigadd(x,xa,n,na);
ye=xb./2;
subplot(3,1,2)
stem(nb,ye)
xlabel('n')
ylabel('Amplitude')
title('Even Signal');
[xa,na]=sigfold(x,n);
[xb,nb]=sigsub(x,xa,n,na);
yo=xb./2;
subplot(3,1,3)
stem(nb,yo)
xlabel('n')
ylabel('Amplitude')
title('Odd Signal');
Functions Used:
Reversal:
function [y,n] = sigfold(x,n)
y=fliplr(x);
n=fliplr(n);
n=-n;
end
2
Addition:
function [y,n] = sigadd(x1,x2,n1,n2)
if n1(1)<n2(1)
t1=n2(1)-n1(1);
z1=zeros(1,t1);
x2=[z1 x2];
n2(1)=n1(1);
elseif n2(1)<n1(1)
t2=n1(1)-n2(1);
z2=zeros(1,t2);
x1=[z2 x1];
n1(1)=n2(1);
end
if n1(end)>n2(end)
t3=n1(end)-n2(end);
z3=zeros(1,t3);
x2=[x2 z3];
n2(end)=n1(end);
elseif n2(end)>n1(end)
t4=n2(end)-n1(end);
z4=zeros(1,t4);
x1=[x1 z4];
n1(end)=n2(end);
end
n=n1(1):n2(end);
y=x1+x2;
end
Subtraction:
function [y,n] = sigsub(x1,x2,n1,n2)
if n1(1)<n2(1)
t1=n2(1)-n1(1);
z1=zeros(1,t1);
x2=[z1 x2];
n2(1)=n1(1);
elseif n2(1)<n1(1)
t2=n1(1)-n2(1);
z2=zeros(1,t2);
x1=[z2 x1];
n1(1)=n2(1);
end
if n1(end)>n2(end)
t3=n1(end)-n2(end);
z3=zeros(1,t3);
x2=[x2 z3];
n2(end)=n1(end);
elseif n2(end)>n1(end)
t4=n2(end)-n1(end);
z4=zeros(1,t4);
x1=[x1 z4];
n1(end)=n2(end);
end
n=n1(1):n2(end);
y=x1-x2;
3
end
OUTPUT:
Task 2:
You should have noticed that ‘conv’ command calculates convolution assuming both input
sequences are starting from origin (i-e no values on –ve t-axis). This is not always the case,
we do have sequences which have values for t<0. Write a code conv_m that would remove
this limitation in the code conv.
Code:
x=[1 2 3];
nx=[0 1 2];
h=[0 6 7];
nh=[0 1 2];
subplot(3,1,1)
stem(nx,x)
xlabel('n')
ylabel('Amplitude')
title('Input Signal');
subplot(3,1,2)
stem(nh,h)
xlabel('n')
ylabel('Amplitude')
title('Impulse Response');
[y,n]=conv_m(x,h,nx,nh);
subplot(3,1,3)
stem(n,y)
xlabel('n')
ylabel('Amplitude')
title('Output Signal');
4
Function:
function [y,n] = conv_m(x,h,n1,n2)
if n1(1)<n2(1)
t1=n2(1)-n1(1);
z1=zeros(1,t1);
h=[z1 h];
n2(1)=n1(1);
elseif n2(1)<n1(1)
t2=n1(1)-n2(1);
z2=zeros(1,t2);
x=[z2 x];
n1(1)=n2(1);
end
if n1(end)>n2(end)
t3=n1(end)-n2(end);
z3=zeros(1,t3);
h=[h z3];
n2(end)=n1(end);
elseif n2(end)>n1(end)
t4=n2(end)-n1(end);
z4=zeros(1,t4);
x=[x z4];
n1(end)=n2(end);
end
nx=n1(1):n2(end);
nh=nx;
y=conv(x,h);
if nx(1)>=0
t1=length(nx)+length(nh)-2;
n=nx(1):t1;
elseif nx(1)<0
t2=length(nx)+length(nh)-2+nx(1)+nx(1);
n=nx(1)+nx(1):t2;
end
end
OUTPUT:
5
Task 3:
Convolve following sequences using MATLAB Function “conv” and “conv_m” and plot the
input, impulse response and output in one figure using “subplot”:
x[n] = [1 2 1], n=[0 1 2] h[n] = [1 1 1], n= [0 1 2]
x[n] = [-1 4 -3 -2 1 0 2], n=[-2:4], h[n] = [1 1 1], n= [-1 0 1]
Part A:
CODE:
x=[1 2 1];
nx=[0 1 2];
h=[1 1 1];
nh=[0 1 2];
subplot(3,1,1),stem(nx,x),xlabel('n'),ylabel('Amplitude'),title('Input
Signal');
subplot(3,1,2),stem(nh,h),xlabel('n'),ylabel('Amplitude'),title('Impulse
Response');
y1=conv(x,h);
n1=0:(length(x)+length(h)-2);
subplot(3,1,3),stem(n1,y1),xlabel('n'),ylabel('Amplitude'),title('Output
Signal using conv');
x=[1 2 1];
nx=[0 1 2];
h=[1 1 1];
nh=[0 1 2];
subplot(4,1,1)
stem(nx,x)
xlabel('n')
ylabel('Amplitude')
title('Input Signal');
subplot(4,1,2)
stem(nh,h)
xlabel('n')
ylabel('Amplitude')
title('Impulse Response');
y1=conv(x,h);
n1=0:(length(x)+length(h)-2);
subplot(4,1,3)
stem(n1,y1)
xlabel('n')
ylabel('Amplitude')
title('Output Signal using conv');
[y2,n2]=conv_m(x,h,nx,nh);
subplot(4,1,4)
stem(n2,y2)
xlabel('n')
ylabel('Amplitude')
title('Output Signal using conv_m');
OUTPUT:
6
Part B:
CODE:
x=[-1 4 -3 -2 1 0 2];
nx=-2:4;
h=[1 1 1];
nh=[-1 0 1];
subplot(4,1,1)
stem(nx,x)
xlabel('n')
ylabel('Amplitude')
title('Input Signal');
subplot(4,1,2)
stem(nh,h)
xlabel('n')
ylabel('Amplitude')
title('Impulse Response');
y1=conv(x,h);
n1=0:(length(x)+length(h)-2);
subplot(4,1,3)
stem(n1,y1)
xlabel('n')
ylabel('Amplitude')
title('Output Signal using conv');
[y2,n2]=conv_m(x,h,nx,nh);
subplot(4,1,4)
stem(n2,y2)
xlabel('n')
ylabel('Amplitude')
title('Output Signal using conv_m');
7
OUTPUT:
CRITICAL ANALYSIS/CONCLUSION:
This lab is performed on the basic operation in DSP which convolution. For a LTI
(Linear Time Invariant) system, the output can be found out by convolution for a
suitable input signal. The system’s Impulse response is convolved with input signal to
get the output. Firstly, we decomposed the signal into even and odd signals. In
MATLAB “y=conv (x, h)” command can be used to find the convolution without
knowing the time axis. To get the accurate time axis we have to do further modification
in our code.