Fourier Series

To define the playing field we choose an interval, and a set of boundary conditions.
For fixed at x=0 and x=L, the appropriate basis is this one, which we check is orthonormal.

ϕ[n_,x_] = Sqrt[2/L] Sin[π n x/L]
Integrate[ϕ[1,x]*ϕ[1,x],{x,0,L}]
Integrate[ϕ[j,x]*ϕ[k,x],{x,0,L}]

Then given a function, e.g.

f[x_] = Piecewise[{{2x,x<1/2}},2(1-x)]
Plot[f[x],{x,0,1},PlotStyle->Blue]

We find the coefficients with a dot product:

Integrate[(ϕ[n,x]/.L->1) f[x],{x,0,1}]
a[n_] = %
Table[a[n],{n,1,5}]

fseries[n_,x_] := Sum[a[j]ϕ[j,x]/.L->1,{j,1,n}]
Plot[{f[x],fseries[9,x]},{x,0,1},PlotStyle->{Hue[.7],Hue[0]},PlotRange->{-.05,1}]

Now let’s repeat the procedure with periodic boundary conditions, and using exponential basis functions.
Choose an interval, e.g. -1 to 1:

x1=-1;x2=1;L=x2-x1;

Define normalized periodic basis functions:

ϕ[n_,x_] = Exp[2 π I n x/L]/Sqrt[L]

Check that they are mutually orthogonal and normalized to one.
Note that to form the complex conjugate we replace n by (-n) here.

overlap = Integrate[ϕ[-1,x]*ϕ[2,x],{x,x1,x2}]

Or evaluate for general n and m.  Note that for m=n we have to take a limit.

overlap = Integrate[ϕ[n,x]*ϕ[-m,x],{x,x1,x2}]
overlap/.{m->3,n->1}
Limit[overlap,m->n]

Pick a function (evaluate one or the other of these)

(*triangle*) f[x_] = If[x<0,1+x,1-x]

(*quadratic*) f[x_] = x^2

Plot[f[x],{x,x1,x2}]

Compute the Fourier series:

a[n_] = Integrate[f[x]*ϕ[-n,x],{x,x1,x2}]

Sometimes n=0 requires special treatment:

Limit[a[n],n->0]
a[0]=%

Simplify[a[n],Assumptions->{Element[n,Integers]}]

Define the n-th approximation by summing the modes from -n to n:

fseries[n_,x_] := Sum[a[j]ϕ[j,x],{j,-n,n}]
Plot[{f[x],fseries[10,x]},{x,x1,x2},PlotStyle->{Hue[.7],Hue[0]}]

Observe that we now have two ways of representing the number f(x0) for some given x0:

x0=1;
f[x0]

On the other hand the Fourier series evaluated at that point is the sum of terms:

table=Table[a[n]ϕ[n,x0]+a[-n]ϕ[-n,x0],{n,0,5}]

Note we are double counting the n = 0, but we can fix that one by hand

table[[1]]= a[0]ϕ[0,x0]
table*Pi^2/4

Mathematica knows all these sums of course.

Sum[1/n^2,{n,1,Infinity}]

One game to play: make up a rule for the a[n] and see what function you build.
Here are some samples.

Clear[a]

a[n_] = 1/n^4; a[0]=1;

a[n_] = n/(n^4+1)

a[n_]:=If[EvenQ[n],I n/(n^2+1),3n/(n^2+1)]

g[n_,x_]:=Sum[a[j]ϕ[j,x],{j,-n,n}]
Plot[Re[g[22,x]],{x,x1,x2},PlotStyle->{Hue[.7],Hue[0]}]

Or change the target function and see what a[n]’s come out:

f[x_] = If[Mod[x,1]<1/2,Mod[x,1],0]

f[x_] = Sin[π x]

f[x_] = Cos[π x]Sin[π x]^3

f[x_] = Exp[-x]

Plot[f[x],{x,0,1}]