我有数据插值问题。我有大量的相位图的x值和y值,因此最终形成螺旋状。我尝试在下面生成一个类似的,简单的例子(其价值较少,因此有点"前卫":
2.5 2
2 1
1.5 1.5
1.25 2.25
1.5 3
2 3.5
3 3
4 1.5
3 0.5
2 0
1 0.5
0 1.75
使用plot "temp.dat" using 1:2 with linespoints
会为您绘制地图。为了进一步处理,我需要所选x值的所有y值。问题是,当然我的数据不包含随机选择的x值的y值,因为它是离散数据。对于上述数据:假设我想要x值为1.75的所有y值。我想对相邻数据点进行线性插值,以获得感兴趣的三个y值(在我的例子中将是0.2,1.2和3.2左右)。
这该怎么做?理想情况下,我希望将所有y值写入输出文件。
非常感谢您的专业建议
答案 0 :(得分:2)
Christoph说,这与gnuplot无关。 然而,它可以做到,幸运的是,我最近做了类似的事情。您使用plot
处理文件,然后查找从大于x值的点到较小点(反之亦然)的转换,并存储相应的y值。
代码如下(假设您的数据位于名为“data”的文件中):
# Give your x value you're interested in
x0 = 1.75
# Initialize some variables and including first point values
n=0; val=""; xlast = 2.5; ylast = 2.
#
# Process data and store number of accourrences in "n"
# and their interpolated values in "val"
plot "data" u ($1 >= x0 && xlast < x0 || $1 <= x0 && xlast > x0 ? \
(n=n+1, val=sprintf("%s %g",val,ylast+($2-ylast)/($1-xlast)*(x0-xlast)), \
$1) : $1) : (xlast=$1, ylast=$2, $2) w l not
上面的代码为您提供了螺旋式
但它还会创建n
和val
,其中包含出现次数及其值,您可以查看:
gnuplot> print n
3
gnuplot> print val
1.25 3.25 0.125
将其保存到文件中,您可以将其与图形一起绘制以进行可视化:
set print "data2"
print val
unset print
plot "data" w l not, \
for [i=1:words(val)] "data2" u (x0):(column(i)) pt 7 lc 3 not
答案 1 :(得分:1)
以下是使用gnuplot执行此操作的方法:
tempfile = 'intersections.txt'
set table tempfile
x0 = x1 = y0 = y1 = 0
xval = 1.75
plot 'data.txt' using (x0 = x1, y0 = y1, x1 = $1, y1 = $2, ((x0 < xval && x1 > xval) || (x0 > xval && x1 < xval)) && $0 > 0 ? xval : 1/0):(((x0 < xval && x1 > xval) || (x0 > xval && x1 < xval)) && $0 > 0 ? y0 + (xval - x0)/(x1 - x0) * (y1 - y0) : 1/0)
unset table
plot 'data.txt' using 1:2 with lines, tempfile using 1:(strcol(3) eq 'i' ? $2 : 1/0) with points pt 7 ps 2 title sprintf('x = %.3f', xval)