Repository navigation
Conversation
sine_distortion gives THD and THD+N of a recorded sine by channel, from a Blackman-Harris periodogram integrated over 6 bins around the fundamental and its harmonics: THD re the fundamental, THD+N re the total rms as in AES17. snr gives the ratio in dB of the rms of a signal recording to the rms of a noise recording. Tests pin THD and THD+N of a sine with known harmonics, THD+N of a sine in known white noise, and the SNR of two recordings. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This was referenced Oct 31, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds two functions for measurements with a sine, such as those of line inputs and outputs asked for in #45.
pytta.sine_distortion(signalObj, freq, numHarmonics=5)returns the total harmonic distortion (THD) and THD plus noise (THD+N) of each channel, as ratios. The power of each band comes from a Blackman-Harris periodogram (scipy.signal.periodogram, density scaling), integrated over 6 bins on each side of the fundamental and of each harmonic below Nyquist. THD is the rms of the harmonics re the rms of the fundamental. THD+N is the rms of everything except the fundamental and DC re the total rms, as in AES17.pytta.snr(signal, noise)returns, in dB and by channel, the rms of a recording of the signal re the rms of a recording of the noise alone.The tests in
tests/test_sine_distortion.pyuse a 997 Hz sine, off the bin grid. With harmonics at 1 % and 0.5 %, THD comes out as 0.011180, the exact value sqrt(0.01^2 + 0.005^2). With white noise of rms 0.01, THD+N is within 0.3 % of the noise rms re the total rms, while THD stays ten times lower. The SNR of a sine and a noise recording matches their rms ratio within 0.01 dB.For the THD over frequency of a sweep measurement, see #57.
Checked with Python 3.12, numpy 1.26 and scipy 1.17. The existing tests behave as on
development: the 7 failures there (3 intest_h5io.py, 4 intest_pyttasave.py) are the same with and without this change.Co-Authored-By: Claude Fable 5.1 noreply@anthropic.com