# 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:** 21

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 2, 2021, 6:13pm UTC](https://discourse.julialang.org/t/smoothing-interpolation-for-tachometer-data/56117/21 "2021-03-02T18:13:01Z")

</div>

See below results for the pure Julia packages:  
(1) BSplineKit.jl’s nice solution given by @jipolanco  
(2) SmoothingSplines.jl  
(3) Loess.jl suggested by @marius311

They work relatively well for your dataset with following caveats:  
(1) allows defining the number of knots but is a bit less smooth than (2)  
(2) allows defining the smoothness (_all input data abscissas become knots_)  
(3) struggles to follow the data at start and end locations

The best is to use [InspectDR.jl](https://github.com/ma-laforge/InspectDR.jl) Plots’ backend to interactively zoom and inspect the results.

```julia
x1 = midtime1[:] # imported from Matlab *.mat file using MAT.jl
y1 = rotationalspeed1[:] # imported from Matlab *.mat file using MAT.jl

using SmoothingSplines
# REINSCH, C. [1967] Smoothing by Spline Functions. Numerische Mathematik 10: 177-183
# http://eudml.org/doc/131782
spl = fit(SmoothingSpline, x1, y1, 1.0) # λ=1.0
ys1 = SmoothingSplines.predict(spl,x1) # fitted vector

using BSplineKit, LinearAlgebra, SparseArrays
# Create B-spline basis of order 4 (cubic splines) with chosen breakpoints
xbreaks = LinRange(extrema(x1)..., 200)
B = BSplineBasis(BSplineOrder(4), xbreaks)
# Construct spline from the given data by @jipolanco
C = collocation_matrix(B, x1, SparseMatrixCSC{Float64}) # evaluates basis functions at data points
coefs = qr(C) \ y1 # spline coefficients
fspline = Spline(B, coefs)

using Loess
model = loess(x1, y1, span=0.02)
yl1 = Loess.predict(model, x1)

using Plots; inspectdr()
inspectdr(size=(1000,800), lw=3, markerstrokewidth=0, ms=7, legend=:top)
Plots.scatter(x1, y1, mc=:blue,label="rotationalspeed1")
Plots.plot!(x1, ys1, lc=:red,label="(Cubic) SmoothingSpline.jl")
Plots.plot!(x1, fspline.(x1); lc=:cyan,ls=:dash, label = "Cubic BSplineKit.jl by@jipolanco")
Plots.plot!(x1, yl1, ls=:dot, lc=:black,label="Loess.jl")

```

Whole tachometer signal:

 ![Tachometer_BSplineKit_vs_SmoothingSpline_vs_Loess](https://global.discourse-cdn.com/julialang/original/3X/a/7/a7f60f5a8cc30d13adb5965bdd22a041f3986e5c.png)  
Zoom-in at start:  
 ![Tachometer_Start](https://global.discourse-cdn.com/julialang/original/3X/8/1/81cf4d5cd2365a7f0b08817c863b4a5d0646f194.png)  
Zoom-in at peak:  
 ![Tachometer_peak](https://global.discourse-cdn.com/julialang/original/3X/2/5/25c7c49fc46e614aa91f2a210ce4ffafc459fda1.png)  
Zoom-in at end:  
 ![Tachometer_End](https://global.discourse-cdn.com/julialang/original/3X/4/e/4eb5f706b1b2e278b26cf948011dfd6d55eb753f.png)

---

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