Visualization of the Fourier series
Here is some Python code that allows you to read in SVG files and approximate their paths using a Fourier series. The Fourier series can be animated and visualized, the function can be output as a two dimensional vector for Desmos and there is a method to output the coefficients as LaTeX code.
Some example videos of the animations can be found under example_animations.
How to use the program
You will need the packages
scipy.optimize. The important settings can be done in the
main function (file
FourierMain.py) using the different variables (shortly explained in the code). If you want to read your own image, change the path for the SVG handler, for example
handler = SVG_Handler("images/img13.svg"). Here
images/img13.svg is the path relative to the
How to create a usable SVG file
I used Inkscape to draw the images. I tested the program with the freehand pen (the result of which can be seen here, for example) and the Bézier tool. Since the Fourier series at discontinuity points is only (mostly) point convergent and no longer uniformly convergent, one should try to start the new path as close as possible to the end of the old path in the case of several lines. For the same reason, the start and end points of the complete image should be close together.
What do the internal equations look like?
It is mainly a set of complex polynomials (representations of mostly Bézier curves). These complex polynomials are then parameterized from to , depending on the setup, to represent a "partial curve". If we concatenates all partial curves together, we have a large parameterization, which can be normalized by means of the method
_get_parameter_func() in the file
svg_handler.py again also to a parameterization for .
Finding the Fourier coefficients
Once one has the parameterization of the function (which corresponds to the paths of "the image") , one can integrate over the complete function . Calculating this integral for coefficients (), the Fourier series is , which approximates the path of the SVG file. Based on the representation of the curves in the
svgpathtools library, the image is now described by the two dimensional path for .
Animation of the "Fourier vector"
Sorting all coefficients by their absolute value and then appending the vectors for each of the "partial sums " with the newly sorted summands, we have a nice looking path the Fourier series takes to approximate a point. If you do this for every single data point of the graph, all plotted one after the other result in the animation.