For periodic functions f(x), the coefficients of the Fourier series are determined here by numerical integration.
Alternatively, you can use the FFT technique. Then the number of evaluated locations (sampling points) of the function f(x) must be a power of 2.
The function f(x) is evaluated for the interval [0, L] or alternatively for the interval [-L/2, L/2].
Without using FFT, you can freely choose the number of sampling points.
This number determines the maximum number of coefficients in the series that can be determined and how accurate the numerical integration is.
If you use the coefficients in the sense of a partial sum of the Fourier series expansion, you get an approximation for the given function f(x).
By specifying kmax you can specify how many coefficients of the series should be taken into account in the graphic and table output.
The given function f(x) (black) and its approximation (yellow) by the first Fourier series terms
a0/2 + ∑ ak*cos(2π*k*x/L)+bk*sin(2π*k*x/L)
are graphically represented in a diagram.
The Fourier series coefficients ak, bk taken into account for the terminated series can optionally be shown in a second diagram.
To the examples
Because the examples are all designed as single-line function definitions, certain parts of the x-axis often have to be given different definitions.
The Heaviside function H(x) is used here. It is defined as follows:
H(x) = 0 for x < 0 and
H(x) = 1 for ≥ 0
H(x) is also called a step function. It is discontinuous at x=0.
With the help of H(x) you can "hide" certain areas of a function.
This is how it is initially
H(x-a) = 0 for x < a and
H(x-a) = 1 for x ≥ a
and
H(a-x) = 1 for x ≤ a and
H(a-x) = 0 for x > a
For any function f(x) then the following applies:
f(x)*H(x-a) = 0 for x < a and
f(x)*H(x-a) = f(x) for x ≥ a
as well as
f(x)*H(a-x) = f(x) for x ≤ a and
f(x)*H(a-x) = 0 for x > a
and applied bilaterally for a, b ∈ ℝ with a < b:
f(x)*H(x-a)*H(b-x) = f(x) for a ≤ x ≤ b
f(x)*H(x-a)*H(b-x) = 0 otherwise
Influence of the selected interval
The calculation of the Fourier coefficients only accesses the selected interval range.
How the function specified in the function definition f(x) behaves outside this interval is irrelevant.
Normally you first decide whether you want to use interval [-L,L] or interval [0,2L].
Then you define the function f(x).
If you then change the interval, a different Fourier series will usually result.
In the examples, all function definitions were chosen so that they lead to the same Fourier series,
regardless of the selected interval.
Normally, this “double definition” will not be necessary.