VC Learning Data Collection (27): How to Use Matlab Toolbox under VC ++

xiaoxiao2021-03-06  46

How to use the MATLAB toolbox function from VC from other pages,

How to use Matlab Toolbox under VC

MATLAB's signal processing toolbox is a collection of signal algorithm files. The basic objects it processes are signals and systems, and the signal processing toolbox is located under Toolbox / Signal. Using the files in the toolbox, the file can be transformed, filter, and spectrum. Estimation, filter design, etc. If other environments such as VC can call files in the MATLAB toolbox, some algorithms are greatly accelerated, and their reliability is also high.

1. Use Matlab Engine

The MATLAB engine uses the customer and server computing mode. In the use of the VC's C language or C language as a front-end client, it passes commands and data information to the MATLAB engine, and receives data information from the MATLAB engine, which provides the following Functions: Engopen, EnggetArray, EngputArray, Engevastring, EngacketputBuffer, Engclose interact with the client.

The following routine is to create a dialog-based application in VC, set a Button control onMatlabEngine in the dialog., Add "Engine.h" and "Math.h" header files in the dialog. CPP file, below Give some of the list of procedures.

Void ctestmatlabdlg :: onmatlabenne () {

ENGINE * EP;

MxArray * t = null, * result = null, * mfs = null, * mnfft = null;

Double DataX [1024];

Char buffer [1024];

For (int J = 0; j <1024; j ) // Note: If you collect data by collecting cards, the acquired data can be placed in the Datax [] array, this loop does not need

{

Double Samt = (Double) (1.0 / 1024);

DataX [J] = SIN (2.0 * 63.0 * Samt * 3.1415926 1.15 * 3.1415926);

}

Double * ppxx, * pfxx;

IF (! ("/ 0")))) {// Open the MATLAB engine to establish a connection to local MATLAB

FPRINTF (stderr, "/ n can't start matlab engine / n");

EXIT (-1);

}

Double FS [1] = {1024}; // Because MATLAB participating in the arithmetic parameters are the form of matrices, the following lines turn parameter transition

Double NFFT [1] = {1024}; // into a matrix form of MATLAB acceptable.

T = MXCreatedoublemtrix (1,1024, mxreal);

Mnfft = mxcreatedoublematrix (1, 1, mxreal);

MFS = MXCREATEDOUBLEMATRIX (1, ​​1, MXREAL);

MxsetName (T, "T");

MxsetName (Mnfft, "Mnfft");

MxsetName (MFS, "MFS");

Memcpy ((char *) MXGETPR (T), (char *) DATAX, 1024 * SIZEOF (DOUBLE));

Memcpy ((char *) mxgetpr (mnfft), (char *) NFFT, SIZEOF (DOUBLE));

Memcpy (CHAR *) MXGETPR (MFS), (CHAR *) FS, 1 * sizeof (double));

ENGPUTARRAY (EP, T); // Put the transformed parameters in the engine, at which time this parameter ENGPUTARRAY (EP, MNFFT) can be viewed under the MATLAB Command window;

ENGPUTARRAY (EP, MFS);

ENGEVALSTRING (EP, "[PXX, FO] = PSD (T, Mnfft, MFS);"); // Use the engine execution toolbox

EngOutputBuffer (EP, BUFFER, 512); // If you just want to see the display graph, you can remove the return parameters, and the PSD is automatically graphically.

Result = ENGGETARRAY (EP, "PXX"); / / The data in the outgoing engine is placed in the specified area for subsequent processing.

ppxx = mxgetpr (Result);

Result = ENGGETARRAY (EP, "FO");

Pfxx = mxgetpr (result);

ENGEVALSTRING (EP, "PLOT (FO, 10 * log10 (pxx));"); // Using the engine drawing

ENGEVALSTRING (EP, "Title ('Power Spectrum Analysis');");

ENGEVALSTRING (EP, "XLabel ('Hz');");

ENGEVALSTRING (EP, "YLABLE ('DB');");

MXDESTROYARRAY (T); // Release memory

MXDESTROYARRAY (MFS);

