# cpsd

Compute cross power spectral density.

## Syntax

Pxy=cpsd(x,y)

Pxy=cpsd(x,y,window)

Pxy=cpsd(x,y,window,overlap)

Pxy=cpsd(x,y,window,overlap,nfft)

Pxy=cpsd(x,y,window,overlap,nfft,fs)

Pxy=cpsd(x,y,window,overlap,nfft,fs,range)

[Pxy,freq]=cpsd(...)

cpsd(...)

## Inputs

x
The first signal.
Type: double
Dimension: vector
y
The second signal.
Type: double
Dimension: vector
window
The window size, or the window vector.
Type: double | integer
Dimension: scalar | vector
overlap
The number of overlapping points in adjacent windows.
Type: integer
Dimension: scalar
nfft
The size of the fft.
Type: integer
Dimension: scalar
fs
The sampling frequency.
Type: double
Dimension: scalar
range
The spectrum type: 'onesided' or 'twosided'.
Type: string

## Outputs

Pxy
The cross power spectral density.
Type: vector
freq
The vector of frequencies corresponding to the density values.
Type: vector

## Example

n = 1125;
fs = 1000;
ts = 1/fs;
t = [0:1:(n-1)]*ts;
f1 = 24;
f2 = 56;
omega1 = 2 * pi * f1;
omega2 = 2 * pi * f2;
signal1 = 3 + 5 * cos(omega1 * t) + 7 * cos(omega2 * t);
signal2 = 3 + 7 * cos(omega1 * t) + 3 * sin(omega2 * t);
window = hann(250,'periodic');
overlap = 125;
fftsize = 250;
range = 'onesided';
[Pxy,frq]=cpsd(signal1,signal2,window,overlap,fftsize,fs,range);
f = frq([1, 2, 6, 7, 8, 14, 15, 16])'
p = Pxy([1, 2, 6, 7, 8, 14, 15, 16])'
f = [Matrix] 1 x 8
0  4  20  24  28  52  56  60
p = [Matrix] 1 x 8
1.50000  0.75000  0.72917  2.91667  0.72917  0.43750  1.75000  0.43750

With no return arguments, the function will automatically plot the magnitude response.

It is often recommended to remove the signal means prior to calling cpsd. The function does not remove the mean automatically.

The optional arguments can be defaulted using []. The defaults are as follows:
window
A Hamming window with the largest length that produces eight data segments.
overlap
One half of the window length.
nfft
The window length. A larger value will pad zeros to each block of windowed data.
fs
1.0 Hz.
range
'onesided'