Error in convolution and i need someone to help me .
4 views (last 30 days)
Show older comments
abdallah el-hambouth
on 30 Nov 2013
Edited: Wayne King
on 30 Nov 2013
Now this is the code but when i run this code it keeps give me this error :- In an assignment A(I) = B, the number of elements in B and I must be the same. so please help me to find the problem. and thanks in advanced .
clear all;
t=-5:0.001:5;
x= zeros(size(t));
x(t>=0 & t<=5)=exp(-t);
subplot(3,1,1),plot(t,x);
h= zeros(size(t));
h(t>=0 & t<=5)=exp(-2*t);
subplot(3,1,2),plot(t,h);
a=conv(x,h);
tt=t(1)+t(1):0.001:t(end)+t(end);
subplot(3,1,3),plot(tt,a*0.001)
hold on
b=conv(h,x);
plot(tt,b*0.001)
hold off
0 Comments
Accepted Answer
Image Analyst
on 30 Nov 2013
Try this:
clc;
clear all;
t=-5:0.01:5;
x= zeros(size(t));
decay = exp(-t);
indexes = t>=0 & t<=5;
x(indexes) = decay(indexes);
subplot(3,1,1);
plot(t,x);
h= zeros(size(t));
decay = exp(-2*t);
h(indexes) = decay(indexes);
subplot(3,1,2),plot(t,h);
a=conv(x,h, 'same');
% Use linspace to get the same number of elements.
tt=linspace(t(1), t(end), length(a));
subplot(3,1,3);
plot(tt,a*0.001)
hold on
b=conv(h,x, 'same');
plot(tt,b*0.001)
hold off
0 Comments
More Answers (1)
Wayne King
on 30 Nov 2013
Edited: Wayne King
on 30 Nov 2013
I'm assuming you want this:
x(t>=0 & t<=5)=exp(-t(t>=0 & t<=5));
h(t>=0 & t<=5) = exp(-2*t(t>=0 & t<=5));
Your mistake is in trying this:
h(t>=0 & t<=5) = exp(-2*t);
You can't restrict the range of the t elements and then try to assign all the elements to h.
So to fix your code:
t=-5:0.001:5;
x= zeros(size(t));
x(t>=0 & t<=5)=exp(-t(t>=0 & t<=5));
subplot(3,1,1),plot(t,x);
h= zeros(size(t));
h(t>=0 & t<=5) = exp(-2*t(t>=0 & t<=5));
subplot(3,1,2),plot(t,h);
a=conv(x,h);
tt=t(1)+t(1):0.001:t(end)+t(end);
subplot(3,1,3),plot(tt,a*0.001)
hold on
b=conv(h,x);
plot(tt,b*0.001)
hold off
0 Comments
See Also
Categories
Find more on File Compression 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!