Fourier Transform, everybody does it | by Elena V Kazakova | Towards Data Science

Literally. Our ears perform a Fourier analysis of all sounds reaching them; they are a build-in Fourier transform piece of equipment given us by Mother Nature. Of course, it’s true for any living thing that can process audio signals. In essence, our ears construct a frequency spectrum out of an audio time series which are constantly bombarding us from all directions.I will not go into the Fourier Transform (FT) mathematical details and formulas, but I will instead explain it as easily as possible. Fourier transform operation takes a time-based pattern (to be fair, it can be a space-based pattern too but let’s keep it simple), runs through “filters” and returns a combination of sinusoidal and cosinusoidal functions with associated functions offsets, frequency, and amplitudes. A signal, which can be any time-dependent periodic function, as a combination of sin and cos functions, looks like that:
In other words, FT transforms a periodic function in Time Domain to a function in Frequency Domain:
Joseph Fourier’s theorem states that any periodic function can be expressed as a sum of sine and cosine functions. This sum can be infinite. The sinusoidal and cosinusoidal functions that contribute to the function shape are called the Fourier series.
How can the Fourier series be used in Time Series Analysis? There are a few applications that I want to address in this post.
The first application of FT in Times Series Analysis is detecting seasonality. The idea behind it is straightforward: apply Discrete Fourier Transform (DFT) to a time series to find frequencies of Fourie series and see what terms have the highest amplitude. These terms are the most influential ones and their periods point to the seasonality of a series. Warning: It is essential to use Discrete FT because time series are not continuous functions.
I will be using a time series from one of my projects, Colorado crime data, between 2015 and 2019 to illustrate that application.
Visual inspection points to a seasonal component of the series (most probably an annual seasonality). To confirm my assessment, I used numpy.fft.rfft()and numpy.fft.rfftfreq() with my training set.
numobs = len(ts)ts_ft = np.abs(rfft(ts))ts_freq = rfftfreq(numobs)plt.figure(figsize=(18, 8))plt.plot(ts_freq[2:], ts_ft[2: ])    plt.xlabel('Frequency (1/day)')plt.show()
ts_season = pd.DataFrame({'Amplitude':ts_ft[2: ], 'Freqency':ts_freq[2: ], 'FFF':ts_raw[2:], 'Phase':ts_phase[2:]})ts_season['Period in days'] = (1/ts_season['Freqency'])df=ts_season.sort_values(by=['Amplitude'], ascending=False).head(6)df=df.reset_index()df=df.rename(columns={'index':'F_label'})for i in range(0,len(df.index)):    df.iloc[i:, 0]='F_'+str(i+1)df
As expected, the results indicate that the seasonality of the dataset has a robust annual component (365 days) and a possible weekly component (~7 days).
The second application: Smoothing a time series by removing some of the frequencies from its’ spectrum, also known as “removing the noise.” The idea is to apply FT to a time series, remove frequencies above a threshold, and inverse FT it back to the time domain. The d value in rfftfreq() function controls how smooth or rough your FFT smoothing function would be. Parameter n in the inverse irfft() function is needed for an odd number of intervals on the time axis; otherwise, your function will throw an error (for even numbers, having no n does not generate an error.
def ts_smooth(df, threshold=2e4):    fourier = rfft(df)    frequencies = rfftfreq(len(df), d=2e-3 / len(df))    fourier[frequencies > threshold] = 0    return irfft(fourier, n=1825)df_ts['Number of Offenses, smoothed'] = ts_smooth(df_ts['Number of Offenses'])with plt.style.context('ggplot'):    fig, ax = plt.subplots(figsize=(18,8))ax.plot(df_ts['Number of Offenses'],linewidth=2)    ax.plot(df_ts['Number of Offenses, smoothed'], linewidth=5)    ax.set_title('FFT Smoothing', fontsize=23);    ax.set_ylabel('Number of Offenses', fontsize=22);    ax.set_xlabel('Year', fontsize=22);    ax.xaxis.set_major_locator(mdates.MonthLocator(interval=2))    plt.gcf().autofmt_xdate()    ax.tick_params(axis='y', labelsize=16)    ax.tick_params(axis='x', labelsize=16)    plt.legend(['Real data','FFT smoothing'], fontsize=15)    plt.show()
The plot above makes the overall trend and seasonal patterns much clearer. It also makes it easier and quicker to model than the noisy original dataset.
As we can see, Fourier Transform is a helpful technique that can detect the seasonality of a time series or smooth out a noisy signal. However, FT is not a universal tool in time series analysis. If the seasonality is strong, one can first detect seasonal components using rfft () and rfftfreq() functions and then model a smoothed-out curve. However, if there is an unexpected event in your time series (sudden drop or otherwise), FT should not be used.