Sponsored Content
Top Forums Shell Programming and Scripting Linear Interpolation of CSV Columnar Data Post 303038314 by hrrruser on Friday 30th of August 2019 08:13:52 AM
Old 08-30-2019
Linear Interpolation of CSV Columnar Data

I am trying to perform linear interpolation on three columns in a CSV file.

When I run this code, I do not get any changes in the output (see Expected Output).

What am I doing wrong here?

Awk Code
Code:
'
    function interpolate(lat1, lon1, t1, lat2, lon2, t2,      i) {
        for (i = 1; i <= (t2 - t1); i++) {
            printf "%.2f,%.2f,%d\n",
                lat1 + i * (lat2 - lat1) / (t2 - t1),
                lon1 + i * (lon2 - lon1) / (t2 - t1),
                t1   + i
        }
    }
    NR <= 1 {print}
    NR >= 2 {interpolate(lat, lon, t, $1, $2, $3)}
    {lat = $1; lon = $2; t = $3}
' file.in.csv > file.out.csv

file.in.csv
22.70,-68.00,0
22.77,-68.13,1
22.83,-68.27,2
22.90,-68.40,3
22.97,-68.53,4
23.03,-68.67,5

Expected Output
22.70,-68.00,0
22.74,-68.07,0.5
22.77,-68.13,1
22.80,-68.20,1.5
22.83,-68.27,2
22.87,-68.34,2.5
22.90,-68.40,3
22.94,-68.47,3.5
22.97,-68.53,4
23.00,-68.60,4.5
23.03,-68.67,5
 

10 More Discussions You Might Find Interesting

1. Programming

LInear Addresses

Hi all, Even after reading many explanation the question still haunting me what's the difference between physical and linear addresses.Can we directly access physical addresses .If not then paging circuitry would have ensure contiguous physical addresses regardless of any linear addresses but this... (2 Replies)
Discussion started by: joshighanshyam
2 Replies

2. Shell Programming and Scripting

Converting to columnar output

