I have several signals as columns in a pandas dataframe (each of them has some NaNs at the beginning or the end, because they all cover slightly different intervals). The signal has some sort of a trend (which basically means a high-wavelength portion with value in the order of X00) and some small wiggles (values in the order of X - X0). I would like to compute a spectrum of each of these columns, and I expect to see two peaks on it - one in the X-X0 and the other one in X00 (which suggests that I should work on a log scale).
However, the "spectra" that I produced using several different methods (scipy.signal.welch
and numpy.fft.fft
) do not look like the expected output. (peaks always at 20 and 40).
Here are several aspects that I don't understand:
- Is there any time series processing inbuilt somewhere deep in these functions so that they actually don't work if I work with wavelengths instead of periods/frequencies?
- I found the documentation rather confusing and not very helpful, in particular when it comes to input parameters and the output. Do I include the signal as it is or do I need to do some sort of pre-processing before? Should I then include the sampling frequency (i.e. 1/wavelength sampling interval, which, in my case, would be let's say 1/0.01 per m) or the sampling interval (i.e. 0.01 m)? Does the output show the same unit or 1/the unit? (I tried all combinations of unit and 1/unit and none of them yielded a reasonable result, so there is another problem here, too, but I still am uncertain with this.)
- Should I use yet another method/are these not suitable?
Not even sure if these are the right questions to ask, but I am afraid if I knew what question to ask, I would know the answer.
Disclaimer: I am not proficient at signal processing, so I am not actually sure if my issue is really with python or in deeper understanding of the problem.
Even if I try a very simple example, I don't understand the behaviour:
x = np.arange(0,10,0.01)
x = x * np.pi
y = np.sin(0.2*x)+np.cos(3*x)
freq, spec = sp.signal.welch(y,fs=(1/(0.01*pi)))
I would expect to see two peaks in the spectrum, one at ~15 and another one at ~2. Or if it is still in frequency, then at ~1/15 and 1/2. But this is what I get in the first case and this is what I get if I plot
1/freq
instead of freq
: - the 15 is even out of range! So I don't know what I am actually plotting.
Thanks a lot.