2

Hello I am trying to do a Fourier transform on some data using Python, but I have difficulty understanding much of the mathematics & code behind it. From my limited understanding, I know a Fourier transform is essentially another way to look at a function. It's the sum of several sin waves that come together to make another representation of the original function.

I have an x & y list each with data from a text file. The x data represents time in years, and the y data represents data from tree rings over that course of time. How do I go about doing the fft using the data from my x & y lists? Every example I see creates random points to do an fft, but I don't think I need to do this if I already have the data? Any assistance would be appreciated. I've attached what my code looks like as well as a photo of the graph from my data.

from scipy import stats
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import linregress


# Define Empty List for data

x = [] # Years from 1906 - 2005
y = [] # Tree Ring Data 1906 - 2005

# Load Data & Append to Empty Lists

for num in np.loadtxt('Years_Observed.txt'):
    x.append(num)

for num in np.loadtxt('Flow_Observed.txt'):
    y.append(num)


# Adjust Size and dpi of Graph

fig = plt.figure(figsize=(15,6), dpi=100)

# Graph Title

plt.title("Tree Ring Data From 1906 to 2005")

# X & Y Axis Labels

plt.xlabel("Year")
plt.ylabel("Streamflow of Colorado River (Millions Acre/ft)")

plt.scatter(x, y, label = 'Tree Ring Data')
plt.legend()
plt.show()

What I'm working with:

What I'm working with

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I'm assuming that ultimately you have a set of (year, datum) points, right? (As opposed to completely unrelated stuff in `x` and `y`.) If so, this presumably means you don't have uniform spacing between your data points in time. That makes things more complex than a traditional vanilla FFT. You're likely to need a more complicated tool called the [*non-uniform discrete Fourier transform*](https://en.wikipedia.org/wiki/Non-uniform_discrete_Fourier_transform). – Oliver Charlesworth Dec 03 '17 at 00:21
  • If you're just after the plot, you might want to try `plt.psd`. As you might notice from the [documentation](https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.psd.html) it will only take your y datarow as argument, in addition Fs which is sampling frequency. As a comment above mentioned you need periodic sampling for most of the default methods. You can check if sampling frequency of the data makes sense (x variable) by either ploting it (`plt.plot(x)` should show straight line), or employing other methods to see wether it is linear (np.diff(x).std() should be close to 0) – zck Dec 03 '17 at 01:17

0 Answers0