# Using the cpsd function

25 views (last 30 days)
Eric on 15 Jan 2021
Commented: Bjorn Gustavsson on 15 Jan 2021
Dear everyone,
I have some data that was collected at a sampling rate of 200 sample/second. However, I want to calculated the cross-spectral density for only 20 sample (or 100 ms). Does anyone know how to use the cpsd function from MATLAB to do this?
So far, I have made this my input:
[Pxy,W] = cpsd(xdata,ydata,[],10,[],0);
But this gives me a weird values as output.
Any help is much appreciated!
Best,
Eric

Bjorn Gustavsson on 15 Jan 2021
Simply use the spectrogram function to calculate the spectrograms of your two time-series, something like this:
[S1,F1,T1] = spectrogram(D1,Window,Noverlap,[],Fs);
[S2,F2,T2] = spectrogram(D2,Window,Noverlap,[],Fs);
C12 = S1.*conj(S2);
xp12 = abs(C12);
That should give you the complex cross-spectra and the cross-spectral power.
20 samples are not that very many though? You might not get that great frequency-resolution on that...
HTH
Eric on 15 Jan 2021
Thanks for the comment!
What would you set the window length and overlap to if you want to calculate the cross-spectral density for smaller segments independently of each other?
Best,
Eric
Bjorn Gustavsson on 15 Jan 2021
You're welcome.
That is a great question - In my experience it comes down to what time and frequency-resolution you need, and the time-variations of your signal. I typically test around a bit. In most of my cases I have interest in variations that occur on "time-scales" of some 32-100 samples, for which I typically use 48 - 150 samples and 16 - 50 sample overlap, and then I live with the spectral resolution that gives me. With hanning/hamming/blackman/etc windows the samples shouldn't be too correlated. (note that this is not my field of expertise - then I'd start to talk about multi-taper something-slepian-something-advanced - but this is what I've gotten good enough results with.)