短时过零率表示信号穿越过零值的次数,或者信号波形中与0轴相交的次数。 可以用来判别语音和噪声。
import librosa
import numpy as np
import soundfile as sf
import matplotlib.pyplot as plt
y, sr = sf.read('sample.wav')
frame_size = 160
# 采用无重叠分帧以方便展现
num_frames = len(y) // frame_size
zero_cross = np.zeros(num_frames)
for index in range(0, num_frames):
y_temp = y[index * frame_size : (index + 1) * frame_size]
temp = 0
for indexc in range(1, frame_size):
temp += np.abs( np.sign( y_temp[indexc] ) - np.sign(y_temp[indexc - 1]))
zero_cross[index] = 0.5 * temp
# 时间轴
time = np.arange(0, len(y[:num_frames * frame_size])) * (1 / sr)
plt.subplot(211)
plt.plot(time, y[:num_frames * frame_size])
plt.ylabel('Amplitude')
plt.subplot(212)
# np.repeat(x,n)表示将x里的元素每个都重复n次
plt.plot(time, np.repeat(zero_cross, frame_size))
plt.show()
下图是语音波形及其对应的短时过零率曲线 https://mp.weixin.qq.com/s/VkaX7PaFZNPEIbpT9auIUA