ya kno it

filter.cpp

Committer:
Thijs12va
Date:
2017-10-25
Revision:
0:09ae59836ddc
Child:
1:db5970567c4d

File content as of revision 0:09ae59836ddc:

#include "filter.h"

float LowPass::filter(float x) // same as function lowpass
{
   float y = a * yprev + (1 - a) * x;
   yprev = y;
   return y;
}

float HighPass::filter(float x){
    xprev[0] = xprev[1];             // renew the 'previous value'
    xprev[1] = x;                    // 2nd value is the current input value
   
    float y = a * yprev + a * (xprev[1] - xprev[0]); // according to: https://en.wikipedia.org/wiki/High-pass_filter
    yprev = y;                       // renew yprev for the next time
    return y;
}

float Notch::filter(float x){
    xprev[0] = xprev[1];
    xprev[1] = xprev[2];
    xprev[2] = x;
                                                                                           
    float y = (
                (1.0+pow(lambda,2))*x
                - 2.0*(1.0-pow(lambda,2))*xprev[1]
                +(1.0+pow(lambda,2))*xprev[0]
                + 2.0*(1.0-pow(lambda,2))*yprev[1]
                -(1.0+pow(lambda,2)-b)*yprev[0]
            )
            /(1.0+pow(lambda,2)+b);
    
    yprev[0] = yprev[1];
    yprev[1] = y;
    return y;
}

float ButterLow::filter(float x){
    xprev[0] = xprev[1];
    xprev[1] = xprev[2];
    xprev[2] = x;
    
    float y =   (pow(w,2)*x
                + 2*w*xprev[1]
                + pow(w,2)*xprev[0]
                + (2+sqrt(2.0f)*w-2*pow(w,2))*yprev[1]
                - (1+pow(w,2))*yprev[0]
                )
                /(1+sqrt(2.0f)*w+pow(w,2));

    yprev[0] = yprev[1];
    yprev[1] = y;
    return y;
}