1

I am currently working on a project where I record the accelerator's magnitude of a mobile phone doing a periodic movement, which is very visible when I visualise it in python and smooth it. However; I am trying to programmatically get the frequency of the signal (mag), and so far all my tries have failed. The data is stored in a csv, which includes several columns, but I extract the magnitude column, smooth it with a lowpass filter, and then finally try to use the numpy's fft function on my filtered magnitude array, but it doesn't give me the expected result.

Here's an image of my visualised magnitude after smoothing:

Smoothed Data

As you can see, it's quite periodic and you can tell by just looking at it, but I have no idea why FFT fails to catch that.

I have tried FFT from both numpy package as well as scipy, both give me the same results.

data = genfromtxt("data.csv", dtype=float, delimiter=',', names=True)
y = data['mag']
w = np.fft.fft(y)
freq = np.fft.fftfreq(len(w))

The output I'm getting from freq an array where the 'highest'frequency is 0, which I don't understand. If it's relevant, I know the sampling rate at which I record the data from the device is 20 milliseconds, and I have already tried FFT with both the raw data as well as the smoothed, and still no luck.

user3570947
  • 305
  • 1
  • 2
  • 9
  • 2
    Frequencies, not frequency. Fourier transform will find more than one is involved and superimposed. A single frequency is a sine or cosine. Your example is not that. – duffymo Apr 04 '19 at 18:11
  • 1
    The zeroth frequency is a constant; it should be the lowest term in the series. I think that happens b/c your signal has a DC component to it that's not zero. – duffymo Apr 04 '19 at 18:12
  • Could you try to convolve the signal with itself to catch the period as shown in https://stackoverflow.com/questions/49531952/find-period-of-a-signal-out-of-the-fft/49545394#49545394 ? Removing the DC component is a good hint. – francis Apr 04 '19 at 21:38

1 Answers1

1

Your signal has a DC bias. A DC bias is the same (to an FFT) as a cosine wave with a frequency of 0. This DC bias also appears to be larger than the periodic amplitude variations in your signal. So f=0 is the expected result.

There are at least two thing you could try. You could look for the next peak value in your FFT result magnitudes after frequency bin 0. Plot the FFT magnitudes and you might see it. Or you could subtract the DC bias (the mean/average of all the samples) before performing the FFT, which will remove the large FFT result at frequency bin 0.

Note that the FFT result's bin center frequencies are only those that are integer periodic in the FFT's length. If the actual frequency is not one of those, you will have to interpolate. Sinc kernal interpolation is the proper interpolator for FFT result data.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153