Reputation: 135
I have a data file example.dat
with xy values, for example
0 10
1 40
5 20
How can I sample the linear interpolation of these points in gnuplot
? I want to store that sampling in another file output.dat
using set table
. With cubic spline smoothing I can do
set table "output.dat"
set samples 10
plot "example.dat" smooth csplines
which yields an equidistant sampling of the cubic spline interpolation with 10 points. But I found no way to have such an equidistant sampling with linear interpolation: The sampling rate is just ignored (gnuplot 5.0).
I tried without any options and with linear interpolation "smoothing", like smooth unique
, hoping that this would make gnuplot think of the dataset as a function which can be sampled, but to no avail.
My application is sampling different data files at a common grid for later comparison. I am aware that this is pushing the boundaries of what gnuplot is intended for, but since there is already a sampling mechanism I wonder if I am simply missing something.
Upvotes: 2
Views: 1381
Reputation: 25734
In case this might still be of interest, the following is a "gnuplot only" solution. Not very elegant, but it seems to work.
### "gnuplot only" linear interpolation of data
reset session
$Data <<EOD
0 10
1 40
5 20
EOD
stats $Data u 1 nooutput
min = STATS_min
max = STATS_max
Samples=10
Interpolate(x0,y0,x1,y1,xi) = y0 + (y1-y0)/(x1-x0)*(xi-x0)
set print $Interpol
set table $Nowhere
do for [i=1:Samples] {
xi = min + (i-1)*(max-min)/(Samples-1)
do for [j=0:STATS_records-1] {
plot $Data u (a=$1,$1):(b=$2,$2) every ::j::j with table
plot $Data u (c=$1,$1):(d=$2,$2) every ::j+1::j+1 with table
if ( xi>=a && xi<=c) {
print sprintf("%g\t%g",xi,Interpolate(a,b,c,d,xi))
break
}
}
}
unset table
set print
set colorsequence classic
plot $Data u 1:2 w lp t "original data",\
$Data u 1:2 w lp smooth cspline t "smooth cspline",\
$Interpol u 1:2 w p pt 6 t "linear interpolation"
### end code
Upvotes: 2
Reputation: 1731
Hope I understood the question properly. You're having an equidistant sampling between 0 and 5, which in this case gives a step of 5/9=0.555556. To get a 0.5 distance between your samples, assuming your xrange[0:5]
, you should do set samples 11
.
However, if you want to stick to 10 samples and all in steps of 0.5, you can tweak your xrange[0.5:5.0]
, which will create 9 steps of 0.5.
Upvotes: 0