【问题标题】:Matlab coder: "All inputs must be constant"Matlab 编码器:“所有输入必须是常数”
【发布时间】:2020-02-13 07:58:17
【问题描述】:

我正在使用 Matlab Coder 将此代码转换为 C++:

fs = 50;
[b,a] = butter(3,0.5/(fs/2),'high');
...
% Other code using fs

然后,我收到此错误:“所有输入必须是常量”。

如果我这样做:[b,a] = butter(3,0.5/(50/2),'high');,它会起作用。

我发现了这个帖子:Constants and Matlab Coder

所以我尝试了:

fs = 50;
[b,a] = coder.const(@butter,3,0.5/(fs/2),'high');

但它仍然报告相同的错误。我该如何解决这个问题?

【问题讨论】:

  • 你的代码中还有 fs 的其他用途吗?
  • @Daniel:是的,这就是为什么我不想用它的值(50)替换变量。

标签: matlab matlab-coder


【解决方案1】:

Define Class Properties with Constant Values

在 ConstInput.m 中

classdef ConstInput
   properties (Constant)
      fs = 50;
   end
end

然后将fs 重命名为ConstInput.fs。 (不幸的是,Shift+Enter 不起作用。也许这个链接对changing variable names 有帮助。)

【讨论】:

    【解决方案2】:

    MATLAB R2020a 现已推出。在 R2020a 中增强了功能 butter 以支持使用非常量输入生成代码。生成的代码可用于在运行时获取任何有效阶数和截止频率的滤波器的系数。

    例如,考虑下面给出高通数字滤波器的滤波器系数的代码:

    function[num,den] = hpbutter(n,w)
    %#codegen
    
    [num,den] = butter(n,w,'high');
    

    现在我们可以生成具有非常量输入的代码,如下所示:

    codegen hpbutter -args {coder.typeof(0),coder.typeof(0)}
    

    您可以将任何有效的滤波器阶数 (n) 和截止频率 (w) 传递给生成的 MEX。

    [num,den] = hpbutter_mex(2,0.3)

    数量 =

    0.5050   -1.0100    0.5050
    

    登=

    1.0000   -0.7478    0.2722
    

    [num,den] = hpbutter_mex(3,0.4)

    数量 =

    0.2569   -0.7707    0.7707   -0.2569
    

    登=

    1.0000   -0.5772    0.4218   -0.0563
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 2016-12-15
      • 2013-11-03
      • 1970-01-01
      相关资源
      最近更新 更多