pyboy1995
pyboy1995

Reputation: 25

find peaks in spectrum and delete them - python

i got the following code to identify peaks.

Frequency = [ 5.0, 6.3, 8.0, 10.0, 12.5, 16.0, 20.0, 25.0, 31.5, 40.0, 50.0, 63.0, 80.0, 100.0, 125.0, 160.0, 200.0, 250.0, 315.0] #third octave band spectrum, 19 Values
Spec = [ 40, 45, 51, 42, 44, 56, 42, 55, 57, 58, 45, 40, 38, 36, 32, 30, 28, 30, 29] #noise level, 19 Values
peaks, _ = find_peaks(Spec, prominence=1)
plt.plot(Frequency[peaks], Spec[peaks], "xr"); plt.plot(Frequency, Spec); plt.legend(['prominence'])
plt.tight_layout()
plt.show()
print( [(i,j) for i, j in zip(Frequency[peaks], Spec[peaks] )] )

the code works quit good. The next aim is to identify the peaks and delete them from the spectrum so that i can smooth the curve. Does someone has got an idea to solve this problem?

Thank you for your help

Upvotes: 0

Views: 1062

Answers (1)

Stef
Stef

Reputation: 30609

Create a boolean array m to select all elements that are not in the index array peaks:

import matplotlib.pyplot as plt
from scipy.signal import find_peaks
import numpy as np

Frequency = np.array([ 5.0, 6.3, 8.0, 10.0, 12.5, 16.0, 20.0, 25.0, 31.5, 40.0, 50.0, 63.0, 80.0, 100.0, 125.0, 160.0, 200.0, 250.0, 315.0]) #third octave band spectrum, 19 Values
Spec = np.array([ 40, 45, 51, 42, 44, 56, 42, 55, 57, 58, 45, 40, 38, 36, 32, 30, 28, 30, 29]) #noise level, 19 Values

peaks, _ = find_peaks(Spec, prominence=1)
m = np.zeros(Frequency.shape, dtype=bool)
m[peaks] = True

plt.plot(Frequency[peaks], Spec[peaks], "xr", label="prominence")
plt.plot(Frequency, Spec, label="original")
plt.plot(Frequency[~m], Spec[~m], label="'smoothed'")
plt.legend() 

plt.tight_layout()
plt.show()

enter image description here

Upvotes: 3

Related Questions