;; Demo of Spline interpolation using GSL.
;; Derived from an example in chapter 25 of the GSL book.

(libload "gsl/aux_globalvar")
(libload "gsl/aux_structure")
(libload "gsl/interpolation")

;; setup graphic window and plotting
(new-window 500 300)
(color-rgb 0 0 0)
(setup-axes 0 0 10 10 1 1 "GSL Spline Demo" closed-circle)

;; synthesize 10 sample points
(setq numpoints 10)
(setq x (double-matrix numpoints))
(setq y (double-matrix numpoints))
(for (i 0 (- numpoints 1))
     (x i (+ i (* 0.5 (sin i))))
     (y i (+ i (cos (* i i)))))

;; plot sample points
(color-rgb 1 0 0)
(idx-bloop ((xx x) (yy y)) (plt-plot (xx) (yy)))

;; build spline evaluator from sample points
(setq acc (gsl_interp_accel_alloc ))
(setq spline (gsl_spline_alloc  (var_gsl_interp_cspline) numpoints))
(gsl_spline_init spline (idx-ptr x) (idx-ptr y) numpoints)

;; evaluate and plot spline
(color-rgb 0 0 1)
(plt-move 0 (gsl_spline_eval spline 0 acc))
(for (xi 0.1 (- numpoints 0.5) 0.1)
     (plt-draw xi (gsl_spline_eval spline xi acc)))

(gsl_spline_free   spline)
(gsl_interp_accel_free  acc)

