Skip to content

Commit baece1e

Browse files
committed
Create working THD analyzer script
Mimic the structure of the "measure frequency" script. Previously the THD functions just printed their results, but now that they return values that isn't going to work. Clarify that THD+N is (R) by default and THD is (F) by default. Keep `thd_analyzer()` so that `thd_analyzer_launcher.py` works, too. Verified that both THD+N(R) and THD(F), in both percent and dB, match my XLS calculator: ```shell λ python scripts\thd_analyzer.py "whine noise test -10 -20 -15 -25 -30 -20 -35 dBFS.wav" Analyzing "whine noise test -10 -20 -15 -25 -30 -20 -35 dBFS.wav"... THD+N(R): 59.9492% or -4.4 dB THD(F): 74.9008% or -2.5 dB ``` And these match the claimed values as well: ``` File Claimed Reference (440Hz) 0.0% Single-ended triode (440SE) 5.0% Solid state (440SS) 0.5% ``` ```shell λ python scripts\thd_analyzer.py tests\thd_files\440Hz.wav Analyzing "tests\thd_files\440Hz.wav"... -- Left and Right channels are identical -- THD+N(R): 0.0022% or -93.3 dB THD(F): 0.0013% or -98.0 dB λ python scripts\thd_analyzer.py tests\thd_files\440SE.wav Analyzing "tests\thd_files\440SE.wav"... -- Left channel -- THD+N(R): 5.0649% or -25.9 dB THD(F): 5.0714% or -25.9 dB -- Right channel -- THD+N(R): 5.0648% or -25.9 dB THD(F): 5.0713% or -25.9 dB λ python scripts\thd_analyzer.py tests\thd_files\440SS.wav Analyzing "tests\thd_files\440SS.wav"... -- Left channel -- THD+N(R): 0.5124% or -45.8 dB THD(F): 0.5062% or -45.9 dB -- Right channel -- THD+N(R): 0.5124% or -45.8 dB THD(F): 0.5061% or -45.9 dB ```
1 parent 48657be commit baece1e

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

scripts/thd_analyzer.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
1+
from math import log10
2+
from time import time
3+
14
from waveform_analysis._common import analyze_channels
2-
from waveform_analysis.thd import THDN
3-
4-
# if __name__ == '__main__':
5-
#
6-
#
7-
# try:
8-
#
9-
# if files:
10-
# for filename in files:
11-
# try:
12-
# analyze_channels(filename, THDN)
13-
# except IOError:
14-
# print('Couldn\'t analyze "' + filename + '"\n')
15-
# print('')
16-
# else:
17-
# sys.exit("You must provide at least one file to analyze")
18-
# except BaseException as e:
19-
# print('Error:')
20-
# print(e)
21-
# raise
22-
# finally:
23-
# # Otherwise Windows closes the window too quickly to read
24-
# input('(Press <Enter> to close)')
25-
#
26-
27-
28-
# print(f'Frequency: {fs * (true_i / len(windowed)):f} Hz')
29-
30-
# print(f"THD+N: {THDN * 100:.4f}% or {20 * log10(THDN):.1f} dB")
31-
# print(f"A-weighted: {THDNA * 100:.4f}% or {20 * log10(THDNA):.1f} dB(A)")
5+
from waveform_analysis.thd import THD, THDN
326

337

34-
def thd_analyzer(files):
35-
import sys
8+
def thd_wrapper(signal, fs):
9+
"""Wrapper function to analyze THD+N and THD, then print results"""
10+
# Calculate THD+N
11+
thdn = THDN(signal, fs)
12+
# Calculate THD
13+
thd = THD(signal, fs)
14+
15+
# Print results in both % and dB
16+
print(f"THD+N(R):\t{thdn * 100:.4f}% or {20 * log10(thdn):.1f} dB")
17+
print(f"THD(F): \t{thd * 100:.4f}% or {20 * log10(thd):.1f} dB")
18+
3619

20+
def thd_analyzer(files):
21+
"""Function for the launcher script to call"""
3722
if files:
3823
for filename in files:
3924
try:
40-
analyze_channels(filename, THDN)
25+
start_time = time()
26+
analyze_channels(filename, thd_wrapper)
27+
print(f'\nTime elapsed: {time() - start_time:.3f} s\n')
4128
except IOError:
4229
print(f"Couldn't analyze \"{filename}\"\n")
4330
print('')
4431
else:
4532
sys.exit("You must provide at least one file to analyze:\n"
46-
"python wave_analyzer.py filename.wav")
33+
"python thd_analyzer.py filename.wav")
34+
35+
36+
if __name__ == '__main__':
37+
import sys
38+
try:
39+
files = sys.argv[1:]
40+
thd_analyzer(files)
41+
except BaseException as e:
42+
print('Error:')
43+
print(e)
44+
raise
45+
finally:
46+
# Only wait for input when running in an interactive console
47+
# Otherwise Windows closes the window too quickly to read
48+
if sys.stdout.isatty():
49+
input('(Press <Enter> to close)')

scripts/thd_analyzer_launcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
r"""
33
Created a SendTo shortcut to
44
5-
C:\...\pythonw.exe "C:\...\wave_analyzer_launcher.py"
5+
C:\...\pythonw.exe "C:\...\thd_analyzer_launcher.py"
66
77
and multiple files can then be selected and analyzed from the Explorer context
88
menu, with no command line window, and any errors are displayed in a GUI

0 commit comments

Comments
 (0)