This week, I studied the Fourier Transform in class and decided to investigate it more. I found the 3Blue1Brown video about the Discrete Fourier Series and I decided to implement it.
Fourier series graphs need a specific drawing format, in my case we are using drawings generated without leaving blank space between 2 points. So I need to track every point which is drawed and the order between them.
I didn't found any useful aplication for this task, and I implemented it using Tkinter
. For calling this program you only need to type in terminal:
python3 drawer.py
Once the program is open, you only need to drag the red point through the screen to draw lines.
Some useful commands:
- s: to save drawing
- x: exit
- z: undo movement
- h: to show help.
- Right-Click: to delete all lines.
- Up key: to increase line width.
- Down key: to decrease line width.
This script takes a TSV
file and returns images with the n-degree Fourier Series graph.
For calling this program you only need to type in terminal:
python3 main.py -i input.tsv -n n1 n2 n3 n4 ... -o output
input.tsv
: File with all points of the drawing sorted.ni
: degree of Fourier Series.output
: name of output file.
Every drawing has 2 mathematical sequences of T dots defined as:
So we define a unique complex sequence as:
We consider the periodic discrete function defined for every integer value between 0 and T-1:
The N-degree Fourier series is defined as:
For solving some computational problems I have split the integral in T different integrals:
If we consider the approximation for a discrete function:
If we consider the 2 arrays:
Every complex coefficient of the Fourier Series:
The following code shows how it is implemented (a possible improvement of this code to vectorize the main for loop):
self.coefs = 1j * np.zeros(2 * N + 1)
for i in range(2 * N + 1):
n = i - N
arguments = -2j * np.pi * n * np.arange(self.T) / self.T
self.coefs[i] = np.dot(signal, np.exp(arguments)) / self.T
If we have an l-dimension array t, the predicted signal will be an l-dimension array w: