Periodogram in Octave/Matlab vs Scipy

前端 未结 4 783
臣服心动
臣服心动 2021-01-02 07:54

I am porting some matlab code to python using scipy and got stuck with the following line:

Matlab/Octave code



        
相关标签:
4条回答
  • 2021-01-02 08:15

    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.

    0 讨论(0)
  • 2021-01-02 08:17

    Did you look at the results ?

    Outputs

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

    0 讨论(0)
  • 2021-01-02 08:22

    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.

    0 讨论(0)
  • 2021-01-02 08:33

    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.

    0 讨论(0)
提交回复
热议问题