0

我需要使用软件电容器。

我有一个带有 n 个样本的信号。我需要过滤它。

是否有包含软件电容器和其他电子元件的 c++ 库(或单个函数)。

4

1 回答 1

2

如果您想要的只是一个非常简单的函数来将自定义过滤器应用于样本数组,那么应该这样做......只需用更类似于适当方程式的东西替换电容器()函数中的循环中的逻辑。

#include <stdio.h>
#include <math.h>

#define INRADS *3.1416/180.0

#define NUM_SAMPLES 1000

double capVoltage = 0;
//this is a simple (capacitor like) filter.
int capacitor(double* sample, long samples, double capacitorValue, double totalTime, double initialCapVoltage){

    capVoltage = initialCapVoltage;

    for (int i = 0; i<= samples-1; i++){ //loop through all the samples
        if (sample[i] > capVoltage){ //charge the cap
            //put your math in here, calculate voltages based on capacitorValue, totalTime and capVoltage
            //this next line is just for testing purposes
            capVoltage += 0.2;
        }
        if (sample[i] < capVoltage){ //discharge the cap
            //put your math in here, calculate voltages based on capacitorValue, totalTime and capVoltage
            //this next line is just for testing purposes
            capVoltage -= 0.2;
        }
        sample[i] = capVoltage;
        printf("Changed sample %d to %f \n", i, sample[i]);
    }

}

double* myVoltageSamples; //generic wave sample
double* myVoltageSamples2; //generic wave sample

int main(){

    myVoltageSamples = new double[NUM_SAMPLES]; //let's say this is 1 sample every millisecond for one second
    myVoltageSamples2 = new double[NUM_SAMPLES]; //let's say this is 1 sample every millisecond for one second

    for (int i = 0; i<= NUM_SAMPLES-1; i++){ //put some data in the sample array
        myVoltageSamples[i] = sin( ( i ) INRADS );      // a simple, generic sin wave
        myVoltageSamples2[i] = myVoltageSamples[i];
        printf("Adding %f to the sample.\n", myVoltageSamples[i]);
    }
    //we now have a generic signal

    //apply your basic (capacitor) filter
    capacitor(myVoltageSamples2, NUM_SAMPLES, 0.001, 1000, 0); //1mF cap, one second, start voltage = 0

    //compare the start and finish:
    printf("first signal:\n");
    for (int i = 0; i<= NUM_SAMPLES-1; i++){ //put some data in the sample array
        for (int j = 0; j<=((int)(myVoltageSamples[i]*20))+20-1; j++){
            printf(".");
        }
        printf("X\n");
    }

    printf("second signal:\n");
    for (int i = 0; i<= NUM_SAMPLES-1; i++){ //put some data in the sample array
        for (int j = 0; j<=((int)(myVoltageSamples2[i]*20))+20-1; j++){
            printf(".");
        }
        printf("X\n");
    }

    delete myVoltageSamples;
    delete myVoltageSamples2;

}
于 2012-07-18T12:24:37.660 回答