MATLAB: Convolution of Heaviside function

#heaviside-convolution#unit_functionconvolutionheavisideMATLAB

I tried Convolving two functions: one is a heaviside ,i.e, j= 200*{ sin(0.5*pi*(t-4)) * ( u(t-4) – u(t-6) ) } and l= exp((-20).*t).*t
This is the code I wrote –
t=0:0.001:10;
j=(200).*(sin((0.5).*pi.*(t-4)).*(heaviside(t-4)-heaviside(t-6)));
l=exp((-20).*t).*t;
c=-(conv(j,l,'same'));
plot(t,c/10);
hold on;
plot(t,j/10,'r');
plot(t,1000.*l,'g');
axis([0 10 -50 50])
The plot came out to be like this-
The convolved curve (blue curve) shouldn't be zero where j is non-zero.But in the plot, it is.
What's wrong ?
Also, how does defining the range of 't' as in the first line of the code gives differnet values of convolution on the y-axis, if the '0.001' in 't=0:0.001:10' is supposed to be the resolution?

Best Answer

Why are do you have a negative sign in the equation for c?
You need to scale the discrete convolution, i.e., the output of conv, by the time step.
Using the 'same' (or 'valid') option doesn't give you the entire convolution, and you have to shift it as well. In this case, the magic number for the shift is 5 seconds, though I'm not quite sure why that is, though probably has something to do with the fact that 5 is the midpoint of the time vector.
Anyway, here is code that computes the convolution integral and three approximations using conv:
jfun = @(t)( 200*sin(0.5*pi*(t-4)) .* ( (t>=4) - (t>=6) ) );
lfun = @(t)( exp(-20*t).*t );
% compute the convolution integral
tt = 0:.01:10;
cint = 0*tt;
for ii = 1:length(tt)
cfun = @(x,t)(jfun(x) .* lfun(t - x));
cint(ii) = integral(@(x)cfun(x,tt(ii)),0,tt(ii));
end
% approximations, note scaling by time step!
zz1 = conv(jfun(tt),lfun(tt))*tt(2);
zz2 = conv(jfun(tt),lfun(tt),'same')*tt(2);
zz3 = conv(lfun(tt),jfun(tt),'same')*tt(2);
plot(tt,cint,'-x',(0:length(zz1)-1)*tt(2),zz1,'-o',tt+5,zz2,'-+',tt+5,zz3,'-*'),grid
You can reduce the time step on tt to get more points and to get the approximations closer to the integral.
Related Question