import librosa import librosa.display import numpy as np import os import matplotlib.pyplot as plt from PIL import Image

设置数据集路径和三种特征图保存路径

dataset_path = 'D:/论文代码/casia汉语情感语料库/' mfcc_path = 'D:/论文代码/MFCC/' spectrogram_path = 'D:/论文代码/语谱图/' mel_spectrogram_path = 'D:/论文代码/梅尔谱图/' concatenated_path = 'D:/论文代码/拼接特征图/'

创建三种特征图保存路径和拼接特征图保存路径

if not os.path.exists(mfcc_path): os.makedirs(mfcc_path) if not os.path.exists(spectrogram_path): os.makedirs(spectrogram_path) if not os.path.exists(mel_spectrogram_path): os.makedirs(mel_spectrogram_path) if not os.path.exists(concatenated_path): os.makedirs(concatenated_path)

定义函数将音频信号转化为特征图并保存

def save_features(file_path, feature_path, feature_type): # 读取音频文件 y, sr = librosa.load(file_path, sr=None) # 计算特征 if feature_type == 'mfcc': feature = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=20) elif feature_type == 'spectrogram': feature = np.abs(librosa.stft(y)) elif feature_type == 'mel_spectrogram': feature = librosa.feature.melspectrogram(y=y, sr=sr) feature = librosa.power_to_db(feature, ref=np.max) # 将特征图转化为png格式并保存 plt.figure(figsize=(5, 3)) librosa.display.specshow(feature, cmap='cool', y_axis=feature_type) plt.axis('off') plt.tight_layout() plt.savefig(feature_path) plt.close()

对数据集中的所有音频文件进行处理

for root, dirs, files in os.walk(dataset_path): for file in files: # 获取音频文件路径和标签 file_path = os.path.join(root, file) label = root.split('/')[-1] # 保存MFCC图 mfcc_file_path = os.path.join(mfcc_path, label, file.replace('.wav', '.png')) if not os.path.exists(os.path.dirname(mfcc_file_path)): os.makedirs(os.path.dirname(mfcc_file_path)) save_features(file_path, mfcc_file_path, 'mfcc') # 保存语谱图 spectrogram_file_path = os.path.join(spectrogram_path, label, file.replace('.wav', '.png')) if not os.path.exists(os.path.dirname(spectrogram_file_path)): os.makedirs(os.path.dirname(spectrogram_file_path)) save_features(file_path, spectrogram_file_path, 'spectrogram') # 保存梅尔谱图 mel_spectrogram_file_path = os.path.join(mel_spectrogram_path, label, file.replace('.wav', '.png')) if not os.path.exists(os.path.dirname(mel_spectrogram_file_path)): os.makedirs(os.path.dirname(mel_spectrogram_file_path)) save_features(file_path, mel_spectrogram_file_path, 'mel_spectrogram')

定义函数将三种特征图拼接并保存

def concatenate_features(mfcc_path, spectrogram_path, mel_spectrogram_path, concatenated_path): # 遍历三种特征图保存路径 for root, dirs, files in os.walk(mfcc_path): for file in files: # 获取MFCC图、语谱图、梅尔谱图的路径 mfcc_file_path = os.path.join(mfcc_path, root.split('/')[-1], file) spectrogram_file_path = os.path.join(spectrogram_path, root.split('/')[-1], file) mel_spectrogram_file_path = os.path.join(mel_spectrogram_path, root.split('/')[-1], file) # 读取三种特征图 mfcc_image = Image.open(mfcc_file_path) spectrogram_image = Image.open(spectrogram_file_path) mel_spectrogram_image = Image.open(mel_spectrogram_file_path) # 将三种特征图拼接 concatenated_image = Image.new('RGB', (mfcc_image.width + spectrogram_image.width + mel_spectrogram_image.width, mfcc_image.height)) concatenated_image.paste(mfcc_image, (0, 0)) concatenated_image.paste(spectrogram_image, (mfcc_image.width, 0)) concatenated_image.paste(mel_spectrogram_image, (mfcc_image.width + spectrogram_image.width, 0)) # 保存拼接后的特征图 concatenated_file_path = os.path.join(concatenated_path, root.split('/')[-1], file) if not os.path.exists(os.path.dirname(concatenated_file_path)): os.makedirs(os.path.dirname(concatenated_file_path)) concatenated_image.save(concatenated_file_path)

对三种特征图进行拼接

concatenate_features(mfcc_path, spectrogram_path, mel_spectrogram_path, concatenated_path)


原文地址: https://www.cveoy.top/t/topic/nJcy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录