Regarding calculating convolution in Matlab.
Show older comments
Just wondering that why my calculation of conv(h,x) is not correct? The red curve in the picture is what I expected for the convolution.


RC=1;
u=1;
t=linspace(-2,10,5000);
x=zeros(1,5000);
for k=1:length(t)
if(t(k)<0)
x(k)=0;
elseif(t(k)>=0 && t(k)<2)
x(k)=0.5;
else
x(k)=0;
end
end
subplot(1,2,1);
plot(t,x,'LineWidth',2);
title('x(t)');
h=zeros(1,5000);
for k=1:length(t)
if(t(k)<0)
h(k)=0;
else
h(k)=RC*exp(-t(k))*u;
end
end
subplot(1,2,2);
plot(t,h,'LineWidth',2);
title('h(x)');
con=conv(h,x);
t=linspace(-2,10,length(con));
hold on;
plot(t,con,'LineWidth',2);
2 Comments
Star Strider
on 30 Jan 2018
In the future, please format your code.
Highlight it, then use the [{}Code] button to format all of it correctly. It is much easier to read.
(I just did it, so it is not necessary for you to do it again.)
HUIDONG XIE
on 30 Jan 2018
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!