在某些情况下,您可能需要重新采样数据。如何使用gnuplot独立于平台执行此操作?下面是一次尝试。
数据集$Dummy
包含插值,但是包含NaN
的许多不必要的行。数据集$DataResampled
最终包含所需的数据。
我的问题是:这可以简化吗?
代码:
### resampling data with linear interpolation
reset session
$Data <<EOD
0 1
1 4
2 10
5 15
10 5
20 11
EOD
# get the x-range
stats $Data u 1 nooutput
MinX = STATS_min
MaxX = STATS_max
Resamples=20
# or alternatively fix the step size
# StepSize=1
# Resamples = int(MaxX-MinX)/StepSize+1
Interpolate(xi) = y0 + (y1-y0)/(x1-x0)*(xi-x0)
x1=y1=NaN
# resample the data
set print $DataResampled
set table $Dummy
do for [i=1:Resamples] {
xi = MinX + (i-1)*(MaxX-MinX)/(Resamples-1)
Flag=0
plot $Data u (x0=x1, x1=$1, y0=y1, y1=$2,\
(xi>=x0 && xi<=x1 && Flag==0 ? (Flag=1, yi=Interpolate(xi), xi) : NaN)): \
(Flag==1 ? yi : NaN) with table
print sprintf("%g\t%g",xi,yi)
}
unset table
set print
set xrange[-1:21]
plot $Data u 1:2 w lp pt 6 ps 2 lc rgb "black" t "original data",\
$DataResampled u 1:2 w lp pt 7 lc rgb "web-green" t "resampled with linear interpolation",\
$Dummy u 1:2 w impulses lc rgb "red" not
### end of code