Problem in plotting the convolution
1 view (last 30 days)
Show older comments
I can't plot the convolution of the two signals and I don't know why
dt= 0.0001;
t = 0:dt:10;
xt = 4*cos(2*pi*730*t) +3*cos(2*pi*440*t) + 5*cos(2*pi*490*t);
subplot(2,2,1)
plot(t,xt)
axis([ 0 0.02 -20 20 ])
title(' Ploting x(t)')
xlabel('time')
ylabel('x(t)')
grid on
ht= abs( (1/2*pi*j*t).*( exp(j*1460*pi*t) - exp(j*1450*pi*t)) );
subplot(2,2, 2)
plot(t,ht)
title(' plotting h(t)')
axis( [0 20 0 70] )
xlabel(' Time')
ylabel(' h(t)')
grid on
yt= conv(xt , ht);
subplot(2,2, 3)
plot(t,yt)
title(' plotting y(t)')
axis( [0 20 0 100] )
xlabel(' Time')
ylabel(' y(t)')
grid on
0 Comments
Accepted Answer
Star Strider
on 19 Jul 2023
Use the 'same' argument so that the output is the same size as the input:
yt= conv(xt , ht, 'same');
This works —
dt= 0.0001;
t = 0:dt:10;
xt = 4*cos(2*pi*730*t) +3*cos(2*pi*440*t) + 5*cos(2*pi*490*t);
subplot(2,2,1)
plot(t,xt)
axis([ 0 0.02 -20 20 ])
title(' Ploting x(t)')
xlabel('time')
ylabel('x(t)')
grid on
ht= abs( (1/2*pi*j*t).*( exp(j*1460*pi*t) - exp(j*1450*pi*t)) );
subplot(2,2, 2)
plot(t,ht)
title(' plotting h(t)')
axis( [0 20 0 70] )
xlabel(' Time')
ylabel(' h(t)')
grid on
yt= conv(xt , ht, 'same');
subplot(2,2, 3)
plot(t,yt)
title(' plotting y(t)')
axis( [0 20 0 100] )
xlabel(' Time')
ylabel(' y(t)')
grid on
.
4 Comments
More Answers (1)
Paul
on 19 Jul 2023
Edited: Paul
on 19 Jul 2023
Hi Abdullah,
Although not stated explicitly in the Question, I'm going to assume that
a) the objective is compute the convolution integral, i.e., xt and ht are continuous time signals, and
b) xt and ht are both zero for t < 0 adn t > 10
If those assumptions are correct, here's a similar problem that shows how to use conv to approximate the convolution integral. Maybe you can adapt it to your problem.
Let's use two simple functions for x(t) and h(t), both of which are zero outside the interval 0 <= t <= 3
syms t real
x(t) = exp(-0.2*t)*rectangularPulse(0,3,t);
h(t) = exp(-0.3*t)*rectangularPulse(0,3,t);
By defintion, the convolution integral for these signals is
syms tau real
y(t) = simplify(int(x(tau)*h(t-tau),tau,0,t))
Plot x(t), h(t), and y(t). The duration of y(t) (6 seconds) is the sum of the durations of x(t) and h(t).
fplot([x(t) h(t) y(t)],[0 7])
Now use conv with numerical valuse of x(t) and h(t) sampled at 0.01 seconds
t = 0:.01:3;
x = exp(-0.2*t);
h = exp(-0.3*t);
To get the correct answer, we need to use the full convolution and multiply the result by the sampling period (because both x(t) and h(t) are finite)
y = conv(x,h,'full')*0.01; % 'full' is the default, so not necessary to specify explicitly
Of course, y will be longer than x and h, so we have to define a new time vector to plot it.
hold on
plot((0:numel(y)-1)*0.01,y)
For this problem, if we use the 'same' convolution, we get a y that has the same number of elements x and h and therefore covers 3 seconds. But that that's not sufficient to approximate the 6-second convolution integral. And we can see that plotting it would require a shift to the right by 1.5 seconds to match up with the "central" part of the convolution integral.
y = conv(x,h,'same')*0.01;
plot(t,y)
legend('x(t)','h(t)','y(t)','yconv-full','yconv-same')
2 Comments
Paul
on 28 Jul 2023
I didn't notice the Octave tag because I don't look at the tags.
I'm also not sure about how to interpret this comment.
Are questions tagged with "octave" not supposed to be answered? Should they be closed?
See Also
Categories
Find more on Octave in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!