From 4a26477965ceb9ac3c8c5afda02b5cb68bbb9348 Mon Sep 17 00:00:00 2001 From: Archit Tamarapu Date: Wed, 3 Sep 2025 13:03:49 +0200 Subject: [PATCH] allow python fallback for arbitrary low-pass filter cutoff frequencies if ITU filter does not support them --- .../audiotools/audioarray.py | 4 +--- .../audiotools/convert/__init__.py | 18 +++++++++++++----- .../audiotools/wrappers/filter.py | 4 ++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ivas_processing_scripts/audiotools/audioarray.py b/ivas_processing_scripts/audiotools/audioarray.py index 3e7d4c13..62c30d19 100755 --- a/ivas_processing_scripts/audiotools/audioarray.py +++ b/ivas_processing_scripts/audiotools/audioarray.py @@ -522,9 +522,7 @@ def lpfilter( N, Wn = sig.cheb2ord(fc / (fs / 2), (fc + 500) / (fs / 2), 3, 60) b, a = sig.cheby2(N, 60, Wn, "low") - # Apply the Butterworth filter for each channels, across time axis - # y = sig.lfilter(b, a, axis=0) # non zero-phase filter - y = sig.filtfilt(b, a, x, axis=0) # zero-phase filer, batch processing + y = sig.filtfilt(b, a, x, axis=0) # zero-phase filter, batch processing else: y = x diff --git a/ivas_processing_scripts/audiotools/convert/__init__.py b/ivas_processing_scripts/audiotools/convert/__init__.py index d7d612af..953742d3 100755 --- a/ivas_processing_scripts/audiotools/convert/__init__.py +++ b/ivas_processing_scripts/audiotools/convert/__init__.py @@ -35,9 +35,11 @@ from copy import copy from pathlib import Path, PurePath from shutil import copyfile from typing import Optional, Union +from warnings import warn from ivas_processing_scripts.audiotools import audio, audioarray, metadata from ivas_processing_scripts.audiotools.audiofile import write +from ivas_processing_scripts.audiotools.audioarray import lpfilter from ivas_processing_scripts.audiotools.convert.channelbased import convert_channelbased from ivas_processing_scripts.audiotools.convert.masa import convert_masa from ivas_processing_scripts.audiotools.convert.objectbased import convert_objectbased @@ -335,11 +337,17 @@ def process_audio( """low-pass filtering""" if fc is not None: - if logger: - logger.debug( - f"Applying low-pass filter with cutoff {fc} Hz using ITU STL filter" - ) - x.audio = lpfilter_itu(x, fc) + try: + if logger: + logger.debug( + f"Applying low-pass filter with cutoff {fc}Hz using ITU STL filter" + ) + x.audio = lpfilter_itu(x, fc) + except NotImplementedError: + warn(f"Low-pass filter cutoff {fc}Hz not supported by ITU filter. Falling back to python implementation.") + if logger: + logger.debug(f" Applying low-pass filter with cutoff {fc}Hz using python") + x.audio = lpfilter(x.audio, fc, x.fs) """MNRU""" if mnru_q is not None: diff --git a/ivas_processing_scripts/audiotools/wrappers/filter.py b/ivas_processing_scripts/audiotools/wrappers/filter.py index 3f6ce956..5d79fc00 100755 --- a/ivas_processing_scripts/audiotools/wrappers/filter.py +++ b/ivas_processing_scripts/audiotools/wrappers/filter.py @@ -231,8 +231,8 @@ def lpfilter_itu( flt_vals = [1500, 3500, 7000, 10000, 12000, 14000, 20000] try: flt_type = flt_types[flt_vals.index(fc)] - except Exception: - raise ValueError(f"LP cut-off frequency {fc}Hz not supported.") + except ValueError as e: + raise NotImplementedError(f"LP cut-off frequency {fc}Hz not supported by ITU filter.") from e # resample if samplingrate is not supported old_fs = None -- GitLab