ValientProcess
ValientProcess

Reputation: 1801

Python: Data analysis using FFT

I have a data which looks like this:

YYYY-MO-DD HH-MI-SS_SSS,  ATMOSPHERIC PRESSURE (hPa) mean,   ATMOSPHERIC PRESSURE (hPa) std
2016-04-20 00:00:00,1006.0515000000001,0.029159119281803602
2016-04-20 00:01:00,1006.039666666667,0.03565211699642609
2016-04-20 00:02:00,1006.0148333333334,0.036891580347842706
2016-04-20 00:03:00,1006.0058333333335,0.03351152934243721
2016-04-20 00:04:00,1005.9714999999999,0.03155973620213212
2016-04-20 00:05:00,1005.955666666667,0.027207094455343653
.............

I'm interested in the Pressure mean which is sampled every minute. My goal is to look for periodic frequencies inside the data.

I've tried the following:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt  
from scipy.fftpack import fft
    df3 = pd.read_csv('Pressure - Dates by Minute.csv', sep=",", skiprows=0)
    Pressure = df3['ATMOSPHERIC PRESSURE (hPa) mean']
    frate = 1/60
    Pfft = np.fft.fft(Pressure[0])
    freqs = fft.fftfreq(len(Pfft), 1/frate)

But i'm getting "tuple index out of range" errors

Any ideas on how to analyze the fft and plot the matching frequencies against the raw data?

The raw data looks like this:

enter image description here

Thanks!

Upvotes: 1

Views: 3269

Answers (2)

Tom Myddeltyn
Tom Myddeltyn

Reputation: 1375

I'm taking a stab at this, I think that the issue is that Pressure[0] is a value and you need to pass an array to np.fft.fft() so try Pfft = np.fft.fft(Pressure)

Upvotes: 1

Nils Werner
Nils Werner

Reputation: 36765

You are retrieving only the first element of Pressure but you should do the fourier analysis on all samples. If you replace

Pfft = np.fft.fft(Pressure[0])

with

Pfft = np.fft.fft(Pressure)

it works:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df3 = pd.read_csv('Pressure - Dates by Minute.csv', sep=",", skiprows=0)
Pressure = df3['ATMOSPHERIC PRESSURE (hPa) mean']
frate = 1. / 60
Pfft = np.fft.fft(Pressure)
Pfft[0] = 0  # Set huge DC component to zero, equates to Pressure = Pressure - numpy.mean(Pressure)

freqs = np.fft.fftfreq(len(Pfft), 1. / frate)
plt.plot(freqs, Pfft)
plt.show()

Upvotes: 3

Related Questions