如何在python3中将.wav文件转换为频谱图 [英] How to convert a .wav file to a spectrogram in python3

查看:725
本文介绍了如何在python3中将.wav文件转换为频谱图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从python3中的.wav文件创建频谱图.

我希望最终保存的图像看起来与此图像相似:

我尝试了以下操作:

此堆栈溢出后: 波形文件的频谱图

这个帖子有些奏效.运行之后,我得到了

但是,此图未包含我需要的颜色.我需要有颜色的声谱图.我尝试修改此代码以尝试添加颜色,但是在花费大量时间和精力之后,我无法弄清楚!

然后我尝试了教程.

当我尝试以错误TypeError运行它时,此代码崩溃了(在第17行):'numpy.float64'对象无法解释为整数.

第17行:

samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)

我试图通过铸造来修复

samples = int(np.append(np.zeros(np.floor(frameSize/2.0)), sig))

我也尝试过

samples = np.append(np.zeros(int(np.floor(frameSize/2.0)), sig))    

但是,这些方法最终都没有起作用.

我真的很想知道如何将.wav文件转换为带有颜色的声谱图,以便我对其进行分析!任何帮助将不胜感激!

请告诉我是否要提供有关我的python版本,我尝试过的内容或想要实现的内容的更多信息.

解决方案

使用scipy.signal.spectrogram.

import matplotlib.pyplot as plt
from scipy import signal
from scipy.io import wavfile

sample_rate, samples = wavfile.read('path-to-mono-audio-file.wav')
frequencies, times, spectrogram = signal.spectrogram(samples, sample_rate)

plt.pcolormesh(times, frequencies, spectrogram)
plt.imshow(spectrogram)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()

在尝试执行此操作之前,请确保您的wav文件是单声道(单声道)而不是立体声(双声道).我强烈建议您阅读 https上的scipy文档. ://docs.scipy.org/doc/scipy- 0.19.0/reference/generation/scipy.signal.spectrogram.html .

plt.pcolormesh放在plt.imshow之前似乎可以解决某些问题,如@Davidjb所指出的,并且如果发生拆包错误,请按照下面@cgnorthcutt的步骤进行操作.

I am trying to create a spectrogram from a .wav file in python3.

I want the final saved image to look similar to this image:

I have tried the following:

This stack overflow post: Spectrogram of a wave file

This post worked, somewhat. After running it, I got

However, This graph does not contain the colors that I need. I need a spectrogram that has colors. I tried to tinker with this code to try and add the colors however after spending significant time and effort on this, I couldn't figure it out!

I then tried this tutorial.

This code crashed(on line 17) when I tried to run it with the error TypeError: 'numpy.float64' object cannot be interpreted as an integer.

line 17:

samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)

I tried to fix it by casting

samples = int(np.append(np.zeros(np.floor(frameSize/2.0)), sig))

and I also tried

samples = np.append(np.zeros(int(np.floor(frameSize/2.0)), sig))    

However neither of these worked in the end.

I would really like to know how to convert my .wav files to spectrograms with color so that I can analyze them! Any help would be appreciated!!!!!

Please tell me if you want me to provide any more information about my version of python, what I tried, or what I want to achieve.

解决方案

Use scipy.signal.spectrogram.

import matplotlib.pyplot as plt
from scipy import signal
from scipy.io import wavfile

sample_rate, samples = wavfile.read('path-to-mono-audio-file.wav')
frequencies, times, spectrogram = signal.spectrogram(samples, sample_rate)

plt.pcolormesh(times, frequencies, spectrogram)
plt.imshow(spectrogram)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()

Be sure that your wav file is mono (single channel) and not stereo (dual channel) before trying to do this. I highly recommend reading the scipy documentation at https://docs.scipy.org/doc/scipy- 0.19.0/reference/generated/scipy.signal.spectrogram.html.

Putting plt.pcolormesh before plt.imshow seems to fix some issues, as pointed out by @Davidjb, and if unpacking error occurs, follow the steps by @cgnorthcutt below.

这篇关于如何在python3中将.wav文件转换为频谱图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