python - 如何在 python3 中减小 matplotlib 频谱图的比例

标签 python audio matplotlib spectrogram

我正在分析 .wav 文件的频谱图。但在让代码最终工作后,我遇到了一个小问题。保存 700 多个 .wav 文件的频谱图后,我意识到它们本质上看起来都是一样的!这并不是因为它们是相同的音频文件,而是因为我不知道如何将绘图的比例变小(这样我就可以看出差异)。

我已经尝试通过查看此 StackOverflow 帖子来解决此问题 Changing plot scale by a factor in matplotlib

我将在下面显示两个不同 .wav 文件的图表

这是 .wav #1 This is a spectrogram of one .wav file

这是 .wav #2 enter image description here

不管你相信与否,这是两个不同的 .wav 文件,但它们看起来非常相似。如果规模如此之大,计算机尤其无法识别这两个 .wav 文件中的差异。

我的代码如下

def individualWavToSpectrogram(myAudio, fileNameToSaveTo):
print(myAudio)
#Read file and get sampling freq [ usually 44100 Hz ]  and sound object
samplingFreq, mySound = wavfile.read(myAudio)

#Check if wave file is 16bit or 32 bit. 24bit is not supported
mySoundDataType = mySound.dtype

#We can convert our sound array to floating point values ranging from -1 to 1 as follows

mySound = mySound / (2.**15)

#Check sample points and sound channel for duel channel(5060, 2) or  (5060, ) for mono channel

mySoundShape = mySound.shape
samplePoints = float(mySound.shape[0])

#Get duration of sound file
signalDuration =  mySound.shape[0] / samplingFreq

#If two channels, then select only one channel
#mySoundOneChannel = mySound[:,0]

#if one channel then index like a 1d array, if 2 channel index into 2 dimensional array
if len(mySound.shape) > 1:
    mySoundOneChannel = mySound[:,0]
else:
    mySoundOneChannel = mySound

#Plotting the tone

# We can represent sound by plotting the pressure values against time axis.
#Create an array of sample point in one dimension
timeArray = numpy.arange(0, samplePoints, 1)

#
timeArray = timeArray / samplingFreq

#Scale to milliSeconds
timeArray = timeArray * 1000

plt.rcParams['agg.path.chunksize'] = 100000


#Plot the tone
plt.plot(timeArray, mySoundOneChannel, color='Black')
#plt.xlabel('Time (ms)')
#plt.ylabel('Amplitude')
print("trying to save")
plt.savefig('/Users/BillyBobJoe/Desktop/' + fileNameToSaveTo + '.jpg')
print("saved")
#plt.show()
#plt.close()

如何修改此代码以提高图形的灵敏度,从而使两个 .wav 文件之间的差异更加明显?

谢谢!

[更新] 我尝试过使用
plt.xlim((0, 16000))

但这只是在图表右侧添加了空白 像enter image description here

我需要一种方法来更改每个单位的比例。这样当我将 x 轴从 0 - 16000 更改时,图表就会被填充

最佳答案

如果问题是:如何限制 x 轴上的比例,例如在 0 到 1000 之间,您可以执行以下操作:

plt.xlim((0, 1000))

关于python - 如何在 python3 中减小 matplotlib 频谱图的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45003858/

相关文章:

Python约束线性优化

python - Twisted Python - IRC 客户端

c++ - 我如何在 JUCE 中录制标题没有 'JUNK' 子 block 的音频?

python - IPython, "name ' plt' 未定义”

python - MySQL On Update 未触发 Django/TastyPie REST API

Python:在字符串中添加字符的副本

python - 如何在后台播放声音? (适用于 Windows 的 python 3.5.2)

ios - ios midi语音赞(0x90,30,127)

Python:填充雷达图中的区域

python - 如何改变matplotlib中y轴点之间的距离?