LabWindows/CVI User Group Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use THD function correctly?

Hi, I am using  HarmonicAnalyzer function to analyze the THD ratio of sinwave.

I use a signal whichconsists of 1KHz and 2KHz sinewave with the amplitude of 1V and 0.01V  to test the function.

I think the THD ratio should be 1%, but the result is a number less than 10^(-8).

I am confused.

the Code is:

#include "toolbox.h"
#include <ansi_c.h>
#include <analysis.h>
#include <formatio.h>
#include <userint.h>
void main()
{
char buf[100];
int N ;
int inputSignalSize;
double rootMeanSquared;
double phase = 0.0;;
double frequency, frequency1, frequency2;
double dutyCycle = 50;

int depth, length;
double dt, df;
double *DataProcessed, *DataProcessed1, *DataProcessed2;
double *harmonicAmplitude ,*harmonicFrequency;
double *autoSpectrum, autoPowerSpectrumSize, numberOfHarmonics, percentTHD,

percentTHDNoise;
int windowType;
double Fs, F_in;
double freqPeak=0, powerPeak = 0;
WindowConst winConstVal;
Fs = 250000.0;
F_in = 1000.0;
frequency = F_in/Fs;
frequency1 = 2.0*F_in/Fs;
depth = 2621442;
length = depth * sizeof(double);
DataProcessed = (double *)malloc(length);
DataProcessed1 = (double *)malloc(length);
DataProcessed2 = (double *)malloc(length);
// generate the wave

SineWave(depth, 1.0, frequency, &phase, DataProcessed1);
SineWave(depth, 0.01, frequency, &phase, DataProcessed2);
Add1D (DataProcessed1, DataProcessed2, depth, DataProcessed);

inputSignalSize = Fs/F_in*(int)(depth/(Fs/F_in));
ScaledWindow (DataProcessed, inputSignalSize, 1, &winConstVal);

numberOfHarmonics = (int)(Fs/2/F_in)-1;
length = numberOfHarmonics * sizeof(double);
harmonicAmplitude = (double *)malloc(length);
harmonicFrequency = (double *)malloc(length);


dt= 1.0/Fs;
length = inputSignalSize * sizeof(double);
autoSpectrum = (double *)malloc(length);
df = 0;
AutoPowerSpectrum (DataProcessed, inputSignalSize, dt, autoSpectrum, &df);
PowerFrequencyEstimate (autoSpectrum, (inputSignalSize/2), -1, winConstVal, df, 7,

&freqPeak, &powerPeak);

autoPowerSpectrumSize = inputSignalSize;
windowType = 0;
HarmonicAnalyzer (autoSpectrum, (inputSignalSize/2), inputSignalSize, numberOfHarmonics,
  windowType, Fs, freqPeak, harmonicAmplitude, harmonicFrequency, &percentTHD,

&percentTHDNoise);
Fmt(buf,"percentTHD = %f[p4] \n percentTHDNoise = %f[p4]",percentTHD,percentTHDNoise);
MessagePopup("",buf);
free(harmonicAmplitude);
free(harmonicFrequency);
free(DataProcessed);
free(DataProcessed1);
free(DataProcessed2);

0 Kudos
Message 1 of 1
(3,651 Views)