Skip to content

[FEATURE] add group_delay, coherence, cepstrum and MLS - #80

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

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

Conversation

@aguirreSL

@aguirreSL aguirreSL commented Oct 31, 2026 •

Copy link
Copy Markdown

Adds four DSP utilities that the ITA-Toolbox has and PyTTa lacks (ita_groupdelay, ita_coherence, ita_cepstrum and ita_icepstrum, ita_generate_mls). The ITA code was read for its semantics only and nothing was copied.

  1. pytta.group_delay(signal): group delay of each channel in seconds, at signal.freqVector. It uses the derivative of the spectrum, tau = Re{DFT(n x[n]) / DFT(x[n])} / fs, which needs no phase unwrapping.
  2. pytta.coherence(x, y, winSize=1024): magnitude squared coherence from scipy.signal.coherence, with a Hann window and 50 % overlap. Channels are paired one to one, and a one-channel signal is paired with every channel of the other.
  3. pytta.cepstrum(signal, kind='complex') and pytta.icepstrum(cepstrumObj, delay): real and complex cepstrum and the inverse complex cepstrum. As in Oppenheim and Schafer, section 13.5, the integer linear phase is removed before the complex cepstrum and returned, so that icepstrum can put it back. The magnitude floor is the same as in minimum_phase ([FEATURE] add minimum_phase for SignalObj #56), but this branch does not depend on [FEATURE] add minimum_phase for SignalObj #56.
  4. pytta.generate.mls(order, samplingRate) and pytta.mls_deconvolution(response, order): one period of a symmetrical maximum length sequence from scipy.signal.max_len_seq, and the impulse response from the response to that sequence played periodically. The deconvolution is a periodic cross-correlation normalized by L + 1 (Peltonen 2000, eq. 2.14 to 2.22). With more than one period, the first is dropped and the others are averaged.

The tests in tests/test_dsp_utilities.py print the measured differences:

  1. The group delay of a delay of 37 samples is 37/fs (error 1.6e-14 samples), and that of a first order allpass with a = 0.8 matches its analytic expression (error 9e-8 samples).
  2. The coherence of y = h * x + white noise matches SNR / (1 + SNR) within 9.7e-3, and a signal against itself gives 1.
  3. The cepstra of (1 - 0.9 z^-1)(1 + 0.6 z^-1) match the analytic series -(a^n + b^n)/n within 3.8e-8 (complex) and 1.9e-8 (real).
  4. A mixed phase FIR delayed by 7 samples goes through the complex cepstrum and back with an error of 3.8e-8.
  5. The periodic autocorrelation of an MLS of order 10 is (L + 1) delta - 1, exactly.
  6. A FIR of 40 taps excited by three periods of an MLS of order 12 is recovered within 1.0e-4, below 1/L. The remaining difference is the constant -sum(h)/(L + 1) of the method, which was checked to 4e-8.

Checked with Python 3.12, numpy 1.26 and scipy 1.17. The 7 failures of development (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

group_delay(signal) returns the group delay of each channel in seconds,
from the derivative of the spectrum, Re{DFT(n x[n]) / DFT(x[n])} / fs,
which needs no phase unwrapping.

coherence(x, y, winSize=1024) returns the magnitude squared coherence
with scipy.signal.coherence (Welch, Hann, 50 % overlap). Channels are
paired one to one, and a single channel is paired with all of the other.

cepstrum(signal, kind='complex') returns the real or complex cepstrum.
For the complex one the integer linear phase is removed first and
returned, and icepstrum(cepstrumObj, delay) inverts it. The magnitude
floor is the same as in minimum_phase.

generate.mls(order, samplingRate) returns one period of a symmetrical
MLS from scipy.signal.max_len_seq, and mls_deconvolution(response,
order) recovers the impulse response by periodic cross-correlation
normalized by L + 1. With more than one period the first is dropped and
the others are averaged.

Tests pin the group delay of a pure delay and of a first order allpass,
the coherence of a noisy filtered signal against SNR / (1 + SNR), the
cepstra of a minimum phase filter against the analytic series, the
complex cepstrum round trip with its delay, the exact MLS
autocorrelation and the deconvolution of a FIR with its known offset
-sum(h) / (L + 1).

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