This question already has an answer here:
I am trying to find peaks in amplitude within a spectrogram I have produced that plots frequency against time and amplitude as the third axis.
I have looked through and attempted to code a solution using scipy.signal.find_peaks_cwt
however the official reference guide did not explain what was meant by the widths
parameter.
When finding peaks using scipy.signal.find_peaks_cwt
will it return the frequency-time coordinates? If not is there another library or method to use to return the coordinates of local maxima?
What is the widths
parameter within scipy.signal.find_peaks_cwt
?
As the scipy.signal.find_peaks_cwt
deals with arrays why do you still need to plot a spectrogram in order to find local maxima?
import numpy as np
import matplotlib.pyplot as plt
import scipy.io.wavfile
from scipy import signal
def create_spectrogram(audio):
rate, data = scipy.io.wavfile.read(audio)
data_1D = data.flatten()
plt.specgram(data_1D, NFFT = 64, Fs = 64, noverlap=32)
plt.savefig("melody")
plt.show()
if __name__ == '__main__':
audio = "melody.WAV"
create_spectrogram(audio)
Links:
scipy.signal.find_peaks_cwt
reference guide: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.find_peaks_cwt.html
Could you please give a bit more information as to how exactly is your data stored in your code? Is it a numpy array, a scipy.signal.spectrogram or simple python lists? A snippet of code would help a lot in trying to help you find a good solution.
For the moment the only "easy" way I would suggest is to have a look at numpy.amax(), choosing wisely the value given to the axis parameter, should get you the maximum you are looking for.
find_peaks_cwt
is for 1-D array. If you want to find local peaks in your 2-D spectrogram, please read this: stackoverflow.com/questions/3684484/… - Gerges Dib