# A little question about THD+N

Started by September 27, 2007
```Hi,
i wrote a little algorithm to measure the THD+N of a 1kHz sinus.

The pattern i follwed is:

y = original sinus;
k = y * blackman-harris window
z = notch(k)

now, my question is:
what's the right value?

is it

thd+n = rms(z)/rms(y) * 100

or

thd+n = rms(k)/rms(y) * 100 ?

(the first one is about half of the second one in every test i did)

Thanks!

--

Clyde
```
```Clyde ha scritto:
> Hi,
> i wrote a little algorithm to measure the THD+N of a 1kHz sinus.
>
> The pattern i follwed is:
>
> y = original sinus;
> k = y * blackman-harris window
> z = notch(k)
>
> now, my question is:
> what's the right value?
>
> is it
>
> thd+n = rms(z)/rms(y) * 100
>
> or
>
> thd+n = rms(k)/rms(y) * 100 ?
>
> (the first one is about half of the second one in every test i did)
>
> Thanks!
>
> --
>
> Clyde
sorry ,  there's a typo:

is it

thd+n = rms(z)/rms(k) * 100

or

thd+n = rms(z)/rms(y) * 100 ?

(the first one is about half of the second one in every test i did)

Sorry again

--

Clyde
```
```The window causes a gain difference, so I cannot directly compare the power
in windowed and unwindowed data.

-mn
```
```mnentwig wrote:
> The window causes a gain difference, so I cannot directly compare the power
> in windowed and unwindowed data.
>
> -mn
>

You mean the right answer is the first one?

--

Clyde
```
```Clyde <nospam@nospam.edu> writes:

> Hi,
> i wrote a little algorithm to measure the THD+N of a 1kHz sinus.
>
> The pattern i follwed is:
>
> y = original sinus;
> k = y * blackman-harris window
> z = notch(k)
>
> now, my question is:
> what's the right value?
>
> is it
>
> thd+n = rms(z)/rms(y) * 100
>
> or
>
> thd+n = rms(k)/rms(y) * 100 ?
>
> (the first one is about half of the second one in every test i did)
>
> Thanks!

Neither one are correct. You don't want to window a THD measurement.

thd+n = rms(y)/rms(notch(y)) * 100
--
%% Fuquay-Varina, NC            %       'cause no one knows which side
%%% 919-577-9882                %                   the coin will fall."
%%%% <yates@ieee.org>           %  'Big Wheels', *Out of the Blue*, ELO
http://www.digitalsignallabs.com
```
```Randy Yates wrote:
>
> Neither one are correct. You don't want to window a THD measurement.
>
> The right answer would be
>
>   thd+n = rms(y)/rms(notch(y)) * 100
>
in that case, shouldn't that be

thd+n = rms(notch(y)) / rms(y) * 100 ?

( http://en.wikipedia.org/wiki/Total_harmonic_distortion#THD.2BN )

Thanks

--

Clyde
```
```>> You don't want to window a THD measurement.
Well, I do. Unless I'm smart enough to figure out a full number of periods
from the known test tone frequency for the FFT filter :)

The numerator / denominator question depends on what I want to get: THD
increases, when the noise increases. Signal-to-noise ratio decreases, when
the noise increases. Feel free to take the inverse (or use a minus sign on
a dB scale).

Now Wikipedia says THD+N is equal to SINAD, and if I blindly use equations
without understanding them you bet I'll get what I deserve...

Cheers

Markus
```
```Clyde <nospam@nospam.edu> writes:

> Randy Yates wrote:
>>
>> Neither one are correct. You don't want to window a THD measurement.
>>
>> The right answer would be
>>
>>   thd+n = rms(y)/rms(notch(y)) * 100
>>
> in that case, shouldn't that be
>
> thd+n = rms(notch(y)) / rms(y) * 100 ?

Yes.
--
%  Randy Yates                  % "She tells me that she likes me very much,
%% Fuquay-Varina, NC            %     but when I try to touch, she makes it
%%% 919-577-9882                %                            all too clear."
%%%% <yates@ieee.org>           %        'Yours Truly, 2095', *Time*, ELO
http://www.digitalsignallabs.com
```
```mnentwig wrote:

>>> You don't want to window a THD measurement.
>>>
> Well, I do. Unless I'm smart enough to figure out a full number of periods
> from the known test tone frequency for the FFT filter :)
>

I just tried that in matlab and i think it's better to window the
signal, otherwise i obtain a filtered signal with a really high noise level
To be clear:

y = signal
k = y * window
thd+n = rms(notch(k)) / rms(k) * 100 = 0.0014%

thd+n = rms(notch(y)) / rms(y) * 100 = 2.35 %

with this signal:
http://kinetrope.dyndns.org/FFT_Signal.png

> The numerator / denominator question depends on what I want to get: THD
> increases, when the noise increases. Signal-to-noise ratio decreases, when
> the noise increases. Feel free to take the inverse (or use a minus sign on
> a dB scale).
>
> Now Wikipedia says THD+N is equal to SINAD, and if I blindly use equations
> without understanding them you bet I'll get what I deserve...

I do not trust blindly wikipedia, it's just that i always read about
thd+n expressed in that form rather than the inverse, and wikipedia was
a quick reference :)

> Cheers
>
> Markus
>

Now...
if i'm right you suggest to do something like that:

y = signal
k = y * window
thd+n = rms(notch(k)) / rms(k) * 100

am i right?

--

Clyde
```
```It can be done without windowing, as Randy said. It can also be done with
windowing, but why make it more complicated.

I've got an example on my web page, that does a similar job. If you like,
have a look (below). The method is a bit more complicated, but you could
copy the general approach:

- Generate a test signal
- Measure the signal power, check that you get what you expect
- Measure the noise power. Check that you get what you expect
- ... SNR

It also gives some idea about what accuracy you can expect. The example is
probably close to optimal (textbook case of a correlation receiver).
A signal buried under 30 dB of noise is measured with << 1 dB accuracy
within 60ksamples.
If your results fluctuate much more, you should have a good explanation