# Confusions on FFT of a square-wave in theory and in scope and simulation

Below is an ideal square-wave in time domain, and its harmonic components in freq. domain: As you see above a square-wave is composed only of its odd harmonics as spikes nothing in between.

And below is an oscilloscope's FFT of a square-wave: Here we see two things are different. First of all the above FFT is not composed of spikes but widened curves. Secondly it is continuous not discrete.

Maybe scope may involve some noise and the non-zero rise time of the square-wave might effect the FFT results.

So instead of scope lets look at what LTspice shows the FFT of a square-wave(pulse-train in this case) with the following setup: Here is the FFT: This doesn't look good either. So I set the rise times much shorter than LTspice's default as follows: Now the FFT became better: Now I can see one of the problem when viewing FFT of a square-wave or a pulse in a scope the rise and fall times are never zero. The other problem is there is some noise involved.

Here is my question:

Here is what I don't understand.. At the beginning I provided a spectrum of an ideal square-wave which was discrete odd harmonics as spikes. But both in scope and in LTspice the FFT is continuous.

I'm confused at this point. Let me give an example. In my last plot above, the pulse frequency is 100Hz. So I would expect it is composed of its odd harmonic sinusoids of 300Hz, 500Hz, 700Hz,.. so forth and so on. I wouldn't expect it would have 130Hz component for instance or 102Hz. In fact according to the last FFT plot there is component at 102Hz and it is even greater than 300Hz component.

Any idea which one represents reality here? What am I knowing wrong?

• 1) You're not using an integer number of cycles (as pointed out in the answer below) -- I'm willing to bet this was the case for the oscilloscope, too -- and 2) If you RClick on the FFT window in LTspice and use "mark data points" you'll see that there is one point at the peak, two at the bottom, making it a very precise bin in frequency domain. Oct 17, 2016 at 6:56
• what does " integer number of cycles" mean in this context? btw thanks for "mark data points" option thats really great. Oct 17, 2016 at 9:36
• To get a more "realistic" view in ltspcie of the ideal thing, you need to use smaller rise times (1n or so), plotwisize=0 option, more samples (>1s), turn off smoothing, use a proper window function and increase the number of samples used in thef ft Oct 17, 2016 at 9:45
• @user16307 One cycle = one period, so if your pulse starts its rise time at t=0 and has f=1Hz, then an integer number of 2 cycles would be at t=2/f=2s, resulting in a 2s window. This would result in the lowest frequency being 1/t=1/2=0.5Hz. Increasing the number of samples would give you a higher frequency range, and if you also increase the number of periods for the signal, you'll lower the minimum FFT frequency. Oct 17, 2016 at 12:21

Welcome to the real world!

The "mathematically perfect" transform you show at the top, with the "discrete" harmonics is generated assuming that the rise and fall times of the waveform are zero, and that you're doing a continuous transform — no discrete sampling in the time domain. It assumes that the integrations are over an infinite span — or equivalently, they're done over an exact whole number of cycles.

When you generate a waveform in a simulator, or feed a real waveform into an oscilloscope, these assumptions no longer hold. The rise and fall times have nonzero values, and the signal is sampled in the time domain. Most importantly, the sampling does not normally encompass an exact number of whole cycles, nor is it precisely synchronized to the waveform.

Violating these assumptions causes the peaks you see in the transform output to "spread out". This does not mean that there's any energy at, say 102 Hz, but only that the limitations of the algorithm means that it can't distinguish between whether or not there's energy there, given the limitations of the set of samples it is working with.

• So if a newbie looks at the scope's continuous FFT plot, he might think a 102Hz component exists and it is greater than 300Hz component. But he would be wrong since it is the fault of oscilloscope's limitations. Did I understand what you mean? Oct 17, 2016 at 0:55
• Yes. One must always understand the limitations of one's instruments. Oct 17, 2016 at 0:57

I am admittedly under-educated in the relevant math, and can easily be humiliated by some elegant formulas with transforms, operators, matrices of functions etc. but let me present what I know in lay man vocabulary - perhaps I'm the right person to try, for exactly that reason:

I would guess that in your first "theoretical" sketch, the "bar graph" shows precisely centered ideal harmonic frequencies. They're integer multiples of the main frequency of your signal. You just do not consider any continuous spectrum inbetween the ideal harmonics :-) And then you compare them to an alternative frequency domain plot, which is continuous.

