Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. // This is a simple example of a 1 dimensional lookup table with linear interpolation.
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. )
  7.  
  8. type table struct {
  9. axis []float64
  10. values []float64
  11. }
  12.  
  13. func main() {
  14.  
  15. // Create slices for table
  16. ax := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  17. vals := []float64{0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000}
  18.  
  19. // Value to be used against lookup table.
  20. // When axis = values then the table output should equal the input.
  21. var inputVal float64 = 2.45
  22.  
  23. // Create table from slices
  24. //t := table{axis: a, values: v}
  25.  
  26. for i, v := range ax {
  27. if v >= inputVal {
  28. x1, x2 := ax[i-1], ax[i]
  29. fmt.Println("Input:", inputVal)
  30. fmt.Println("Value between: ", x1, x2)
  31.  
  32. // Lazily done... probably can be simplified.
  33. output := (((inputVal-x1)/(x2-x1))*(vals[i]-vals[i-1]) + vals[i-1])
  34. fmt.Println("The output value is: ", output)
  35. break
  36. }
  37.  
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement