<jupyter_start><jupyter_text>Fourier transfrom is an useful tool in analytical approaches. They can determine the periodic features in data or can delete redundant noises.In the following, I start with a single sine wave (remember that for sin(bx), T=2*pi/b):<jupyter_code>import numpy as np
import pandas as pd
import scipy 
import matplotlib.pyplot as plt
from scipy.fft import rfft
from scipy.fft import rfftfreq
from scipy.signal import find_peaks
x = np.arange(0,20,0.1)
sinn = np.sin(2*np.pi*x) # frequency = 1
plt.plot(x,sinn)
plt.show()
f_sinn = rfft(sinn) # rfft stands for real fast fourier transform
f_k = rfftfreq(len(sinn), 20/len(sinn))

plt.plot(f_k,np.abs(f_sinn)) # rfft returns imaginary part, therefore, we must convert them to real numbers
plt.show()<jupyter_output><empty_output><jupyter_text>As you can see, it shows that our signal has a peridoic pattern in w = 1 (remember that we generate a sin wave with f = 1).Lets combine two different waves (with f = 1 and 2):<jupyter_code>x = np.arange(0,20,0.1)
sinn = np.sin(2*np.pi*x) + np.sin(4*np.pi*x)
plt.plot(x,sinn)
plt.show()
print (len(sinn))
f_sinn = rfft(sinn)
f_k = rfftfreq(len(sinn), 20/len(sinn))

plt.plot(f_k,np.abs(f_sinn))
plt.show()<jupyter_output><empty_output><jupyter_text>Again, it is obvious that our signal's period is in w = 1 & 2 We can do the same thing for sun spot (read its notebook to get some information):<jupyter_code>data = pd.read_csv('/content/drive/MyDrive/Sunspots.csv')
dataa = data.Monthly.values
plt.plot(dataa)<jupyter_output><empty_output><jupyter_text>from this graph, we can not understand the underlying feature, so we must transform it to the fourier space:<jupyter_code># it is better to normalize data:
dataa = (dataa-np.mean(dataa))/np.std(dataa)
f_dataa = rfft(dataa)
f_k = rfftfreq(len(dataa), 1)

plt.plot(f_k,np.abs(f_dataa))
plt.show()
# you can see that the [0,0.1] range is informative for us:
plt.plot(f_k,np.abs(f_dataa))
plt.xlim(0,0.1)
plt.show()
# Usually, associated information is in the peak so we should locate that:
peakk = np.argmax(np.abs(f_dataa))
print (f_k[peakk])
print (np.abs(f_dataa[peakk]))
# t = 1/w
t = 1/f_k[peakk]
print (t)<jupyter_output>130.6
{'fraction_non_alphanumeric': 0.12397447584320875, 'fraction_numerical': 0.01731996353691887, 'mean_word_length': 5.494082840236686, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '602911', 'license': 'MIT', 'n_tokens_mistral': 913, 'n_tokens_neox': 858, 'n_words': 247, 'path': 'codes/1D_Fourier_transform.ipynb', 'repo_name': 'Hossein-1991/Statistics', 'size': '2194'}