I am given two functions x(t) =5[u(t+1)-u(t-1)] and h(t)=u(t-1)-u(t-7). I am asked to convolve these two signals and plot the result in the range -3 to 10. Here is the code that I wrote:
t=-3:0.1:10; t_c=-3:0.05:10; h_t=heaviside(t-1)-heaviside(t-7); x_t1=5.*(heaviside(t+1)-heaviside(t-1)); c_x_h=conv(x_t1,h_t); figure(1) plot(t_c,c_x_h)
However, since this is a simple convolution I verified it by hand and it does not look like the plot i get in MATLAB. The max value should be 10 but in MATLAB i get 100. Also, the duration of the function should be 8 but i get 4 with MATLAB. Please help.

 Akzeptierte Antwort

Rick Rosson
Rick Rosson am 23 Okt. 2014

1 Stimme

Please try:
t_c = -6:0.1:20;
and:
dt = t(2) - t(1);
c_x_h = dt*conv(x_t1,h_t);

4 Kommentare

Dennis
Dennis am 23 Okt. 2014
Thank you for the tip Rick. I tried that and it worked. I am unclear as to why the dt is needed though and why t_c needed to be extended to -6 to 20. Thanks again for your time.
Baiyu Zhang
Baiyu Zhang am 28 Jun. 2018
What is dt = t(2)-t(1)? What does it mean?
Celeste MacNeil
Celeste MacNeil am 14 Okt. 2018
What do you mean dt = t(2)-t(1)?
Celeste MacNeil
Celeste MacNeil am 14 Okt. 2018
I assume you just mean the step?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

IKRAM jebali
IKRAM jebali am 31 Mär. 2017

0 Stimmen

Hello! Thank you. I'had the same problem. Me too amnclear as to why the dt is needed though and why t_c needed to be extended to -6 to 20. I use it but i don't know why? There is any answer please. Thank you.

1 Kommentar

Because the length of the convolution of two matrices of ,length m and n will be m+n-1. inorder to get the same vector size, you need to do this.

Melden Sie sich an, um zu kommentieren.

Paul
Paul am 30 Sep. 2026 um 3:21
Bearbeitet: Paul am 1 Okt. 2026 um 1:44
Define the signals and plot them
syms t tau real
u(t) = heaviside(t);
x(t) = 5*(u(t+1)-u(t-1)); h(t) = u(t-1) - u(t-7);
figure
fplot([x(t),h(t)],[-5,10])
If y(t) is the convolution if x(t) and h(t), then we know that y(t) = 0 for t < 0 and t > 8 by adding the left and right edges of x(t) and h(t) respectively.
Compute the closed form expression of the convolution
ysym(t) = int(x(tau)*h(t-tau),tau,-inf,inf);
To compute the convolution numerically using conv, first define the the unit step function and the sampling period
u = @(t) heaviside(t);
dt = 0.05; % T
Define the time vectors to compute samples x(n*T) and h(n*T) where they are non-zero
tx = -1:dt:1; x = 5*(u(tx+1) - u(tx-1));
th = 1:dt:7; h = u(th-1) - u(th-7);
Now approximate the convolution integral using conv. We need to multiply by dt because conv computes the convolution sum, which is essentially dividing the integrand of the convolution integral into rectangles of width dt and summing their heights. We need to multiply the output of conv by dt to so that the result is the sum of their areas (if either h or x include a Dirac delta, then we'd have to modify this approach by jsut a bit).
y = conv(x,h)*dt;
By the shift property of convolution, we know that the first element of y corresponds to tx(1) + th(1), we know the number of samples in y, and we know that the sampling period is dt. Hence, the time vector that corresponds to the elements of y is
ty = tx(1) + th(1) + (0:numel(y)-1)*dt;
Plot the closed form expression and overlay with the conv approximation, which only covers the finite duration of ysym
figure
fplot(ysym,[-3,10]);
hold on
plot(ty,y,'o'),axis padded
Note that y(1) is not exactly equal to zero
y(1)
ans = 0.0625
which is a consequence of approximating the integral via rectangular integration.
The original problem statement said to get the solution over the interval -3 <= t <= 10. We can take the solution above and pad y on the left and right with the appropriate number of zeros and then, of course, adjust ty as well. I think it's easier to start with the same time vector for both signals, making sure that it covers the non-zero portions of x and h and the desired duration of the output, and then extracting the part we want at the end
t = -2:dt:7; % covers both x and h, output will cover -4 <= t <= 14,
x = 5*(u(t+1) - u(t-1));
h = u(t-1) - u(t-7);
y = conv(x,h)*dt;
ty = 2*t(1) + (0:numel(y)-1)*dt;
ii = ty >= -3 & ty <= 10; % extract the part we care about
ty = ty(ii);
y = y(ii);
figure
fplot(ysym,[-3,10]);
hold on
plot(ty,y,'o'),axis padded

Gefragt:

am 23 Okt. 2014

Bearbeitet:

am 1 Okt. 2026 um 1:44

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by