% This file gives: (1) a brief Matlab tutorial of the "conv" function and
%                  (2) an example of the convolution of two simple curves.
%
% (1) Convolution and polynomial multiplication, whose Syntax is w = conv(u,v).
%     Descriptionw = conv(u,v) convolves vectors u and v. Algebraically, 
%     convolution is the same operation as multiplying the polynomials whose 
%     coefficients are the elements of u and v. 
%
%     Definition: 
%     Let m = length(u) and n = length(v). 
%     Then w is the vector of length m+n-1 whose kth element is 
%           w(k)=SUM [u(j)v(k+1-j)]. 
%     The sum is over all the values of j which lead to legal subscripts 
%     for u(j) and v(k+1-j), specifically j = max(1,k+1-n): min(k,m). 
%     When m = n, this gives w(1) = u(1)*v(1)
%     w(2) = u(1)*v(2)+u(2)*v(1)
%     w(3) = u(1)*v(3)+u(2)*v(2)+u(3)*v(1)
%     ...
%     w(n) = u(1)*v(n)+u(2)*v(n-1)+ ... +u(n)*v(1)
%     ...
%     w(2*n-1) = u(n)*v(n)
%
%     Algorithm:
%     The convolution theorem says, roughly, that convolving two sequences is
%     the same as multiplying their Fourier transforms. 
%     In order to make this precise, it is necessary to pad the two vectors 
%     with zeros and ignore roundoff error. 
%     Thus, if X = fft([x zeros(1,length(y)-1)])
%     and Y = fft([y zeros(1,length(x)-1)])
%     then conv(x,y) = ifft(X.*Y)
%
% (2) Below, here is a simple example of the convolution of sin(t) and 
%     cos(t) with -2*pi < t < 2*pi.
%

t1=-2.*pi:0.001:2.*pi;
s1=sin(t1);s2=cos(t1);
oo=conv(s1,s2);

% Curve 1:
subplot(311); plot(t1,s1,'b');grid
axis([-2.*pi 2.*pi min(s1) max(s1)]);
title('sin(t), with -2\pi<t<2\pi');

% Curve 2:
subplot(312);plot(t1,s2,'r');grid
axis([-2.*pi 2.*pi min(s2) max(s2)]);
title('cos(t), with -2\pi<t<2\pi');

% Convolution:
subplot(313); plot(oo,'b');grid
axis([1 length(oo) min(oo) max(oo)]);
title('Convolution of both curves');



% "Complex and Chaotic Nonlinear Dynamics. 
% Advances in Economics and Finance, 
% Mathematics and Statistics" 
% T.Vialar, Springer 2009
% Copyright(c).