C中的butterworth过滤器

时间:2017-07-27 17:43:53

标签: c filter adc butterworth

我不知道我是否正确实施了它。 我从ADC" MCP3008"中获取数据。以每秒512个样本的速度。 然后我将数据传递给butterworth功能过滤器。低通滤波器0,05-150HZ。 但印刷数据增加和分歧.. 我使用了由此生成的代码 page 有什么不对的,请帮忙。

#include <stdio.h> //printf
#include <string.h>    //strlen
#include <sys/socket.h>    //socket
#include <arpa/inet.h> //inet_addr
#include <wiringPi.h>
#include <mcp3004.h>
#include <stdlib.h>
#include <unistd.h>

#define BASE 200
#define SPI_CHAN 0
#define chan 0
#define SAMPLING_RATE 512

/* Butterworth filter constants */

#define NZEROS 6
#define NPOLES 6


static double ButterworthFilter (int input)
{

     static double xv[NZEROS+1], yv[NPOLES+1];

            /* 0.05 to 150 Hz */
            double dCoefficient1 = -0.9976320693;
            double dCoefficient2 = 5.9881575406;
            double dCoefficient3 = -14.9763094670;
            double dCoefficient4 = 19.9763038520;
            double dCoefficient5 = -14.9881491170;
            double dCoefficient6 = 5.9976292614;
            double dGain = 5.764545976e+17;

    xv[0] = xv[1]; 
    xv[1] = xv[2]; 
    xv[2] = xv[3]; 
    xv[3] = xv[4]; 
    xv[4] = xv[5]; 
    xv[5] = xv[6]; 
    xv[6] = (double)(input / dGain);
    yv[0] = yv[1]; 
    yv[1] = yv[2]; 
    yv[2] = yv[3]; 
    yv[3] = yv[4]; 
    yv[4] = yv[5]; 
    yv[5] = yv[6]; 
    yv[6] =   (xv[0] + xv[6]) + 6 * (xv[1] + xv[5]) + 15 * (xv[2] + xv[4])
                     + 20 * xv[3]
                     + ( dCoefficient1 * yv[0]) + (  dCoefficient2 * yv[1])
                     + (dCoefficient3 * yv[2]) + ( dCoefficient4 * yv[3])
                     + (dCoefficient5 * yv[4]) + (  dCoefficient6 * yv[5]);
    return (yv[6]);
}

void error(char *msg)
{
    perror(msg);
    exit(1);
}


int main(int argc , char *argv[])

{
    int j;
    int buffer[1024];


    //configurazione wiringPi
    printf ("Raspberry Pi wiringPi test program\n") ;

    if (wiringPiSetup () == -1)
    exit (1) ;

    // configurazione spi
    mcp3004Setup (BASE, SPI_CHAN); // 3004 and 3008 are the same 4/8 channels


      printf("Connesso\n");

     //Invio Buffer da 512 campioni
        while(1) {
        for (j=0; j <= 1023; j++) {
            buffer[j] = (int)ButterworthFilter(analogRead (BASE + chan));
            printf("%d \n", buffer[j]);
            delay(1024/SAMPLING_RATE);
        }


     }

    return 0;
}

0 个答案:

没有答案