Insight to the Fourier Transform and The Simple Implementation of It

In the real world, we will not extract it using a vanilla DFT instead we using Fast Fourier Transform (FFT).

FFT is just a more efficient way to calculate DFT.

We will not cover the FFT algorithm in this story but for your information, the result of a vanilla DFT and FFT is almost the same.

That’s the theory and now we will implement it.

With the Fourier transform, we will try to figure out the frequency of beep sound.

We will be using this sound in this experiment.

Let’s see what “The shape” of this sound with this script.

#!/usr/bin/python3from scipy.

io import wavfilefrom matplotlib import pyplot as pltimport numpy as np#settingdatasound = '7detik.

wav'fs, data = wavfile.

read(datasound)data = np.

array(data, dtype=float)#normalizedata = (data – np.

mean(data)) / np.

std(data)time = range(len(data))plt.



plot(time, data)and we got this.

To convert this data from the time spectrum to the frequency spectrum A.


A do the FFT, Let’s run this script below.

#!/usr/bin/python3from scipy.

io import wavfilefrom matplotlib import pyplot as pltimport numpy as np#settingdatasound = '7detik.

wav'fs, data = wavfile.

read(datasound)data = np.

array(data, dtype=float)#print(len(data), np.

shape(data), fs)#normalizedata = (data – np.

mean(data)) / np.

std(data)time = range(len(data))fftdata = np.


fft(data)fftdatafreq = np.

zeros((len(data)))for i in range(len(fftdata)): fftdatafreq[i] = abs(fftdata[i].




plot(time, fftdatafreq)plt.

show()And you will get a graph like this.

Wow, there is a huge difference amplitude between some or one of frequency and another frequency.

Actually, the output of FFT is symmetrical (just look at the graph above, ).

It means we just need half of the frequency to show.


plot(time[:len(fftdatafreq) // 2], fftdatafreq[:len(fftdatafreq) // 2])And we got this graph below.

Ok, now the interesting part.

We will try to figure out the frequency of this beep sound.

Let’s check the bin who have the highest amplitude with this code.

maxfreq = np.

argmax(fftdatafreq)print('dominant freq ', maxfreq)and the result isdominant freq 60096009?.Does this mean the frequency of this beep sound is 6009?.Not so fast!.The FFT function we using herefftdata = np.


fft(data)is assuming the total duration of our data is 1 second even though in reality it is not true.

Let’s check the real duration of our beep sound.

This functionfs, data = wavfile.

read(datasound)returning sample rate in 1 second (fs) and the array real data of sound (data).

Let’s check the value of fs and the length of the array of data.

print(len(data), fs)It’s return265039 44100So the length of the array is 265039 and the sample rate is 44100.

And to check the duration of the sound is 265039 / 44100 = 6.

009954648526077 seconds.

It’s mean in the 6.

009954648526077 seconds, our dominant frequency has formed 6009 sine waves in that time.

Thinking logically, so the dominant frequency is 6009 / 6.

009954648526077 = 999.

8411554525939 Hz or we can round this number to 100 Hz.

Summary, the frequency of our beep sound is 100 Hz.

To verify it just google “100 Hz sound”.

Play the video and compare it with our beep sound.

Very similar isn’t it.

That’s all.

The application of FFT is very vast, especially in electrical engineering.

The original plan is I want to figure out the pattern of daily rainfall using FFT but my rainfall data is not sufficient.

See you in another post.


. More details

Leave a Reply