Skip to main content

Convolution of two signals using conv() in Matlab

We use the conv(x1,x2) function in Matlab to perform convolution of two discrete signals.

For example, to perform the convolution of two discrete time sequences x1 (n) = {1, 0, 1,2} 

and x2 (n) = {1, 2, 4, 6}.

Code:

 x1=[1,0,1,2]; %1st sequence

N1=length(x1); %stores length of 1st sequence

n1=0:1:N1-1; %range of x axis for 1st graph of 1st sequence

subplot(1,3,1); % in a 1x3 grid, 1st sequence occupies the 1st grid

stem(n1,x1,'linewidth',1.5); %to plot the sequence specifying time axis, signal and line width

xlabel('n1----->'); %title of x axis

ylabel('x1----->');%title of y axis

grid on;

x2=[1,2,4,6];%2nd sequence

N2=length(x2);  %stores length of 2nd sequence

n2=0:1:N2-1; %range of x axis for 2nd graph of 2nd sequence

subplot(1,3,2); % in a 1x3 grid, 2nd sequence occupies the 2nd grid

stem(n2,x2,'red','linewidth',1.5); %to plot the sequence specifying time axis,signal,line width and colour

xlabel('n2----->');%title of x axis

ylabel('x2----->');%title of y axis

grid on;

x3=conv(x1,x2);%Performing Convolution

n3=0:1:N1+N2-2;%x-axis specification for convoluted signal

subplot(1,3,3); % in a 1x3 grid, convoluted signal occupies the 3rd grid

stem(n3,x3,'black','linewidth',1.5);%to plot the convoluted sequence 

xlabel('n3----->'); %title of x axis

ylabel('x3----->'); %title of y axis

grid on;

Output:



The graph in blue represents the signal x1(n) and the graph in  red represents the signal x2(n) and the graph in black represents the convoluted signal of x3 = x1(n).x2(n).



Comments

Popular posts from this blog

How to plot a Continuous Time Sequence and Discrete Time Sequence in Matlab

1. For Continuous Time Sequence:        Use the function:  plot(X-axis, Y-axis)          For example to plot a parabolic function p(t)=  At^2 for t>=0                                                                                    0        for t<0           Code:           %UNIT PARABOLIC FUNCTION t=linspace(-20,20,20);      %range of X-asis A=1;                  %amplitude of the function for A=1 it is called unit parabolic function p=[t>=0].*(A.*t.^2)/2;      %parabola equation as defined above subplot(1,2,1);                ...

Narrate a story from your own life experience which reflects kindness to the self or kindness to others or kindness to the environment.

As Shakespeare quoted ‘ Beauty lives with kindness’, kindness is a noble act of gentleness to others, to environment or to one’s own self.   It gives inner happiness and makes one feel good about oneself. I did not know the gift of kindness until I experienced it myself. In my first year in college I went on a trip to Dehradun along with my family. We were heading towards Haridwar. On our way through the jungles of the Garhwal Himalayas amidst the lust green forest, we saw a young girl suddenly appear from behind the trees and jump in front of our car. Our driver stopped the car just on time. There I saw a girl of about ten years old whimpering. Her dress was torn, her hands and feet were bruised and she looked starved. We gave her some food and water and asked about her whereabouts. By now we could understand that she was blind. She told us that she lived in Chidderwala and that she was out with her brother to fetch some fire woods, when it started to rain and she slipped and fe...

A Night without Electricity

It was raining heavily as I hurriedly ran towards my flat in Southern Park. It was already eleven when I was returning home after work. I soon realised by the darkness of the lane that there was a power cut in the locality which made it very difficult to find my way.  Adapting my eyes to the darkness of the lane, I strode along the path I knew so well. I quickly opened the door and entered the apartment. It was a three storey building, the ground floor was Dr. Kunal's Chamber and the first floor belonged to Mr. Bose while I lived on the second floor.  As I entered I could see a faint light coming from a candle burning in the doctor's cabin and feeling for the stair case in the dark , I slowly started to climb the stairs. There was an empty bird cage at the door of Mr. Bose who was a bird lover and would often go to the nearest lake with his DSLR camera early in the morning hoping to take a snap of  the river kingfisher or the common emerald dove. So carefully passing by t...