基于MATLAB Coder将matlab代码转换成C代码

MATLAB Coder可以从MATLAB代码生成独立的、可读性强、可移植的C/C++代码。本文基于matlab2015a编制,但是也适用于matlab 2014~2016的版本

1. 使用MATLAB Coder产生C代码的4个步骤:

(1) 开发实现特定算法功能的MATLAB代码及其测试文件;

(2) 检查MATLAB代码的兼容性确保MATLAB代码中的语句都能被转换成C代码(有些matlab代码语句并不能生成c/c++代码,例如matlab中的imreadimshow函数就不能被转换c代码);

(3) 产生最终使用的源代码或MEX;

(4) 利用MATLAB Coder生成c++代码,并在vs2008中验证

2. Matlab功能模块代码及其测试代码

2.1 foo.m

function c = foo(a, b)%#codegen

%This function muliplies a and b

c = a.* b;

2.2 foo_high.m

 其中,%#codegen可以防止出现警告错误

function r1 = foo_high(v1, v2)%#codegen

%This function muliplies a and b

r1=zeros(1,3);

r1(1)=foo(v1(1),v2(1));

r1(2:3) = foo(v1(2:3),v2(2:3));

 

2.3 test_main.m

clear all

clc

close all

a=8.97;

b=1.786;

a=[2.1 7.3 11];

b=[1.9 3.3 8];

rc = foo_high(a, b)

 

2.4 results

rc =3.9900   24.0900   88.0000

 

3. matlab代码转成C代码的详细步骤

(1) 在命令窗口,输入mex -setpu,选中一个存在的编译器;

(2) 在命令窗口输入coder(图形界面),回车,弹出MATLAB Coder Project对话框;

或在matlab软件的界面中点击MatLab Coder图标。

 

生成的MATLAB Coder界面如下图所示:

 

(3) Generate code for function中输入需要转换为C代码的matlab功能模块文件:foo.mfoo_high.m

 

 

(4) 点击next按钮,进入Define Input Types界面。输入test_main.m测试文件

 

 

(5) 点击Autodefine Input Types按钮,从而使得matlab能自动检测出foo函数的接口变量的维度和类型。

 

(6) 点击next按钮,进入check for run-time issues界面

 

点击check for issues按钮进入对应的界面:

 

上述问题表示激活的代码没有调用foo函数的入口foo函数没有被调用。因为foo函数的功能已经被包含在foo_high函数中了。这里的红色部分可以不用理会。

(7) 点击next按钮,进入Generate Code界面:

 

  点击“Generate”按钮matlab生成对应的C代码并显示对应的界面:

 

(8) 点击next按钮,显示结束Finish Workflow”界面

 

 

4. 在vs2013中对转换获得的C代码进行测试。

Matlab转换生成的C代码,会生成一个codegen文件夹,下面包含两个子文件夹libmexLib子文件夹下面包含xxx.h/xxx.c/xxx_initialize.c/xxx_initialize.c/xxx_tpyes.h

/rt_nonfinite.c/rt_nonfinite.h/rtGetInf.c/rtGetInf.h/rtGetNaN.c/rtGetNaN.h文件,其中只有xxx.c转换后所获得的核心文件。其它文件都是支持性文件。Mex文件夹下的文件是具有冗余的文件可不予考虑。

4.1 foo_high.c

void foo_high(const double v1[3],const double v2[3],double r1[3])

{

  int i1;

  /* This function muliplies a and b */

  r1[0] = v1[0] * v2[0];

 

  /* This function muliplies a and b */

  for (i1 = 0; i1 < 2; i1++) {

    r1[1 + i1] = v1[1 + i1] * v2[1 + i1];

  }

}

 

4.2 Main.c

#include <iostream>

#include <D:\matlab_to_C\test_folder_5\vs_project2\foo_high.c>

using namespace std;

int main()

{

double a[3] = {4,9,2};

double b[3] = {2,7,8};

double c[3];

//cin >> a >> b;

foo_high(a,b,c);

cout << "c = " << c << endl;

cout << "c[0] = " << c[0] << endl;

cout << "c[1] = " << c[1] << endl;

cout << "c[2] = " << c[2] << endl;

cin.get();

return 0;

}


