【问题标题】:How to decrease the scale of a matplotlib spectrogram in python3如何在 python3 中减小 matplotlib 频谱图的比例
【发布时间】:2017-07-10 16:01:49
【问题描述】:

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

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

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

这是 .wav #1

这是 .wav #2

信不信由你,这是两个不同的 .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))

但这只是在图表的右侧添加了空格 赞

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

【问题讨论】:

  • 您在这里想要实现的目标并不完全清楚。如果你想放大,你可以增加比例,就像@Nipun's anwer 建议的那样。如果您想保持比例但提高分辨率,您可以使用更大的绘图尺寸,例如plt.rcParams['figure.figsize'] = 15, 5.
  • 当我使用代码 plt.rcParams['figure.figsize'] = 15, 5 。输出图像看起来完全一样
  • 我非常怀疑;它在宽度方向上几乎是 3 倍。
  • 我现在可以使用命令 plt.figure(figsize=(35,15))

标签: python audio matplotlib spectrogram


【解决方案1】:

如果问题是:如何限制x轴上的比例,比如0到1000之间,你可以这样做:

plt.xlim((0, 1000))

【讨论】:

  • 我可以用命令 plt.ylim((-1,0)) 对 y 轴做同样的事情来将 y 轴从 -1 缩放到 0 吗?
  • 这只是扩展了图表,留下了很多空白。我需要一种方法来改变每个单位的规模。例如 1 个网格空间 = 像 0.1
  • @sreehari 这个答案告诉您使用较低的值作为右轴限制(1000 而不是8000)。如果你增加限制,当然还有一些空白。
  • 这样做的问题是,虽然我得到了 0 - 1000 的详细信息,但我丢失了 1000-8000 的所有数据。
  • @sreeharirammohan :那么也许是了解更多关于 matplotlib 的好时机。您不会丢失数据,只是在当前图中看不到它。我建议使用具有不同频率范围的子图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 2020-04-07
相关资源
最近更新 更多