Computing for linearly-interpolated values using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Computing for linearly-interpolated values using awk
# 1  
Old 01-18-2013
Computing for linearly-interpolated values using awk

Hi,

I want to compute for linearly-interpolated values for my data using awk, any help is highly appreciated.

How do I apply the linear interpolation formula to my data in awk given the equation below:
Code:
x   y
15  0
25  0.1633611
35  0.0741623

desired output: linear interpolation at 0.1 interval
x        y_interpolated
15       0
15.1     0.001633611
15.2     0.003267222
15.3 ..  0.004900833 
24.9     0.161727489
25       0.1633611

the interpolated values (yn) were calculated using the ff. equation

y_n=((x_n-x_1)*(y_2 - y_1)/x_2 - x_1) + y_1

y_15.1=((15.1-15)*(0.1633611-0)/25-15)/0

Thank you very much.
# 2  
Old 01-18-2013
Code:
$ cat interp.awk

(STEP=="") { STEP=1 } U {
        D=(U<$1)?1:-1;
        M=((V-$2)/(U-$1)) * D * STEP;

        while( ((-D)*((U+=(D*STEP)) - $1)) > (STEP/2))
                print U, V += M;
}

{ U=$1+0; V=$2+0; $1=$1 } 1

$ cat data

15  0
25  0.1633611
35  0.0741623
15  0

$ awk -f interp.awk STEP=0.2 OFS="\t" data
15      0
15.2    0.00326722
15.4    0.00653444
15.6    0.00980167
15.8    0.0130689
16      0.0163361
16.2    0.0196033
16.4    0.0228706
16.6    0.0261378
16.8    0.029405
17      0.0326722
17.2    0.0359394
17.4    0.0392067
17.6    0.0424739
17.8    0.0457411
18      0.0490083
18.2    0.0522756
18.4    0.0555428
18.6    0.05881
18.8    0.0620772
19      0.0653444
19.2    0.0686117
19.4    0.0718789
19.6    0.0751461
19.8    0.0784133
20      0.0816806
20.2    0.0849478
20.4    0.088215
20.6    0.0914822
20.8    0.0947494
21      0.0980167
21.2    0.101284
21.4    0.104551
21.6    0.107818
21.8    0.111086
22      0.114353
22.2    0.11762
22.4    0.120887
22.6    0.124154
22.8    0.127422
23      0.130689
23.2    0.133956
23.4    0.137223
23.6    0.140491
23.8    0.143758
24      0.147025
24.2    0.150292
24.4    0.153559
24.6    0.156827
24.8    0.160094
25      0.1633611
25.2    0.161577
25.4    0.159793
25.6    0.158009
25.8    0.156225
26      0.154441
26.2    0.152657
26.4    0.150873
26.6    0.149089
26.8    0.147305
27      0.145521
27.2    0.143737
27.4    0.141953
27.6    0.140169
27.8    0.138385
28      0.136601
28.2    0.134817
28.4    0.133034
28.6    0.13125
28.8    0.129466
29      0.127682
29.2    0.125898
29.4    0.124114
29.6    0.12233
29.8    0.120546
30      0.118762
30.2    0.116978
30.4    0.115194
30.6    0.11341
30.8    0.111626
31      0.109842
31.2    0.108058
31.4    0.106274
31.6    0.10449
31.8    0.102706
32      0.100922
32.2    0.099138
32.4    0.097354
32.6    0.09557
32.8    0.093786
33      0.0920021
33.2    0.0902181
33.4    0.0884341
33.6    0.0866501
33.8    0.0848662
34      0.0830822
34.2    0.0812982
34.4    0.0795142
34.6    0.0777303
34.8    0.0759463
35      0.0741623
34.8    0.0734207
34.6    0.0726791
34.4    0.0719374
34.2    0.0711958
34      0.0704542
33.8    0.0697126
33.6    0.0689709
33.4    0.0682293
33.2    0.0674877
33      0.0667461
32.8    0.0660044
32.6    0.0652628
32.4    0.0645212
32.2    0.0637796
32      0.063038
31.8    0.0622963
31.6    0.0615547
31.4    0.0608131
31.2    0.0600715
31      0.0593298
30.8    0.0585882
30.6    0.0578466
30.4    0.057105
30.2    0.0563633
30      0.0556217
29.8    0.0548801
29.6    0.0541385
29.4    0.0533969
29.2    0.0526552
29      0.0519136
28.8    0.051172
28.6    0.0504304
28.4    0.0496887
28.2    0.0489471
28      0.0482055
27.8    0.0474639
27.6    0.0467222
27.4    0.0459806
27.2    0.045239
27      0.0444974
26.8    0.0437558
26.6    0.0430141
26.4    0.0422725
26.2    0.0415309
26      0.0407893
25.8    0.0400476
25.6    0.039306
25.4    0.0385644
25.2    0.0378228
25      0.0370812
24.8    0.0363395
24.6    0.0355979
24.4    0.0348563
24.2    0.0341147
24      0.033373
23.8    0.0326314
23.6    0.0318898
23.4    0.0311482
23.2    0.0304065
23      0.0296649
22.8    0.0289233
22.6    0.0281817
22.4    0.0274401
22.2    0.0266984
22      0.0259568
21.8    0.0252152
21.6    0.0244736
21.4    0.0237319
21.2    0.0229903
21      0.0222487
20.8    0.0215071
20.6    0.0207654
20.4    0.0200238
20.2    0.0192822
20      0.0185406
19.8    0.017799
19.6    0.0170573
19.4    0.0163157
19.2    0.0155741
19      0.0148325
18.8    0.0140908
18.6    0.0133492
18.4    0.0126076
18.2    0.011866
18      0.0111243
17.8    0.0103827
17.6    0.0096411
17.4    0.00889948
17.2    0.00815785
17      0.00741623
16.8    0.00667461
16.6    0.00593298
16.4    0.00519136
16.2    0.00444974
16      0.00370812
15.8    0.00296649
15.6    0.00222487
15.4    0.00148325
15.2    0.000741623
15      0