Hi All, I need some help in script or awk to create a textfile. I have a directory where two subdirectories exists say A and B. Now I need to write a ".txt" file with well arranged(space wraped) three columns in it with the data as follows: 1st column:Serial number(which will increment... (4 Replies)
Discussion started by: Sreejith_VK
4 Replies

3. Shell Programming and Scripting

Linear data to column data..script help seeked

Hello Take a look at following lines. This is giving me an o/p all in one array where as i want the column to be printed.How can i do it? e.g I am getting: 1575028616...... whereas i want 1 5750 28616 I am writing this small piece and trying to get this column o/p in a CSV. ... (1 Reply)
Discussion started by: ak835
1 Replies

4. Shell Programming and Scripting

Formatting Data - CSV

I want to check whether if any column data has any + , - , = prefixed to it then convert it in such a form that in excel its not read as formula. echo "$DATA" | awk 'BEGIN { OFS="," } -F" " {print $1,$2,$3,$4,$5,$6,$7,$8.$9,$10,$11,$12}' (4 Replies)
Discussion started by: dinjo_jo
4 Replies

5. Shell Programming and Scripting

File Comparison Columnar?

I'm looking to build up a process which would compare 2 files and show difference, the difference needs to be done in such a way that it also shows which column value is differing. So i think of this. Run a diff between the files , then someway find which columns have different values need to... (6 Replies)
Discussion started by: dinjo_jo
6 Replies

6. Programming

Linear hashing implementation in C language

Hi, I'm looking for linear hashing implementation in C language. Please help. PS: I have implement this on Ubuntu 10.04 Linux on 64 bit machine. (1 Reply)
Discussion started by: sajjar
1 Replies

7. Shell Programming and Scripting

Converting variable space width data into CSV data in bash

Hi All, I was wondering how I can convert each line in an input file where fields are separated by variable width spaces into a CSV file. Below is the scenario what I am looking for. My Input data in inputfile.txt 19 15657 15685 Sr2dReader 107.88 105.51... (4 Replies)
Discussion started by: vharsha
4 Replies

8. Programming

Find gaps in time data and replace missing time value and column 2 value by interpolation in awk

Dear all, I am kindly seeking assistance on the following issue. I am working with data that is sampled every 0.05 hours (that is 3 minutes intervals) here is a sample data from the file 5.00000 15.5030 5.05000 15.6680 5.10000 16.0100 5.15000 16.3450 5.20000 16.7120 5.25000... (4 Replies)
Discussion started by: malandisa
4 Replies

9. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

10. Programming

Joining Columnar heading from 2 lines

Hi, Below is the format of a report generated by a custom reporting solution. I opened the report in Notepad++ and junked data and values as in the image below. https://www.unix.com/attachment.php?attachmentid=7907&stc=1&d=1575507708 I want to convert the values to a PIPE delimited format as... (2 Replies)
Discussion started by: ramkrix
2 Replies
math::interpolate(n)						 Tcl Math Library					      math::interpolate(n)

__________________________________________________________________________________________________________________________________________________

NAME
math::interpolate - Interpolation routines SYNOPSIS
package require Tcl ?8.4? package require struct package require math::interpolate ?1.0.2? ::math::interpolate::defineTable name colnames values ::math::interpolate::interp-1d-table name xval ::math::interpolate::interp-table name xval yval ::math::interpolate::interp-linear xyvalues xval ::math::interpolate::interp-lagrange xyvalues xval ::math::interpolate::prepare-cubic-splines xcoord ycoord ::math::interpolate::interp-cubic-splines coeffs x ::math::interpolate::interp-spatial xyvalues coord ::math::interpolate::interp-spatial-params max_search power ::math::interpolate::neville xlist ylist x _________________________________________________________________ DESCRIPTION
This package implements several interpolation algorithms: o Interpolation into a table (one or two independent variables), this is useful for example, if the data are static, like with tables of statistical functions. o Linear interpolation into a given set of data (organised as (x,y) pairs). o Lagrange interpolation. This is mainly of theoretical interest, because there is no guarantee about error bounds. One possible use: if you need a line or a parabola through given points (it will calculate the values, but not return the coefficients). A variation is Neville's method which has better behaviour and error bounds. o Spatial interpolation using a straightforward distance-weight method. This procedure allows any number of spatial dimensions and any number of dependent variables. o Interpolation in one dimension using cubic splines. This document describes the procedures and explains their usage. PROCEDURES
The interpolation package defines the following public procedures: ::math::interpolate::defineTable name colnames values Define a table with one or two independent variables (the distinction is implicit in the data). The procedure returns the name of the table - this name is used whenever you want to interpolate the values. Note: this procedure is a convenient wrapper for the struct::matrix procedure. Therefore you can access the data at any location in your program. string name (in) Name of the table to be created list colnames (in) List of column names list values (in) List of values (the number of elements should be a multiple of the number of columns. See EXAMPLES for more information on the interpretation of the data. The values must be sorted with respect to the independent variable(s). ::math::interpolate::interp-1d-table name xval Interpolate into the one-dimensional table "name" and return a list of values, one for each dependent column. string name (in) Name of an existing table float xval (in) Value of the independent row variable ::math::interpolate::interp-table name xval yval Interpolate into the two-dimensional table "name" and return the interpolated value. string name (in) Name of an existing table float xval (in) Value of the independent row variable float yval (in) Value of the independent column variable ::math::interpolate::interp-linear xyvalues xval Interpolate linearly into the list of x,y pairs and return the interpolated value. list xyvalues (in) List of pairs of (x,y) values, sorted to increasing x. They are used as the breakpoints of a piecewise linear function. float xval (in) Value of the independent variable for which the value of y must be computed. ::math::interpolate::interp-lagrange xyvalues xval Use the list of x,y pairs to construct the unique polynomial of lowest degree that passes through all points and return the interpo- lated value. list xyvalues (in) List of pairs of (x,y) values float xval (in) Value of the independent variable for which the value of y must be computed. ::math::interpolate::prepare-cubic-splines xcoord ycoord Returns a list of coefficients for the second routine interp-cubic-splines to actually interpolate. list xcoord List of x-coordinates for the value of the function to be interpolated is known. The coordinates must be strictly ascending. At least three points are required. list ycoord List of y-coordinates (the values of the function at the given x-coordinates). ::math::interpolate::interp-cubic-splines coeffs x Returns the interpolated value at coordinate x. The coefficients are computed by the procedure prepare-cubic-splines. list coeffs List of coefficients as returned by prepare-cubic-splines float x x-coordinate at which to estimate the function. Must be between the first and last x-coordinate for which values were given. ::math::interpolate::interp-spatial xyvalues coord Use a straightforward interpolation method with weights as function of the inverse distance to interpolate in 2D and N-dimensional space The list xyvalues is a list of lists: { {x1 y1 z1 {v11 v12 v13 v14}} {x2 y2 z2 {v21 v22 v23 v24}} ... } The last element of each inner list is either a single number or a list in itself. In the latter case the return value is a list with the same number of elements. The method is influenced by the search radius and the power of the inverse distance list xyvalues (in) List of lists, each sublist being a list of coordinates and of dependent values. list coord (in) List of coordinates for which the values must be calculated ::math::interpolate::interp-spatial-params max_search power Set the parameters for spatial interpolation float max_search (in) Search radius (data points further than this are ignored) integer power (in) Power for the distance (either 1 or 2; defaults to 2) ::math::interpolate::neville xlist ylist x Interpolates between the tabulated values of a function whose abscissae are xlist and whose ordinates are ylist to produce an esti- mate for the value of the function at x. The result is a two-element list; the first element is the function's estimated value, and the second is an estimate of the absolute error of the result. Neville's algorithm for polynomial interpolation is used. Note that a large table of values will use an interpolating polynomial of high degree, which is likely to result in numerical instabilities; one is better off using only a few tabulated values near the desired abscissa. EXAMPLES
TODO Example of using the cubic splines: Suppose the following values are given: x y 0.1 1.0 0.3 2.1 0.4 2.2 0.8 4.11 1.0 4.12 Then to estimate the values at 0.1, 0.2, 0.3, ... 1.0, you can use: set coeffs [::math::interpolate::prepare-cubic-splines {0.1 0.3 0.4 0.8 1.0} {1.0 2.1 2.2 4.11 4.12}] foreach x {0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0} { puts "$x: [::math::interpolate::interp-cubic-splines $coeffs $x]" } to get the following output: 0.1: 1.0 0.2: 1.68044117647 0.3: 2.1 0.4: 2.2 0.5: 3.11221507353 0.6: 4.25242647059 0.7: 5.41804227941 0.8: 4.11 0.9: 3.95675857843 1.0: 4.12 As you can see, the values at the abscissae are reproduced perfectly. BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category math :: interpolate of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. KEYWORDS
interpolation, math, spatial interpolation CATEGORY
Mathematics COPYRIGHT
Copyright (c) 2004 Arjen Markus <arjenmarkus@users.sourceforge.net> Copyright (c) 2004 Kevn B. Kenny <kennykb@users.sourceforge.net> math 1.0.2 math::interpolate(n)
All times are GMT -4. The time now is 11:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy