[SWIFT] Filter the fluctuations of raw data

Significance of filtering

The sensor is noisy. The data may suddenly bounce. Humans are also very analog. Even if you think you are holding your finger, the data will fluctuate.

If this is used digitally as it is, it will result in a very unstable system. Therefore, it is important to have a filter that prepares the raw data.

filter

Use the Exponential Moving Average (EMA) filter. [(Reference)](https://en.wikipedia.org/wiki/%E7%A7%BB%E5%8B%95%E5%B9%B3%E5%9D%87#%E6%8C%87% E6% 95% B0% E7% A7% BB% E5% 8B% 95% E5% B9% B3% E5% 9D% 87)

S_t = α \times Y_{t-1} + (1-α) \times S_{t-1}

The larger the value of α, the smaller the weight of the old value. And vice versa.

Low pass filter

Attenuates momentary fluctuations. You can get data that suppresses buzzing.

var lastVal: CGFloat = 0.0
var alpha: CGFloat = 0.4

func lowpass(val: CGFloat) -> CGFloat {
  lastVal = alpha * val + lastVal * (1 - alpha)  
  return lastVal
}

High pass filter

Only the momentary fluctuation component is used. You can get the data when it changes suddenly.

var lastVal: CGFloat = 0.0
var alpha: CGFloat = 0.4

func highpass(val: CGFloat) -> CGFloat {
  lastVal = alpha * val + lastVal * (1 - alpha)
  return val - lastVal
}

Recommended Posts

Filter the fluctuations of raw data
Filter the result of BindingResult [Spring]
What is the data structure of ActionText?
The contents of the data saved by CarrierWave.
[Order method] Set the order of data in Rails
Add empty data to the top of the list
The world of clara-rules (2)
The world of clara-rules (4)
The world of clara-rules (1)
Until the use of Spring Data and JPA Part 2
Rails6: Input the initial data of ActionText using seed
The world of clara-rules (5)
Until the use of Spring Data and JPA Part 1
The idea of quicksort
The idea of jQuery
[Enum] Let's improve the readability of data by using rails enum
The story of throwing BLOB data from EXCEL in DBUnit
[Java] Simplify the implementation of data history management with Reladomo
Docker monitoring-explaining the basics of basics-
About the description of Docker-compose.yml
Understand the basics of docker
The play of instantiating java.lang.Void
Explanation of the FizzBuzz problem
The basics of Swift's TableView
Median of the three values
The illusion of object orientation
Switch the version of bundler
See the behavior of entity update with Spring Boot + Spring Data JPA