Skip to content

[FEATURE] add sine_distortion and snr for measurements with a sine - #64

Open
aguirreSL wants to merge 1 commit into
PyTTaMaster:developmentfrom
aguirreSL:dev-sine_distortion
Open

aguirreSL wants to merge 1 commit into
PyTTaMaster:developmentfrom
aguirreSL:dev-sine_distortion

Conversation

@aguirreSL

@aguirreSL aguirreSL commented Oct 31, 2026 •

Copy link
Copy Markdown

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.py use 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 in test_h5io.py, 4 in test_pyttasave.py) are the same with and without this change.

Co-Authored-By: Claude Fable 5.1 noreply@anthropic.com

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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant