speechbrain.processing.signal_processing 模块

低级信号处理实用程序

作者
  • Peter Plantinga 2020

  • Francois Grondin 2020

  • William Aris 2020

  • Samuele Cornell 2020

  • Sarthak Yadav 2022

摘要

函数

compute_amplitude

计算一批波形的幅度。

convolve1d

使用 torch.nn.functional 执行一维填充和卷积。

dB_to_amplitude

返回从分贝转换的幅度比。

gabor_impulse_response

用于生成 GaborConv1d 中使用的 Gabor 脉冲响应的函数,该函数在以下文献中提出:

gabor_impulse_response_legacy_complex

用于生成 Gabor 脉冲响应的函数,但不使用 GaborConv1d 中使用的 complex64 dtype,该函数在以下文献中提出:

mean_std_norm

此函数归一化输入的均值和标准差

normalize

此函数将信号归一化为单位平均或峰值幅度。

notch_filter

返回由高通滤波器和低通滤波器构建的陷波滤波器。

overlap_and_add

取自 https://github.com/kaituoxu/Conv-TasNet/blob/master/src/utils.py

rescale

此函数执行信号重缩放至目标电平。

resynthesize

从增强幅度重合成波形的函数。

reverberate

给定室内脉冲响应 (RIR),用于用混响污染给定信号的通用函数。

参考

speechbrain.processing.signal_processing.compute_amplitude(waveforms, lengths=None, amp_type='avg', scale='linear')[source]

计算一批波形的幅度。

参数:
  • waveforms (tensor) – 用于计算幅度的波形。形状应为 [time][batch, time][batch, time, channels]

  • lengths (tensor) – 不包括填充的波形长度。形状应为单维度,[batch]

  • amp_type (str) – 计算“avg”平均幅度还是“peak”峰值幅度。选择范围 [“avg”, “peak”]。

  • scale (str) – 计算幅度在“dB”分贝还是“linear”线性尺度下。选择范围 [“linear”, “dB”]。

返回类型:

波形的平均幅度。

示例

>>> signal = torch.sin(torch.arange(16000.0)).unsqueeze(0)
>>> compute_amplitude(signal, signal.size(1))
tensor([[0.6366]])
speechbrain.processing.signal_processing.normalize(waveforms, lengths=None, amp_type='avg', eps=1e-14)[source]

此函数将信号归一化为单位平均或峰值幅度。

参数:
  • waveforms (tensor) – 要归一化的波形。形状应为 [batch, time][batch, time, channels]

  • lengths (tensor) – 不包括填充的波形长度。形状应为单维度,[batch]

  • amp_type (str) – 是否希望参照“avg”平均幅度或“peak”峰值幅度进行归一化。选择范围 [“avg”, “peak”]。注意:对于“avg”,不阻止削波,并且可能发生。

  • eps (float) – 添加到分母以防止 NaN 的小数值。

返回:

waveforms – 归一化电平波形。

返回类型:

tensor

speechbrain.processing.signal_processing.mean_std_norm(waveforms, dims=1, eps=1e-06)[source]
此函数归一化输入的均值和标准差

波形(沿指定轴)。

参数:
  • waveforms (tensor) – 要归一化的波形。形状应为 [batch, time][batch, time, channels]

  • dims (inttuple) – 计算均值和标准差的维度

  • eps (float) – 添加到分母以防止 NaN 的小数值。

返回:

waveforms – 归一化电平波形。

返回类型:

tensor

speechbrain.processing.signal_processing.rescale(waveforms, lengths, target_lvl, amp_type='avg', scale='linear')[source]

此函数执行信号重缩放至目标电平。

参数:
  • waveforms (tensor) – 要归一化的波形。形状应为 [batch, time][batch, time, channels]

  • lengths (tensor) – 不包括填充的波形长度。形状应为单维度,[batch]

  • target_lvl (float) – 目标电平(dB 或线性尺度)。

  • amp_type (str) – 是否希望参照“avg”平均幅度或“peak”峰值幅度进行重缩放。选择范围 [“avg”, “peak”]。

  • scale (str) – target_lvl 属于线性尺度还是 dB 尺度。选择范围 [“linear”, “dB”]。

返回:

waveforms – 重缩放后的波形。

返回类型:

tensor

speechbrain.processing.signal_processing.convolve1d(waveform, kernel, padding=0, pad_type='constant', stride=1, groups=1, use_fft=False, rotation_index=0)[source]

使用 torch.nn.functional 执行一维填充和卷积。

参数:
  • waveform (tensor) – 要执行操作的张量。

  • kernel (tensor) – 卷积期间应用的滤波器。

  • padding (inttuple) – 要应用的填充 (pad_left, pad_right)。如果改为传递整数,则将其传递给 conv1d 函数并忽略 pad_type。

  • pad_type (str) – 要使用的填充类型。直接传递给 torch.nn.functional.pad,请参阅 PyTorch 文档了解可用选项。

  • stride (int) – 每次应用卷积时移动的单元数。传递给 conv1d。如果 use_fft 为 True,则无效。

  • groups (int) – 此选项传递给 conv1d,用于将输入分成组进行卷积。输入通道应可被组数整除。

  • use_fft (bool) – 当 use_fft 传递 True 时,则使用复数乘法在频谱域计算卷积。当核的大小较大时(例如混响),这在 CPU 上更有效。警告:没有填充时,会发生循环卷积。这在混响情况下影响不大,但在不同核的情况下可能会产生更大的影响。

  • rotation_index (int) – 此选项仅在 use_fft 为 true 时适用。如果是,则在卷积前将核滚动此量以移动输出位置。

