kks*_*kks 4 python audio neural-network spectrogram tensorflow
我正在使用神经网络进行语音识别.为此,我需要获取那些训练音频文件(.wav)的频谱图.如何在python中获取这些频谱图?
有很多方法可以做到这一点.最简单的方法是查看Kernel中关于Kaggle竞争的TensorFlow语音识别挑战中提出的方法(按大多数投票排序).这一点特别清晰简单,包含以下功能.输入是从wav文件中提取的样本的数字向量,采样率,帧的大小(以毫秒为单位),步长(步幅或跳跃)大小(以毫秒为单位)和小偏移量.
from scipy.io import wavfile
from scipy import signal
import numpy as np
sample_rate, audio = wavfile.read(path_to_wav_file)
def log_specgram(audio, sample_rate, window_size=20,
step_size=10, eps=1e-10):
nperseg = int(round(window_size * sample_rate / 1e3))
noverlap = int(round(step_size * sample_rate / 1e3))
freqs, times, spec = signal.spectrogram(audio,
fs=sample_rate,
window='hann',
nperseg=nperseg,
noverlap=noverlap,
detrend=False)
return freqs, times, np.log(spec.T.astype(np.float32) + eps)
Run Code Online (Sandbox Code Playgroud)
输出在SciPy手册中定义,但光谱图使用单调函数(Log())重新调整,除了较小的值之外,其压下的值越大,而较大的值仍然大于较小的值.这样,规范中没有极端值将支配计算.或者,可以将值限制在某个分位数处,但优选log(或甚至平方根).还有许多其他方法来规范化频谱图的高度,即防止极端值"欺负"输出:)
freq (f) : ndarray, Array of sample frequencies.
times (t) : ndarray, Array of segment times.
spec (Sxx) : ndarray, Spectrogram of x. By default, the last axis of Sxx corresponds to the segment times.
Run Code Online (Sandbox Code Playgroud)
或者,您可以在音频识别的Tensorflow示例中检查github repo上的train.py和models.py代码.
这是另一个解释并提供有关在Python中构建光谱图的代码的线程.