$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-19-2013
Thanks so much, it worked perfectly.

---------- Post updated at 08:16 PM ---------- Previous update was at 01:59 AM ----------

Hi again, just a quick question. how can I print the output from the interp.awk in 2 decimal places? Thanks much.
# 4  
Old 01-19-2013
If you are using print, use OFMT='%.2f'.
Code:
awk -f interp.awk STEP=0.2 OFS="\t" OFMT='%.2f' data

Or, you may use printf.
These 2 Users Gave Thanks to elixir_sinari For This Post:
# 5  
Old 01-19-2013
Very subtle. You fixed my program without changing a single line of it. Neat trick!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

2. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

3. Shell Programming and Scripting

Computing the ratio of similar columns in the two files using awk script

Thanks Bartus11 for your help in the following code to compare the two files "t1" and "t2". awk 'NR==FNR{a=1;next}$2 in a{print $2}' t1 t2 First can anyone explain that what is the purpose of assigning a =1? Second, the current script is printing out the matched columns between the... (4 Replies)
Discussion started by: coder83
4 Replies

4. Shell Programming and Scripting

Computing average values from multiple text files

Hi, first, I have searched in the forum for this, but I could not find the right answer. (There were some similar threads, but I was not sure how to adapt the ideas.) Anyway, I have a quite natural problem: Given are several text files. All files contain the same number of lines and the same... (3 Replies)
Discussion started by: rbredereck
3 Replies

5. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

6. Shell Programming and Scripting

Computing data in awk

Hello, I am a newbie in programing. I want to compute the following in awk. I have the following data file: ID Value1 Value2 Value3 sade 0.21 0.45 23 foly 0.31 0.34 43 jude 0.40 0.11 63 jude 0.53 0.32 34 sade 0.67 0.49 66 foly 0.30 0.20 56 I want to take an ID “sade” , then take its... (6 Replies)
Discussion started by: ubeejani
6 Replies

7. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

8. Virtualization and Cloud Computing

Event Cloud Computing - IBM Turning Data Centers Into ?Computing Cloud?

Tim Bass Thu, 15 Nov 2007 23:55:07 +0000 *I predict we may experience less*debates*on the use of the term “event cloud”*related to*CEP in the future, now that both IBM and Google* have made announcements about “cloud computing” and “computing cloud”, IBM Turning Data Centers Into ‘Computing... (0 Replies)
Discussion started by: Linux Bot
0 Replies
Login or Register to Ask a Question