如何预处理音频数据输入神经网络?TIDIGITS数据集适配方案
Hey there! I’ve worked with the TIDIGITS dataset a handful of times for digit recognition tasks, so I can walk you through the exact preprocessing steps you need to fix those two core issues—multi-digit audio splits and variable-length inputs. Let’s break this down step by step:
TIDIGITS actually gives you all the info you need to split those multi-digit files cleanly—you just need to leverage its built-in annotations. Here’s how:
- First, locate the
.labor annotation files that come with each WAV. These files list the start/end timestamps (in seconds) for each digit in the audio clip. For example, a file with "one two three" will have three timestamp pairs corresponding to each spoken digit. - Use a library like
librosaorsoundfileto load the audio, then slice the waveform based on those timestamps. Avoid relying solely on Voice Activity Detection (VAD) here—while VAD works, the dataset’s official annotations are way more accurate, especially for quiet or fast-spoken digits.
Code snippet for splitting:
import librosa import numpy as np # Load audio (keep original sample rate) audio_path = "path/to/tidigits/man_B_seven_nine.wav" y, sr = librosa.load(audio_path, sr=None) # Example timestamps from the .lab file (replace with actual annotation data) digit_timestamps = [(0.2, 0.9), (1.0, 1.7)] # For "seven nine" # Split into single-digit clips single_digit_clips = [] for start_sec, end_sec in digit_timestamps: start_sample = int(start_sec * sr) end_sample = int(end_sec * sr) single_digit = y[start_sample:end_sample] single_digit_clips.append(single_digit)
Once you have single-digit clips, you need to standardize their lengths so they can be fed into your neural network. There are two reliable approaches here:
Option 1: Truncation + Zero Padding
This is the most common and least destructive method:
- First, analyze your dataset to find the maximum length of all single-digit clips (most TIDIGITS single digits are ~0.5-1.2 seconds long). Pick a target length slightly longer than the 95th percentile of your clip lengths to avoid cutting off valid audio.
- For clips longer than the target: Truncate the end (or middle, but end is safer since speech usually tails off).
- For clips shorter than the target: Pad the end with zeros (silence) to reach the target length.
Code snippet for this:
# Define target length (e.g., 1.2 seconds = 1.2 * sample rate) target_duration = 1.2 target_samples = int(target_duration * sr) processed_clips = [] for clip in single_digit_clips: clip_len = len(clip) if clip_len > target_samples: # Truncate to target length processed = clip[:target_samples] else: # Pad with zeros at the end pad_amount = target_samples - clip_len processed = np.pad(clip, (0, pad_amount), mode="constant") processed_clips.append(processed)
Option 2: Time-Stretching (Use Sparingly)
If you want to preserve all audio content instead of truncating, you can time-stretch clips to fit the target length without changing pitch. Note that this can introduce minor distortion, so only use it if truncating would cut off critical speech parts.
You can use librosa.effects.time_stretch for this:
from librosa.effects import time_stretch processed_clips = [] for clip in single_digit_clips: clip_duration = len(clip) / sr stretch_factor = target_duration / clip_duration stretched_clip = time_stretch(clip, rate=stretch_factor) processed_clips.append(stretched_clip)
Raw audio waveforms can work, but extracting features like MFCCs (Mel-Frequency Cepstral Coefficients) or Mel Spectrograms will make your model train faster and perform better. Here’s how to extract standardized MFCC features:
def extract_standardized_mfcc(clip, sr, n_mfcc=13, target_time_steps=40): # Extract base MFCCs mfcc = librosa.feature.mfcc(y=clip, sr=sr, n_mfcc=n_mfcc) # Standardize time steps if mfcc.shape[1] > target_time_steps: mfcc = mfcc[:, :target_time_steps] else: pad_width = target_time_steps - mfcc.shape[1] mfcc = np.pad(mfcc, ((0, 0), (0, pad_width)), mode="constant") # Add delta and delta-delta features for temporal context delta_mfcc = librosa.feature.delta(mfcc) delta2_mfcc = librosa.feature.delta(mfcc, order=2) # Combine all features combined_mfcc = np.concatenate([mfcc, delta_mfcc, delta2_mfcc], axis=0) return combined_mfcc # Extract features for all processed clips mfcc_features = [extract_standardized_mfcc(clip, sr) for clip in processed_clips] # Optional: Normalize features across the entire dataset (mean=0, std=1) all_features = np.array(mfcc_features) mean = np.mean(all_features) std = np.std(all_features) normalized_features = (all_features - mean) / std
- Pre-Emphasis: Apply a pre-emphasis filter to boost high-frequency sounds (common in speech processing) using
librosa.effects.preemphasis. - Noise Reduction: TIDIGITS uses telephone-quality audio, so you can reduce background noise with spectral subtraction or
librosa.decompose.nn_filter. - Validate Splits: Randomly listen to a few split clips to make sure you didn’t cut off digits or include extra silence—this saves you from wasting time training on bad data.
内容的提问来源于stack exchange,提问作者UrmLmn

