function PD=calcpd(net,TS,Q,Pc)
%CALCPD Calculate delayed network inputs.
%
%	Syntax
%
%	  PD = calcpd(net,TS,Q,Pc)
%
%	Warning!!
%
%	  This function may be altered or removed in future
%	  releases of the Neural Network Toolbox. We recommend
%	  you do not write code which calls this function.

% Mark Beale, 11-31-97
% Copyright (c) 1992-1998 by The MathWorks, Inc.
% $Revision: 1.4 $

totalTS = TS + net.numInputDelays;

PD = cell(net.numLayers,net.numInputs,totalTS);
start = 1;
for ts=1:totalTS
  for i=1:net.numLayers
    for j=find(net.inputConnect(i,:))
	  delays = net.inputWeights{i,j}.delays;
	  inputsize = net.inputs{j}.size;
	  numDelays = length(delays);
	  pd = zeros(0,Q);
	  for k=1:numDelays
	    d = delays(k);
		if (ts-d) > 0
		  pd = [pd; Pc{j,ts-d}];
		else
		  pd = [pd; zeros(inputsize,Q)];
		  start = ts+1;
		end
	  PD{i,j,ts} = pd;
	  end
	end
  end
end

PD = PD(:,:,start:ts);