MXDESTROYARRAY (MNFFT);

MXDESTROYARRAY (RESULT);

ENGEVALSTRING (EP, "Close;");

ENGCLOSE (EP);

}

The above program is compiled under VC to use the two dynamic libraries of libeng.dll and libmx.dll to utilize the following commands: lib / def: E: / matLab / extern / include / *. DEF / MACHINE: IX86 /OUT :*.LIB Generates the static connection library libeng.lib and libmx.lib required for the program, add libeng.lib and libmx.lib to VC Project / Link / Object / Library Modules I.e.

2. Use Matlab's compiler to call the function in the toolbox

MATLAB compilers convert MATLAB's M files into c or c source code to generate a separate application of the MATLAB running environment, but the data of MATLAB itself describes the compiler if the compiler is used to establish an independent application program. Call the functions in the MATLAB toolbox, which is very disadvantageous to engage in some special algorithms. I studied for a while, Item of the function in the toolbox, since the M file, you can use a compiler to provide a call function such as a VC, but the compiler can only compile a separate M file, that is, this m file is not Depending on other M files. If other M files are called in the M file, copy the called M file to the corresponding position of the call M file, and make appropriate changes can be used to compile compilation. The compiler does not support graphics functions, so if there is a graphic function in the m file.

When MATLAB's compiler MCC adds an appropriate parameter -E -E (MCC -E *. *) Or -p (mcc -p *. *), You can generate C or C or C or C or C sources for a specific application to a specific application. Code. This way, if you want to compile the pass under the VC, you also need to connect the following library libmmfile.dll, libmatlb.dll, libmc.dll, libmat.dll. Libmx.dll. Mibut.dll, and Matlab C Math library, it is recommended to use the aforementioned Methods Dynamic connections are changed to static connections. For the setting of the C / C compilation environment, run the MEX -SETUP under the MATLAB Command window and then operate according to the prompt, and for the setting of the C / C connection environment, run the MBUILD -SETUP can be operated. The following is given to generate functions available to the MATLAB toolbox in the MATLAB toolbox.

Copy the PSD.m file into the MATLAB / BIN directory to rewrite the corresponding M files such as Nargchk.m, Hanning.m, etc. Configuration is concise for the generated code, which can make a large number of deletions without the collection of data processing input parameters, and ultimately make PSD.m a separate M file that does not depend on other M files, pay attention to the remarks The code is finally changed to the following form, limited to the paramount to give a key step:

Function [PXX, F] = PSD (FS, NFFT, NoverLAP, X)

