Skip to content

LinAlgError in ButterworthFilter #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mfuchs93 opened this issue Feb 19, 2025 · 2 comments
Open

LinAlgError in ButterworthFilter #377

mfuchs93 opened this issue Feb 19, 2025 · 2 comments

Comments

@mfuchs93
Copy link

Error Description

I encountered a numpy.linalg.LinAlgError: Singular matrix error while using audiomentations==0.39.0. It seems to be related to the Butterworth filter implementation.

Stack Trace

File "/opt/conda/lib/python3.11/site-packages/audiomentations/core/composition.py", line 130, in __call__
    samples = transform(samples, sample_rate)

File "/opt/conda/lib/python3.11/site-packages/audiomentations/core/transforms_interface.py", line 139, in __call__
    return self.apply(samples, sample_rate)

File "/opt/conda/lib/python3.11/site-packages/audiomentations/augmentations/base_butterword_filter.py", line 227, in apply
    sos, samples, zi = sosfilt_zi(sos) * samples[0]

File "/opt/conda/lib/python3.11/site-packages/scipy/signal/_signaltools.py", line 3812, in sosfilt_zi
    zi[section] = scale * lfilter_zi(b, a)

File "/opt/conda/lib/python3.11/site-packages/scipy/signal/_signaltools.py", line 3728, in lfilter_zi
    zi = np.linalg.solve(IminusA, B)

File "/opt/conda/lib/python3.11/site-packages/numpy/linalg/linalg.py", line 409, in solve
    r = gufunc(a, b, signature=signature, extobj=extobj)

File "/opt/conda/lib/python3.11/site-packages/numpy/linalg/linalg.py", line 112, in _raise_linalgerror_singular
    raise LinAlgError("Singular matrix")

numpy.linalg.LinAlgError: Singular matrix

Parameters for HighPassFilter
hp_min_cutoff=0, hp_max_cutoff=20000

Parameters for LowPassFilter
lp_min_cutoff=100000, lp_max_cutoff=150000

sample_rate=384000

Python 3.11

I cannot give exact steps to reproduce as I encountered this issue for the first time after hundreds of training runs, all with the same parameters for Low and HighPassFilter. Also the error occurred in the middle of the training process. My question would be if this is an error that I can avoid through transformation of the audio before passing to the filter, or if it is an error that should be handled by audiomentations.

Let me know if you need further information. Thanks in advance.

Best regards,
Michael

@iver56
Copy link
Owner

iver56 commented Feb 25, 2025

Thanks for reporting! My first hunch is that it's a bug, and not something wrong with your audio.

I see some interesting facts:

  • You are using a high sample rate of 384 kHz. This could mean you are using audiomentations in a way that is not yet well tested, although in theory it should just work. Are you working on a bioacoustic application, where ultrasonic frequencies are of importance?
  • Your hp_min_cutoff=0 is very low, and this could be a source of problems. Try setting it a bit higher, like maybe 1 or 10.

If you have any more information, like how to reproduce this, please share. If not, you could consider adding a try...except in your code, and in the exception handler you can log a few things, like the transform parameters and a few properties of the audio, like duration, standard deviation, min value, max value. That way you can gather information that is useful for narrowing down the issue.

@mfuchs93
Copy link
Author

mfuchs93 commented Mar 5, 2025

Thanks for your reply.

Yes, I am working on bat detection which requires a high sample rate. So far I can say the library works great!

Regarding the error, I set the hp_min_cutoff=10. So far, the error hasn't shown up again. However, as I said it only showed up in one of more than 100 training runs. If I ever run into it again I'll post it with the information you asked.

Best regards

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

No branches or pull requests

2 participants