gpt4 book ai didi

c++ - 模板非类型参数常量限制过滤库

转载 作者:行者123 更新时间:2023-11-28 00:13:35 27 4
gpt4 key购买 nike

我正在使用 DSP 滤波器库 http://www.linux-usb-daq.co.uk/howto2/filter/

使用模板非类型参数来初始化过滤器对象。这就是过滤器对象的创建和使用方式。

    const int order = 3;
Iir::Butterworth::LowPass<order> f;
const float samplingrate = 1000; // Hz
const float cutoff_frequency = 50; // Hz
f.setup (order, samplingrate, cutoff_frequency);
f.reset ();
FILE *fimpulse = fopen("lp.dat","wt");

// let's generate an input signal and filter it
// instantly!
for(int i=0;i<1000;i++)
{
float a=0;
if (i==10) a = 1; // delta pulse at t=10
float b = f.filter(a);
fprintf(fimpulse,"%f\n",b);
}

现在,如果我想在我的代码中使用这个库来对不同顺序的滤波器(如 3、4、5 等)进行 IIR 滤波。我应该如何进行而不用重写整个代码?

因为顺序是一个编译时常量

const int order = 3;
Iir::Butterworth::LowPass<order> f;

有什么技巧可以让我为不同的订单编写这段代码而无需重写所有内容吗?

谢谢,

最佳答案

看来你想要模板函数:

template <int order>
void filter_demo(/*args*/)
{
Iir::Butterworth::LowPass<order> f;
const float samplingrate = 1000; // Hz
const float cutoff_frequency = 50; // Hz
f.setup (order, samplingrate, cutoff_frequency);
f.reset ();
FILE *fimpulse = fopen("lp.dat","wt");

// let's generate an input signal and filter it
// instantly!
for(int i=0;i<1000;i++)
{
float a=0;
if (i==10) a = 1; // delta pulse at t=10
float b = f.filter(a);
fprintf(fimpulse,"%f\n",b);
}
}

然后调用它:

filter_demo<3>();
filter_demo<4>();

关于c++ - 模板非类型参数常量限制过滤库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31806358/

27 4 0
文章推荐: html - 页面上的多个选项卡 - HTML CSS
文章推荐: python - 使用 Python Bottle 的 Webhelpers
文章推荐: javascript - 制作
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com