Skip to content

Commit

Permalink
Add THD calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
billthefarmer committed Nov 22, 2023
1 parent fe05b8c commit 0a89eaa
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/billthefarmer/scope/SpectrumActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,23 @@ protected void processAudio()
}
}

// Sum of harmonics
double sumh = 0.0;
// Sum of fundamental
double sumf = 0.0;
for (int i = 1; i < RANGE; i++)
// Sum the fundamental
if (Math.abs(xf[i] - frequency) < fps)
sumf += xa[i] * xa[i];

// Sum the harmonics
else if (Math.IEEEremainder(xf[i], frequency) <
Math.round(xf[i] / frequency) * fps)
sumh += xa[i] * xa[i];

// Total harmonic distortion
double thd = Math.sqrt(sumh / sumf) * 100.0;

// Level
double level = 0.0;

Expand All @@ -709,8 +726,8 @@ protected void processAudio()
if (max > MIN)
{
final String s = String.format(Locale.getDefault(),
"%1.1fHz %1.1fdB",
frequency, dB);
"%1.0f%% %1.1fHz %1.1fdB",
thd, frequency, dB);
text.post(() -> text.setText(s));
}
else
Expand Down

0 comments on commit 0a89eaa

Please # to comment.