【发布时间】:2017-07-10 16:01:49
【问题描述】:
我正在分析 .wav 文件的频谱图。但是在让代码最终工作之后,我遇到了一个小问题。在保存了 700 多个 .wav 文件的频谱图后,我意识到它们基本上看起来都一样!!!这不是因为它们是相同的音频文件,而是因为我不知道如何将绘图的比例更改为更小(这样我才能分辨出差异)。
我已经尝试通过查看此 StackOverflow 帖子来解决此问题 Changing plot scale by a factor in matplotlib
我将在下面显示两个不同 .wav 文件的图表
信不信由你,这是两个不同的 .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