Reputation: 97
I have an issue with data interpolation. I have a huge amount of x- and y-values of a phase map, therefore ending up in a spiral-like shape. I tried to generate a similar, easy example below (which has way less values and is therefore a bit "edgy":
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
Using plot "temp.dat" using 1:2 with linespoints
will plot you the map. For further processing I need all y-values for a chosen x-value. The problem is, that of course my data doesn't contain the y-values for a randomly chosen x-value since it is discrete data. For the above data: Let's say I want all y-values for the x-value of 1.75. I would like to linear interpolate the neighbouring data points in order to get the three y-values of interest (would be something around 0.2, 1.2, and 3.2 in my example).
How to do this? Ideally I want all the y-values written into an output file.
Many thanks in advance for your professional advice
Upvotes: 1
Views: 1856
Reputation: 7627
As Christoph says, this has nothing to do with gnuplot. However, it can be done and luckily enough I have recently done something similar. You use plot
to process the file and then look for a transition from a point greater than your x value to a point smaller (and viceversa) and store the corresponding y value.
The code is as follows (assuming your data is in a file called "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
The above code gives you your spiral
but it also creates n
and val
with number of occurrences and their values, which you can check:
gnuplot> print n
3
gnuplot> print val
1.25 3.25 0.125
Saving this to a file you can plot it together with your figure for visualization:
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
Upvotes: 2
Reputation: 48390
Here is how you can do this with 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)
Upvotes: 2