WINDOW = O, 5 * (1-COS (2 * pi * (1: nfft) '/ (nffft 1))); // Hanning Window

DFLAG = 'None';

Window = window (;)

......................................

As long as you know the Matlab language and signal processing knowledge can complete this work.

Suppose the above code is re-existing as TestWin.m, set the environmental parameters to run MCC-E TestWin under the MATLAB Command window, and TestWin.c can be generated under matlab / bin, such as running MCC -P TestWin, generating TestWin.cpp.

Create a dialog-based file, then add a Button control onButtonPSD in the dialog, add the header file of the above-described .C file to the project. CPP, and will

#ifdef_cplusplus

Extern "C" {

#end IF

C code declarations between the VC contains files and generated .C, will

#ifdef_cplusplus

}

#end IF

Join the .CPP file is not tail.

For the sake of simplicity and facilitating processing, the generated C function is slightly changed, and some code is given as follows:

Void ctestpsdwindlg :: onbuttonpsd () {

MxArray * x_rhs _; // points to the acquisition data storage area

FS = 23510; // Data acquisition frequency NFFT = 1024; // 1024 points FFT

Double DataX [1024] // Collected data

X_RHS_MXCREATEDOUBLEMATRIX (1,1024, mxreal);

Memcpy (MxGetpr (X_RHS _), DATAX, 1024 * SIZEOF (DOUBLE));

Noverlap = 512;

...................

...................

Mccopy (& PXX, & SPEC);

Mccopy (& F, & FREVGG_VECTOR);

For (int J = 0; j <(int) (NFFT / 2 1); J )

{

DataP [J] = McCgetRealvectoreElement (& PXX, (J 1)); // Power Spectral Density Saves DataP [] array

DataF [J] = McCgetRealVectorElement (& F, (J 1)); // The corresponding frequency is stored in array dataf []

}

MccFreematrix (& PXX);

...................

SendMessageBox (WM_Paint, 0, 0); // Drawing graphics function under VC

Return;

}

The program readability as above is not very good, and the generated C code is readily readable, but pay attention to only the MATLAB's Math library, not available C Math library, otherwise compile will be wrong, limited to the space This is not described.

3. Use the function in the toolbox with MATCOM

Matcom compiles the M file, first translate the M file into the CPP source code according to the correspondence with MATcom's CPP library, and then compile the CPP file into the corresponding EXE or DLL file with the corresponding version of the CPP, so the first time you want Specifies the path to the C compiler, otherwise it will not be able to compile, specify the compilation information to write in the MATCOM / BIN / MATCOM.INI file, but this step When MATCOM is installed, it automatically looks for the compiler and write it to Matcom.ini. In the file, use the Teechart3.0 OCX control in Matcom 4.5, so it supports graphical operations.

We still use the above TestWin.m files, do not comment out the graphic function, use MIDEVA to generate a signal handler that can be called by the VC.

Running Mideva on the main interface to open the M file, select Compile to DLL in the menu, enter Testwin .. You can find such a file in the Matcom Debug directory, testwin.c, testwin.h, testwin.cpp, testwin. LIB, Testwin.dll, Testwin.exp, etc. Add the above TestWin.cpp and TestWin.h to the project, Project / Add to Project / Files and add "stdafx.h" plus connecting library in the appropriate file: Tools / Option / Directory /, select the incrude option, join E: / Matcom45 / lib (including matcom.h) library option, add E: / MATCOM45 / LIB. The Project / Add to Project / Files file type option is selected (.lib) to add E: /MATCOM45/LIB/v4501.lib to the project compilation. The corresponding code is as follows:

Void ctestmatcomdlg :: onpsdbutton () {

Double DataP [512], DATAF [512];

INITM (MATCOM_VERSION); // Initialize MATCOM Library

MM FS, NFFT, NoverLap; // Create Matrix

MM x = Zeros (1,1024);

FS = 1024; NFFT = 1024; NoverLap = 128;

DMM (PXX_O); DMM (f_o); // Create and name matrix

Datax []; // data acquisition data exists in this array

For (INT i = 1; i <= 1024; i )

{

X.R (1, i) = DATAX [i 1]; // assigns the X array

}

Testwin (FS, NFFT, NOVERLAP, X, I_O, PXX, F_O); // Matcom generated functions

For (i = 0; i <513; i ) {// Remove power spectral density analysis results

DATAF [I] = f_o.r (i 1, 1);

DataP [i] = pxx_o.R (i 1, 1);

exitm ();

Return;

}

It can be seen that using MATCOM for M file conversion is very easy. The generated code readability is very good, and the above conversion simultaneously generates a dynamic connection library available for VC calls, which is used in use and a general dynamic library. At the same time, it is necessary to specify MATCOM not only convert independent M files that do not depend on other M files, while converting M files nested by calling other M files. The condition is that this M file is below the same directory, as previously described PSD.m can be directly converted with the above method, generated a plurality of overloaded PSD functions. Conclusion: Use the function in the MTLAB Engine Call Toolbox to save a large number of system resources, the overall performance of the application is better, but it is not detachably operated by Matlab. Use the MATLAB compiler to call the toolbox function, you must convert the corresponding M files to become a separate M file, and does not support graphics functions, and the transcoded code readability is not very good. Conversion with MATCOM is very convenient, the generated code readability is very good, support graphics functions, and the speed of code is more faster than conversion. The above programs are adjusted in VC 6.0, Matlab5.2, Matcom 4.5, and the above methods have been well used in engineering practice.

转载请注明原文地址:https://www.9cbs.com/read-71761.html

New Post(0)