Skip to content Skip to sidebar Skip to footer

Periodogram In Octave/matlab Vs Scipy

I am porting some matlab code to python using scipy and got stuck with the following line: Matlab/Octave code [Pxx, f] = periodogram(x, [], 512, 5) Python code f, Pxx = signal.per

Solution 1:

After researching octave's and scipy's periodogram source code I found that they use different algorithm to calculate power spectral density estimate. Octave (and MATLAB) use FFT, whereas scipy's periodogram use the Welch method.

As @georgesl has mentioned, the output looks quite alike, but still, it differs. And for porting reason it was critical. In the end, I simply wrote a small function to calculate PSD estimate using FFT, and now output is the same. According to timeit testing, it works ~50% faster (1.9006s vs 2.9176s on a loop with 10.000 iterations). I think it's due to the FFT being faster than Welch in scipy's implementation, of just being faster.

Thanks to everyone who showed interest.

Solution 2:

I faced the same problem but then I came across the documentation of scipy's periodogram

As you would see there that detrend='constant' is the default argument. This means that python automatically subtracts the mean of the input data from each point. (Read here). While Matlab/Octave do no such thing. I believe that is the reason why the outputs are different. Try specifying detrend=False, while calling scipy's periodogram you should get the same output as Matlab.

Solution 3:

After reading the Matlab and Scipy documentation, another contribution to the different values could be that they use different default window function. Matlab uses a Hamming window, and Scipy uses a Hanning. The two window functions and similar but not identical.

Solution 4:

Did you look at the results ?

Outputs

The slight differences between the two results may comes from optimizations/default windows/implementations/whatever etc.

Post a Comment for "Periodogram In Octave/matlab Vs Scipy"