Dec 11, 2019

[Arduino code] First order Low pass filter (LPF) (아두이노에서 구현한 1차 저역 통과 필터)

- Simple Arduino code to implement a 1st Low pass filter (LPF)
- 1차 저역 통과 필터를 구현한 간단한 아두이노 코드

double pre_value_1 = 0;
double pre_value_2 = 0;
double pre_time = 0;
//Set the LPF parameter
double tau_1 = 30;
double tau_2 = 100;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Low pass filter starts!");
}

void loop() {
  // put your main code here, to run repeatedly:
  double st_time = millis();
  double dt = (st_time - pre_time);   
  //cur_value: measured value from a sensor (센서에서 측정된 값)
  double cur_value = analogRead(A0);

  double af_value_1, af_value_2;
  
  af_value_1 = tau_1/(tau_1+dt)*pre_value_1 + dt/(tau_1+dt)*cur_value;
  af_value_2 = tau_2/(tau_2+dt)*pre_value_2 + dt/(tau_2+dt)*cur_value;
  // cur_value vs af_value_1 vs af_value_2
  Serial.print(cur_value); Serial.print("\t");
  Serial.print(af_value_1); Serial.print("\t");
  Serial.println(af_value_2);

  pre_value_1 = af_value_1;
  pre_value_2 = af_value_2;
  pre_time = st_time; 

} 
<Snap shot of Arduino serial plotter>

[blue line: cur_value(raw data), red line: af_value_1(tau_1:30), green line: af_value_2(tau_2:100)]


As tau parameter increases, noise decreases but a time delay occurs.
tau 가 커질수록 노이즈는 줄어들지만 시간 지연 현상이 발생한다.

1 comment: