微立顶科技

新闻资讯

创新 服务 价值

  基于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     联系人:石先生/雷先生