less_retarded_wiki/fourier_transform.md
2023-12-01 16:27:38 +01:00

7.9 KiB

Fourier Transform

Fourier Transform (FT) is one of the most important transformations/algorithms in signal processing (and really in computer science and mathematics in general), which enables us to express and manipulate a signal (such as a sound or picture) in terms of frequencies it is composed of (rather than in terms of individual samples). It is so important because frequencies (basically sine waves) are actually THE important thing in signals, they allow us to detect things (voices, visual objects, chemical elements, ...), compress signals, modify them in useful ways (e.g. filter out noise of specific frequency band, enhance specific frequency bands, ...). There also exists a related algorithms called Fast Fourier Transform (FFT) which is able to compute one specific version of FT very quickly and so is often used in practice.

For newcomers FT is typically not easy to understand, it takes time to wrap one's head around it. There is also a very confusing terminology; there exist slightly different kinds of the Fourier Transform that are called by similar names, or sometimes all simply just "Fourier Transform" -- what programmers usually mean by FT is DFT or FFT. There also exist Fourier Transforms in higher dimensions (2D, 3D, ...) -- the base case is called one dimensional (because our input signal has one coordinate). All this will be explained below.

What FT does in essence: it transforms an input signal (which can also be seen as a function) from time (also space) domain, i.e. the usual representation that for each x says the sample value f(x), to frequency domain, another function that for each frequency f says "how much of the frequency is present" (amplitude and phase). For example an FT of a simple sine wave will be a function with a single spike at the frequency of the sine wave. There is also an inverse Fourier Transform that does the opposite (transforms the signal from frequencies back to time samples). The time and frequency representations are EQUIVALENT in that either one can be used to represent the signal -- it turns out that even "weird" looking functions can be decomposed into just sums of many differently shifted and scaled sine waves. In the frequency domain we can usually do two important things we cannot do in time domain: firstly analyze what frequencies are present (which can help e.g. in voice recognition, spectral analysis, earthquake detection, music etc.) and also MODIFY them (typicaly example is e.g. music equalizer or compression that removes or quantizes some frequencies); if we modify the frequencies, we may use the inverse FT to get back the "normal" (time representation) signal back. Some things are also easier to do in the frequency domain, for example convolution becomes mere multiplication.

FT is actually just one of many so called integral transforms that are all quite similar -- they always transform the signal to some other domain and back, they use similar equation but usually use a different kind of function. Other integral transforms are for example discrete cosine transformation (DCT) or wavelet transform. DCT is actually a bit simpler than FT, so if you are having hard time with FT, go check out DCT.

If you know linear algebra, this may help you understand what (D)FT really does: Imagine the signal we work with is a POINT (we can also say a vector) in many dimensional space; if for example we have a recorded sound that has 1000 samples, it is really a 1000 dimensional vector, a point in 1000 dimensional space, expressed as an "array" of 1000 numbers (vector components). A short note: since we consider a finite number of discrete samples here, we are actually dealing with what's called DISCRETE FT here, not the "vanilla" FT, but for now let's not diverge. (D)FT does nothing more than transforming from one vector basis ("system of coordinates", "frame of reference") to another basis; i.e. by default the signal is expressed in time domain (our usual vector basis), the numbers in the sound "array" are such because we are viewing them from the time "frame of reference" -- (D)FT will NOT do anything with to the signal itself (it is a vector/point in space, which will stay where it is, the recorded sound itself will not change), it will merely express this same point/vector from a different "point of view"/"frame of reference" (set of basis vectors) -- that of frequencies. That's basically how all the integral transforms work, they just have to ensure the basis they are transforming to is orthogonal (i.e. kind of legit, "usable") of course.

Details

First let's make clearer the whole terminology around FT:

  • Fourier Series (FS): Transforms a PERIODIC (repeating) signal into a DISCRETE (non-continuous) spectrum. We can see this spectrum also as an infinite SERIES of coefficients c0, c1, c2, etc. The input signal can generally be complex, in which case the output spectrum also has negative part (c-1, c-2, c-3 etc.) and shows us COMPLEX EXPONENTIALS (i.e. not mere sine waves) of the input signal; however if the input signal is real (probably most signals we practically deal with), the spectrum's negative part is symmetric to the positive part and the corresponding positive and negative complex exponentials always together give a sine wave, so for "normal" signals we can see the spectrum only being in the non-negative part and showing us the sine waves of the signal.
  • Fourier Transform (FT): Generalization of FS to work on any signal, not just periodic ones, i.e. FT takes a NON-PERIODIC signal and transforms it into a CONTINUOUS spectrum. This is achieved simply by considering the period of the signal to be infinite -- the spectrum now becomes continuous exactly because the input is non-periodic (this is a relationship that generally holds); since the output is continuous, we now rather see it as a function rather than a series. Same as with FS the input can be complex (in which case the same implications apply), but we usually work with real signals.
  • Inverse Fourier Transform (IFT): Does the opposite of FT, i.e. transforms the signal back from frequency domain to time domain.
  • Discrete Time Fourier Transform (DTFT) (not to be confused with DFT!): Fourier Transform for DISCRETE (non-continuous) input signals (e.g. sound pressure captured only at specific points in time) -- since the input is discrete, the spectrum will be PERIODIC (this is another relationship that generally holds).
  • Discrete Fourier Series (DFS): Version of FS for discrete (non-continuous) signals, transforms the input DISCRETE and PERIODIC signal to a spectrum (series of coefficients) of which there are infinitely many and are also PERIODIC (with the same period as the input signal).
  • Discrete Fourier Transform (DFT) (not to be confused with DTFT!): Uses DFS to transform a FINITE DISCRETE signal to a FINITE DISCRETE spectrum (with the same period as the input) by simply "pretending" the finite input signal is actually repeating over and over and then, after the transform, only leaving in the first period of the result (since the rest is just repeating). This is actually what programmers usually mean by Fourier Transform because in computers we practically always only deal with finite discrete signals (i.e. arrays of data).
  • Fast Fourier Transform (FFT): Computes DFT (NOT FT!) that's faster than the naive implementation, i.e. computing the equation that defines DFT as it's written has time complexity O(n^2) while FFT improves this to O(n * log(n)).

TODO: equations, more, code, pictures etc.