attila
October 29, 2013, 8:58am
1
Dear All,
I have input like this,
infile:
10 464310.16
20 464309.44
30 464309.02
40 464316.93
...
...
Desired output per step:
out_step01:
10 464310.16
11
12
13
14
15
16
17
18
19
20 464309.44
21
22
23
24
25
26
27
28
29
30 464309.02
31
32
33
34
35
36
37
38
39
40 464316.93
...
...
algorithm: expand first column with increment 1,
And then final output
out_final:
10 464310.16
11 464310.09
12 464310.02
13 464309.94
14 464309.87
15 464309.80
16 464309.73
17 464309.66
18 464309.58
19 464309.51
20 464309.44
21 464309.40
22 464309.36
23 464309.31
24 464309.27
25 464309.23
26 464309.19
27 464309.15
28 464309.10
29 464309.06
30 464309.02
31 464309.81
32 464310.60
33 464311.39
34 464312.18
35 464312.98
36 464313.77
37 464314.56
38 464315.35
39 464316.14
40 464316.93
...
...
algorithm: interpolation between the known values.
I have tried using Excel but it is very time-consuming.
Excel:
Expand -> manual
Interpolation -> = (A10-A01) / (ROW (A10)-ROW (A01))
with A10 and A01 are known values (column 2)
Thanks for advance,
Attila
Hi,
Here is the step1st Output, will make code for step2 and update you.
awk 'a=$1;b=$2 {for(i=1;i<=9;i++) print a+i}' file_name
10 464310.16
11
12
13
14
15
16
17
18
19
20 464309.44
21
22
23
24
25
26
27
28
29
30 464309.02
31
32
33
34
35
36
37
38
39
40 464316.93
41
42
43
44
45
46
47
48
49
Thanks,
R. Singh
---------- Post updated at 08:32 AM ---------- Previous update was at 08:23 AM ----------
Hello,
Could you please give an example for step2nd please, it will be easier for us to do it then.
Thanks,
R. Singh
attila
October 29, 2013, 9:35am
3
Desired output
10 464310.16
11 464310.09
12 464310.02
13 464309.94
14 464309.87
15 464309.80
16 464309.73
17 464309.66
18 464309.58
19 464309.51
20 464309.44
21 464309.40
22 464309.36
23 464309.31
24 464309.27
25 464309.23
26 464309.19
27 464309.15
28 464309.10
29 464309.06
30 464309.02
31 464309.81
32 464310.60
33 464311.39
34 464312.18
35 464312.98
36 464313.77
37 464314.56
38 464315.35
39 464316.14
40 464316.93
...
...
Not sure what type of interpolation you are after but linear it is not...
Please be more specific...
attila
October 29, 2013, 9:52am
5
wisecracker , you are right. It is linear interpolation between two point (red).
---------- Post updated at 08:52 AM ---------- Previous update was at 08:49 AM ----------
10 464310.16 <= known value
11 464310.09 <=result of linier interpolation
12 464310.02 <=result of linier interpolation
13 464309.94 <=result of linier interpolation
14 464309.87 <=result of linier interpolation
15 464309.80 <=result of linier interpolation
16 464309.73 <=result of linier interpolation
17 464309.66 <=result of linier interpolation
18 464309.58 <=result of linier interpolation
19 464309.51 <=result of linier interpolation
20 464309.44 <= known value
21 464309.40 <=result of linier interpolation
22 464309.36 <=result of linier interpolation
23 464309.31 <=result of linier interpolation
24 464309.27 <=result of linier interpolation
25 464309.23 <=result of linier interpolation
26 464309.19 <=result of linier interpolation
27 464309.15 <=result of linier interpolation
28 464309.10 <=result of linier interpolation
29 464309.06 <=result of linier interpolation
30 464309.02 <= known value
31 464309.81 <=result of linier interpolation
32 464310.60 <=result of linier interpolation
33 464311.39 <=result of linier interpolation
34 464312.18 <=result of linier interpolation
35 464312.98 <=result of linier interpolation
36 464313.77 <=result of linier interpolation
37 464314.56 <=result of linier interpolation
38 464315.35 <=result of linier interpolation
39 464316.14 <=result of linier interpolation
40 464316.93 <= known value
...
...
But what you show is NOT linear...
Subtract 12 from 11 gives 0.07.
BUT...
Subtract 13 from 12 gives 0.06.
This occurs more than once in all of your subsections...
Do I take it you are rounding down per iteration per subsection?
attila
October 29, 2013, 10:17am
7
I have tried in Excel using this algorithm:
---------- Post updated at 09:17 AM ---------- Previous update was at 09:14 AM ----------
Intruction when using Excel:
To create a sample linear interpolation formula, follow these steps:
Type the following values in a worksheet:
text A1: 9 B1: =(A7-A1)/(ROW(A7)-ROW(A1)) A2: =A1+$B$1 A3: A4: A5: A6: A7: 11
Select cells A2:A6. On the Edit menu, point to Fill, and then click Down. The formula is filled down, and the following values are displayed in cells A2:A6:
text A2: 9.33333 A3: 9.66667 A4: 10. A5: 10.33333 A6: 10.66667
Try
Let me know if it's helpful for you
$ cat file
10 464310.16
20 464309.44
30 464309.02
40 464316.93
$ cat int.sh
#!/bin/bash
awk '
{
x[FNR]=$1
y[FNR]=$2
}
END{
for(i=1;i<=NR;i++){
a=0
x1=x;x3=x[i+1]
y1=y;y3=y[i+1]
for(j=1;j<=x3-x1+1;j++){
if((x2=x1+a) <= x3 ){
print x2,sprintf("%.2f",y1+(x2-x1)*(y3-y1)/(x3-x1))
a++
}
}
}
}' file
$ bash int.sh
Resulting
10 464310.16
11 464310.09
12 464310.02
13 464309.94
14 464309.87
15 464309.80
16 464309.73
17 464309.66
18 464309.58
19 464309.51
20 464309.44
21 464309.40
22 464309.36
23 464309.31
24 464309.27
25 464309.23
26 464309.19
27 464309.15
28 464309.10
29 464309.06
30 464309.02
31 464309.81
32 464310.60
33 464311.39
34 464312.18
35 464312.97
36 464313.77
37 464314.56
38 464315.35
39 464316.14
40 464316.93
2 Likes
RudiC
October 29, 2013, 4:22pm
9
Trying to remember analytical geometry ... quite some time ago ...
awk ' {X[NR]=$1; Y[NR]=$2}
NR==1 {next}
{a=(Y[NR]-Y[NR-1])/(X[NR]-X[NR-1]) # calculate linear slope
Y0=Y[NR-1]-a*X[NR-1] # calc. Y axis intercept
for (x=X[NR-1]; x<X[NR]; x++) # step through abscissa values
{y=Y0+a*x; printf "%d\t%8.2f\n", x,y} # calc. function value; print
}
END {printf "%d\t%8.2f\n", $1, $2} # print final X,Y point
---------- Post updated at 21:22 ---------- Previous update was at 21:17 ----------
EDIT: Actually, arrays not needed:
awk ' {XL=X; YL=Y; X=$1; Y=$2}
NR==1 {next}
{a=(Y-YL)/(X-XL) # calculate linear slope
Y0=YL-a*XL # calc. Y axis intercept
for (x=XL; x<X; x++) # step through abscissa values
{y=Y0+a*x; printf "%d\t%8.2f\n", x,y} # calc. function value; print
}
END {printf "%d\t%8.2f\n", $1, $2} # print final X,Y point
' file
1 Like
attila
October 29, 2013, 9:29pm
10
Akshay Hegde ,
I added code "uniq " to end of your code.
#!/bin/bash
awk '
{
x[FNR]=$1
y[FNR]=$2
}
END{
for(i=1;i<=NR;i++){
a=0
x1=x;x3=x[i+1]
y1=y;y3=y[i+1]
for(j=1;j<=x3-x1+1;j++){
if((x2=x1+a) <= x3 ){
print x2,sprintf("%.2f",y1+(x2-x1)*(y3-y1)/(x3-x1))
a++
}
}
}
}' file | uniq > output
Problem solve, thanks.