function [Pxy, Pxyc, f] = csd(P1, P2, P3, P4, P5, P6, P7, P8)
% CSD	Cross Spectral Density estimate
%	Pxy = CSD(X,Y,NFFT,Fs,WINDOW) estimates the Cross Spectral Density of 
%	signal vectors X and Y using Welch's averaged periodogram method.  X and
%	Y are divided into overlapping sections, each of which is detrended, 
%	then windowed by the WINDOW parameter, then zero-padded to length NFFT.
%	The products of the length NFFT DFTs of the sections of X and Y are 
%	averaged to form Pxy.  Pxy is length NFFT/2+1 for NFFT even, (NFFT+1)/2
%	for NFFT odd, or NFFT if the either X or Y is complex.  If you specify 
%	a scalar for WINDOW, a Hanning window of that length is used.  Fs is the
%	sampling frequency which doesn't effect the cross spectrum estimate 
%	but is used for scaling of plots.
%
%	[Pxy,F] = CSD(X,Y,NFFT,Fs,WINDOW,NOVERLAP) returns a vector of frequen-
%	cies the same size as Pxy at which the CSD is estimated, and overlaps
%	the X and Y sections NOVERLAP samples.
%
%	[Pxy, Pxyc, F] = CSD(X,Y,NFFT,Fs,WINDOW,NOVERLAP,P) where P is a scalar
%	between 0 and 1, returns the P*100% confidence interval for Pxy.
%
%	CSD(X,Y,...,DFLAG), where DFLAG can be 'linear', 'mean' or 'none', 
%	specifies a detrending mode for the prewindowed sections of X and Y.
%	DFLAG can take the place of any parameter in the parameter list
%	(besides X and Y) as long as it is last, e.g. CSD(X,Y,'none');
%	
%	CSD with no output arguments plots the CSD in the current figure window,
%	with confidence intervals if you provide the P parameter.
%
%	The default values for the parameters are NFFT = 256 (or LENGTH(X),
%	whichever is smaller), NOVERLAP = 0, WINDOW = HANNING(NFFT), Fs = 2, 
%	P = .95, and DFLAG = 'linear'.  You can obtain a default parameter by 
%	leaving it off or inserting an empty matrix [], e.g. CSD(X,Y,[],10000).
%
%	See also PSD, COHERE, TFE
%	ETFE, SPA, and ARX in the Identification Toolbox.

%	Author(s): T. Krauss, 3-30-93
%	Copyright (c) 1984-94 by The MathWorks, Inc.
%       $Revision: 1.10 $  $Date: 1994/01/25 17:58:54 $

error(nargchk(2,8,nargin))
if (length(P1)~=length(P2)),
    error('X and Y must be the same length.')
end
[msg,x,y,nfft,Fs,window,noverlap,p,dflag]=eval(optargs('psdchk',nargin,''));
error(msg)
    
% compute CSD
x = x(:);		% Make sure x is a column vector
y = y(:);		% Make sure y is a column vector
window = window(:);
n = length(x);		% Number of data points
nwind = length(window); % length of window
k = fix((n-noverlap)/(nwind-noverlap));	% Number of windows
					% (k = fix(n/nwind) for noverlap=0)
index = 1:nwind;
KMU = k*norm(window)^2;	% Normalizing scale factor ==> asymptotically unbiased
% KMU = k*sum(window)^2;% alt. Nrmlzng scale factor ==> peaks are about right

Spec = zeros(nfft,1); Spec2 = zeros(nfft,1);
for i=1:k
    if strcmp(dflag,'linear')
        xw = window.*detrend(x(index));
        yw = window.*detrend(y(index));
    elseif strcmp(dflag,'mean')
        xw = window.*detrend(x(index),0);
        yw = window.*detrend(y(index),0);
    else
        xw = window.*x(index);
        yw = window.*y(index);
    end
    index = index + (nwind - noverlap);
    Xx = fft(xw,nfft);
    Yy = fft(yw,nfft);
    Xy2 = Yy.*conj(Xx);
    Spec = Spec + Xy2;
    Spec2 = Spec2 + Xy2.*conj(Xy2);
end

% Select first half
if ~any(any(imag([x y])~=0)),   % if x and y are not complex
    if rem(nfft,2),    % nfft odd
        select = [1:(nfft+1)/2];
    else
        select = [1:nfft/2+1];   % include DC AND Nyquist
    end
    Spec = Spec(select);
    Spec2 = Spec2(select);
else
    select = 1:nfft;
end
Spec = Spec*(1/KMU);
freq_vector = (select - 1)'*Fs/nfft;

% find confidence interval if needed
if (nargout == 3)|((nargout == 0)&~isempty(p)),
    if isempty(p),
        p = .95;    % default
    end
    confid = zeros(size(Spec));
    if k > 1
        c = (k.*Spec2-abs(Spec).^2)./(k-1);
        c = max(c,zeros(size(Spec)));
        confid = sqrt(c);
    end
    ff = sqrt(2)*erfinv(p);  % Equal-tails.
    confid = (ff.*confid)*(1/KMU);
end

% set up output parameters
if (nargout == 3),
   Pxy = Spec;
   Pxyc = confid;
   f = freq_vector;
elseif (nargout == 2),
   Pxy = Spec;
   Pxyc = freq_vector;
elseif (nargout == 1),
   Pxy = Spec;
elseif (nargout == 0),
   if ~isempty(p),
       P = [Spec max(abs(Spec)-confid,0) abs(Spec)+confid];
   else
       P = Spec;
   end
   newplot;
   plot(freq_vector,10*log10(abs(P))), grid
   xlabel('Frequency'), ylabel('Cross Spectrum Magnitude (dB)');
end
