0

ソフトウェアコンデンサを使用する必要があります。

n サンプルの信号があります。そして私はそれをフィルタリングする必要があります。

ソフトウェア コンデンサやその他の電気部品を含む C++ ライブラリ (または単一の関数) はありますか。

4

1 に答える 1

2

サンプルの配列にカスタム フィルターを適用するための非常に単純な関数が必要な場合は、これで十分です。単に、capacitor() 関数のループ内のロジックを適切な方程式に近いものに置き換えてください。

#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 に答える