# Calculating THD where peaks are not exact harmonics

I have captured an FFT of a 1 kHz signal that I am generating using a WM8904 codec running at 48k samples/second connected via I2S to a ARM microcontroller (Atmel SAME70). It appears to have some jitter in it, which I believe is due to a small delay in restarting the DMA after each block (which I know how to fix, but I was more interested at this point in the particulars of taking this measurement). I am using a Agilent MSO7054B to make the measurements. The fundatmental is clearly visible at 1.0 kHz. But the next peak is at 2.1 kHz, not 2.0; the one after that at 3.2 kHz, etc. Is that due to the jitter?

Can I still use those peaks to calculate a THD value? When doing so, I got a value of 4.12%. Does that seem reasonable?

• How many points N in the FFT? I see the sample rate Fs = 111 kHz. The reason I ask is that if N is small enough, i.e. N = 1024, the frequency resolution will be Fs/N = 108.4 Hz, which could explain your errors. – Robert L. Jul 26 '18 at 20:25
• @CarlosDanger The only control I have over that is the "span" figure, which I do have set at 10 kHz. If I increase that (to 20 or 50 kHz), the peaks do seem to move left relative to the frequency grid, although it gets harder to read since the scale changes. So can I use these peaks as shown to compute a valid THD? – tcrosley Jul 26 '18 at 20:50
• If you put in a triangle wave, do the harmonica appear where they are supposed to? – user28715 Jul 27 '18 at 0:59
• I don't believe that's a jitter at all. More like a noise. – jojek Jul 27 '18 at 9:12

## 1 Answer

Jitter should pretty much be zero-mean, otherwise it'd be a frequency offset.

I've zoomed into your screenshot and it seems the horizontal distance between two points in the FFT plot is about 5 pixels, and your picture happens to be 1000 pixel wide – wild guess, this is an 256-point FFT.

In that case, the frequency bin spacing is $\frac{f_{sample}}{256}=\frac{111\,\text{kHz}}{256}\approx433.6\,\text{Hz}$; that explains why you see an "unsharp" peak for your fundamental, and why your harmonics don't happen to land in exact multiples of where you presume they should be. (The wide peak is just leakage in action – oscillations that don't fit in one DFT window of observation leak energy in multiple DFT bins.)

Either, use a signal frequency that is a multiple of the resolution calculated above, or change the sampling rate or DFT length. Simple as that: make your oscillation fit an integer time in the 256 sample points that your DFT is long.

Generally, you'll get finer frequency bin spacing and a more accurate PSD estimate with larger DFT sizes – so, if your scope allows you to somehow export a couple thousand points to your PC, just do the frequency analysis there; it's really not that hard using python these days :)

• +1, forgive my fake nerdishness but, shall you replace (or append) the term frequency resolution with frequency bin spacing if you intent to use you that formula to compute it ? Just as minute as distinguishing between DFT and FFT :-) – Fat32 Jul 27 '18 at 13:39
• :) will do in a minute once time at hand @Fat32 – Marcus Müller Jul 27 '18 at 13:59