torchaudio.transforms¶
torchaudio.transforms 模組包含常用的音訊處理和特徵提取。以下圖表顯示了一些可用變換之間的關係。
變換使用 torch.nn.Module 實現。構建處理流水線的常用方法是定義自定義 Module 類或使用 torch.nn.Sequential 將 Module 串聯起來,然後將其移至目標裝置和資料型別。
# Define custom feature extraction pipeline.
#
# 1. Resample audio
# 2. Convert to power spectrogram
# 3. Apply augmentations
# 4. Convert to mel-scale
#
class MyPipeline(torch.nn.Module):
def __init__(
self,
input_freq=16000,
resample_freq=8000,
n_fft=1024,
n_mel=256,
stretch_factor=0.8,
):
super().__init__()
self.resample = Resample(orig_freq=input_freq, new_freq=resample_freq)
self.spec = Spectrogram(n_fft=n_fft, power=2)
self.spec_aug = torch.nn.Sequential(
TimeStretch(stretch_factor, fixed_rate=True),
FrequencyMasking(freq_mask_param=80),
TimeMasking(time_mask_param=80),
)
self.mel_scale = MelScale(
n_mels=n_mel, sample_rate=resample_freq, n_stft=n_fft // 2 + 1)
def forward(self, waveform: torch.Tensor) -> torch.Tensor:
# Resample the input
resampled = self.resample(waveform)
# Convert to power spectrogram
spec = self.spec(resampled)
# Apply SpecAugment
spec = self.spec_aug(spec)
# Convert to mel-scale
mel = self.mel_scale(spec)
return mel
# Instantiate a pipeline
pipeline = MyPipeline()
# Move the computation graph to CUDA
pipeline.to(device=torch.device("cuda"), dtype=torch.float32)
# Perform the transform
features = pipeline(waveform)
請查閱深入介紹變換用法的教程。
實用工具¶
將張量從功率/振幅標度轉換為分貝標度。 |
|
基於 mu-law 壓縮編碼訊號。 |
|
解碼 mu-law 編碼訊號。 |
|
將訊號從一個頻率重取樣到另一個頻率。 |
|
為波形新增淡入和/或淡出效果。 |
|
調整波形音量。 |
|
根據 ITU-R BS.1770-4 建議測量音訊響度。 |
|
根據信噪比縮放並向波形新增噪聲。 |
|
使用直接方法沿輸入的最後一維進行卷積。 |
|
使用 FFT 沿輸入的最後一維進行卷積。 |
|
調整波形速度。 |
|
應用 *Audio augmentation for speech recognition* [Ko 等人, 2015] 中引入的速度擾動增強。 |
|
沿波形的最後一維進行去加重。 |
|
沿波形的最後一維進行預加重。 |
特徵提取¶
從音訊訊號建立頻譜圖。 |
|
建立逆頻譜圖以從頻譜圖恢復音訊訊號。 |
|
使用三角濾波器組將普通 STFT 轉換為梅爾頻率 STFT。 |
|
從梅爾頻率域估計普通頻率域的 STFT。 |
|
為原始音訊訊號建立 MelSpectrogram。 |
|
使用 Griffin-Lim 變換從線性幅度頻譜圖計算波形。 |
|
從音訊訊號建立梅爾頻率倒譜系數。 |
|
從音訊訊號建立線性頻率倒譜系數。 |
|
計算張量的 delta 係數,通常是頻譜圖。 |
|
將波形的音高偏移 |
|
按話語應用滑動視窗倒譜均值(和可選的方差)歸一化。 |
|
沿時間軸計算每個通道的譜質心。 |
|
語音活動檢測器。 |
增強¶
以下變換實現了稱為 *SpecAugment* [Park 等人, 2019] 的流行增強技術。
在頻域對頻譜圖應用掩蔽。 |
|
在時域對頻譜圖應用掩蔽。 |
|
以給定速率在時間上拉伸 STFT,而不改變音高。 |
損失函式¶
計算 *Sequence Transduction with Recurrent Neural Networks* [Graves, 2012] 中的 RNN Transducer 損失。 |
多通道¶
計算跨通道功率譜密度 (PSD) 矩陣。 |
|
執行帶時頻掩蔽的 MVDR 波束賦形的最小方差無失真響應 (MVDR) 模組。 |
|
基於相對傳遞函式 (RTF) 和噪聲功率譜密度 (PSD) 矩陣的最小方差無失真響應 (*MVDR* [Capon, 1969] ) 模組。 |
|
基於 *Souden 等人* [Souden 等人, 2009] 提出的方法的最小方差無失真響應 (*MVDR* [Capon, 1969] ) 模組。 |