spkit.peak_detection

spkit.peak_detection(mX, thr)

Detect spectral peaks

Detecting spectral peaks

Parameters:
mXmagnitude spectrum(in dB)
thrthreshold (dB)
Returns:
ploc: peak locations

Examples

#sp.peak_detection
import numpy as np
import matplotlib.pyplot as plt
import spkit as sp
X, fs, ch_names = sp.data.eeg_sample_14ch()
x = X[:,0]
mX, fr = sp.periodogram(x,fs=fs)
mX = 20*np.log10(mX)
ploc = sp.peak_detection(mX, thr=0)
plt.figure(figsize=(7,4))
plt.plot(fr,mX)
plt.plot(fr[ploc],mX[ploc],'o')
plt.vlines(fr[ploc],ymin=-80,ymax=70,color='C1',ls='--',alpha=0.5)
plt.axhline(0,color='k',lw=1,ls='--')
plt.ylim([-80,70])
plt.xlim([0,fr[-1]])
plt.grid()
plt.ylabel('mX (dB)')
plt.title('Spectral Peaks: EEG Signal')
plt.show()
../../_images/spkit-peak_detection-1_00_00.png
print('Peaks at : ',fr[ploc],'Hz')

######################################
#sp.peak_detection
import requests
from scipy.io import wavfile
import numpy as np
import matplotlib.pyplot as plt
import spkit as sp

path = 'https://github.com/MLEndDatasets/samples/raw/main/HWD_HP_hum_1.wav?raw=True'
req = requests.get(path)
with open('downloaded_file.wav', 'wb') as f:
        f.write(req.content)
fs, x = wavfile.read('downloaded_file.wav')
t = np.arange(x.shape[0])/fs

mX, fr = sp.periodogram(x,fs=fs)
mX = 20*np.log10(mX)
thr = -40
ploc = sp.peak_detection(mX, thr=thr)
plt.figure(figsize=(7,4))
plt.plot(fr,mX)
plt.plot(fr[ploc],mX[ploc],'o')
plt.vlines(fr[ploc],ymin=-80,ymax=80,color='C1',ls='--',alpha=0.5)
plt.axhline(thr,color='k',lw=1,ls='--')
plt.ylim([-80,80])
plt.xlim([-1,fr[-1]])
plt.grid()
plt.ylabel('mX (dB)')
plt.title('Spectral Peaks: Audio')
plt.show()
../../_images/spkit-peak_detection-1_01_00.png
print('peaks at : ',fr[ploc], 'Hz')