0% found this document useful (1 vote)
416 views8 pages

Lab Report 3 DSP

This document describes tasks from a lab on signal processing. It includes: 1) Writing code to decompose a signal into its even and odd components using MATLAB. 2) Noting a limitation of the "conv" command and writing a "conv_m" function to remove it by allowing signals to start from times other than zero. 3) Convolving two sample signals using "conv" and "conv_m" and plotting the results. 4) Writing a convolution function in MATLAB to perform 1D linear convolution with multiple steps.

Uploaded by

Qasim Idrees
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
416 views8 pages

Lab Report 3 DSP

This document describes tasks from a lab on signal processing. It includes: 1) Writing code to decompose a signal into its even and odd components using MATLAB. 2) Noting a limitation of the "conv" command and writing a "conv_m" function to remove it by allowing signals to start from times other than zero. 3) Convolving two sample signals using "conv" and "conv_m" and plotting the results. 4) Writing a convolution function in MATLAB to perform 1D linear convolution with multiple steps.

Uploaded by

Qasim Idrees
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Page |1

In Lab Tasks
Task-1: Write a MATLAB code to decompose a sequence into its even and odd components. Take
help from Pre-Lab work.

M-FILE CODE:
t=-10:10; %vector time
x=rand(1,numel(t)); % Your signal
xmt=[fliplr(x(t>=0)) fliplr(x(t<0))]
xe=(xmt+x)./2
xo=(x-xmt)./2
subplot(3,1,1);
stem(t,x,'FILLED');
legend('x[n]');
title('Your signal x')
subplot(3,1,2);
stem(t,xe,'FILLED');
title('Even part')
legend('xe[n]');
subplot(3,1,3);
stem(t,xo,'FILLED');
legend('xo[n]');
title('Odd part');

GRAPH:

Your signal x
1
x[n]
0.5

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
Even part
1
xe[n]
0.5

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
Odd part
0.5
xo[n]
0

-0.5
-10 -8 -6 -4 -2 0 2 4 6 8 10
Page |2

FUNCTION M-FILE CODE:


function [y ] = conv_m( x,x1,x2,h,h1,h2 )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
n_x=x1:x2;
n_h=h1:h2;

subplot(3,1,1);
stem(n_x,x,'FILLED');
title('Original Signal');
legend('x[n]');
subplot(3,1,2);
stem(n_h,h,'FILLED');
title('Impulse Signal');
legend('h[n]');

dif_1=x1-h1;
dif_2=x2-h2;

z_1=zeros(1,abs(dif_1)); % difference of starting points of two


arrays

if(x1<h1) % if x array starts before h array


h_new=[z_1 h]; % concatenate zeries inffront of h array
x_new=x;
else
x_new=[z_1 x]; % otherwise seconcatenate zeries inffront of x
array
h_new=[h];
end

z_2=zeros(1,abs(dif_2)); % difference of ending points of two arrays

if(x2<h2) % if x array ends before h array


x_n=[x_new z_2]; % concatenate zeries inffront of x array
h_n=[h_new];
else
h_n=[h_new z_2]; % otherwise seconcatenate zeries inffront of h
array
x_n=[x_new];
end

y=conv(x_n,h_n);

subplot(3,1,3);
n_y=-1*(length(y)-1)./2:(length(y)-1)./2;
stem(n_y,y,'FILLED')
Page |3

title('Output Signal');
legend('y[n]');

end
COMMAND WINDOW CODE:
x1=-5;
x2=+5;
nx=x1:x2;
h1=-10;
h2=10;
nh=h1:h2;
x=cos(nx);
h=sin(nh);
plot(nx,x)
plot(nh,h)
conv_m(x,x1,x2,h,h1,h2)

GRAPH:

Original Signal
1
x[n]
0

-1
-5 -4 -3 -2 -1 0 1 2 3 4 5
Impulse Signal
1
h[n]
0

-1
-10 -8 -6 -4 -2 0 2 4 6 8 10
Output Signal
5
y[n]
0

-5
-20 -15 -10 -5 0 5 10 15 20
Page |4

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.
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]

PART (a)
M-FILE CODE:
x = [1 2 1];
n=[0 1 2];
h = [1 1 1];
subplot(3,1,1);
stem(n,x,'FILLED');
legend('x[n]');
title('input signal')
subplot(3,1,2);
stem(n,h,'FILLED');
legend('h[n]');
title('impulse signal')
subplot(3,1,3);
y=conv(h,x);
stem(0:length(y)-1,y,'FILLED');
legend('y[n]');
title('output signal')
GRAPH:
Page |5

input signal
2
x[n]
1

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
impulse signal
1
h[n]
0.5

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
output signal
4
y[n]
2

0
0 0.5 1 1.5 2 2.5 3 3.5 4

PART (b)
M-FILE CODE:
x= [-1 4 -3 -2 1 0 2];
nx=[-2:4];
h= [1 1 1];
nh= [-1 0 1]
stem(nx,x)
title('input signal')
subplot(3,1,2);
stem(nh,h);
title('impulse signal')
y=conv_m(x,-2,4,h,-1,1)

GRAPH:
Page |6

Original Signal
5
x[n]
0

-5
-2 -1 0 1 2 3 4
Impulse Signal
1
h[n]
0.5

0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Output Signal
5
y[n]
0

-5
-6 -4 -2 0 2 4 6

 x[n] = [-1 4 -3 -2 1 0 2], n=[-2:4] h[n] = [1 1 1], n= [-1 0 1]

Task 4: Write a function convolution in MATLAB that performs 1D linear convolution by the
steps mentioned above. You can use any method.
Page |7

M-FILE CODE:
n=-5,1;
x=[-1 5 -3 -5 1 8 2];
n1=0:3;
h = [1 2 1 2]
f=conv(x,h);
stem(-5:4,f)
end
GRAPH:
Page |8

Critical Analysis / Conclusion


(By Student about Learning from the Lab)

In this lab different techniques of analysis of LTI systems were discussed in


detail and performed practically on MATLAB.
The decomposition of a signal into it’d even and odd components was observed.

The output of an LTI system was derived from the input and impulse signals
through convolution.
Convolution was performed using different methods an the drawback of ‘conv’
command was discussed in detail. The conv command after convolution
automatically starts the signal from n=0 which is not the case for most periodic
signals centered at n=0.
To overcome this drawback, a new usermade function was designed called
conv_m which removed this drawback and also allowed the convolution of
signals with unequal length which in not possible using the built in conv
command.

You might also like