返回类型:

卷积后的波形。

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> signal = signal.unsqueeze(0).unsqueeze(2)
>>> kernel = torch.rand(1, 10, 1)
>>> signal = convolve1d(signal, kernel, padding=(9, 0))
speechbrain.processing.signal_processing.reverberate(waveforms, rir_waveform, rescale_amp=None)[source]

给定室内脉冲响应 (RIR),用于用混响污染给定信号的通用函数。它执行 RIR 和信号之间的卷积,但不改变信号的原始幅度。

参数:
  • waveforms (tensor) – 要归一化的波形。形状应为 [batch, time][batch, time, channels]

  • rir_waveform (tensor) – RIR 张量,形状应为 [time, channels]。

  • rescale_amp (strNone) – 是否对混响信号进行重缩放(None 表示不重缩放),以及是参照原始信号的“peak”峰值幅度还是“avg”平均幅度。选择范围 [None, “avg”, “peak”]。

返回:

waveforms – 混响信号。

返回类型:

tensor

speechbrain.processing.signal_processing.dB_to_amplitude(SNR)[source]

返回从分贝转换的幅度比。

参数:

SNR (float) – 要转换的分贝比率。

返回类型:

幅度比

示例

>>> round(dB_to_amplitude(SNR=10), 3)
3.162
>>> dB_to_amplitude(SNR=0)
1.0
speechbrain.processing.signal_processing.notch_filter(notch_freq, filter_width=101, notch_width=0.05)[source]

返回由高通滤波器和低通滤波器构建的陷波滤波器。

(摘自 https://tomroelandts.com/articles/ how-to-create-simple-band-pass-and-band-reject-filters)

参数:
  • notch_freq (float) – 陷波频率,作为采样率 / 2 的分数。可能的输入范围是 0 到 1。

  • filter_width (int) – 以样本为单位的滤波器宽度。更长的滤波器具有更小的过渡带,但效率较低。

  • notch_width (float) – 陷波宽度,作为 sampling_rate / 2 的分数。

返回类型:

计算出的滤波器

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> signal = signal.unsqueeze(0).unsqueeze(2)
>>> kernel = notch_filter(0.25)
>>> notched_signal = convolve1d(signal, kernel)
speechbrain.processing.signal_processing.overlap_and_add(signal, frame_step)[source]

取自 https://github.com/kaituoxu/Conv-TasNet/blob/master/src/utils.py

从分帧表示重建信号。将形状为 [..., frames, frame_length] 的信号的可能重叠的帧相加,并将后续帧偏移 frame_step。结果张量的形状为 [..., output_size],其中

output_size = (frames - 1) * frame_step + frame_length

参数:
  • signal (一个 [..., frames, frame_length] torch.Tensor。) – 所有维度可能未知,且秩必须至少为 2。

  • frame_step (int) – 表示重叠偏移的整数。必须小于或等于 frame_length。

返回:

示例

>>> signal = torch.randn(5, 20)
>>> overlapped = overlap_and_add(signal, 20)
>>> overlapped.shape
torch.Size([100])
speechbrain.processing.signal_processing.resynthesize(enhanced_mag, noisy_inputs, stft, istft, normalize_wavs=True)[source]

从增强幅度重合成波形的函数。

参数:
  • enhanced_mag (torch.Tensor) – 预测的频谱幅度,应为三维。

  • noisy_inputs (torch.Tensor) – 处理前的噪声波形,用于提取相位。

  • stft (torch.nn.Module) – 用于计算 STFT 以提取相位的模块。

  • istft (torch.nn.Module) – 用于计算 iSTFT 以进行重合成的模块。

  • normalize_wavs (bool) – 返回之前是否对输出波形进行归一化。

返回:

enhanced_wav – 使用噪声相位对增强幅度进行重合成的波形。

返回类型:

torch.Tensor

speechbrain.processing.signal_processing.gabor_impulse_response(t, center, fwhm)[source]

用于生成 GaborConv1d 中使用的 Gabor 脉冲响应的函数,该函数在以下文献中提出:

Neil Zeghidour, Olivier Teboul, F{‘e}lix de Chaumont Quitry & Marco Tagliasacchi, “LEAF: A LEARNABLE FRONTEND FOR AUDIO CLASSIFICATION”,发表于 Proc of ICLR 2021 (https://arxiv.org/abs/2101.08596)

speechbrain.processing.signal_processing.gabor_impulse_response_legacy_complex(t, center, fwhm)[source]

用于生成 Gabor 脉冲响应的函数,但不使用 GaborConv1d 中使用的 complex64 dtype,该函数在以下文献中提出:

Neil Zeghidour, Olivier Teboul, F{‘e}lix de Chaumont Quitry & Marco Tagliasacchi, “LEAF: A LEARNABLE FRONTEND FOR AUDIO CLASSIFICATION”,发表于 Proc of ICLR 2021 (https://arxiv.org/abs/2101.08596)