C++与Matlab混合编程
2010-10-20 19:12
232 查看
启动CodeBlocks(SVN6688+GCC4.5.1)建立C++项目,在项目管理中项目名称上单击右键,选择最下边的Properties选项,如下图所示;
在打开的窗口中选择右下角的Project’s build options选项,如图 0.4所示;
在打开的窗口中选择Search directories选项中的Compliler选项,在其中加入SP++2.0与Matlab(2008a)中相关的include目录,如下图所示,注意:此处必须先加载SP++2.0的目录,然后再加载Matalb的目录,加为SP++2.0与Matlab中都有matrix.h头文件,如果加载顺序相反,则无法通过编译;
同上步,在打开的窗口中选择Search directories选项中的Linker选项,在其中加入Matlab中相关的lib目录,如下图所示;
同上步,在打开的窗口中选择Linder setting选项,在其中加入Matlab中相关的lib文件,如下图所示;
在项目中运行如下测试代码;
/*****************************************************************************
* CppMatlab_test.cpp
*
* C++ and Matlab mixed programming testing.
*
* Zhang Ming, 2010-10, Xi'an Jiaotong University.
*****************************************************************************/
#define BOUNDS_CHECK
#include
#include
#include
#include
#include
#include
#include "engine.h"
using namespace std;
using namespace splab;
typedef double Type;
const int Lg = 128;
const int Ls = 1000;
const Type Fs = 1000;
int main()
{
/******************************* [ signal ] ******************************/
Vector t = linspace( Type(0), Type(Ls-1), Ls ) / Type(Fs);
Vector s = sin( Type(400*PI) * pow(t,Type(2.0)) );
/******************************** [ widow ] ******************************/
t = linspace(Type(0),Type(Lg-1),Lg);
Vector g = gauss( t, (Lg-1)/Type(2), Lg/Type(8) );
/********************************* [ WFT ] *******************************/
cout << "Taking windowed Fourier transform." << endl;
Matrix< complex > coefs = wft( s, g );
/******************************** [ IWFT ] *******************************/
cout << "Taking inverse windowed Fourier transform." << endl;
Vector x = iwft( coefs, g );
cout << "The relative error is : " << "norm(s-x) / norm(s) = "
<< norm(s-x)/norm(s) << endl << endl;
/******************************** [ PLOT ] *******************************/
Engine *ep = engOpen( NULL );
if( !ep )
{
cerr << "Cannot open Matlab Engine!" << endl;
exit(1);
}
Matrix C = transpose( abs(coefs) );
int M = C.rows(), N = C.cols();
// define mxArray as 1-by-1 Real Scalar
mxArray *mFs = mxCreateDoubleMatrix( 1, 1, mxREAL );
// define mxArray as N-by-1 Real Vector
mxArray *ms = mxCreateDoubleMatrix( Ls, 1, mxREAL );
mxArray *mx = mxCreateDoubleMatrix( Ls, 1, mxREAL );
// define mxArray as N-by-M Real Matrix, BECAUSE matalb is ROW MAJOR
// and C/C++ is COLUMN MAJOR, the row of SP++ matrix is copied as the
// column of Matlab matrix
mxArray *mC = mxCreateDoubleMatrix( N, M, mxREAL );
// array copy from Scalar to mxArray.
memcpy( mxGetPr(mFs), &Fs, sizeof(Type) );
// array copy from Vectors to mxArray.
memcpy( mxGetPr(ms), s, Ls*sizeof(Type) );
memcpy( mxGetPr(mx), x, Ls*sizeof(Type) );
// array copy from Matrix to mxArray.
memcpy( mxGetPr(mC), C, C.size()*sizeof(Type) );
// send command to Matlab engine
engPutVariable( ep, "fs", mFs );
engPutVariable( ep, "s", ms );
engPutVariable( ep, "x", mx );
engPutVariable( ep, "C", mC );
// Matlab commands
const char *mCmd = " \
figure('name','C++ and Matlab Mixed Programming Testing'); \
hFN = floor(size(C,1)/2); tN = size(C,2); \
subplot(2,2,1); plot((0:tN-1), s); \
axis([0,tN,min(s),max(s)]); \
xlabel('Time (ms)', 'fontsize',12); ylabel('Amplitude', 'fontsize',12); \
title('(a)'); \
subplot(2,2,2); pcolor((0:tN-1),(0:hFN)'/hFN, C(1:hFN+1,:)); \
shading interp; \
axis([0,tN, 0,1]); \
yt = 0 : 0.2 : 1; set(gca, 'YTick',yt); set(gca, 'YTickLabel',fs*yt/2); \
xlabel('Time (ms)','fontsize',12); ylabel('Frequency (Hz)','fontsize',12); \
title('(b)'); \
subplot(2,2,3); plot((0:tN-1),x); \
axis([0,tN,min(x),max(x)]); \
xlabel('Time (ms)','fontsize',12); \
ylabel('Amplitude', 'fontsize',12); \
title('(c)'); \
subplot(2,2,4); e=s-x; plot((0:tN-1),e); \
axis([0,tN,min(e),max(e)]); \
xlabel('Time (ms)','fontsize',12); \
ylabel('Amplitude', 'fontsize',12); \
title('(d)'); \
";
// send command to Matlab engine
engEvalString( ep, mCmd );
// delete mxArray
mxDestroyArray( mFs );
mxDestroyArray( ms );
mxDestroyArray( mx );
mxDestroyArray( mC );
system( "pause" );
engClose(ep);
return 0;
}
运行结果为:
在打开的窗口中选择右下角的Project’s build options选项,如图 0.4所示;
在打开的窗口中选择Search directories选项中的Compliler选项,在其中加入SP++2.0与Matlab(2008a)中相关的include目录,如下图所示,注意:此处必须先加载SP++2.0的目录,然后再加载Matalb的目录,加为SP++2.0与Matlab中都有matrix.h头文件,如果加载顺序相反,则无法通过编译;
同上步,在打开的窗口中选择Search directories选项中的Linker选项,在其中加入Matlab中相关的lib目录,如下图所示;
同上步,在打开的窗口中选择Linder setting选项,在其中加入Matlab中相关的lib文件,如下图所示;
在项目中运行如下测试代码;
/*****************************************************************************
* CppMatlab_test.cpp
*
* C++ and Matlab mixed programming testing.
*
* Zhang Ming, 2010-10, Xi'an Jiaotong University.
*****************************************************************************/
#define BOUNDS_CHECK
#include
#include
#include
#include
#include
#include
#include "engine.h"
using namespace std;
using namespace splab;
typedef double Type;
const int Lg = 128;
const int Ls = 1000;
const Type Fs = 1000;
int main()
{
/******************************* [ signal ] ******************************/
Vector t = linspace( Type(0), Type(Ls-1), Ls ) / Type(Fs);
Vector s = sin( Type(400*PI) * pow(t,Type(2.0)) );
/******************************** [ widow ] ******************************/
t = linspace(Type(0),Type(Lg-1),Lg);
Vector g = gauss( t, (Lg-1)/Type(2), Lg/Type(8) );
/********************************* [ WFT ] *******************************/
cout << "Taking windowed Fourier transform." << endl;
Matrix< complex > coefs = wft( s, g );
/******************************** [ IWFT ] *******************************/
cout << "Taking inverse windowed Fourier transform." << endl;
Vector x = iwft( coefs, g );
cout << "The relative error is : " << "norm(s-x) / norm(s) = "
<< norm(s-x)/norm(s) << endl << endl;
/******************************** [ PLOT ] *******************************/
Engine *ep = engOpen( NULL );
if( !ep )
{
cerr << "Cannot open Matlab Engine!" << endl;
exit(1);
}
Matrix C = transpose( abs(coefs) );
int M = C.rows(), N = C.cols();
// define mxArray as 1-by-1 Real Scalar
mxArray *mFs = mxCreateDoubleMatrix( 1, 1, mxREAL );
// define mxArray as N-by-1 Real Vector
mxArray *ms = mxCreateDoubleMatrix( Ls, 1, mxREAL );
mxArray *mx = mxCreateDoubleMatrix( Ls, 1, mxREAL );
// define mxArray as N-by-M Real Matrix, BECAUSE matalb is ROW MAJOR
// and C/C++ is COLUMN MAJOR, the row of SP++ matrix is copied as the
// column of Matlab matrix
mxArray *mC = mxCreateDoubleMatrix( N, M, mxREAL );
// array copy from Scalar to mxArray.
memcpy( mxGetPr(mFs), &Fs, sizeof(Type) );
// array copy from Vectors to mxArray.
memcpy( mxGetPr(ms), s, Ls*sizeof(Type) );
memcpy( mxGetPr(mx), x, Ls*sizeof(Type) );
// array copy from Matrix to mxArray.
memcpy( mxGetPr(mC), C, C.size()*sizeof(Type) );
// send command to Matlab engine
engPutVariable( ep, "fs", mFs );
engPutVariable( ep, "s", ms );
engPutVariable( ep, "x", mx );
engPutVariable( ep, "C", mC );
// Matlab commands
const char *mCmd = " \
figure('name','C++ and Matlab Mixed Programming Testing'); \
hFN = floor(size(C,1)/2); tN = size(C,2); \
subplot(2,2,1); plot((0:tN-1), s); \
axis([0,tN,min(s),max(s)]); \
xlabel('Time (ms)', 'fontsize',12); ylabel('Amplitude', 'fontsize',12); \
title('(a)'); \
subplot(2,2,2); pcolor((0:tN-1),(0:hFN)'/hFN, C(1:hFN+1,:)); \
shading interp; \
axis([0,tN, 0,1]); \
yt = 0 : 0.2 : 1; set(gca, 'YTick',yt); set(gca, 'YTickLabel',fs*yt/2); \
xlabel('Time (ms)','fontsize',12); ylabel('Frequency (Hz)','fontsize',12); \
title('(b)'); \
subplot(2,2,3); plot((0:tN-1),x); \
axis([0,tN,min(x),max(x)]); \
xlabel('Time (ms)','fontsize',12); \
ylabel('Amplitude', 'fontsize',12); \
title('(c)'); \
subplot(2,2,4); e=s-x; plot((0:tN-1),e); \
axis([0,tN,min(e),max(e)]); \
xlabel('Time (ms)','fontsize',12); \
ylabel('Amplitude', 'fontsize',12); \
title('(d)'); \
";
// send command to Matlab engine
engEvalString( ep, mCmd );
// delete mxArray
mxDestroyArray( mFs );
mxDestroyArray( ms );
mxDestroyArray( mx );
mxDestroyArray( mC );
system( "pause" );
engClose(ep);
return 0;
}
运行结果为:
相关文章推荐
- C++&MATLAB混合编程之mwArray使用实例
- Matlab混合编程之C++SL(C++篇)
- MATLAB调用C/C++进行混合编程
- 基础知识(一)matlab与c++混合编程之环境搭建
- Matlab与C++接口与混合编程讨论小结
- 基础知识(二)matlab与c++混合编程之经验笔记
- matlab c++ 混合编程初始设置
- matlab和c++混合编程---Mex结构和mexFunction参数传递
- matlab和c++混合编程---matlab和vs的环境配置问题
- Matlab C/C++混合编程实例学习
- vs2013中使用matlab2014b引擎实现c++和matlab混合编程
- matlab c++ 混合编程初始设置
- c++与matla混合编程之一---调用matlab工具箱内的函数
- matlab和c++混合编程---方法和步骤
- matlab和c++混合编程---方法和步骤
- 混合编程---c++调用matlab生成的dll----findCircles的应用
- Matlab生成C++代码及混合编程步骤说明
- Matlab与C++接口与混合编程讨论小结
- Matlab与C和C++混合编程接口及应用
- Matlab与C++的混合编程