Search code examples
pythonscipysignalssignal-processingspectral-density

Why does scipy.signal.welch suppress zero frequency?


I try to use whelch method and I found that zero frequency is abnormal

import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt

n = 100000
s = np.ones(n)
f, psd = signal.welch(s, return_onesided=False)
plt.plot(f, psd)
plt.show()

So result PSD is zero. Why does scipy.signal.welch suppress zero frequency?


Solution

  • Take another look at the docstring for welch In particular, note the detrend argument.

    The default detrend is 'constant', which subtracts the mean from the input before computing the spectrum. To disable detrending, use detrend=False:

    In [57]: from scipy.signal import welch
    
    In [58]: from scipy.fftpack import fftshift
    
    In [59]: n = 1000
    
    In [60]: s = np.ones(n)
    
    In [61]: f, psd = signal.welch(s, return_onesided=False, detrend=False)
    
    In [62]: plot(fftshift(f), fftshift(psd))
    Out[62]: [<matplotlib.lines.Line2D at 0x10f8b6dd8>]
    

    plot