Wednesday, February 6, 2013

Convolution of two functions

Enter the following code in to your MATLAB editor. You could see how the convolution of two function in calculated. Try the same for your functions.


clc;
clear all;
t = 0  : 0.05 : 4;
x = inline ('(exp(-0.001*t)).*(t >= 0)','t');
h = inline ('(exp(-0.001*t).*(t >= 0)) + (exp(-3*t)).*(t>=0)','t');
dtau = 0.005;
tau = -1:dtau:4;
ti = 0;
tvec = -0.25:0.1:3.75;
y = NaN * zeros (1, length (tvec));
figure(1);
for t= tvec
    ti = ti + 1;
    xh = x(t - tau) .* h(tau);
    y(ti) = sum (xh .* dtau);
    subplot(2,1,1), plot(tau, h(tau), 'k-', tau,x(t-tau),'k--',t,0,'ok');
    axis([tau(1) tau(end) -0.25 3]);
    subplot(2,1,2), plot(tvec, y, 'k', tvec(ti), y(ti),'ok');
    axis([tau(1) tau(end) -0.25 5]);
    drawnow;
    pause(0.1);
end

No comments:

Post a Comment