|
From: Ethan A M. <eam...@gm...> - 2018-10-20 18:58:30
|
On Saturday, 20 October 2018 10:18:54 theozh wrote:
> Thank you, Ethan, for your hint.
> ah, once again the powerful ternary operator ;-)
> Theo.
>
> The first example gets the first x value where y(x) is above the threshold and the last x value where y(x) is still below the threshold.
> Actually, this method could also be used to find FWHM of spectral data (see second example). However, if the spectrum is a Gaussian or Lorentzian or whatever peak, fitting with appropriate parameters might probably give better results.
I have been thinking about the possibility of somehow providing a window
into the sequence of data points, always centered about the "current" point
when plotting. I don't know how this would work exactly, but I am imagining
something like a reserved array name "window" that could only be accessed
only from inside a smoothing function.
For example to produce a running average over 5 points:
Avg(N) = 1/N * sum [i=1:N] window[i]
plot 'data' using 1:2 smooth window Avg(5)
Your case is a bit more complicated, but having the local data
points in an array rather than in a mess of separate variables
maintained by the ternary operator seems like it would be a win.
Thoughts or suggestions welcome.
Ethan
> First example:
>
> ### find x range when data passes a certain threshold
> # getting the first x value where y(x) is above the threshold
> # and the last x value wher y(x) is below the threshold
> reset session
> # generate some dummy data
> set print $Data
> do for [i=1:100] {
> print sprintf("%g %.3f",i*2+rand(0), rand(0)*20+i)
> }
> set print
>
> Threshold = 50
>
> set table $Dummy
> plot $Data u 1:($2<Threshold ? ($2) : NaN):($2>=Threshold ? ($2) : NaN) with table
> unset table
> # print $Dummy
>
> stats $Dummy u 1:3 nooutput
> Min_x = STATS_min_x
> stats $Dummy u 1:2 nooutput
> Max_x = STATS_max_x
>
> set label sprintf("min: %.1f, max: %.1f", Min_x, Max_x) at Min_x, Threshold+20 center
>
> set key top left
> plot $Data using 1:2 with lp lt 6, \
> Threshold w l,\
> $Dummy u 1:2 w impulse lc rgb "green",\
> $Dummy u 1:3 w impulse lc rgb "red"
> ### end of code
>
>
> Second example:
> ### find Full Width at Half Maximum (FWHM)
> reset session
> # generate some dummy data
> set print $Data
> Intensity = rand(0)*100
> Position = rand(0)*200+480
> Width = rand(0)*50+50
> do for [i=380:780] {
> print sprintf("%g %.3f",i, Intensity*exp(-((i-Position)/Width)**2)+rand(0)*Intensity*0.1)
> }
> set print
>
> stats $Data nooutput
> Int_max = STATS_max_y
> set table $Dummy
> plot $Data u (Peak=$1,$1) every ::STATS_index_max_y::STATS_index_max_y with table
> unset table
> Threshold = 0.5*Int_max
>
> set table $FWHM
> plot $Data u ($2>=Threshold ? ($1) : NaN):($2>=Threshold ? Threshold : NaN) with table
> unset table
> stats $FWHM nooutput
> FWHM = STATS_max_x - STATS_min_x
>
> reset
> set label 1 sprintf("Peak %.1f", Peak) at Peak*1.1, Int_max*0.95 center boxed
> set label 2 sprintf("FWHM %.1f", FWHM) at Peak, Threshold*1.1 center boxed
> set arrow 1 nohead from Peak,graph 0 to Peak,Int_max
> set yrange[*:*]
>
> plot $Data using 1:2 with l lc rgb "red" t "Spectrum", \
> $FWHM using 1:2 w l lc rgb "blue" not
>
> ### end of code
|