Skip to content

tns: prevent division-by-zero or instability on highly correlated signals#91

Merged
fabiangreffrath merged 2 commits intoknik0:masterfrom
nschimme:tns-fix
Mar 23, 2026
Merged

tns: prevent division-by-zero or instability on highly correlated signals#91
fabiangreffrath merged 2 commits intoknik0:masterfrom
nschimme:tns-fix

Conversation

@nschimme
Copy link
Copy Markdown
Contributor

No description provided.

libfaac/tns.c Outdated
}
error = error * (1 - kTemp*kTemp);
/* Prevent numerical instability or division-by-zero with highly correlated signals */
if (error < 1e-10) error = 1e-10;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly 1e-10?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was arbitrary, I found a better solution that avoided its use

- Fixes potential division-by-zero by checking error <= 0.0 before
  computing reflection coefficients.
- Implements a filter stability guard (|kTemp| >= error) to break
  recursion if the model is saturated or unstable.
- Removes magic 1e-10 epsilon in favor of an early exit for
  perfectly predicted signals.
- Returns a value above DEF_TNS_GAIN_THRESH on perfect prediction
  to ensure TNS is correctly triggered without math overflow.
Copy link
Copy Markdown
Collaborator

@fabiangreffrath fabiangreffrath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@fabiangreffrath fabiangreffrath merged commit 5724411 into knik0:master Mar 23, 2026
6 checks passed
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.

2 participants