Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
7年前关闭。
我需要使用一个软件电容器。
我有一个带有n个样本的信号。我需要过滤它。
是否有一个c ++库(或单个函数),其中包含软件电容器和其他电子组件。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
7年前关闭。
我需要使用一个软件电容器。
我有一个带有n个样本的信号。我需要过滤它。
是否有一个c ++库(或单个函数),其中包含软件电容器和其他电子组件。
最佳答案
如果您想要的只是一个非常简单的函数,即可将自定义过滤器应用于样本数组,则应做到这一点...只需用更合适的方程式替换电容器()函数中循环中的逻辑即可。
#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;
}
关于c++ - 软件电容器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11540300/
10-10 21:51