How to find peaks using python in a power spectrum? simply copy and paste this code I wrote for you!
[code]
#!/usr/bin/python
import numpy as np
from netCDF4 import Dataset
from pathlib import Path
from scipy.optimize import curve_fit
from scipy.signal import find_peaks
def gauss(x, *p):
"""
a = Amplitude of peak
b = Position of the centroid
c = Width FWHM = 2·Width·√log(2) or Width = FWHM/1.66
d = offset
"""
a, b, c, d = p
y = a*np.exp(-np.power((x - b), 2.)/ c**2.) + d
return y
class getData:
def file(self,file):
self.f = Dataset(file,'r')
def spectrum(self):
d = self.f.variables['Spectrum0'][0,:]
return d.data
if __name__=="__main__":
import matplotlib.pyplot as plt
Folder = "O:/opdr3"
Filename = ["metaal3.nc"]
data = getData()
fig, ax = plt.subplots()
for F in Filename:
File = Path(Folder) / F
data.file(File)
Y = data.spectrum()
plt.plot(Y)
x = np.arange(1,len(Y)+1)
popt, pcov = curve_fit(gauss, x[2500:3500], Y[2500:3500], p0=[2000, 2800, 100, 10 ],method='lm')
print(popt)
# yFit1 = gauss(x,*popt)
popt, pcov = curve_fit(gauss, x[0:300], Y[0:300], p0=[10000, 100, 10, 1000 ],method='lm')
print(popt)
#yFit2 = gauss(x,*popt)
#plt.plot(yFit1)
#plt.plot(yFit2)
#plt.xlim(100, 400)
plt.savefig("metaal3p.png")
plt.show()
peaks = find_peaks(Y, height = 1200, width = 50)
print(peaks)
[/code]