Custom THD Calculation Help

Post your C and C++ discussions here
Post Reply
dan_iq
Newbie
Posts: 0
Joined: Fri Aug 01, 2014 4:53 pm

Custom THD Calculation Help

Post by dan_iq »

Hello Everyone,

I am using my own software and a PicoScope 2204A to calculate THD.
I am very close to getting it right, but I am missing something. Please bare with me as this is my first time working with transforms and THD, so I am a little confused by the whole thing.

I am currently applying a 1khz signal. 0.707V PK (.5V RMS) and collecting 10000 samples.
I am using the FFTW open source library to do the Fourier transform. I am using a real to real transform.

I calculate the distortion of each of my harmonics from 2k to 10k and get the THD.
If I apply a 1khz sine wave, my calculated THD almost always matches the PicoScope 6 software result with some fluctuation.
PicoScope gives me -52 dBc while my code gives me around -55 dBc or so, with +/-5dBc variation (and then some every now and then)
If I change it to a triangle wave, however, I do not get the correct result at all.
PicoScope6 gives me -18.5 dBc while my calculation is coming up -11.14 dBc with a LOT of variation.

My PicoScope is configured as follows in fast streaming mode:

#define BUFFER_SIZE 10000
ps2000_run_streaming_ns(picoscope, 1, PS2000_US, BUFFER_SIZE, TRUE, 1, 50000);
Delay(1); //Wait for streaming to fill the buffer
while (!autostop)
{
ps2000_get_streaming_last_values (picoscope, ps2000FastStreamingReady);
}
ps2000_overview_buffer_status (picoscope, &overrun);
ps2000_stop(picoscope);
no_of_samples = ps2000_get_streaming_values_no_aggregation (picoscope,
&starttime, // get samples from the beginning
values_a, // set buffer for channel A
values_b, // set buffer for channel B
NULL,
NULL,
&overflow,
&triggerat,
&trigger,
BUFFER_SIZE);

I need the 1 second delay to verify my buffer is full.

My FFTW plan is:
plan = fftw_plan_r2r_1d(BUFFER_SIZE, cha_in, cha_out, FFTW_R2HC, FFTW_ESTIMATE);

I am confident my THD calculations from the FFTW results are correct. But I cannot understand what is causing a variation in my output.

Basically I have a few concerns:
Do I need to convert my sine wave to RMS before doing the transform, or do I keep the peak voltages?
What can I do to smooth my results? It's not uncommon for me to get a 10% variation in the number (10% to 20%THD for example) with an occasional outlier.
Does triggering have anything to do with this?

If anyone who can give me some advice I would be grateful.

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: Custom THD Calculation Help

Post by Martyn »

With Picoscope 6 running in spectrum mode, and the THD measurement active, can you right click on the display and select View Properties, this will show you how the scope is setup when performing this test in Picoscope.

Now try configuring the scope with these values, in your software, and check if the THD results improve.
Martyn
Technical Support Manager

dan_iq
Newbie
Posts: 0
Joined: Fri Aug 01, 2014 4:53 pm

Re: Custom THD Calculation Help

Post by dan_iq »

THD is configured as a 40.96 us 24.14kS/s, 8060 sample.
I am using ps2000_run_streaming_ns(picoscope, 1, PS2000_US, BUFFER_SIZE, TRUE, 1, 50000);
with 10000 samples.

I will have to experiment with this, but you think it's related to my sample time?

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: Custom THD Calculation Help

Post by Martyn »

I would suggest configuring your software to be the same as the PicoScope 6 settings and then check the readings.
Martyn
Technical Support Manager

Post Reply