How to generate square wave from sin and cos?

I have a signal that looks almost like a perfect square wave. When looking at its FFT, I see that the real part has components at the fundamental and third harmonic of almost equal magnitudes. When looking at the imaginary part, I see spectral components at the fundamental and the third harmonic of notably different levels.

As far as I know, the Fourier series of a square wave has $$a_n = 0$$, hence I was expecting to see almost no real part of the FFT. Any idea how the signal described above might have been synthesized?

• If your square wave is centered with respect to the vertical axis so that it has even symmetry, that is $$x(-t)=x(t) \qquad \qquad \forall t \in \mathbb{R}$$ then you should have only a real part to the Fourier series coefficients and the imaginary part of all Fourier series coefficients should all be zero. Oct 6, 2023 at 19:38

Elaborating on the comment from RBJ: The phase of the harmonics depends on how the square wave lines up with $$t=0$$. If it steps at $$t=0$$ the function has odd symmetry and it's all $$\sin()$$ functions, i.e. the spectrum is imaginary. If it's centered around $$t=0$$, the function has even symmetry, it's all $$\cos()$$ and the spectrum is real. Any other alignment will be something in between.

These are all just time shifted versions of each other and a shift in time results in a phase shift in frequency owing to

$$x[n-M] \leftrightarrow X[k]\cdot e^{-j2\pi\frac{kM}{N}}$$

What happens to $$a_0$$ depends on how the square wave is aligned horizontally with respect to the amplitude being zero. If it's symmetric ($$[-x_{max},+x_{max}]$$), than $$a_0$$ is indeed zero. If it's offset, $$a_0$$ will be equal to that offset.

One thing to remember here is that a square wave is NOT bandlimited and cannot be sampled without aliasing. So the properties of a time continuous square wave are somewhat different than that of a time discrete one.

• Thanks for the input about the signal symmetry. My main goal though is to understand how sin and cos can synthesize an almost perfect square wave mathematically. As the attached FFT results show, this square wave seems to be composed of 2 cos and 2 sin (fundamental + 3rd harmonic in this example), but I am missing the equation that synthesizes the final square wave from the components.
– Yaz
Oct 7, 2023 at 6:24
• You can build ANY periodic function from sines and cosines. That's the whole idea of the Fourier Series. The square wave is probably the most common text book example. See for example mathsisfun.com/calculus/fourier-series.html. This is one example for a "orthogonal basis": you can construct any function from a set of basis functions as long as they are "orthogonal", which is a very useful mathematical concept. Sines, cosines and complex sines at different frequencies are indeed orthogonal (with the right choice of interval). Oct 7, 2023 at 12:27
• Hi. I understand the concept of Fourier transformation and I am aware that I can build a square wave by summing scaled sines (fundamental and harmonics), but I am not aware of a square wave that can be built through sin and cos together, which more or less look like the ones provided in the figure above. This question is about having the analytical equation that produces such a square wave: meaning, writing down the sin and cos whose summation is a square wave analytically. If you can provide these equations, it would be very appreciated.
– Yaz
Oct 8, 2023 at 8:38