# Smoothing interpolation for tachometer data

**URL:** <https://discourse.julialang.org/t/smoothing-interpolation-for-tachometer-data/56117>\
**Category:** New to Julia\
**Tags:** smoothing, splines\
**Created:** [February 26, 2021, 8:45pm UTC](https://discourse.julialang.org/t/smoothing-interpolation-for-tachometer-data/56117 "2021-02-26T20:45:59Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [February 28, 2021, 11:01am UTC](https://discourse.julialang.org/t/smoothing-interpolation-for-tachometer-data/56117/11 "2021-02-28T11:01:43Z")

</div>

In that case, here is a quick example showing how such a fit would be done in the current version of BSplineKit. I may add a higher level interface for this in the future.

```julia
using BSplineKit
using LinearAlgebra
using SparseArrays
using Random
using Plots

# Exact function
f(x) = exp(-x) * cos(8x)

# Locations where data is available (here is equispaced for simplicity)
xdata = 0:0.025:1

# Spline breakpoints (should be sparser than the data points)
xbreaks = 0:0.1:1

# Data with some random noise
ydata = map(x -> f(x) + 0.1 * randn(), xdata)

# Create B-spline basis of order 4 (cubic splines) with the chosen breakpoints
B = BSplineBasis(BSplineOrder(4), xbreaks)

# Construct spline from the given data
C = collocation_matrix(B, xdata, SparseMatrixCSC{Float64}) # evaluates basis functions at data points
coefs = qr(C) \ ydata # spline coefficients
fspline = Spline(B, coefs)

plot(f, 0, 1; label = "f(x)", xlabel = "x", linestyle = :dash)
scatter!(xdata, ydata; label = "data")
plot!(x -> fspline(x), 0, 1; label = "fit")

```

 ![data_fit](https://global.discourse-cdn.com/julialang/original/3X/5/1/5115a8f10dd701959c0e8d0efc2646d112f19af1.png)

---

_[View the full topic](https://discourse.julialang.org/t/smoothing-interpolation-for-tachometer-data/56117)._
