# How to get the Fourier series using Python's $\tt fft$

I Would like to be able to reconstruct every individual sinusoid that makes up a Discrete signal. I Have the following signal: (I am working in Python)

The signal is essentially an array with about 400 elements that varies with time. In Python after calling the fft function on the data

 dft= rfft(dat)/len(dat)  #real fft


I am aware that I can use the result of the fft to obtain the individual Fourier series components, but I am unsure exactly how. The code I have yields me an array of magnitudes for each harmonic of the signal, how do I use that to get each individual component of the Fourier series?

• now the Fourier Series is specifically for periodic signals. (and the DFT or FFT will directly get your Fourier series coefficients from the sampled periodic signal.) are you sure your data is periodic? – robert bristow-johnson May 6 '17 at 4:51
• Could i just perform a periodic extension on the data? – Mustard Tiger May 6 '17 at 4:59
• yes, you can. and that is inherently what happens when you pass your data to the FFT. the DFT treats your $N$ samples of data supplied as a single period of a periodic function or sequence. but your data above has a nasty discontinuity between the end (at 2082) and the beginning (at 2091). periodically extending that data without windowing might look bad after the FFT. – robert bristow-johnson May 6 '17 at 5:59

If you assume that your signal is one period of the original signal, then the FFT gives you the Fourier Series coefficients up to frequency $F_s/2$ (due to Nyquist theorem). The DFT result are exactly the coefficients of the complex Fourier Series. You can convert the complex coefficients to $a_n$ and $b_n$ of the real Fourier series (with sines and cosines) according to Wikipedia. Also, two of my articles (Complex Fourier Series, Spectral Leakage) might help you understand further.

I am no expert in this topic, but have some useful examples to share.

### 1 component example

To keep the i-eth Fourier component, you can zero the rest of the components:

n = len(y)
Y = numpy.fft.fft(y)
numpy.put(Y, range(0, i), 0.0)
numpy.put(Y, range(i+1, n), 0.0)
# Now Y holds 1 imaginary coefficient corresponding with the i-eth Fourier component


If you're looking for the Real signal corresponding to the chosen i-eth component, you can afterwards perform the inverse transform through ifft:

ifft = numpy.fft.ifft(Y) # the sinusoid you're looking for


### Multiple components example

The more Fourier components you keep, the closer you'll mimic the original signal. This example shows what happens when you keep 10, 20, ...up tp n components.

Assuming x and y are your data vectors.

import numpy
from matplotlib import pyplot as plt

n = len(y)
COMPONENTS = [10, 20, n]

for c in COMPONENTS:
colors = numpy.linspace(start=100, stop=255, num=c)
for i in range(c):
Y = numpy.fft.fft(y)
numpy.put(Y, range(i+1, n), 0.0)
ifft = numpy.fft.ifft(Y)
plt.plot(x, ifft, color=plt.cm.Reds(int(colors[i])), alpha=.70)

plt.title("First {c} fourier components".format(c=c))
plt.plot(x,y, label="Original dataset")
plt.grid(linestyle='dashed')
plt.legend()
plt.show()


...see how it mimics the original signal?

A DFT result vector will only provide you with the Fourier series coefficients if (1) the signal is exactly integer periodic within the DFT/FFT length, and (2) the signal (before sampling) is perfectly band-limited to frequencies below Fs/2. Otherwise, the DFT will contain either window artifacts and/or frequency aliasing.