6. Noisy peak finding. Finding peak in a noisy data is a very common task in analyzing physical from sensors. Consider the following data shown below. Your task for this problem is to find the peak position and peak height from noisy data. There are 10 dataset make sure you succeed in finding all the peak in at least 9. def find peaks(xs, ys): return [(xpeak, ypeak), (xpeak, ypeak), ...] Here are some hints: • You should first find candidates for peak in noisy data. Fit the peak(and neighbor) with parabola • Make sure the parameter from the fitted parabola actually indicate that it is a peak. • Then use the parameter from the fitted parabola to find the peak location and height. (Recall High School Math) 1 np.random.seed(9999) 2 def is_good_peak(mu, min_dist=0.8): if mu is None: return False smu = np.sort(mu) if smu[0] < 0.5: return False if smu[-1] > 2.5: return False for p, n in zip(smu, smu[1:]): #print(abs(p-n)) if abs(p-n) < min_dist: return False return True 16 maxx = 3 17 ndata = 500 18 nset = 10 19 1 = [] 20 answers = [] for iset in range(1, nset): npeak = np.random.randint(2,4) xs = np.linspace(0, maxx, ndata) ys = np.zeros(ndata) mu = None while not is_good_peak(mu): mu = np.random.random(npeak)*maxx for ipeak in range(npeak): m = mu[ipeak] sigma = np.random.random()*0.3 + 0.2 height = np.random.random()*0.5 + 1 ys += height*np.exp(-(xs-m)**2/sigma**2) ys += np.random.randn (ndata)*0.07 1.append(ys) answers.append(mu) 39 p6_ys = 1 40 p6_xs = np.linspace (0, maxx,ndata) 41 43 p6_answers = answers for ys, ans in zip(p6_ys, p6_answers): plt.figure() plt.plot(xs, ys, '.') 46 for a in ans: 47 plt.axvline(a,color='red') Python M Python Python Python Python
import numpy as np
%matplotlib inline
from matplotlib import pyplot as plt
import math
from math import exp
np.random.seed(9999)
def is_good_peak(mu, min_dist=0.8):
if mu is None:
return False
smu = np.sort(mu)
if smu[0] < 0.5:
return False
if smu[-1] > 2.5:
return False
for p, n in zip(smu, smu[1:]):
#print(abs(p-n))
if abs(p-n) < min_dist:
return False
return True
maxx = 3
ndata = 500
nset = 10
l = []
answers = []
for iset in range(1, nset):
npeak = np.random.randint(2,4)
xs = np.linspace(0,maxx,ndata)
ys = np.zeros(ndata)
mu = None
while not is_good_peak(mu):
mu = np.random.random(npeak)*maxx
for ipeak in range(npeak):
m = mu[ipeak]
sigma = np.random.random()*0.3 + 0.2
height = np.random.random()*0.5 + 1
ys += height*np.exp(-(xs-m)**2/sigma**2)
ys += np.random.randn(ndata)*0.07
l.append(ys)
answers.append(mu)
p6_ys = l
p6_xs = np.linspace(0,maxx,ndata)
p6_answers = answers
for ys, ans in zip(p6_ys, p6_answers):
plt.figure()
plt.plot(xs, ys, '.')
for a in ans:
plt.axvline(a,color='red')
Step by step
Solved in 2 steps