音频信号转换为 MFCC 图、语谱图和对数梅尔谱图,并进行特征图拼接
import os import librosa import librosa.display import numpy as np from PIL import Image
转换为 MFCC 图
def wav2mfcc(file_path, max_pad_len=300): wave, sr = librosa.load(file_path, mono=True, sr=None) mfcc = librosa.feature.mfcc(wave, sr=sr) pad_width = max_pad_len - mfcc.shape[1] mfcc = np.pad(mfcc, pad_width=((0, 0), (0, pad_width)), mode='constant') return mfcc
转换为语谱图
def wav2spectrogram(file_path): y, sr = librosa.load(file_path, sr=None) S = np.abs(librosa.stft(y)) return librosa.amplitude_to_db(S, ref=np.max)
转换为对数梅尔谱图
def wav2mel_spectrogram(file_path): y, sr = librosa.load(file_path, sr=None) S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000) return librosa.power_to_db(S, ref=np.max)
保存图片
def save_image(data, file_path): img = Image.fromarray(data, 'RGB') img.save(file_path)
if name == 'main': dataset_path = 'D:/论文代码/casia汉语情感语料库/' mfcc_path = 'D:/论文代码/MFCC/' spectrogram_path = 'D:/论文代码/语谱图/' mel_spectrogram_path = 'D:/论文代码/梅尔谱图/' new_path = 'D:/论文代码/特征图拼接/' max_pad_len = 300
for subdir, dirs, files in os.walk(dataset_path):
for file in files:
try:
file_path = os.path.join(subdir, file)
emotion = os.path.basename(subdir)
mfcc_feature = wav2mfcc(file_path, max_pad_len=max_pad_len)
spectrogram_feature = wav2spectrogram(file_path)
mel_spectrogram_feature = wav2mel_spectrogram(file_path)
# 保存 MFCC 特征图
mfcc_file_path = os.path.join(mfcc_path, emotion, os.path.splitext(file)[0] + '.png')
os.makedirs(os.path.dirname(mfcc_file_path), exist_ok=True)
save_image(mfcc_feature, mfcc_file_path)
# 保存语谱图特征图
spectrogram_file_path = os.path.join(spectrogram_path, emotion, os.path.splitext(file)[0] + '.png')
os.makedirs(os.path.dirname(spectrogram_file_path), exist_ok=True)
save_image(spectrogram_feature, spectrogram_file_path)
# 保存对数梅尔谱图特征图
mel_spectrogram_file_path = os.path.join(mel_spectrogram_path, emotion, os.path.splitext(file)[0] + '.png')
os.makedirs(os.path.dirname(mel_spectrogram_file_path), exist_ok=True)
save_image(mel_spectrogram_feature, mel_spectrogram_file_path)
# 拼接特征图
feature_map = np.concatenate((mfcc_feature, spectrogram_feature, mel_spectrogram_feature), axis=1)
new_file_path = os.path.join(new_path, emotion, os.path.splitext(file)[0] + '.png')
os.makedirs(os.path.dirname(new_file_path), exist_ok=True)
save_image(feature_map, new_file_path)
except Exception as e:
print(file_path, e)
原文地址: https://www.cveoy.top/t/topic/nJcE 著作权归作者所有。请勿转载和采集!