%	Function to perform a DT convolution on two signals and return
%	their convolution at specified discrete times.  The two 
%	signals are in column vectors, x1 and x2, and their times 
%	are in column vectors, n1 and n2.  The discrete times at which 
%	the convolution is desired are in the column , n12.  The 
%	returned convolution is in column vector, x12, and its 
%	time is in column vector, n12.  If n12 is not included 
%	in the function call it is generated in the function as the 
%	total time determined by the individual time vectors
%
%	[x12,n12] = convD(x1,n1,x2,n2,n12)

function [x12,n12] = convD(x1,n1,x2,n2,n12)

%     Convolve the two vectors using the MATLAB conv command

	xtmp = conv(x1,x2) ;
 
%     Set a temporary vector of times for the convolution 
%     based on the input time vectors

	ntmp = n1(1) + n2(1) + [0:length(n1)+length(n2)-2]' ;

%     Set the first and last times in temporary vector

	nmin = ntmp(1) ; nmax = ntmp(length(ntmp)) ;    	

if nargin < 5,  %   If no input time vector is specified use ntmp
		x12 = xtmp ; n12 = ntmp ;

	else        
%   		If an input time vector is specified, compute the 
%		convolution at those times
		x12 = 0*n12 ;   %   Initialize output convolution to zero

%   		Find the indices of the desired times which are between 
%		the minimum and maximum of the temporary time vector

		I12intmp = find(n12 >= nmin & n12 <= nmax) ; 

%   		Translate them to the indices in the temporary time vector

		Itmp = (n12(I12intmp) - nmin) + 1 ;

%   		Replace the convolution values for those times 
%		in the desired time vector

		x12(I12intmp) = xtmp(Itmp) ;
	end
