基于python3下wav文件的語(yǔ)譜圖
發(fā)布日期:2022/8/23 16:31:38 瀏覽量:
基于python3下wav文件的語(yǔ)譜圖
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()
實(shí)現(xiàn)效果
馬上咨詢: 如果您有業(yè)務(wù)方面的問題或者需求,歡迎您咨詢!我們帶來的不僅僅是技術(shù),還有行業(yè)經(jīng)驗(yàn)積累。
QQ: 39764417/308460098 Phone: 13 9800 1 9844 / 135 6887 9550 聯(lián)系人:石先生/雷先生