Engee documentation
Notebook

Removing high frequency noise from gyroscope data

This example shows how to remove high-frequency emissions from streaming data using the MedianFilter block from the EngeeDSP library.

A median filter is a non-linear digital filtering technique often used to remove noise from an image or signal.

The implementation of this algorithm starts by connecting the libraries:

In [ ]:
Pkg.add(["DelimitedFiles"])
   Resolving package versions...
   Installed MPItrampoline_jll ── v5.5.2+0
   Installed IntervalArithmetic ─ v0.22.23
   Installed Documenter ───────── v1.8.1
  No Changes to `~/.project/Project.toml`
  No Changes to `~/.project/Manifest.toml`
In [ ]:
import .EngeeDSP
using DelimitedFiles
using Plots
plotlyjs()
Out[0]:
Plots.PlotlyJSBackend()

Loading a signal with high-frequency noise from the JL file is the Julia source code file.

In [ ]:
matrix = readdlm("$(@__DIR__)/data.jl", '\t', Float64, '\n');

Encoding the file into 714 samples so that each column of data contains 10 frames.

In [ ]:
In1 = map(Iterators.partition(axes(matrix,1), 714)) do cols
    matrix[cols,:]
end 
Out[0]:
10-element Vector{Matrix{Float64}}:
 [-122.0 -13.0 3.0; -101.0 17.0 -4.0; … ; -109.0 12.0 1.0; -104.0 8.0 -4.0]
 [-99.0 18.0 -7.0; -92.0 19.0 15.0; … ; -110.0 22.0 -7.0; -113.0 21.0 -3.0]
 [-89.0 9.0 9.0; -112.0 27.0 -7.0; … ; -99.0 16.0 5.0; -94.0 23.0 9.0]
 [-94.0 23.0 9.0; -94.0 23.0 -7.0; … ; -113.0 10.0 -2.0; -106.0 4.0 6.0]
 [-91.0 -2.0 -6.0; -108.0 10.0 5.0; … ; -91.0 16.0 0.0; -91.0 16.0 0.0]
 [-111.0 8.0 3.0; -111.0 -2.0 4.0; … ; -105.0 13.0 9.0; -99.0 25.0 -2.0]
 [-99.0 25.0 -2.0; -102.0 7.0 7.0; … ; -101.0 14.0 6.0; -107.0 24.0 14.0]
 [-97.0 8.0 9.0; -95.0 1.0 14.0; … ; -112.0 10.0 19.0; -101.0 1.0 -2.0]
 [-91.0 1.0 28.0; -107.0 18.0 6.0; … ; -109.0 -3.0 3.0; -106.0 12.0 -5.0]
 [-106.0 12.0 -5.0; -112.0 5.0 9.0; … ; -98.0 6.0 28.0; -120.0 6.0 28.0]

Implementing filtering, the system uses a window length of 10.

In [ ]:
var_median_fir = EngeeDSP.MedianFilter(10) # WindowLength = 10
EngeeDSP.setup!( var_median_fir, In1[1] )
Out1 = In1
for i = 1:10 # Обработка сигнала в цикле.
    global  gyroData = In1[i]
    global filteredData = EngeeDSP.step!(var_median_fir,gyroData)
    Out1[i] = filteredData
end
Out1 = vcat(Out1...)
Out[0]:
7140×3 Matrix{Float64}:
    0.0   0.0    0.0
    0.0   0.0    0.0
    0.0   0.0    0.0
    0.0   0.0    0.0
  -49.0   0.0    0.0
  -98.0   0.0   -0.5
  -99.5   0.0   -2.5
  -99.5   1.0   -2.5
 -101.0   3.0   -5.0
 -107.0   5.5   -5.5
 -103.5   6.5   -7.5
 -108.0   6.5  -10.5
 -110.0   7.0  -12.0
    ⋮          
 -110.5  12.0    9.0
 -110.0  13.0   12.0
 -110.0  14.0   12.5
 -110.0  14.0   12.5
 -112.0  14.0   12.5
 -112.0  16.0   12.5
 -110.0  16.0   12.5
 -110.0  14.0   10.0
 -110.0  14.0    8.0
 -110.0  14.0    7.0
 -105.0  10.5    7.0
 -107.0   7.0    7.0

Create a graph of the object to view the filtered output and compare it to the input signal with noise.

In [ ]:
plot( [matrix[:,3],Out1[:,3]], ylims = [-300, 300], label=["Входной сигнал" "Отфильтрованный выход"], legendfontsize=6)
Out[0]:

Conclusion

In this example we have demonstrated the possibilities of using in the Engee development environment additional libraries that extend the functionality of the environment, namely the EngeeDSP library and the MedianFilter implemented in it.