pyigal
pyigal

Reputation: 389

Different result using welch function between Matlab and Python

When I run welch function on the same data in Matlab and Python, I get slightly PSD estimation difference, while the sample frequencies are identical.

Here is the parameters i used in both Matlab and Python: Matlab:

winlength=512;
overlap=0;
fftlength=1024;
fs=127.886;
[PSD, freqs] = pwelch(data, winlength, overlap, fftlength, fs);

Python:

freqs, PSD = welch(data, fs=127.886, window='hamming', nperseg=512,
noverlap=None, nfft=1024)

here's a plot presenting the difference: enter image description here

Does anyone have any idea what should I change to get the same results of PSD?

Upvotes: 0

Views: 813

Answers (1)

matiastofteby
matiastofteby

Reputation: 431

In the Matlab documentation https://se.mathworks.com/help/signal/ref/pwelch.html it says that the overlap parameter has to be a positive integer thus 0 is not a valid value.

If you omit the overlap value - (or declare a non-valid value) the parameter is automatically set to a 50% overlap i.e. changing the curve.

Try to set the Python function to a 50% overlap and see if they match.

BTW you rarely want to have zero overlap as this is likely to cause transients in the signal.

Upvotes: 1

Related Questions