% function [c] = convolution( [x[, y[, Lx[, Ly]]]])
%
%          Convolution of two sequences
%          No input arguments triggers demo
%

%
% Copyright © 24-Oct-2012 George P. Pavlidis® (sepdek@sepdek.net, gpavlid@ceti.gr)
%
function varargout = convolution( varargin)

	switch nargin
		case 0
			x = [2 1 3 4];
			y = [1 2 -1];
			Lx = 0;
			Ly = 0;
		case 1
			error( 'Provide at least two sequences!');
		case 2
			x = varargin{1};
			y = varargin{2};
			Lx = 0;
			Ly = 0;
		case 3
			x = varargin{1};
			y = varargin{2};
			Lx = varargin{3};
			Ly = 0;
		case 4
			x = varargin{1};
			y = varargin{2};
			Lx = varargin{3};
			Ly = varargin{4};
	end
	Nx = length(x);
	Ny = length(y);
	Rx = Nx+Lx-1;
	Ry = Ny+Ly-1;
	nx = Lx:Rx;
	ny = Ly:Ry;
	
	z = zeros( length(y), length(x));
	for i=1:length(y),
		z(i,:) = x*y(i);
	end
	zz = fliplr(z);
	c=zeros(1,Nx+Ny-1);
	j=1;
	for i=Rx:-1:-Ry
		c(j)=sum(diag(zz,i));
		j=j+1;
	end
	
	switch nargout
		case 0
			disp(c);
		case 1
			varargout{1} = c;
	end