Comparison of mlab.csd and Matlab's cpsd

Hi Ludwig (responding also to list),

I don’t think that the cause of the discrepancy is because of the hamming/hanning window difference. I do set the window in the matlab part to also be a hanning window of length nfft.

Cheers,

Ariel

···

On Sun, Feb 7, 2010 at 7:45 AM, Ludwig Schwardt <ludwig.schwardt@…149…> wrote:

Hi,

On Sun, Feb 7, 2010 at 12:46 AM, Ariel Rokem <arokem@…453…> wrote:

I don’t think that a major reworking of the logic of the function is needed.

Simply replacing the line you mentioned with:

Pxy *= 1 / (np.abs(windowVals)**2).sum()
Pxy[1:-1] *= scaling_factor
if scale_by_freq:
    Pxy[[0,-1]] /= Fs

I agree. I was hoping the first two lines above would be sufficient.

Then I saw scaling_factor also included Fs if scaling by frequency,

and Pxy is of shape (numFreqs,n), i.e. not a straightforward 1-D

array, which caused me to reserve my judgment a little bit… :slight_smile:

What does become more apparent when I do that is that in frequency bands in

which the power is rather small, the ratio discrepancies between the mlab

result and the matlab result can be rather large, on the order of a factor

of 2-2.5, even when the differences are tiny. Similarly, when the power is

rather large, there can be non-negligible differences between the two

results. Is there anything to do about that?

Could this be because Matlab uses a Hamming window by default, while

mlab uses a Hanning window by default? Very similar-sounding names,

and also very similar windows numerically (but not exactly the

same)…

Ludwig


Ariel Rokem
Helen Wills Neuroscience Institute
University of California, Berkeley
http://argentum.ucbso.berkeley.edu/ariel