<jupyter_start><jupyter_text>Fourier Transforms Discrete Fourier TransformsGiven a set of sampled data you can apply the transform on the vector by multiplying it by matrices. This will give you a set of coefficients for each frequency in the data up to the nth frequency possible for n data points.the nth frequency coefficient $$\hat{f_k} = \sum_{j=0}^{n-1} f_je^{-i2\pi jk/n}$$To reconstruct the data from the coefficients$$f_k = \bigg(\sum_{j=0}^{n-1} \hat{f_j}e^{i2\pi jk/n}\bigg)\frac{1}{n}$$The DFT takes the sample as a vector and returns the coefficients as a vector. The data has to start with a zero index.$$\{f_0, f_1,f_2 ...f_n\} \implies \{\hat{f_0},\hat{f_1},\hat{f_2}...\hat{f_n}\}$$There is a fundamental frequency:$$\omega_n = e^{-i2\pi /n}$$This frequency can be used to form a DFT matrix. Given in complex numbers so that the magnitude and phase are communicated through the coeffs Fast Fourier TransformsA technique for computing the DFT. An Algorithm. Clever and faster. Standard<jupyter_code>import numpy as np
import scipy as sci
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline 
pwd
df = pd.read_csv('fileName.csv')
df.head()
plt.plot(df.index,df['Ax'])<jupyter_output><empty_output><jupyter_text>Applying the FFTThe input it the time series. This converts everything into the frequency space. This example uses scipy but numpy also has an fft package.<jupyter_code>from scipy import fftpack
axfft = fftpack.fft(df['Ax'])
type(df['Ax'])
df['Ax'].head()
sns.distplot(axfft)
amplitude = np.abs(axfft)
power = amplitude**2
angle = np.angle(axfft)<jupyter_output><empty_output><jupyter_text>The FFT returns a complex number.<jupyter_code>axfft[0]<jupyter_output><empty_output><jupyter_text>To find the frequencies a time step is necessary.<jupyter_code>time_step = df.index[1] - df.index[0] 

samplefreq = fftpack.fftfreq(df['Ax'].size,d=time_step)
amp_freq = np.array([amplitude, samplefreq])
print(amp_freq.shape)
print(amp_freq[:,0])
amp_position = amp_freq[0,:].argmax()
peak_freq = amp_freq[1,amp_position]
print(amp_position)
print(peak_freq)<jupyter_output>1
0.001<jupyter_text>An Example of filtering out only the most prominent frequency<jupyter_code>denoise = axfft.copy()
denoise[np.abs(samplefreq) > peak_freq] = 0
filteredsig = fftpack.ifft(denoise)
filteredsig[0]
plt.figure()
plt.plot(df.index,df['Ax']) #sns does not handle imaginary numbers automatically
plt.plot(df.index,filteredsig)<jupyter_output>C:\Users\starlord\anaconda3\lib\site-packages\numpy\core\_asarray.py:85: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)<jupyter_text>**Note** This is actually a bad way of creating a filter: such brutal cut-off in frequency space does not control distorsion on the signal.<jupyter_code>plt.plot(df.index,filteredsig)<jupyter_output>C:\Users\starlord\anaconda3\lib\site-packages\numpy\core\_asarray.py:85: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)
{'fraction_non_alphanumeric': 0.11521035598705502, 'fraction_numerical': 0.011003236245954692, 'mean_word_length': 6.188372093023256, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '629847', 'license': 'MIT', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1037, 'n_words': 307, 'path': 'Fourier Transforms.ipynb', 'repo_name': 'giglioq/Ocean-Buoy', 'size': '3090'}