Created
September 9, 2019 02:47
-
-
Save kaipyroami/6f72de9e442fbbd473e66ca4c36af243 to your computer and use it in GitHub Desktop.
This is a simple example of a 1 dimensional lookup table with linear interpolation in Go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a simple example of a 1 dimensional lookup table with linear interpolation. | |
package main | |
import ( | |
"fmt" | |
) | |
type table struct { | |
axis []float64 | |
values []float64 | |
} | |
func main() { | |
// Create slices for table | |
ax := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} | |
vals := []float64{0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000} | |
// Value to be used against lookup table. | |
// When axis = values then the table output should equal the input. | |
var inputVal float64 = 2.45 | |
// Create table from slices | |
//t := table{axis: a, values: v} | |
for i, v := range ax { | |
if v >= inputVal { | |
x1, x2 := ax[i-1], ax[i] | |
fmt.Println("Input:", inputVal) | |
fmt.Println("Value between: ", x1, x2) | |
// Lazily done... probably can be simplified. | |
output := (((inputVal-x1)/(x2-x1))*(vals[i]-vals[i-1]) + vals[i-1]) | |
fmt.Println("The output value is: ", output) | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment