11-09-2010 02:36 PM
I'm trying to analyze a wav file and get the thd.
During the wav file read, I read in the data into a (void *) array. And depending on the bit type, depends on how I call PlotWaveform. So in my current example, i have a mono 16 bit signal. And I call Plot Waveform like so:
PlotWaveform (panelHandle, PANEL_CHANNEL_LEFT, channel1, samplesPerChannel, type, 1.0, 0.0, 0.0, 1.0, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1,
VAL_RED);
And a perfect sine wav comes out.
Now i am trying to do the THD on the signal, but am having trouble converting my byte array to a double array. i have the following code, but all my values I get out are to teh -319th power.
channel1 is a (void *)
double *auto_power_spectrum;double *harmonic_amplitudes;double *harmonic_frequencies;double *x_axis;double *dChannel1;double *noise;WindowConst windowConstants;double df;double thd;double thdnoise;int number_of_harmonics = 7;int status;double power_peak,freq_peak;double phase;double *auto_power_spectrum; double *harmonic_amplitudes; double *harmonic_frequencies; double *x_axis; double *dChannel1; double *noise; WindowConst windowConstants; double df; double thd; double thdnoise; int number_of_harmonics = 7; int status; double power_peak,freq_peak; double phase;
dChannel1 = malloc(samplesPerChannel*sizeof(double));
for (int i = 0; i < samplesPerChannel; i++) {
memcpy(&dChannel1[i], (char*)channel1+i*2, 2);
}
ScaledWindow(dChannel1,samplesPerChannel,0,&windowConstants);
auto_power_spectrum = malloc(samplesPerChannel/2*sizeof(double));
AutoPowerSpectrum (dChannel1, samplesPerChannel, 1/(double)samplingFrequency, auto_power_spectrum, &df);
//df = (double)samplingFrequency/(double)samplesPerChannel;
status = PowerFrequencyEstimate (auto_power_spectrum, samplesPerChannel/2, 1000, windowConstants, df, 7, &freq_peak, &power_peak);
x_axis = (double *)malloc((samplesPerChannel/2)*sizeof(double));
for(int i = 0; i < (samplesPerChannel/2); i++) *(x_axis + i) = i * df;
PlotXY (PANEL, PANEL_APSGRAPH, x_axis, auto_power_spectrum,samplesPerChannel/2, VAL_DOUBLE, VAL_DOUBLE, VAL_THIN_LINE,VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
harmonic_amplitudes = (double *)malloc(number_of_harmonics * sizeof(double));
harmonic_frequencies = (double *)malloc(number_of_harmonics * sizeof(double));
status = HarmonicAnalyzer(auto_power_spectrum, samplesPerChannel/2, samplesPerChannel, number_of_harmonics,
0, samplingFrequency, freq_peak,
harmonic_amplitudes, harmonic_frequencies, &thd, &thdnoise);
SetCtrlAttribute (PANEL, PANEL_APSGRAPH, ATTR_NUM_CURSORS,
number_of_harmonics);
for(int i = 0; i < number_of_harmonics; i++)
{
int BOLE;
SetCursorAttribute (PANEL, PANEL_APSGRAPH, i+1, ATTR_CURSOR_POINT_STYLE,
VAL_SOLID_CIRCLE);
SetCursorAttribute (PANEL, PANEL_APSGRAPH, i+1, ATTR_CROSS_HAIR_STYLE,
VAL_VERTICAL_LINE);
SetCursorAttribute (PANEL, PANEL_APSGRAPH, i+1, ATTR_CURSOR_COLOR,
VAL_BLUE);
SetActiveGraphCursor (PANEL, PANEL_APSGRAPH, i+1);
BOLE = SetBreakOnLibraryErrors (0);
SetGraphCursor (PANEL, PANEL_APSGRAPH, i+1, *(harmonic_frequencies+i),
(*(harmonic_amplitudes+i) * *(harmonic_amplitudes+i)));
SetBreakOnLibraryErrors (BOLE);
}
free(harmonic_amplitudes);
free(harmonic_frequencies);
free(auto_power_spectrum);
free(x_axis);
free(dChannel1);
Can someone explain to me how I should do this? This who bit shifting and void referencing/dereferencing stuff sure hasn't stuck yet?!
Thanks in Advance
Solved! Go to Solution.
11-09-2010 03:03 PM
Would this be correct to save the signal as a double?
for (int i = 0; i < samplesPerChannel; i++) {
dChannel1[i] = *((char*)channel1+i*2+1) + *((char*)channel1+i*2) << 8;
}
11-09-2010 04:07 PM
I suppose ConvertArrayType () function can help you in this problem.
11-09-2010 05:18 PM
Nice catch. I swear I looked through the toolbox, guess it just wasn't obvious.