简介
基于语音信号的性别分类是许多音频系统的重要组成部分,例如自动语音识别、说话者识别和基于内容的多媒体索引。
本示例使用长短期记忆 (LSTM) 网络,这是一种循环神经网络 (RNN),非常适合研究序列和时序数据。LSTM 网络可以学习序列的时间步之间的长期相关性。LSTM 层 (lstmLayer) 可以前向分析时间序列,而双向 LSTM 层 (bilstmLayer) 可以前向和后向分析时间序列。此示例使用双向 LSTM 层。
此示例用 Gammatone 倒频谱系数 (gtcc(Audio Toolbox))、音调估计 (pitch(Audio Toolbox))、谐波比 (harmonicRatio(Audio Toolbox)) 和几个频谱形状描述符 (Spectral Descriptors(Audio Toolbox)) 序列来训练 LSTM 网络。
为了加快训练过程,请在具有 GPU 的机器上运行此示例。如果您的机器同时有 GPU 和 Parallel Computing Toolbox™,则 MATLAB© 会自动使用 GPU 进行训练;否则,它使用 CPU。
通过预训练网络进行性别分类
在进入详细训练过程之前,您将使用预训练网络对两个测试信号中说话者的性别进行分类。
加载预训练网络以及用于特性归一化的预先计算的向量。
load('genderIDNet.mat', 'genderIDNet', 'M', 'S');
加载一个男性说话者的测试信号。
[audioIn, Fs] = audioread('maleSpeech.flac');
sound(audioIn, Fs)
隔离信号中的语音区域。
boundaries = detectSpeech(audioIn, Fs);
audioIn = audioIn(boundaries(1):boundaries(2));
创建一个 audioFeatureExtractor(Audio Toolbox) 以从音频数据中提取特征。您将使用同一对象来提取特征进行训练。
extractor = audioFeatureExtractor( ...
"SampleRate",Fs, ...
"Window",hamming(round(0.03*Fs),"periodic"), ...
"OverlapLength",round(0.02*Fs), ...
...
"gtcc",true, ...
"gtccDelta",true, ...
"gtccDeltaDelta",true, ...
...
"SpectralDescriptorInput","melSpectrum", ...
"spectralCentroid",true, ...
"spectralEntropy",true, ...
"spectralFlux",true, ...
"spectralSlope",true, ...
...
"pitch",true, ...
"harmonicRatio",true);
从信号中提取特征并对其进行归一化。
features = extract(extractor, audioIn);
features = (features.' - M)./S;
对信号进行分类。
gender = classify(genderIDNet, features)
gender = categorical
male
对另一个女性说话者的信号进行分类。
[audioIn, Fs] = audioread('femaleSpeech.flac');
sound(audioIn, Fs)
boundaries = detectSpeech(audioIn, Fs);
audioIn = audioIn(boundaries(1):boundaries(2));
features = extract(extractor, audioIn);
features = (features.' - M)./S;
classify(genderIDNet, features)
ans = categorical
female
预处理训练音频数据
当使用特征向量序列时,此示例中使用的 BiLSTM 网络效果最佳。为了说明如何预处理管道,此示例逐步演示了针对单个音频文件的步骤。
读取包含语音的音频文件的内容。说话者的性别是男性。
[audioIn,Fs] = audioread('Counting-16-44p1-mono-15secs.wav');
labels = {'male'};
绘制音频信号,然后使用 sound 命令收听它。
timeVector = (1/Fs) * (0:size(audioIn,1)-1);
figure
plot(timeVector,audioIn)
ylabel("Amplitude")
xlabel("Time (s)")
title("Sample Audio")
grid on
sound(audioIn,Fs)
语音信号具有静默段,其中不包含与说话者性别相关的有用信息。使用 detectSpeech(Audio Toolbox) 定位音频信号中的语音段。
speechIndices = detectSpeech(audioIn,Fs);
创建一个 audioFeatureExtractor(Audio Toolbox) 以从音频数据中提取特征。语音信号在本质上是动态的,并且随着时间而变化。假设语音信号在短时间尺度上是稳定的,并且其处理通常在 20-40 毫秒的时间窗中完成。指定 30 毫秒时间窗,其中包含 20 毫秒重叠。
extractor = audioFeatureExtractor( ...
"SampleRate",Fs, ...
"Window",hamming(round(0.03*Fs),"periodic"), ...
"OverlapLength",round(0.02*Fs), ...
...
"gtcc",true, ...
"gtccDelta",true, ...
"gtccDeltaDelta",true, ...
...
"SpectralDescriptorInput","melSpectrum", ...
"spectralCentroid",tru