Fourier Series in C++ ImGui

Markus Buchholz
2 min readFeb 28, 2023

--

Fourier series for the periodic function is the sum of sine and cosine curves. Generally speaking, each periodic signal is composed of sine functions (with different oscillation periods).
I provided an extensive introduction to signal processing here, where we could see how to design a digital filter.

In the following article, I use the square wave which shape can be composed by adding sine signals according to the following formula (in frequency domain). The other function you will find here (Table of common Fourier series) — in our example, we use function nr 6 (see table).

A sine wave may be compared to the point which rotates on the circle with the radius r which is associated with the amplitude of the sine signal. The speed of point rotation on the circle equals the sine signal period P (one rotation is the one period).
Adding additional sine waves, we can approximate better our reference signal (here the square wave). More sine signal causes us to add more circles with a smaller radius.
Decreasing the circle radius affects the speed rotation of the point around the original circle (the smaller circle the point rotates at a faster rate.
These rotating circles are occurring more frequently and in odd numbers. The function that we started with becomes closer when additional circles (more sines) are added (simulate in C++ ImGui).

The introduction to the C++ ImGui and information on how to compile and build programs you will find in one of my previous articles.

The source code you will find on my GitHub.

Simulations

The below figure depicts the relationship between the number of sine waves added together and the shape of the final function. As we discussed above more sine waves better approximation.

Fourie series of the square wave (by author)

Below I depicted the demonstration of the simulation in C++ ImGui.
On the many panels, you can choose the number of harmonically related sine waves whose sum approximates the square wave (more harmonics better approximation).

Simulation of Fourier series (by author)

Thank you for reading.

--

--