BTW, in your first frequency-domain "bar graph", the spacing of 0-1-3 seems weird :-) And, I would actually expect the main frequency to be the highest spike on the left, and all the harmonics to the right of it should be lower.

Next: discrete Fourier tranform (of which FFT is a special case) has an important parameter: the window size. This is the finite batch size (a sequence of time-domain samples), over which your "averaging" algorithm integrates. DFT/FFT is just a carton-full of weighted averages :-) One average per frequency of interest. So the size of your batch of samples determines, to what degree of precision (or statistical confidence/certainty) you can calculate the frequency-domain image of your original signal. The longer your window, the leaner your spectral lines will turn out. FFT is a version of DFT, optimized for 2^n-sized windows.

Mind the following apparent paradox: the leaner (more precise) you want to have your spectral lines, especially at relatively lower frequencies, the longer time you need to watch the time-domain signal (= longer "window"). Which at the same time means, that the resulting spectral change will be less "focused in time" :-) A bit of an "uncertainty principle". If at some point in time there is some change in your time-domain signal, prone to affect the resulting spectrum (e.g., the main frequency changes), it will take you longer to take notice, if your DFT "window" is large. A longer window means more frequency precision but also a slower reaction in the spectrum domain.

BTW2: note that in your first freq-domain "bar graph", the amplitude is linear, whereas in your later graphs, the amplitude is logarithmic (dB units). This optically pulls the "skirts" around your "pure" spectrum lines up from the "noise background" - but it's really just an optical side-effect of your choice of scale (linear vs. logarithmic). Try it in any spectrum analyzer software that can switch between a linear and log scale. Therefore also, the "decay" of harmonics towards higher frequencies appears much less steep (more gradual, slower). Same effect for logarithmic frequency scale. In your first bar-graph, the frequency scale is linear (except for the 0-1-3 anomaly). In th latter spectrum graphs, the scale appears logarithmic.

On my job, I have a USB scopey gadget (the M595 by ETC) that comes with an interesting software-based spectrum analyzer app (only good as an overview spectrograph) that has tweakable knobs, making some of these effects very noticeable. You can tweak the vert. scale (lin/log), the window size and sampling frequency (by sizing and shifting the window of frequencies along the frequency axis in the freq.domain display). You can see the effects of your tweaks in your human real time :-) Similar effects can be obtained in various FFT softwares - there's a freeware app called the SpectrumLab - can work with a sound card or a WAV file, has waterfall output etc. and allows you to select a very broad range of Windows sizes. Similar effects can be observed in "RTLSDR-scanner": you can use a compatible USB/DVB-T dongle to scan the radio spectrum, and the software allows you to select the window size and other parameters... the effect of FFT window size is again clearly visible.

Somewhere at home I used to keep a paper copy of an old article: "More on Averages" by "Jack W. Crenshaw", published in the "Embedded Systems Programming" mag back in 1996 - I recall starting to get the grasp of the basics of FFT while reading that. I cannot find it now, but maybe you could get a copy in a library or something.

Imagine that for any frequency that you're interested in (in the spectral domain), you take a precise sinewave of that frequency, and use it as the "weight" in a weighted average, over your time-domain discrete sampling window. You do that for every frequency that you're interested in. Maybe that's where it should start dawning on you. (You should actually do this twice for each frequency, using the sine and cosine wave shape, resulting in a complex amplitude for the particular frequency = easily transformed into a scalar amplitude and a phase angle.)

Think of weighted-averating your time-domain window with the sinewave you're interested in. Now... what happens if your window is relatively small, maybe close to a single period of the frequency that you're trying to evaluate? Or some small non-integer multiple? Right - uncertainty / spurious artifacts at the lowest edge of your spectrum. For a given frequency, the longer your window, the finer your spectral image. The shorter the window, the coarser the spectral image (especially noticeable at the lower end, in logarithmic frequency scale).

Apart from window size, there's a related parameter called the "windowing algorithm" (Triangular/Hanning/Blackman etc.) - the way I understand it, it allows you to put "gradually faded edges" on your time-domain window of samples (= yet another layer of a filter / weighted average) which allows you to suppress some coarse artifacts at the lowest end of your spectral image which "are not really there" before you apply a sharply edged window.

• Note that the 0-1-3 spacing is an artifact of the fact that the signal has a DC offset. The 0 component represents that offset, the1 component is the fundamental and the 3 component is the harmonic at 3x the fundamental. Oct 17, 2016 at 10:46