Jun 2024
This commit is contained in:
64
script/DemoScan.py
Normal file
64
script/DemoScan.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# Run detached as:
|
||||
# pshell -p=DemoScan.java -d -sbar -tbar -l -dplt
|
||||
from mathutils import fit_harmonic, HarmonicOscillator
|
||||
|
||||
|
||||
if get_exec_pars().innerArgs is None:
|
||||
print 1
|
||||
START = 0.0
|
||||
END = 0.400
|
||||
STEPS = 10
|
||||
PLOT = None
|
||||
if PLOT is None:
|
||||
PLOT = plot(None,title="HFit")[0]
|
||||
PLOT.clear()
|
||||
|
||||
sd = lscan(inp, (sin,out,arr), START, END, STEPS, 0.1)
|
||||
|
||||
|
||||
ydata = sd[sin]
|
||||
xdata = sd[inp]
|
||||
|
||||
max_y= max(ydata)
|
||||
index_max = ydata.index(max_y)
|
||||
max_x= xdata[index_max]
|
||||
|
||||
start,end = min(xdata), max(xdata)
|
||||
(amplitude, angular_frequency, phase) = fit_harmonic(ydata, xdata)
|
||||
fitted_harmonic_function = HarmonicOscillator(amplitude, angular_frequency, phase)
|
||||
|
||||
print "amplitude = ", amplitude
|
||||
print "angular frequency = ", angular_frequency
|
||||
print "phase = ", phase
|
||||
|
||||
f = angular_frequency/ (2* math.pi)
|
||||
print "frequency = ", f
|
||||
|
||||
resolution = 0.01
|
||||
fit_y = []
|
||||
for x in frange(start,end,resolution, True):
|
||||
fit_y.append(fitted_harmonic_function.value(x))
|
||||
fit_x = frange(start, end+resolution, resolution)
|
||||
|
||||
|
||||
PLOT.addSeries(LinePlotSeries("data"))
|
||||
PLOT.addSeries(LinePlotSeries("fit"))
|
||||
PLOT.getSeries(0).setData(xdata, ydata)
|
||||
PLOT.getSeries(1).setData(fit_x, fit_y)
|
||||
|
||||
#m = (phase + math.pi)/ angular_frequency
|
||||
m = -phase / angular_frequency
|
||||
if (m<start):
|
||||
m+=(1.0/f)
|
||||
|
||||
if start <= m <=end:
|
||||
print "fit = ", m
|
||||
PLOT.addMarker(m, None, "Fit="+str(round(m ,2)), Color.MAGENTA.darker())
|
||||
valid_fit = True
|
||||
else:
|
||||
print "max = ",max_x
|
||||
PLOT.addMarker(max_x, None, "Max="+str(round(max_x ,2)), Color.MAGENTA.darker())
|
||||
valid_fit = False
|
||||
|
||||
set_return([amplitude, angular_frequency, phase, valid_fit, m])
|
||||
|
||||
Reference in New Issue
Block a user