Startup
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from mathutils import Gaussian,fit_gaussian, calculate_peaks
|
||||
|
||||
##############################################################################################
|
||||
#Setup
|
||||
##############################################################################################
|
||||
#try:
|
||||
# collimator.move("In")
|
||||
#except:
|
||||
# pass
|
||||
|
||||
shutter.write(1)
|
||||
|
||||
step_size = 0.05
|
||||
|
||||
##############################################################################################
|
||||
#Scan
|
||||
##############################################################################################
|
||||
|
||||
result = lscan (collimatorX, diode, -0.3, 0.3 , 30, 0.2, relative = True)
|
||||
shutter.write(0)
|
||||
|
||||
|
||||
##############################################################################################
|
||||
#Peak detection
|
||||
##############################################################################################
|
||||
|
||||
y = result.getReadable(0)
|
||||
x = result.getPositions(0)
|
||||
(normalization, mean, sigma) = fit_gaussian(y, x, True)
|
||||
fitted_gaussian_function = Gaussian(normalization, mean, sigma)
|
||||
print "Mean = " + str(mean)
|
||||
|
||||
|
||||
resolution = step_size/100
|
||||
fit_gaussian = []
|
||||
for p in frange(x[0],x[-1],resolution, True):
|
||||
fit_gaussian.append(fitted_gaussian_function.value(p))
|
||||
gx = frange(x[0], x[-1]+resolution, resolution)
|
||||
|
||||
plots = plot([y, fit_gaussian], ["data", "gaussian"], xdata = [x,gx] )
|
||||
plots[0].addMarker(mean, None, "Mean=" + str(round(mean,2)), None)
|
||||
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
Multi-peak search and gaussian fitting
|
||||
"""
|
||||
|
||||
from mathutils import estimate_peak_indexes, fit_gaussians, create_fit_point_list
|
||||
|
||||
start = 1
|
||||
end = 30
|
||||
step_size = 0.2
|
||||
|
||||
result= lscan(sout,sinp,start,end,[step_size,],0.05)
|
||||
|
||||
path = get_current_data_group()
|
||||
set_attribute(path, "ApertureX", apertureX.read())
|
||||
|
||||
readable = result.getReadable(0)
|
||||
positions = result.getPositions(0)
|
||||
|
||||
threshold = (min(readable) + max(readable))/2
|
||||
min_peak_distance = 5.0
|
||||
|
||||
peaks = estimate_peak_indexes(readable, positions, threshold, min_peak_distance)
|
||||
print "Peak indexes: " + str(peaks)
|
||||
print "Peak x: " + str(map(lambda x:positions[x], peaks))
|
||||
print "Peak y: " + str(map(lambda x:readable[x], peaks))
|
||||
|
||||
log("Highest peak index = " +str(peaks[0]))
|
||||
|
||||
#Manually adding a dataset
|
||||
data = [ [1,2,3,4,5], [2,3,4,5,6], [3,4,5,6,7]]
|
||||
path="group/data2"
|
||||
save_dataset(path, data)
|
||||
|
||||
gaussians = fit_gaussians(readable, positions, peaks)
|
||||
|
||||
|
||||
plots = plot([readable],["Multi-peak search"],[positions])
|
||||
for i in range(len(peaks)):
|
||||
peak = peaks[i]
|
||||
(norm, mean, sigma) = gaussians[i]
|
||||
if abs(mean - positions[peak]) < min_peak_distance:
|
||||
print "Peak -> " + str(mean)
|
||||
plots[0].addMarker(mean, None, "N="+str(round(norm,2)), None)
|
||||
else:
|
||||
print "Invalid gaussian fit: " + str(mean)
|
||||
@@ -0,0 +1,57 @@
|
||||
"""
|
||||
Function fitting and peak search with mathutils facade
|
||||
"""
|
||||
from mathutils import fit_polynomial,fit_gaussian, fit_harmonic, calculate_peaks
|
||||
from mathutils import PolynomialFunction, Gaussian, HarmonicOscillator
|
||||
import math
|
||||
|
||||
|
||||
start = 0
|
||||
end = 5
|
||||
step_size = 0.1
|
||||
|
||||
result= lscan(sout,sinp,start,end,[step_size,],0.01)
|
||||
|
||||
readable = result.getReadable(0)
|
||||
positions = result.getPositions(0)
|
||||
|
||||
def get_function_data(function, start, end, resolution):
|
||||
ret = []
|
||||
for x in frange(start, end, resolution, True):
|
||||
fit_polinomial.append(function.value(x))
|
||||
|
||||
|
||||
pars_polynomial = (a0, a1, a2, a3, a4, a5, a6) = fit_polynomial(readable, positions, 6)
|
||||
fitted_polynomial_function = PolynomialFunction(pars_polynomial)
|
||||
print pars_polynomial
|
||||
|
||||
(normalization, mean, sigma) = fit_gaussian(readable, positions, True)
|
||||
fitted_gaussian_function = Gaussian(normalization, mean, sigma)
|
||||
print (normalization, mean, sigma)
|
||||
|
||||
(amplitude, angular_frequency, phase) = fit_harmonic(readable, positions)
|
||||
fitted_harmonic_function = HarmonicOscillator(amplitude, angular_frequency, phase)
|
||||
print (amplitude, angular_frequency, phase)
|
||||
|
||||
|
||||
resolution = step_size/100
|
||||
fit_polinomial = []
|
||||
fit_gaussian = []
|
||||
fit_harmonic = []
|
||||
for x in frange(start,end,resolution, True):
|
||||
fit_polinomial.append(fitted_polynomial_function.value(x))
|
||||
fit_gaussian.append(fitted_gaussian_function.value(x))
|
||||
fit_harmonic.append(fitted_harmonic_function.value(x))
|
||||
x = frange(start, end+resolution, resolution)
|
||||
|
||||
|
||||
peaks = calculate_peaks(fitted_polynomial_function)
|
||||
|
||||
plots = plot([readable, fit_polinomial, fit_gaussian, fit_harmonic] ,
|
||||
["data", "polinomial", "gaussian", "harmonic"], xdata = [positions,x,x,x] )
|
||||
|
||||
for p in peaks:
|
||||
print "Max: " + str(p)
|
||||
plots[0].addMarker(p, None, "Max=" + str(round(p,2)), None)
|
||||
import java.awt.Color
|
||||
plots[0].addMarker(mean, None, "Mean=" + str(round(mean,2)), java.awt.Color.LIGHT_GRAY)
|
||||
+22
-1
@@ -4,4 +4,25 @@
|
||||
|
||||
|
||||
#Uncomment this line to create the simulated devices needed to the tutorial scripts.
|
||||
#run("tutorial/devices")
|
||||
#run("tutorial/devices")
|
||||
|
||||
import random
|
||||
|
||||
|
||||
class SimulatedOutput(Writable):
|
||||
def write(self, value):
|
||||
pass
|
||||
|
||||
|
||||
class SimulatedInput(Readable):
|
||||
def __init__(self):
|
||||
self.x = 0.0
|
||||
|
||||
def read(self):
|
||||
self.x = self.x + 0.2
|
||||
noise = (random.random() - 0.5) / 20.0
|
||||
return math.sin(self.x) + noise
|
||||
|
||||
|
||||
sout = SimulatedOutput()
|
||||
sinp = SimulatedInput()
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
###################################################################################################
|
||||
# Deployment specific global definitions - executed after startup.py
|
||||
###################################################################################################
|
||||
|
||||
|
||||
#Uncomment this line to create the simulated devices needed to the tutorial scripts.
|
||||
#run("tutorial/devices")
|
||||
Reference in New Issue
Block a user