필터 지우기
필터 지우기

Regarding calculating convolution in Matlab.

조회 수: 23 (최근 30일)
HUIDONG XIE
HUIDONG XIE 2018년 1월 30일
댓글: Faezeh Manesh 2020년 3월 6일
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
Star Strider
Star Strider 2018년 1월 30일
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
HUIDONG XIE 2018년 1월 30일
I thought I could just copy paste my code. Thank you, I will format my code next time I ask a question.

댓글을 달려면 로그인하십시오.

채택된 답변

David Goodmanson
David Goodmanson 2018년 2월 2일
Hi Huidong,
When you do c = conv(a,b) the array length of c is the sum of the array lengths of a and b, minus one. The resulting step size is the same. For the output of conv, you have a longer time array with the same spacing. When you did the second linspace command you cut the array spacing in half, which is not allowed.
Also, you are trying to reproduce a convolution integral in the time domain. Conv does a sum, so you need to multiply by the array step width delt. Replacing the last four lines of code with
delt = t(2)-t(1);
con = conv(h,x)*delt;
t = (1:length(con))*delt - 4; % new time array
hold on;
plot(t,con,'LineWidth',2);
xlim([-2 10])
hold off
should reproduce the example.
After the convolution you want something to start happening at t = 0. If you look at the delays in starting out from the beginning in the x and h waveforms, you should be able to see what the -4 is about in the new time array.
p.s. I replaced subplot(1,2,n) with subplot(2,1,n) so the aspect ratio would be the same as in the example.
  댓글 수: 1
Faezeh Manesh
Faezeh Manesh 2020년 3월 6일
Hello David,
Thanks for your comments. Actually, I have the same problem with convolving my two functions (f and y) in the following MATLAB code:
close all;
clear all;
clc;
%set time vector
t0=0;
tf=100;
N=10000;
dt=(tf-t0)/N;
t=t0:dt:tf;
T0=60;
alpha=1;
% beta=0:0.01:1;
beta=0.01;
%construct the firxt part of the convolution
% for j=1:size(beta,2)
for i=1:length(t)
if(t(i)<=T0)
y(i)=0;
else
y(i)=alpha+beta*(t(i)-T0);
end
end
%plot y(t)
subplot(1,2,1);
plot(t,y,'LineWidth',2);
title('y(t)');
%Second function of the convolution
f2= 0.08787*exp(-((t-63.08)/1.593).^2);
%Convolution of these 2 functions
z=conv(y,f2,'full');
con = z*dt;
subplot(1,2,2);
plot(t,f2,'LineWidth',2);
title('f2');
t = (1:length(con))*dt ;
hold on;
plot(t,con,'LineWidth',2);
% end
I tried to use your guidelines to fix my problem but still I don't know the logic behind your previous comment that you mentioned t = (1:length(con))*delt - 4;
I don't know how you find this -4. And as you can see in my results I have the same delay.
Here are my results:
As you see, the convolution result (which is the red curve) starts from a later time while my both functions (y and f2) jump earlier (around 60). Could you please help me how to fix this problem? I would really appreciate it

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by