The rectangle signal is the classic signal type to carry out the Fourier transformation

To keep the calculations easy I start with a rectangle impulse periodic to 2π:

For the ak components:


and the bk components:

There are 2 cases:
If k is odd

If k is even

K = 0

For a symmetric rectangle signal we can use the same formulation:

Only a0 becomes 0 now. The rest is identical

Implemented in a function that would look like:
private void Rectangle(double peak)
{
int
j;
c[0].real = 0;
c[0].imag = 0;
for (j = 1; j < 500; j++)
{
}c[0].real = 0;
c[0].imag = 0;
for (j = 1; j < 500; j++)
{
c[j].real
= 0;
if (j % 2 == 0)
c[j].imag = 0;
else
c[j].imag = peak * 4 / Math.PI / j;
}if (j % 2 == 0)
c[j].imag = 0;
else
c[j].imag = peak * 4 / Math.PI / j;
And that calculates this spectrum for the first 30 harmonics:

And if all the harmonics are put together, we get the signal shape.
The brightest line is the base frequency. The next darker is the base frequency plus the first harmonic, then comes the same plus the next harmonic … and so on. The more harmonics are included, the closer the shape approximates the origin shape.

C# Demo Project Fourier signals