1

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:

  1. 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?
  2. 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.)
  3. 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)

The signal with a long wavelength and short wavelength portion

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 Spectrum and this is what I get if I plot 1/freq instead of freq: the other spectrum - the 15 is even out of range! So I don't know what I am actually plotting. Thanks a lot.

durbachit
  • 4,626
  • 10
  • 36
  • 49
  • http://stackoverflow.com/questions/25735153/plotting-a-fast-fourier-transform-in-python/25735274#25735274 – Paul H Dec 06 '16 at 05:08
  • Thank you. This does not quite answer my question, but at least gives me a hint - I have to do something with my x as well as with y. But I still don't understand what it is. `xf = np.linspace(0.0, 1.0/(2.0*T), N/2)` - why is it just for half of the points? And then `ax.plot(xf, 2.0/N * np.abs(yf[:N//2]))` - so what are we actually plotting here (why not `yf`, what is the output of the `fft`?) if we still need to multiply `yf` by something and get an absolute value of only half of the points? (If it is a stupid question, I'm sorry. As I said, I have no idea what is going on in these functions) – durbachit Dec 06 '16 at 05:32
  • these are mathematics/signal processing questions, not programming questions. I recommend you read up on theory behind fourier transforms. – Paul H Dec 06 '16 at 16:09
  • Not really. I asked what these numpy/scipy functions do. From what you said, I get it that `scipy.fftpack.fft` is very general, it computes the symmetrical 2-sided spectrum of the real and imaginary signal, so you have to do lots of post-processing (the absolute value, half of the signal etc.) and then the matters regarding fs and length of the signal need to be addressed when you define x , if you want to plot it. Is the same implementation valid for `numpy fft.fft`? http://stackoverflow.com/questions/6363154/what-is-the-difference-between-numpy-fft-and-scipy-fftpack is not too conclusive. – durbachit Dec 06 '16 at 22:21
  • 1
    Try dsp.stackexchange.com – percusse Dec 07 '16 at 19:21

0 Answers0