Q-bertsuit
Q-bertsuit

Reputation: 3437

Understanding the output from the fast Fourier transform method

I'm trying to make sense of the output produced by the python FFT library.

I have a sqlite database where I have logged several series of ADC values. Each series consist of 1024 samples taken with a frequency of 1 ms.

After importing a dataseries, I normalize it and run int through the fft method. I've included a few plots of the original signal compared to the FFT output.

import sqlite3
import struct
import numpy as np
from matplotlib import pyplot as plt
import time
import math

conn = sqlite3.connect(r"C:\my_test_data.sqlite")
c = conn.cursor()

c.execute('SELECT ID, time, data_blob FROM log_tbl')


for row in c:
    data_raw = bytes(row[2])
    data_raw_floats = struct.unpack('f'*1024, data_raw)
    data_np = np.asarray(data_raw_floats)

    data_normalized = (data_np - data_np.mean()) / (data_np.max() - data_np.min())

    fft = np.fft.fft(data_normalized)
    N = data_normalized .size

    plt.figure(1)
    plt.subplot(211)
    plt.plot(data_normalized )

    plt.subplot(212)
    plt.plot(np.abs(fft)[:N // 2] * 1 / N)
    plt.show()

    plt.clf()

enter image description here

enter image description here

enter image description here

The signal clearly contains some frequencies, and I was expecting them to be visible from the FFT output.

What am I doing wrong?

Upvotes: 2

Views: 634

Answers (1)

zabop
zabop

Reputation: 7852

You need to make sure that your data is evenly spaced when using np.fft.fft, otherwise the output will not be accurate. If they are not evenly spaced, you can use LS periodograms for example: http://docs.astropy.org/en/stable/stats/lombscargle.html. Or look up non-uniform fft.

About the plots: I don't think that you are doing something obviously wrong. Your signal consists a signal with period in the order of magnitude 100, so you can expect a strong frequency signal around 1/period=0.01. This is what is visible on your graphs. The time-domain signals are not that sinusoidal, so your peak in the frequency domain will be blurry, as seen on your graphs.

Upvotes: 4

Related Questions