# How to do convolution in time domain in Matlab

27 views (last 30 days)
mandy on 27 Oct 2015
Commented: zi-yan liu on 20 May 2020
I am learning the use of the convolution command and I am trying to do convolution of two rect signals in Matlab. Every time I type in the code below I get the error that the vectors must be the same length. If i look in my workspace it says that x=1x80001double and t=1x40001double. I need to know how to fix this error. Also, should I be using the heaviside command for doing convolution of rects in matlab? I want to make sure that the convolution will turn out correct.
t=-20:0.001:20;
y=heaviside(t+1)-heaviside(t-1)
plot(t,y);
axis([-10 10 0 2])
figure
t1=-20:0.001:20;
y1=heaviside(t+1)-heaviside(t-3)
plot(t1,y1)
axis([-10 10 0 2])
figure
x=conv(y, y1);
plot(x,t)
t=-20:0.001:20;
zi-yan liu on 20 May 2020
The error lies in the last line: t=-20:0.001:20: The length of x is 1x80001double, but the length of the t is 1x40001double. So, you cannot get a plot successfully.
To get the conv plot, you need to fix the value of t of the last plot to -40:0.001:40.