Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement