math::fourier(3tcl)
π math::fourier β bestie edition π
okay so this is the math::fourier page from tcllib, version 1.0.3, manual section 3tcl (Tcl Math Library, if you're keeping track). it's giving math girlie, it's giving signals, we're doing transforms today. let's GO β¨
πΈ NAME πΈ
math::fourier β Discrete and fast fourier transforms. yes, lowercase "fourier", no cap, that's literally how it's spelled here. π¦
π SYNOPSIS π
package require Tcl 8.5 9
package require math::fourier 1.0.3
::math::fourier::dft in_data
::math::fourier::inverse_dft in_data
::math::fourier::lowpass cutoff in_data
::math::fourier::highpass cutoff in_data
so you need Tcl 8.5 or 9 (both listed, bestie, don't @ me), and you need math::fourier version 1.0.3. then it's just four little procs and you're thriving. π
β¨ DESCRIPTION β¨
so the math::fourier package will use the fast Fourier transform if it's applicable, and otherwise it falls back to the ordinary transform, all to implement the discrete Fourier transform. it also throws in a couple of simple filter procedures as a little illustration of how filters like this can be implemented. cute, right? π«Ά
the whole point of this document is to describe the implemented procedures and give you some examples of how to use them. there is ample literature on the algorithms involved, so for deeper explanations they just point you at relevant textbooks. they also point you at the original Wiki page on the subject, which describes some of the considerations behind the current implementation. iykyk. π
π GENERAL INFORMATION π
the two top-level procedures defined are:
dft data-listinverse_dft data-list
both of them take a list of complex numbers and apply a Discrete Fourier Transform (DFT) or its inverse, respectively, to those lists of numbers.
now, a "complex number" in this context is either:
- a pair (a two-element list) of numbers, interpreted as the real and imaginary parts of the complex number, or
- a single number, interpreted as the real part of a complex number whose imaginary part is zero.
the return value is always in the first format. (the DFT generally produces complex results even if the input is purely real β that's just how she is. πβοΈ)
and here's the slay part: if you apply first one of these procedures and then the other to a list of complex numbers, you get the original list of numbers back β modulo rounding errors due to floating point arithmetic. so it round-trips. iconic. π
if the input length N is a power of two, these procedures will utilize the O(N log N) Fast Fourier Transform algorithm. if the input length is not a power of two, then the DFT gets computed with the naive quadratic algorithm instead. so like, powers of two are the main character, and everything else is lowkey grinding. π
some examples, verbatim, because we do not touch the receipts:
% dft {1 2 3 4}
{10 0.0} {-2.0 2.0} {-2 0.0} {-2.0 -2.0}
% inverse_dft {{10 0.0} {-2.0 2.0} {-2 0.0} {-2.0 -2.0}}
{1.0 0.0} {2.0 0.0} {3.0 0.0} {4.0 0.0}
% dft {1 2 3 4 5}
{15.0 0.0} {-2.5 3.44095480118} {-2.5 0.812299240582} {-2.5 -0.812299240582} {-2.5 -3.44095480118}
% inverse_dft {{15.0 0.0} {-2.5 3.44095480118} {-2.5 0.812299240582} {-2.5 -0.812299240582} {-2.5 -3.44095480118}}
{1.0 0.0} {2.0 8.881784197e-17} {3.0 4.4408920985e-17} {4.0 4.4408920985e-17} {5.0 -8.881784197e-17}
in that last case, the imaginary parts <1e-16 would have been zero in exact arithmetic, but they aren't here due to rounding errors. floating point said "not today, bestie." π
internally, the procedures use a flat list format where every even index element of a list is a real part and every odd index element is an imaginary part. that's why the variable names carry Re_ and Im_ prefixes. sneaky but consistent. π§
the package includes two simple filters. they have an analogue equivalent in a simple electronic circuit β a resistor and a capacitance in series. and heads up: using these filters requires the math::complexnumbers package. π₯
π PROCEDURES π
the public Fourier transform procedures are:
::math::fourier::dft in_dataβ determine the Fourier transform of the given list of complex numbers. the result is a list of complex numbers representing the (complex) amplitudes of the Fourier components.list in_dataβ list of data
::math::fourier::inverse_dft in_dataβ determine the inverse Fourier transform of the given list of complex numbers (interpreted as amplitudes). the result is a list of complex numbers representing the original (complex) data.list in_dataβ list of data (amplitudes)
::math::fourier::lowpass cutoff in_dataβ filter the (complex) amplitudes so that high-frequency components are suppressed. the implemented filter is a first-order low-pass filter, the discrete equivalent of a simple electronic circuit with a resistor and a capacitance.float cutoffβ cut-off frequencylist in_dataβ list of data (amplitudes)
::math::fourier::highpass cutoff in_dataβ filter the (complex) amplitudes so that low-frequency components are suppressed. the implemented filter is a first-order low-pass filter, the discrete equivalent of a simple electronic circuit with a resistor and a capacitance.
yes, bestie, the docs literally say "first-order low-pass filter" here too. that's what it says. we are reporting faithfully, no notes, no edits. π«‘
float cutoffβ cut-off frequencylist in_dataβ list of data (amplitudes)
π BUGS, IDEAS, FEEDBACK π
this document, and the package it describes, will undoubtedly contain bugs and other problems. (their words! not mine! but also, fr.) please report such things in the category math :: fourier of the Tcllib Trackers at [http://core.tcl.tk/tcllib/reportlist].
please also report any ideas for enhancements you may have for either the package and/or the documentation. they want the ideas. bring the ideas. π
when proposing code changes, please provide unified diffs, i.e. the output of diff -u. periodt.
also note: attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. that's the whole treasure map, go forth. πΊοΈ
π KEYWORDS π
FFT, Fourier transform, complex numbers, mathematics
π CATEGORY π
Mathematics
and that's the whole page!! you now know how to dft, how to inverse_dft, how to lowpass and highpass, and you know that powers of two get the fast algorithm while everything else gets the quadratic grind. stay complex, stay iconic. π
πΈβ¨ this has been tcllib, version 1.0.3, manual page math::fourier(3tcl) β mwah, go transform something pretty. πππ¦
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms