基于python3下wav文件的语谱图
发布日期:2022/8/23 16:31:38 浏览量:
基于python3下wav文件的语谱图
import numpy, wave
import numpy, matplotlib.pyplot as plt
# target: gain spec from framename
# input: filename, wav file path, string
# window_length_ms(/ms),window length(/ms), int
# window_shift_times(),rate of shit length, float
def getSpectrum(filename, window_length_ms, window_shift_times):
# read data
wav_file = wave.open(filename, ’r’)
params = wav_file.getparams()
# nchannels, channel number (like, 2 channel wav)
# sampwidth, sample percision rate (like, 2)
# framerate, sample rate, (like, 44100)
# wav_length, how much points after sampled, (int)
nchannels, sampwidth, framerate, wav_length = params[:4]
str_data = wav_file.readframes(wav_length)
wave_data = numpy.fromstring(str_data, dtype=numpy.short)
wav_file.close()
# gain log spectrogram
window_length = framerate * window_length_ms // 1000 # change time to points number
window_shift = int(window_length * window_shift_times) # change time to points number
nframe = (wav_length - (window_length - window_shift)) // window_shift # gain frame number
spec = numpy.zeros((window_length//2, nframe)) # store spectrogram [only half part]
for i in range(nframe):
start = i * window_shift
end = start + window_length
spec[:, i] = numpy.log(numpy.abs(numpy.fft.fft(wave_data[start:end])))[:window_length//2]
return spec
# main process
speech_spectrum = getSpectrum(’1.wav’, 20, 0.5)
plt.imshow(speech_spectrum[:, :])
plt.xlim(0, 300)
plt.ylim(0, 150)
plt.show()
实现效果
业务实施流程
马上咨询: 如果您有业务方面的问题或者需求,欢迎您咨询!我们带来的不仅仅是技术,还有行业经验积累。
QQ: 39764417/308460098 Phone: 13 9800 1 9844 / 135 6887 9550 联系人:石先生/雷先生