数据文件的样本线性插值 [英] Sample linear interpolation of data file

查看:157
本文介绍了数据文件的样本线性插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个具有xy值的数据文件example.dat

I have a data file example.dat with xy values, for example

0 10
1 40
5 20

如何在gnuplot中对这些点的线性插值进行采样?我想使用set table将该采样存储在另一个文件output.dat中.使用三次样条平滑可以做到

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

产生三次样条插值的等距采样,具有10个点.但是我发现没有办法通过线性插值进行等距采样:采样率只是被忽略(gnuplot 5.0).

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).

我尝试不使用任何选项并且使用线性插值平滑"(如smooth unique),希望gnuplot将gnuplot认为数据集是可以采样的函数,但无济于事.

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.

我的应用程序正在公共网格上对不同的数据文件进行采样,以供以后进行比较.我知道这正在突破gnuplot的目标范围,但是由于已经有了一种采样机制,所以我想知道我是否只是缺少某些东西.

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.

推荐答案

如果仍然有兴趣的话,以下是仅限gnuplot"解决方案.不太优雅,但似乎可以正常工作.

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

这篇关于数据文件的样本线性插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