void THD1()
{
//get the current time;
Starttime = millis();
while((millis() - Starttime) < Sampletime_ms)
{
if (fft1024_1.available())
{
// each time new FFT data is available
// print it all to the Arduino Serial Monitor
Serial.print("FFT bins ");
for (i = 0; i < 511; i++) {
n = fft1024_1.read(i);
n = n * a_weight[i];
v[i] = n;
v[i] = 0.001953125 *0.3750 * pow(v[i],2); // 1/512 samples = .0019....
magnitude = v[i]+ magnitude;
float p = pow(magnitude,2);
// print only the bins with data for this sine wave binning test
if (n >= 0.01) {
Serial.print(i);
Serial.print(": ");
Serial.print(n);
Serial.print(" ");
Serial.print(p);
Serial.print(" ");
} else {
//Serial.print(" - "); // don't print "0.00"
}
}
Serial.println();
// print the bands (these are NOT ISO bands.....)
//bin size = 44,100 Hz / 1024 or 43.08 Hz per bin
level[0] = fft1024_1.read(0); // FFT "0Hz" -> "DC"
level[1] = fft1024_1.read(1);
level[2] = fft1024_1.read(23);
level[3] = fft1024_1.read(46);
level[4] = fft1024_1.read(69);
level[5] = fft1024_1.read(93);
level[6] = fft1024_1.read(116);
float f1 = 0.001953125 *0.3750 * pow(level[2],2);
float f2 = 0.001953125 *0.3750 * pow(level[3],2);
float f3 = 0.001953125 *0.3750 * pow(level[4],2);
float f4 = 0.001953125 *0.3750 * pow(level[5],2);
float f5 = 0.001953125 *0.3750 * pow(level[6],2);
float p1 = pow(f1,2);
float p2 = pow(f2,2);
float p3 = pow(f3,2);
float p4 = pow(f4,2);
float p5 = pow(f5,2);
Serial.printf("%.6f\n",p1);
Serial.printf("%.6f\n",p2);
Serial.printf("%.6f\n",p3);
Serial.printf("%.6f\n",p4);
Serial.printf("%.6f\n",p5);
float THD = ((p2 + p3 + p4 + p5)/p1) * 100;
Serial.printf("THD Percentage:%.6f\n", THD);
float THD_dB = log10f(THD) * 10.0f;
Serial.printf("THD_dB:%.2f\n", THD_dB);
// }
}
}
}