C++与Matlab混合编程

启动CodeBlocks(SVN6688+GCC4.5.1)建立C++项目,在项目管理中项目名称上单击右键,选择最下边的Properties选项,如下所示;

C++与Matlab混合编程_第1张图片

在打开的窗口中选择右下角的Project’s build options选项,如0.4所示;

C++与Matlab混合编程_第2张图片

在打开的窗口中选择Search directories选项中的Compliler选项,在其中加入SP++2.0Matlab(2008a)中相关的include目录,如下所示,注意:此处必须先加载SP++2.0的目录,然后再加载Matalb的目录,加为SP++2.0Matlab中都有matrix.h头文件,如果加载顺序相反,则无法通过编译

C++与Matlab混合编程_第3张图片

同上步,在打开的窗口中选择Search directories选项中的Linker选项,在其中加入Matlab中相关的lib目录,如下图所示;

C++与Matlab混合编程_第4张图片

同上步,在打开的窗口中选择Linder setting选项,在其中加入Matlab中相关的lib文件,如下图所示;

C++与Matlab混合编程_第5张图片

在项目中运行如下测试代码;

/*****************************************************************************
 *                             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混合编程_第6张图片

你可能感兴趣的:(C++与Matlab混合编程)