%Matlab demonstration of example convolution in notes, part 1
%clear any old variables
clear

%total length of f1, f2 time series in seconds
N=10;

%time step size in seconds
dt=0.02;

%length of vectors to create
M=N/dt;

%zero time reference point
ztime=M/4;

%Here is the boxcar function
%initialize f1
f1=zeros(M,1);
%insert ones into the correct elements for f1
f1((ztime-0.5/dt):ztime+(0.5/dt))=ones(1+1/dt,1);

%Here is the decaying exponential function (starting at zero time);
%initialize f2
f2=zeros(M,1);
%insert a decaying exponential into the correct elements
f2(ztime:M)=exp(-dt*(0:M-ztime));

%Create the time axis vector for plotting f1 and f2
taxis = ((1:M)-ztime)*dt;


%Do the convolution (normalized by dt, to make the sum scale like the integral)
c=conv(f1,f2)*dt;

%Create the time axis vector for the convolution (which has length(c)=2*M-1)
taxisc=((1:length(c))-2*ztime)*dt;

figure(1)

%plot f1
subplot(3,1,1)
plot(taxis,f1)
grid
ylabel('f_1(t)')
axis([-1 8 0 1])

%plot f2
subplot(3,1,2)
plot(taxis,f2)
grid
ylabel('f_2(t)')
axis([-1 8 0 1])

%plot the convolution
subplot(3,1,3)
plot(taxisc,c)
grid
ylabel('c(t)=f_1(t)*f_2(t)')
xlabel('Time (s)')
axis([-1 8 0 1])