5. Example of C_Plus_Plus code

5.1 func1

#include <iostream>

using namespace std;

int func1(int a, int b)

{

       int i0;

    i0 = a*b;

       cout << "\n***Good Test*******" << endl;

       //return(int)i0;

       return i0;

}

 

5.2 main

#include <stdio.h>

#include <iostream>

extern int func1(int a, int b);

//#include"func1.cpp"

using namespace std;

int main(void){

       int i, a[10];

       int b[8] = {11,22,33,44,55,66,77,88};

       cout << "*****Good Test*******" << endl;

       for (i = 0; i <= 9; i++)

              a[i] = i;

       for (i = 9; i >= 0; i--)

              printf("%d ", a[i]);

       for (i = 7; i >= 0; i--)

              printf("%d ", b[i]);

             

       int rt;

       rt = func1(8, 9);

       cout << "rt="<< rt << endl;;

    cin.get();

       return 0;

}


6. Ref: “C Code Generation Using the MATLAB Coder App” from the matlab website.

 

7. 备注

(1) Overview选项卡中,点击Add files,弹出对话框,选中foo.m打开;

(2) 单击变量a,选择Define by Example…,弹出MATLAB Coder Define by Example对话框,在MATLAB Expression中输入5,点击OK;同样变量b也进行相应操作,输入6

(3) 选中Build选项卡,Output type中选择c/c++ Static Library;选中Generate code only

(4) 点击More settingsGeneralàLanguage选择C++Interface选项中去掉所有选项;Close

(5) 点击Build,进行编译;点击View report,弹出Code Generation Report对话框,此时,变量abc会显示相应的变量信息;

(6) 利用vs2008建立一个控制台应用程序,将生成的相关文件foo.hfoo.cpprtwtypes.hfoo_types.h拷到相关目录下并添加到应用程序中;

(7) foo.cpp文件中添加#include “stdafx.h”

(8) test.cpp文件中代码为:

#include "stdafx.h"

#include "foo.h"

#include <iostream> 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{   double a = 0.0, b = 0.0, c = 0.0;

     cin>>a>>b;

     c = foo(a, b);

     cout<<"c = "<<c<<endl;

     return 0;}

 

7. 一个复杂的例子

求一个数的n次方根:

4.1两个.m文件:

nrt.m:

function [nth_rt, iterations, hstry] = nrt(varargin)%#codegen

%This function will use a Newton Search Technique to find

%the nth root of a number, a, to the tolerance, tol.

%The square root

% nrt(10, 2), or nrt(10, 2, 1e-9)

%The "n" root

%nrt(10, n), or nrt(10, n, 1e-9)

 

a = varargin{1};

n = varargin{2};

 

if nargin ~= 3

    tol = 1e-9;

else

    tol = varargin{3};

end

 

if a < 0

    nth_rt = 0;

    iterations = 0;

    hstry = 0;

else

    [nth_rt, hstry] = newtonSearchAlgorithm(a, n, tol);

    iterations = length(find(hstry ~= 0));

    %iterations = sum(hstry ~= 0);

end

 

newtonSearchAlgorithm.m

function [x, h] = newtonSearchAlgorithm(b, n, tol) %#codegen

%Given, "a", this function finds the nth root of a

%number by finding where: x^n-a = 0

coder.inline('never'); %使其生成一个单独的c++文件

notDone = 1;

aNew    = 0; %Refined Guess Initialization

a       = 1; %Initial Guess

cnt     = 0;

h = zeros(50, 1);

h(1)    = a;

while notDone

    cnt = cnt + 1;

    [curVal, slope] = f_and_df(a, b, n); %  square

    yint = curVal - slope * a;

    aNew = -yint / slope; %The new guess

    h(cnt) = aNew;

    if (abs(aNew-a) < tol) %Break if it's converged

        notDone = 0;

    elseif cnt > 49 %after 50 iterations, stop

        notDone = 0;

        aNew = 0;

    else

        a = aNew;

    end

end

x = aNew;

 

function [f, df] = f_and_df(a, b, n)

%Our function is f=a^n-b and it's derivative is n*a^(n-1).

 

f  = a^n-b;

df = n*a^(n-1);

 

