首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >软件电容器

软件电容器
EN

Stack Overflow用户
提问于 2012-07-18 19:30:05
回答 1查看 1.4K关注 0票数 0

我需要使用一个软件电容器。

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

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

EN

回答 1

Stack Overflow用户

发布于 2012-07-18 20:24:38

如果你想要的是一个非常简单的函数来对样本数组应用自定义过滤器,那么这应该可以做到……只需用更像正确的方程式的东西替换电容器()函数中循环中的逻辑即可。

代码语言:javascript
复制
#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;

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11540300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档