본문 바로가기

카테고리 없음

LPF(Low Pass Filter) C 코드화

 

MATLAB을 이용하여 a와 b값을 구한다.

 

 

 
N = 5; %차수


fs = 1000; %샘플링 주파수


fc = 20; %차단 주파수


w_c = 2*pi*fc/fs


[b,a]=butter(N, w_c); %버터웍스 필터일 경우

차수
2
3
4
5
6
7
8

 

 

원칩에 들어가는 프로그램



 

 

3차일 경우

 

 




 

 

 

5차일 경우

 




 

 

y[2] = -  (a[1] * y[1]) -  (a[2] * y[0])

         + (b[0] * x[2]) + (b[1] * x[1]) + (b[2] * x[0]);

 

y[3] = -  (a[1] * y[2]) -  (a[2] * y[1]) -  (a[3] * y[0])

         + (b[0] * x[3]) + (b[1] * x[2]) + (b[2] * x[1]) + (b[3]* x[0]);

 

아두이노 코드

/*
 * N = 2; %차수
 * fs = 100; %샘플링 주파수
 * fc = 1; %차단 주파수
 * w_c = 2*pi*fc/fs
 * [b,a]=butter(N, w_c); %버터웍스 필터일 경우
 */

void setup() {
  Serial.begin(115200);
}

void loop() {
  const float a[3] = {1.0              , -1.722323555674884, 0.756431064226591};
  const float b[3] = {0.008526877137927,  0.017053754275854, 0.008526877137927};
  static float x[3]={0,0,0}, y[3]={0,0,0};
  static uint32_t timer = 0;
  uint8_t i, j;
  
  if(millis() > timer) {
    timer = millis() + 10;
      for(i=1 ; i<3 ; i++) {
        x[i-1] = x[i];
        y[i-1] = y[i];
      }
    }
    x[2] = analogRead(A0);

    for(i=0 ; i<2 ; i++) {
      y[2] = - (a[1] * y[1]) - (a[2] * y[0])
             + (b[0] * x[2]) + (b[1] * x[1]) + (b[2] * x[0]);
    }

    Serial.println(y[2]);
  }
}