2 在命令窗口输入coder(图形界面),回车,弹出MATLAB Coder Project对话框;

3、在New选项卡Name中输入一个工程名nrt.prj;点击Ok,弹出MATLAB Coder MEX Function对话框;

4、在Overview选项卡中,点击Add files,弹出对话框,选中nrt.m打开;

5、添加三个输入,分别为1021e-9;两个输入也可以;

6、选中Build选项卡,Output type中选择c/c++ Static Library;选中Generate code only

7、点击More settingsGeneral-->Language选择C++Interface选项中去掉所有选项;Close

8、点击Build,进行编译;点击View report,弹出Code Generation Report对话框;

9、利用vs2008建立一个控制台应用程序,将生成的相关文件nrt.cppnrt.hnewtonSearchAlgorithm.cppnewtonSearchAlgorithm.hnrt_types.hrtwtypes.h拷到相关目录下并添加到应用程序中;

10、分别在nrt.cppnewtonSearchAlgorithm.cpp文件中添加#include “stdafx.h”

11test.cpp文件中代码为:

#include "stdafx.h"

#include "nrt.h"

 

#include <iostream>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

    double varargin_1 = 0, varargin_2 = 0, varargin_3 = 1e-9;

 

    cin>>varargin_1>>varargin_2;

 

    double nth_rt = 0, iterations = 0;

 

    double hstry_data[50] = {0};

 

    int hstry_sizes[1] = {0};

 

    nrt(varargin_1, varargin_2, varargin_3, &nth_rt, &iterations, hstry_data, hstry_sizes);

 

    cout<<"nth_rt = "<<nth_rt<<endl;

    cout<<"iterations = "<<iterations<<endl;

 

    cout<<"hstry_data = "<<endl;

    for (int i=0; i<50; i++)

    {

        cout<<hstry_data[i]<<endl;

    }

     cout<<"hstry_sizes = "<<hstry_sizes[0]<<endl;

     return 0;

}

  • 15
    点赞
  • 272
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
HDL Coder是一个Matlab工具箱,可以将Matlab代码转换为Verilog或VHDL代码,以便在FPGA或ASIC上实现。在本教程中,我们将介绍如何使用HDL Coder将Verilog代码转换Matlab可调用的模块。 步骤1:创建Verilog代码 首先,创建一个简单的Verilog模块,例如一个加法器。以下是一个简单的Verilog代码示例: ``` module adder(a, b, c); input [7:0] a; input [7:0] b; output [7:0] c; assign c = a + b; endmodule ``` 此代码创建一个8位加法器,将两个8位输入相加,并将结果输出到8位输出端口c中。 步骤2:使用HDL CoderMatlab函数 现在,我们将使用HDL Coder将Verilog代码转换Matlab可调用的模块。打开Matlab并创建一个新的脚本。在脚本中,将以下代码输入到控制台中: ``` coder -hdlcoder -int -input -args {fi(0,1,8),fi(0,1,8)} -top adder adder.v ``` 此命令告诉HDL Coder将adder.v文件转换Matlab函数,并将其命名为adder。参数fi(0,1,8)指定每个输入和输出的数据类型和位宽。在本例中,我们使用8位有符号定点数。 执行此命令后,HDL Coder将生两个文件:adder.m和hdl_prj/hdlsrc/adder.vhd。adder.m包含Matlab函数,可以调用Verilog模块。hdl_prj/hdlsrc/adder.vhd是HDL Coder的VHDL代码。 步骤3:测试生Matlab函数 现在,我们将测试生Matlab函数,以确保它可以正确调用Verilog模块。在Matlab命令行中,输入以下代码: ``` a = fi(5,1,8); b = fi(10,1,8); c = adder(a, b); ``` 此代码将创建两个8位有符号定点数,将它们作为输入传递给adder函数,并将结果保存在c中。执行此代码后,c应该等于15。 总结 使用HDL Coder将Verilog代码转换Matlab可调用的模块非常简单。只需要几个简单的步骤,就可以将Verilog代码转换Matlab函数,并在Matlab中测试它们。通过使用HDL Coder,您可以轻松地将Matlab算法转换为可在FPGA或ASIC上实现的硬件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值