# FFT of Square Wave and Sin Wave

When we have FFT of Square wave of a pure real signal with high frequency(close to f(max), less than half of sampling frequency) we see only 2 symmetric high peaks but when we decrease the frequency we see the two peaks and peaks of harmonics and they are getting smaller and smaller, why? Also the highest peaks are getting away from each other, movig <- and -> , vice versa -> <- when we increase the frequency. why?

FFT of Sin wave has only 2 peaks no matter what and they also get away from each other when the frequency is low and get more and more closer when the frequency gets higher. Why? I would be glad if you can enlighten me!

Lets say our sample count is 64 and sampling frequency is 8kHz and signal frequency 2kHz. We should see 2 peaks one at 16 and one at 48.

The picture above is only the left half of my graph, there is also a symmetrical part on the right. My signal is pure real. These small harmonics dont appear at my above settings but when I decrease the freuency to 600 Hz for example I see harmonics like this for square wave and none for Sine Wave. So on high frequencies they disappear and low frequencies they appear, the lower the more of these harmonics. Also the peaks are getting away from each other when I lower the frequency and getting closer when I increase the frequency. At 4 kHz I expect to see only 1 Peak, two of them will unite. Why? Also I couldnt test what happens if I run with 5kHz :( and when I run the code with lots of samples like 2048, the graph displayed makes no sense at all. Why?

I know I'm asking a lot questions but I would be glad if you have some time and help me!

• If you added the code you use to generate and analyze your signals, it would become much easier to see what's going on. – Matt L. May 13 '14 at 12:47
• @Anarkie: I suggest you to play with the following applet to start with: FFT of waveforms. You can see that by increasing the sampling frequency there are more and more harmonics appearing for rectangular wave - that's because they are no longer above Nyquist frequency and you can observe them. But your code will be appreciated. – jojek May 13 '14 at 12:51
• @MattL. I elaborated the question a little more, hope it helps! – Anarkie May 13 '14 at 13:13
• @Anarkie: Question is very unclear. I presume that you understand presence of odd harmonics for square wave? On the other hand when you say that some harmonics do appear at low frequencies, this might be the aliasing you are dealing with. – jojek May 13 '14 at 13:17
• [Please check this question and its answers.][1] [1]: dsp.stackexchange.com/questions/4825/why-is-the-fft-mirrored – pichenettes May 13 '14 at 14:11

By definition, a sine wave is given by: $\frac{e^{j2\pi f_0 n} - e^{-j2\pi f_0 n}}{2}$. Therefore, you will see one harmonics at index $k_1 = N\frac{f_0}{F_s}$ and a copy of it at $k_2 = N - k_1$. Where $N$ is the FFT size which is 64 in your case. So, $k_1 = 64.2k/8k = 16$, and $k_2 = 48$.

Similarly, you need to find the definition of a square wave, which must be having the main component $e^{j2\pi f_0 n}$ and its harmonics $e^{j2\pi k f_0 n}$, where $k = 0, 1, 2, ...$.

For high frequencies, there are lesser Harmonics as by definition Harmonic frequencies are always the integer multiple of the fundamental frequency. Therefore, if your fundamental is at $600$Hz, you'll have Harmonics at $1200, 1800, 2400, ...$. Now as you increase your fundamental, the Harmonics become fewer and fewer.

There is more to it in terms of aliasing, however, I'll not dwell into that topic.

• Thanks a lot for your very well explained answe. About the harmonics, if my fundamental frequency is 2kHz I should see a harmonic at 4kHz? Also why are they getting smaller and smaller? – Anarkie May 25 '14 at 20:34

The odd harmonics are what makes a square wave different from a sine wave in the frequency domain. Otherwise the FFT would lose information about the wave shape of the time domain input.

The higher the fundamental frequency, the farther apart are any multiples of that frequency, such as overtones or harmonics.