我目前正在处理一个项目,其中我需要使用音频文件的光谱。
我目前正在使用另一个程序来生成音频文件的功率谱,并使用matplotlib绘制它,如下:
for elements in file_names_data_start_end_index:
## Entries = Rows of the output data
## Length = Columns of the output data
entries = elements[2] - elements[1]+1
name = elements[0]
length = data_splitted[elements[1]].count(" ")-2
print "Filename: " + name + " Entries: " + str(entries) + " Length: " + str(length)
output = ""
for i in range(elements[1],elements[2]+1):
output += data_splitted[i]
#print data_splitted[i]
#print output[:-1]
output = np.fromstring(output[:-1],count=-1,sep=' ')
#output_log = np.log(output)
output_reshaped = output.reshape(entries,length)
#print output_reshaped.shape
##Do the actual plot!
# 1 second audio creates 98 rows, as there is a overlap on 10 ms will
# extra data be created, normally would it have been 16000/512 = 31 rows
# if the frames where disjoint, but not in this case. 160 samples is the extra overlap.
# (16000-512)/160+1 = 97.8 ~ 98
Y = np.array(range(0,length)) * (16000.0/512.0)
X = np.array(range(0,entries))*(1/98.0)
X,Y = np.meshgrid(X, Y)
#print type(X)
#print type(Y)
#print type(output_reshaped)
plt.pcolormesh(X,Y,output_reshaped.T, cmap=cm.jet)
#surf = ax.plot_surface(X,Y,output_reshaped)
plt.xlabel('Time(s)')
plt.ylabel('Frequency(Hz)')
plt.title('Power spectrum of ' + name)
plt.colorbar()
plt.savefig(spectogram+"test_spect_plots/"+name+"_plot.png")
plt.close()
print "Saved"
问题在于,即使音频文件的时间更长或更短,情节总是具有相同的大小......
时间轴的分辨率不固定,有没有办法保持它固定,这样matplotlib不会尝试将图像拟合到前缀大小,而只是扩展x轴并使绘图有点更长?