I'm implementing an existing MATLAB script in Python and noticing some differences in the behaviour of MATLAB's pwelch
function compared to scipy.signal.welch
.
From the docs, scipyscipy.signal.welch
uses a default window size of 256, whereas MATLABMATLAB's pwelch
doc says: "By default, x is divided into the longest possible segments to obtain as close to but not exceed 8 segments with 50% overlap".
By default, x is divided into the longest possible segments to obtain as close to but not exceed 8 segments with 50% overlap".
Both use Hamming windows and overlap by 50%, so I assume the differences I'm seeing are due to window sizes.
However, if I set the same window size for both, I still see different frequency resolution in the results. e.g. for data series of length 6144, sampled every 0.292969 seconds, and set window size as follows:
n = 1000
freqs, power = signal.welch(timeseries, fs=1.0/0.292969, scaling='density', nperseg=n)
print(freqs[2]) # output 0.0034
n = 1500
freqs, power = signal.welch(timeseries, fs=1.0/0.292969, scaling='density', nperseg=n)
print(freqs[2]) # output 0.00227555370371
for MATLAB:
n = 1000;
dt = 0.292969;
[psd,freq] = pwelch(timeseries,n,[],[],1/dt);
freq(2) % output 0.0033
n = 1500;
[psd,freq] = pwelch(timeseries,n,[],[],1/dt);
freq(2) % output 0.0017
For higher window size the difference in output resolution is quite stark, and results in quite different amplitudes for peaks in the spectrum. Am I missing some other parameter which needs adjusting?