Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

linear interpolation - Resampling data with gnuplot

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%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: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...