我得到了一些代码,它从一个文件夹中获取wav歌曲,并在另一个文件夹中生成相应的频谱图。但是,我想在生成频谱时提取每首歌曲采样的频率,最终绘制直方图以表示每首歌曲中的各种频率范围。我该怎么做呢?
import cv2
import matplotlib.pyplot as plt
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
import glob, os
import numpy as np
import wave from scipy.io
import wavfile
import time
results_dir = "/home/undead/Documents/MetricTestFolder/"
for root, dirs, files in os.walk("/home/undead/ShareWindowsDesktop/SampleWavs"): #LOADS SONGS FROM DIRECTORY
for file in files:
if file.endswith(".wav"):
filepath = os.path.join(root, file)
fs, frames = wavfile.read(filepath)
channels = [
np.array(frames[:, 0]),
np.array(frames[:, 1])
]
fig, (ax2) = plt.subplots(nrows=1)
data, freqs, bins, im = ax2.specgram(channels[0], NFFT=8192, Fs=44100, noverlap=int(8192 * 0.75))
start = time.time()
ax2.pcolormesh(bins, freqs, 10. * np.log10(data))
ax2.axis('tight')
plt.axis('off')
plt.subplots_adjust(left=0.0, right=1.0, bottom=0.0, top=1.0)
plt.xlim([0, 250])
plt.ylim([0, 15000])
plt.savefig(results_dir + "%s.png" % os.path.splitext(file)[0], dpi=450)
plt.close("all")
endt = time.time()
print "%f" % (endt - start)
#end