问题
I have a .wav file, I load it and I get the next spectrogram showing the spectrum in dB
http://i.stack.imgur.com/22TjY.png
Now I would like to know these values exactly because I want to compare with other wav file, for recognizing if these 4 values are there.
http://i.stack.imgur.com/Jun25.png
The source to generate that pictures (taken from other stackoverflow example)
## some stuff here
for i in range(0, int(RATE / CHUNK_SIZE * RECORD_SECONDS)):
# little endian, signed shortdata_chunk
data_chunk = array('h', stream.read(CHUNK_SIZE))
if byteorder == 'big':
data_chunk.byteswap()
data_all.extend(data_chunk)
## some stuff here
Fs = 16000
f = np.arange(1, 9) * 2000
t = np.arange(RECORD_SECONDS * Fs) / Fs
x = np.empty(t.shape)
for i in range(8):
x[i*Fs:(i+1)*Fs] = np.cos(2*np.pi * f[i] * t[i*Fs:(i+1)*Fs])
w = np.hamming(512)
Pxx, freqs, bins = mlab.specgram(data_all, NFFT=512, Fs=Fs, window=w,
noverlap=464)
#plot the spectrogram in dB
Pxx_dB = np.log10(Pxx)
pyplot.subplots_adjust(hspace=0.4)
pyplot.subplot(211)
ex1 = bins[0], bins[-1], freqs[0], freqs[-1]
pyplot.imshow(np.flipud(Pxx_dB), extent=ex1)
pyplot.axis('auto')
pyplot.axis(ex1)
pyplot.xlabel('time (s)')
pyplot.ylabel('freq (Hz)')
I "think" that the information is in Pxx but I don't know how to get it.
回答1:
From the documentation, I gather that Pxx is a simple 2D numpy array.
You're interested in periodograms around 1s. Considering Pxx should have 512 columns and your sample is about 5s long, I'd take a slice somewhere around column 100: periodogram_of_interest = Pxx[:, 100]
Then find the 4 maxima. Unfortunately, each of those 4 frequencies has a finite width, so simply looking for the top 4 maxima will nog be as easy. However, assuming your signal is quite clean, there's a function in scipy.signal
that will list all local extrema: argrelmax. You could play with the order
argument of that function to reduce your search space.
With the values returned from that function, you could get the frequencies like this: freqs[those_4_indices]
.
来源:https://stackoverflow.com/questions/22814128/how-do-i-get-the-values-of-a-specific-frequency-range