Resampling data with gnuplot

匆匆过客 提交于 2021-02-04 08:27:10

问题


In some cases you might need to resample your data. How can this be done platform-independently with gnuplot? Below is one attempt.

The dataset $Dummy contains the interpolated values, however, with a lot of unnecessary lines containing NaN. The dataset $DataResampled finally contains the desired data.

My question is: can this be done simpler?

The code:

### 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

The result:

来源:https://stackoverflow.com/questions/54362441/resampling-data-with-gnuplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!