# CSD MATLAB Resources

May 24, 2019

MATLAB® offers operators supporting several statistical estimators using Welch’s method. These include the operation cpsd<…>:

 cpsd(…) Estimate cross power spectral density. https://www.mathworks.com/help/signal/ref/cpsd.html

### Example Code from MATLAB

```  clear                                    % clear all variables
format long g;                           % set format of text output
N = 16;                                  % length of input data sequence x
Fs = 1;                                  % sample rate (used for scaling)
percent = 50;                            % percent overlap
Nfft = N * (1-percent/100);
x= [0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0]';
y= [0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0]';
%   0        3          7              11  13  15
%----------------------------------------------------------------------------
% smoothing window
%----------------------------------------------------------------------------
window = hanning(Nfft);                  % Hanning       window
%----------------------------------------------------------------------------
% calculate estimates
%----------------------------------------------------------------------------
[Sxy,freq]=cpsd(               x,y,window,Noverlap,Nfft,Fs);  % estimate Sxy
% |      |         |           |  |  |         |    |    |_____ sample rate
% |      |         |           |  |  |         |    |__________ estimate length
% |      |         |           |  |  |         |_______________ num. overlapping positions
% |      |         |           |  |  |_________________________ window type
% |      |         |           |  |____________________________ data sequence y
% |      |         |           |_______________________________ data sequence x
% |      |         |___________________________________________ estimation operation
% |      |_____________________________________________________ x-axis sequence
% |____________________________________________________________ estimate sequence
```