From c264cf2156880db775345dc9ed54a736d5ee1496 Mon Sep 17 00:00:00 2001 From: Treffehn Date: Mon, 22 May 2023 15:31:58 +0200 Subject: [PATCH 1/5] added padding for delay compensation in filtering --- .../audiotools/audioarray.py | 42 +++++++++++++++++-- .../audiotools/wrappers/filter.py | 37 +++++++++------- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/ivas_processing_scripts/audiotools/audioarray.py b/ivas_processing_scripts/audiotools/audioarray.py index 9278b240..9c53a078 100755 --- a/ivas_processing_scripts/audiotools/audioarray.py +++ b/ivas_processing_scripts/audiotools/audioarray.py @@ -163,12 +163,29 @@ def window( return x -def delay_compensation( +def pad_delay( x: np.ndarray, flt_type: str, fs: Optional[int] = 48000, up: Optional[bool] = False, down: Optional[bool] = False, + before: Optional[bool] = False, +): + + # Get the delay in number of samples + d_samples = get_delay_flt_type(flt_type, up, down, before=before) + + # pad by length of delay + y = trim(x, fs, (0, -d_samples), pad_noise=True, samples=True, seed=0) + + return y + + +def delay_compensation( + x: np.ndarray, + flt_type: str, + up: Optional[bool] = False, + down: Optional[bool] = False, ) -> np.ndarray: """ Compensation for a delayed signal @@ -192,21 +209,38 @@ def delay_compensation( Delay compensated test array """ + # Get the delay in number of samples + d_samples = get_delay_flt_type(flt_type, up, down) + + # Delay compensation + x = x[d_samples:, :] + + return x + + +def get_delay_flt_type(flt_type, up, down, before=False): + # Get the delay in number of samples if flt_type == "SHQ2" and up: d_samples = DELAY_COMPENSATION_FOR_FILTERING["SHQ2"]["up"] + if before: + d_samples = int(d_samples / 2) elif flt_type == "SHQ2" and down: d_samples = DELAY_COMPENSATION_FOR_FILTERING["SHQ2"]["down"] + if before: + d_samples = d_samples * 2 elif flt_type == "SHQ3" and up: d_samples = DELAY_COMPENSATION_FOR_FILTERING["SHQ3"]["up"] + if before: + d_samples = int(d_samples / 3) elif flt_type == "SHQ3" and down: d_samples = DELAY_COMPENSATION_FOR_FILTERING["SHQ3"]["down"] + if before: + d_samples = d_samples * 3 else: d_samples = DELAY_COMPENSATION_FOR_FILTERING[flt_type] - # Delay compensation - x = delay(x, fs, -d_samples, samples=True) - return x + return d_samples def delay( diff --git a/ivas_processing_scripts/audiotools/wrappers/filter.py b/ivas_processing_scripts/audiotools/wrappers/filter.py index 426a0f2c..017f2e77 100755 --- a/ivas_processing_scripts/audiotools/wrappers/filter.py +++ b/ivas_processing_scripts/audiotools/wrappers/filter.py @@ -31,7 +31,7 @@ # import re -from copy import copy +from copy import deepcopy from pathlib import Path from tempfile import TemporaryDirectory from typing import Optional @@ -40,7 +40,7 @@ from warnings import warn import numpy as np from ivas_processing_scripts.audiotools.audio import Audio, ChannelBasedAudio -from ivas_processing_scripts.audiotools.audioarray import delay_compensation +from ivas_processing_scripts.audiotools.audioarray import delay_compensation, pad_delay from ivas_processing_scripts.audiotools.audiofile import read, write from ivas_processing_scripts.constants import DEFAULT_CONFIG_BINARIES from ivas_processing_scripts.utils import find_binary, run @@ -214,7 +214,7 @@ def lpfilter_itu( # resample if samplingrate is not supported old_fs = None - tmp = copy(x) + tmp = deepcopy(x) if x.fs != 48000: warn( f"Filter type {flt_type} only supported for 48kHz samplingrate, not for {x.fs}Hz -> resampling" @@ -223,6 +223,9 @@ def lpfilter_itu( tmp.audio = resample_itu(tmp, 48000) tmp.fs = 48000 + # pad to avoid loosing samples due to delay + tmp.audio = pad_delay(tmp.audio, flt_type=flt_type, fs=tmp.fs) + # apply filter y = filter_itu(tmp, flt_type=flt_type, block_size=960) @@ -256,7 +259,7 @@ def hp50filter_itu( # set filter type and check if sampling rate is supported old_fs = None - tmp = copy(x) + tmp = deepcopy(x) if x.fs == 48000: flt_type = "HP50_48KHZ" elif x.fs == 32000: @@ -277,6 +280,9 @@ def hp50filter_itu( else: skip_channel = None + # pad to avoid loosing samples due to delay + tmp.audio = pad_delay(tmp.audio, flt_type=flt_type, fs=tmp.fs) + # apply filter y = filter_itu(tmp, flt_type=flt_type, skip_channel=skip_channel) @@ -310,7 +316,7 @@ def kbp20filter_itu( # set filter type and check if sampling rate is supported old_fs = None - tmp = copy(x) + tmp = deepcopy(x) if x.fs == 48000: flt_type = "20KBP" else: @@ -329,6 +335,9 @@ def kbp20filter_itu( else: skip_channel = None + # pad to avoid loosing samples due to delay + tmp.audio = pad_delay(tmp.audio, flt_type=flt_type, fs=tmp.fs) + # apply filter y = filter_itu(tmp, flt_type=flt_type, skip_channel=skip_channel) @@ -428,21 +437,17 @@ def resample_itu( raise ValueError("Ratio of input and output sampling frequency not supported") # apply filter - y = copy(x) + y = deepcopy(x) for i, flt in enumerate(flt_type): + # pad to avoid loosing samples due to delay + y.audio = pad_delay(y.audio, flt_type=flt, fs=y.fs, up=up[i], down=down[i], before=True) + + # resampling y.audio = filter_itu(y, flt_type=flt, up=up[i], down=down[i]) + + # delay compensation y.audio = delay_compensation( y.audio, flt_type=flt, fs=y.fs, up=up[i], down=down[i] ) - # if up[i]: - # if flt == "SHQ2": - # y.fs = y.fs * 2 - # elif flt == "SHQ3": - # y.fs = y.fs * 3 - # elif down[i]: - # if flt == "SHQ2": - # y.fs = int(y.fs / 2) - # elif flt == "SHQ3": - # y.fs = int(y.fs / 3) return y.audio -- GitLab From 48ecb0e57af6a884273aa1b9c3ddad05d1ed0ff0 Mon Sep 17 00:00:00 2001 From: Treffehn Date: Mon, 22 May 2023 15:36:49 +0200 Subject: [PATCH 2/5] removed fs argument from function calls --- ivas_processing_scripts/audiotools/wrappers/filter.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ivas_processing_scripts/audiotools/wrappers/filter.py b/ivas_processing_scripts/audiotools/wrappers/filter.py index 017f2e77..ff7ea902 100755 --- a/ivas_processing_scripts/audiotools/wrappers/filter.py +++ b/ivas_processing_scripts/audiotools/wrappers/filter.py @@ -230,7 +230,7 @@ def lpfilter_itu( y = filter_itu(tmp, flt_type=flt_type, block_size=960) # delay compensation - y = delay_compensation(y, flt_type=flt_type, fs=tmp.fs) + y = delay_compensation(y, flt_type=flt_type) # reverse resampling if old_fs: @@ -287,7 +287,7 @@ def hp50filter_itu( y = filter_itu(tmp, flt_type=flt_type, skip_channel=skip_channel) # delay compensation - y = delay_compensation(y, flt_type=flt_type, fs=tmp.fs) + y = delay_compensation(y, flt_type=flt_type) # reverse resampling if old_fs: @@ -342,7 +342,7 @@ def kbp20filter_itu( y = filter_itu(tmp, flt_type=flt_type, skip_channel=skip_channel) # delay compensation - y = delay_compensation(y, flt_type=flt_type, fs=tmp.fs) + y = delay_compensation(y, flt_type=flt_type) # reverse resampling if old_fs: @@ -447,7 +447,7 @@ def resample_itu( # delay compensation y.audio = delay_compensation( - y.audio, flt_type=flt, fs=y.fs, up=up[i], down=down[i] + y.audio, flt_type=flt, up=up[i], down=down[i] ) return y.audio -- GitLab From 563b3e14521d3c5642275a9a5dc480f7d0577aab Mon Sep 17 00:00:00 2001 From: Treffehn Date: Mon, 22 May 2023 16:07:06 +0200 Subject: [PATCH 3/5] formatting --- ivas_processing_scripts/audiotools/audioarray.py | 1 - ivas_processing_scripts/audiotools/wrappers/filter.py | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ivas_processing_scripts/audiotools/audioarray.py b/ivas_processing_scripts/audiotools/audioarray.py index 9c53a078..474c6f32 100755 --- a/ivas_processing_scripts/audiotools/audioarray.py +++ b/ivas_processing_scripts/audiotools/audioarray.py @@ -171,7 +171,6 @@ def pad_delay( down: Optional[bool] = False, before: Optional[bool] = False, ): - # Get the delay in number of samples d_samples = get_delay_flt_type(flt_type, up, down, before=before) diff --git a/ivas_processing_scripts/audiotools/wrappers/filter.py b/ivas_processing_scripts/audiotools/wrappers/filter.py index ff7ea902..f9734b35 100755 --- a/ivas_processing_scripts/audiotools/wrappers/filter.py +++ b/ivas_processing_scripts/audiotools/wrappers/filter.py @@ -440,14 +440,14 @@ def resample_itu( y = deepcopy(x) for i, flt in enumerate(flt_type): # pad to avoid loosing samples due to delay - y.audio = pad_delay(y.audio, flt_type=flt, fs=y.fs, up=up[i], down=down[i], before=True) + y.audio = pad_delay( + y.audio, flt_type=flt, fs=y.fs, up=up[i], down=down[i], before=True + ) # resampling y.audio = filter_itu(y, flt_type=flt, up=up[i], down=down[i]) # delay compensation - y.audio = delay_compensation( - y.audio, flt_type=flt, up=up[i], down=down[i] - ) + y.audio = delay_compensation(y.audio, flt_type=flt, up=up[i], down=down[i]) return y.audio -- GitLab From adeccd99c01d022dd2931b5df980c84df8be0b33 Mon Sep 17 00:00:00 2001 From: Treffehn Date: Mon, 22 May 2023 16:38:07 +0200 Subject: [PATCH 4/5] fix problem for delay not divisible by three --- ivas_processing_scripts/audiotools/audioarray.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ivas_processing_scripts/audiotools/audioarray.py b/ivas_processing_scripts/audiotools/audioarray.py index 474c6f32..e85bd6e6 100755 --- a/ivas_processing_scripts/audiotools/audioarray.py +++ b/ivas_processing_scripts/audiotools/audioarray.py @@ -212,6 +212,10 @@ def delay_compensation( d_samples = get_delay_flt_type(flt_type, up, down) # Delay compensation + if flt_type == "SHQ3" and up: + # delay not divisible by 3 + d_samples += 2 + x = x[d_samples:, :] return x @@ -231,7 +235,7 @@ def get_delay_flt_type(flt_type, up, down, before=False): elif flt_type == "SHQ3" and up: d_samples = DELAY_COMPENSATION_FOR_FILTERING["SHQ3"]["up"] if before: - d_samples = int(d_samples / 3) + d_samples = int(np.ceil(d_samples / 3)) elif flt_type == "SHQ3" and down: d_samples = DELAY_COMPENSATION_FOR_FILTERING["SHQ3"]["down"] if before: -- GitLab From 9a452db2ec64dcd2a3df5767ccba33174054eb2f Mon Sep 17 00:00:00 2001 From: Treffehn Date: Mon, 22 May 2023 17:38:00 +0200 Subject: [PATCH 5/5] format --- ivas_processing_scripts/audiotools/audioarray.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ivas_processing_scripts/audiotools/audioarray.py b/ivas_processing_scripts/audiotools/audioarray.py index e85bd6e6..50096ba1 100755 --- a/ivas_processing_scripts/audiotools/audioarray.py +++ b/ivas_processing_scripts/audiotools/audioarray.py @@ -222,7 +222,6 @@ def delay_compensation( def get_delay_flt_type(flt_type, up, down, before=False): - # Get the delay in number of samples if flt_type == "SHQ2" and up: d_samples = DELAY_COMPENSATION_FOR_FILTERING["SHQ2"]["up"] -- GitLab