【问题标题】:software capacitor [closed]软件电容器[关闭]
【发布时间】:2012-07-18 11:30:04
【问题描述】:

我需要使用软件电容器。

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

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

【问题讨论】:

  • 您需要低通/高通滤波器吗?如果您需要考虑使用 C/C++ DSP 库。如果没有,并且您想对任意电气/电子电路进行建模,那么 单个函数 或库将根本不够...
  • 投票结束作为离题,因为对于单个组件(如电容器),微分方程是众所周知的 (en.wikipedia.org/wiki/Capacitor)。对于离散滤波器,您可能正在寻找 FIR 滤波器 (en.wikipedia.org/wiki/Finite_impulse_response) 之类的东西。对于组件的组合,您可能会想要更像 PSPICE (en.wikipedia.org/wiki/PSPICE) 的东西,这些都与 stackoverflow 无关(也许可以尝试electronics.stackexchange.com)。
  • @eharvest 如果您想对几个电容器或电阻器建模,那么 问题 不是。重点是为什么你必须这样做。您想简单地过滤信号吗?检查 DSP 软件。您想模拟(简单与否)电子电路吗?使用一个程序来做这件事,自己做并不是一件容易的事,而且很长。
  • @eharvest 但物理过滤器会延迟您的信号...
  • @eharvest 用电容器制成的滤波器实际上会延迟您的信号。您的“数字”过滤器将针对少数样本弹出“零”(这取决于过滤器顺序),然​​后您将得到您想要的。此外,任何滤波器(数字的物理)都不是完美的模型,并且一开始会很嘈杂(因此,除了延迟的几个样本之外,您不会看到“干净”的信号......)。最后:您不需要未来的样本。看看这里MusicDSP.org。

标签: c++ signal-processing


【解决方案1】:

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

#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;

}

【讨论】:

  • 主函数底部的两个循环只是为了提供命令行中信号的可视化表示。
猜你喜欢
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 2011-03-05
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多