# convolution of Heaviside function

32 views (last 30 days)
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?

Paul on 31 Jul 2020
Edited: Paul on 31 Jul 2020
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.

Lomalsvi Bischerre on 31 Jul 2020
Thanks Paul .
You were right- 'same' doesn't show the whole convolution and there is some time shifting required ,otherwise.
The code you wrote worked and I corrected mine with this line- plot((0:length(c)-1)*t(2),c);
Still , I am not sure why reducing or playing with the time steps changes the convolution value on the y-axis.
Paul on 31 Jul 2020
You need to get rid of that negative sign on the computation of c and you need to scale by the time step:
c = conv(j,l)*t(2);
As far as I can tell that negative sign is incorrect. The convolution of these two functions should be positive, as shown in the results from my code.
Multiplying by the time step gives you the proper scaling between the underlying continuous functions and their discretizations. If you do this, you'll find that c is essentially invariant to the time step, in the sense that reducing the time step just makes c a better approximation to the convolution integral. If you reduce the time time step in tt in my code, you should see that effect.
Also, just want to be clear that I've been assuming that l==0 for t<0. That's obviously true for j by it's definition.
Lomalsvi Bischerre on 2 Aug 2020
The negative sign is for the response . The convolution is positive but the response is with respect to the force ( j ), in this case , a spring force for which the displacement response needs to be negative.

R2014a

### Community Treasure Hunt

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

Start Hunting!