FFT
The FFT (“Fast Fourier Transform”) is an algorithm for analysing signals in the frequency domain.
Splitting the signal by frequency
From now on we assume that the digital signal contains several superimposed signals of different frequency, amplitude and phase. This can look like this, for example, when the numbers in memory (120 memory locations) are plotted:

This number stream is available to the FFT algorithm as an input quantity. As a result, the FFT algorithm outputs the peak value and phase at defined frequencies. We assume that the frequency of a signal component does not change over time. This allows us to identify components of the signal by their frequency. The frequency is much better suited than the time waveform for separating the signals from each other and then analysing them. Separated by frequency, the time waveform above looks like this:

If these two signals are added, we obtain the original signal again. Once the FFT algorithm has split the signal into the different frequencies, the signal parameters peak value and phase can be determined more easily. You specify the frequencies to the algorithm. How does that work?
Determining the peak value
The FFT algorithm is optimised for low computing time and is therefore difficult to follow from its code. Instead, we get to the bottom of the basic operating principle of the Fourier transform.
If two signals A(t) = A0 ∙ sin (2π ∙ fA ∙ t) and B(t) = B0 ∙ sin (2π ∙ fB ∙ t) of the same frequency fA = fB are multiplied together, the result looks like this:

If two signals A(t) = A0 ∙ sin (2π ∙ fA ∙ t) and B(t) = B0 ∙ sin (2π ∙ fB ∙ t) of different frequency fA ≠ fB are multiplied together, the result looks like this:

The grey signal A ∙ B is only positive in the upper of the two figures. In the lower figure it is sometimes positive and sometimes negative. The mean value of the product of the signals in the upper figure is a positive number. At all times the product A ∙ B is positive. In the lower figure, positive and negative signal components in A ∙ B cancel each other out exactly, so that the mean value is 0. For now we simplify by assuming that both signals have the same phase, i.e. are not shifted in time relative to each other.
The central statement is:
The mean value of the product of two signals with different frequencies is equal to 0.
The mean value of the product of two signals with the same frequency equals the term A0 ∙ B0 / 2.
As a formula:
The parameter t for time in analogue signals becomes a counter n in digital signals. The sinusoidal, sampled, time-discrete signal A(t) is read from memory as A(n). A sinusoidal signal that has been digitised with 20 samples per period is stored in memory as follows:
| n | A(n) |
|---|---|
| 0 | 128 |
| 1 | 167 |
| 2 | 202 |
| 3 | 231 |
| 4 | 249 |
| 5 | 255 |
| 6 | 249 |
| 7 | 231 |
| 8 | 202 |
| 9 | 167 |
| 10 | 128 |
| 11 | 88 |
| 12 | 53 |
| 13 | 24 |
| 14 | 6 |
| 15 | 0 |
| 16 | 6 |
| 17 | 24 |
| 18 | 53 |
| 19 | 88 |
So the following applies
In many applications we know in which range the frequencies present in the signal lie. If, for example, we measure the voltage at a socket, we know that the frequency is f = 50 Hz. In the conductivity measurement from the previous chapter, we generate a signal with f = 1 kHz. There too we know that voltage and current both have the frequency f = 1 kHz. So we can test whether a signal is present at a certain frequency and how high its peak value is. To do this, we only have to multiply the signal by a test signal with the frequency at which we expect a signal.
Back to the example of the conductivity measurement: to determine which peak value at 1 kHz is contained in the digital data stream of the voltage U, we multiply the data stream by a test signal T with the frequency fT = 1 kHz and the peak value T0 = 1:
The voltage signal U(n) comes from the ADC. We specify the test signal ourselves, so we can optimise it. With T0 = 1 the result simplifies to
With this formula we can determine the peak value U0 of the signal stored in memory as a data stream.
As an example, we consider 20 numbers for U(n) that were sampled one after the other by the ADC. The peak value of U (blue) here is 5. In addition, we consider the test signal T(n) with peak value 1 (red). The frequencies of U and T are chosen to be equal.

In grey you see the waveform of U(n) ∙ T(n) and in yellow the mean value of the grey signal. The following applies
The table below shows the corresponding data from Excel. The mean value at the very bottom is the mean of the numbers in the right column.
| n | U(n) | T(n) | U(n) · T(n) |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1.54508497 | 0.30901699 | 0.47745751 |
| 2 | 2.93892626 | 0.58778525 | 1.72745751 |
| 3 | 4.04508497 | 0.80901699 | 3.27254249 |
| 4 | 4.75528258 | 0.95105652 | 4.52254249 |
| 5 | 5 | 1 | 5 |
| 6 | 4.75528258 | 0.95105652 | 4.52254249 |
| 7 | 4.04508497 | 0.80901699 | 3.27254249 |
| 8 | 2.93892626 | 0.58778525 | 1.72745751 |
| 9 | 1.54508497 | 0.30901699 | 0.47745751 |
| 10 | 6.1257E-16 | 1.2251E-16 | 7.5049E-32 |
| 11 | -1.54508497 | -0.30901699 | 0.47745751 |
| 12 | -2.93892626 | -0.58778525 | 1.72745751 |
| 13 | -4.04508497 | -0.80901699 | 3.27254249 |
| 14 | -4.75528258 | -0.95105652 | 4.52254249 |
| 15 | -5 | -1 | 5 |
| 16 | -4.75528258 | -0.95105652 | 4.52254249 |
| 17 | -4.04508497 | -0.80901699 | 3.27254249 |
| 18 | -2.93892626 | -0.58778525 | 1.72745751 |
| 19 | -1.54508497 | -0.30901699 | 0.47745751 |
| Mean value | 2.5 | ||
Application to many frequencies
The FFT carries out this calculation (very highly optimised) for many frequencies with a constant spacing between them. You specify the lowest frequency, the highest frequency and the number of equally spaced frequencies in between. The following example shows a signal with two frequency components:

The following applies
U (n) = 3 ∙ sin(2π 100Hz ∙ n) + 1 ∙ sin(2π 300Hz ∙ n)
One after the other, we multiply test signals
T1 (n) = 1 ∙ sin(2π 100Hz ∙ n)
T2 (n) = 1 ∙ sin(2π 200Hz ∙ n)
T3 (n) = 1 ∙ sin(2π 300Hz ∙ n)
T4 (n) = 1 ∙ sin(2π 400Hz ∙ n)
by the voltage signal U(n). The result is:
So the peak values are determined correctly. The classic representation of the peak values at different discrete frequencies looks like this:

In the Excel sheet below you can try to program this frequency analysis yourself.
Exercise on the Fourier transform (Excel)
Phases
So far we have simplified by assuming that the phases of all signals are 0. So all signal components are not shifted in time relative to each other. In real measurement signals, however, this is not the case. That is why we need a more general solution. To determine peak values and phases, we multiply the input signal, for each test frequency, by two test signals that are phase-shifted by 90° or π / 2 relative to each other:
TSin = 1 ∙ sin(2π ∙ f ∙ t)
TCos = 1 ∙ cos(2π ∙ f ∙ t)
This gives you the real part and imaginary part of the input signal relative to the phase of the test signal. The following applies
You calculate the peak value and phase with
Exercise: shift one frequency component of the signal in phase in the Excel example. Program the evaluation so that the phase and peak value of all frequency components are calculated correctly.