Current location - Education and Training Encyclopedia - Graduation thesis - Regarding the application of MATLAB in signal and system analysis, the writing method of this paper is new to me.
Regarding the application of MATLAB in signal and system analysis, the writing method of this paper is new to me.
Related papers:

How to use Matlab toolbox for digital signal processing under Vc++

This paper introduces how to use Matlab toolbox to process digital signals in Vc environment. Taking the power spectral density analysis function in Matlab toolbox as an example, this paper introduces how to call the toolbox function through the engine of Matlab, the compiler of Matlab and Matcom of MathTools Company.

Keywords: Matlab M file engine compiler Matcom Vc++

The signal processing toolbox of Matlab is a collection of signal algorithm files, and its basic objects are signals and systems. The signal processing toolbox is located in the directory Toolbox\Signal, and the files in the toolbox can be used to realize signal transformation, filtering, spectrum estimation, filter design and so on. In other environments, such as Vc, if you can call the files in Matlab toolbox, it will greatly speed up the implementation of some algorithms and have high reliability.

Using Matlab engine

Matlab engine adopts client and server computing methods. In the application, the C language of Vc or the program of C++ language is used as the front-end client to send commands and data information to the Matlab engine and receive data information from the Matlab engine. It provides the following functions: Engopen, Enggetarray, Engputarray, Engevasting,

EngOutputBuffer, engClose interacts with the client.

The following routine is to build a dialog-based application under Vc, set a button control on the dialog box, and add "engine.h" and "math.h" header files in the dialog box. Cpp file. The following are some programs.

void CtestmatlabDlg::OnMatlabEngine(){

Engine * ep

mxArray* T=NULL,*result=NULL,*mFs=NULL,* mnfft = NULL

double datax[ 1024];

Char buffer [1024];

for(int j = 0; j & lt 1024; J++)// Note: If the data collected by the acquisition card can be put into the datax[] array, this loop is not needed.

{

double samt =(double)( 1.0/ 1024);

datax[j]= sin(2.0 * 63.0 * samt * 3. 14 15926+ 1. 15 * 3. 14 15926);

}

Double *pPxx, * pFxx

If (! (EP = eng Open(" \ 0 "){// Open the Matlab engine and establish a connection with the local Matlab.

Fprintf(stderr, "\nUnable to start MATLAB engine \ n");

Exit (-1);

}

Double fs [1] = {1024}; //Because the parameters involved in Matlab operations are in the form of matrix, the following lines will change the parameters.

double nfft[ 1]= { 1024 }; //into a matrix form acceptable to Matlab.

T=mxCreateDoubleMatrix( 1, 1024,MX real);

Mn FFT = mxCreateDoubleMatrix( 1, 1,MX real);

mFs = mxCreateDoubleMatrix( 1, 1,MX real);

mxSetName(T," T ");

mxSetName(mnfft,“Mn FFT”);

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 converted parameters into the engine. At this time, you can view the parameters in the Matlab command window.

engPutArray(ep,Mn FFT);

engPutArray(ep,mFs);

engEvalString(ep,"[pxx,fo]=psd(T,mnfft,mFs);" ); //Use the engine to execute the files in the toolbox.

engOutputBuffer(ep,Buffer,5 12); //If you only want to see the displayed graphics, you can remove the return parameter. Psd will automatically draw graphics, and will not return parameters by default.

result=engGetArray(ep," pxx "); //Take out the data from the engine and put it into the indicated area for subsequent processing.

PPxx=mxGetPr (result);

result=engGetArray(ep," fo ");

PFxx=mxGetPr (result);

engEvalString(ep," plot(fo, 10 * log 10(pxx));" ); //Drawing with an engine

EngEvalString(ep, "Title ('Power Spectrum Analysis');" );

engEvalString(ep," xlabel(' Hz ');" );

engEvalString(ep," ylable(' db ');" );

mxDestroyArray(T); //Free memory

mxDestroyArray(mFs);

mxDestroyArray(Mn FFT);

MxDestroyArray (result);

engEvalString(ep,“close”);

eng close(EP);

}

To compile the above program under Vc, you need to use the following commands for two dynamic libraries, libmx.dll and libmx.dll:

lib/def:& lt; The installation path with Matlab, the same below > E: \ MATLAB \ extern \ include \ *. def/machine: ix86/out: *。 Lib to generate the static connection libraries libeng.lib and libmx.lib needed by the program. Just add the directories where libeng.lib and libmx.lib are located to VC++project/link/object/librarymodules.

Call the functions in the toolbox with the compiler that comes with Matlab.

The compiler of Matlab can convert the M file of Matlab into the source code of C or C++, and generate an independent application completely out of the running environment of Matlab. But the data of Matlab itself shows that if it is used to build an independent application, the compiler can't call the functions in the Matlab toolbox, which is very unfavorable for some special algorithms. I studied it for a period of time and found that because the functions in the toolbox are all M files, a compiler can compile them to provide calling functions such as Vc, but the compiler can only compile an independent M file, that is, this M file does not depend on other M files. If other M-files are called in the M-file, the called M-file can be copied to the corresponding location of the calling M-file, and can be used for compiler compilation after appropriate modification. The compiler does not support graphic functions, so if there are graphic functions in the M file, you need to comment them out.

When the compiler mcc of Matlab adds the appropriate parameter-E (MCC–E *). *) or-p (MCC–p *). *), which can generate the input M file and convert it into C or C++ source code suitable for a specific application. In this way, if you want to compile under Vc, you need to connect the following libraries: libmmfile.dll, libmatlb.dll, libmcc.dll, libmat.dll, libmx.dll, mibut.dll and Matlab C MATH. It is suggested that the dynamic connection be changed to static connection by the above method. For the setting of C/C++ compilation environment, run mex -setup under the Matlab command window and follow the prompts, while for the setting of C/C++ connection environment, run mbuild -setup and follow the prompts.

Here is a function that can be called by Vc by using compiler to generate psd.m file in Matlab toolbox.

Copy the psd.m file to the Matlab\bin directory, and rewrite the corresponding calling m files such as nargchk.m, hanning.m, etc. In order to generate concise code, a large number of deletions can be made when the input parameters of data acquisition and processing are clear, and psd.m will eventually become an independent M file independent of other M files. Be careful not to comment out the drawing code, and finally change it to the following form. Due to the limited space, the key steps are given:

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 Matlab language and signal processing knowledge, you can finish this work.

Assuming that the above code is saved as testwin.m again, setting environmental parameters and running mcc -e testwin in the Matlab command window, testwin.c can be generated in Matlab\bin, and testwin.cpp can be generated if mcc -p testwin is run.

Create a file based on dialog box under Vc, and then add a button control to the dialog box on ButtonPsd.

Add the generated header file. C file is added to. Cpp of the project and add #ifdef_cplusplus.

External "c"{

# End if

Add a C code declaration between the Vc include file and the generated include file. c.

Will #ifdef_cplusplus

}

#end if add. Cpp file.

In order to be concise and easy to handle, the generated C function is slightly changed, and some codes are given as follows:

void ctestpsddwindlg::OnButtonPsd(){

MxArray * x _ rhs _// points to the acquisition data storage area.

fs = 235 10; //data acquisition frequency nfft =1024; ///the fft at kloc-0/024.

Double datax[ 1024]// collected data

x _ RHS _ mxCreateDoubleMatrix( 1, 1024,MX real);

memcpy(mxGetPr(x_rhs_),datax, 1024 * sizeof(double));

nover lap = 5 12;

……………….

……………….

mccCopy(& amp; pxx & amp; spec);

mccCopy(& amp; f & amp; frevgg _ vector);

for(int j = 0; j & lt(int)(nfft/2+ 1); j++)

{

datap[j]= mccGetRealVectorElement(& amp; Pxx,(j+ 1)); //The power spectral density is stored in the datap[] array.

dataf[j]= mccGetRealVectorElement(& amp; f,(j+ 1)); //The corresponding frequency is stored in the array dataf[].

}

mccFreeMatrix(& amp; pxx);

……………….

SendMessageBox(WM_PAINT,0,0); //Drawing with the graphic function in Vc

Return;

}

The generated program is not very readable, but the generated c++ code is very readable. However, it must be noted that only the mathematical library of Matlab can be used, but not the mathematical library of c++, otherwise the compilation will be wrong and the space will be limited.

3) Call the functions in the toolbox with Matcom.

Matcom compiles m files. Firstly, it translates the M file into cpp source code according to the correspondence with Matcom's cpp library, and then compiles the cpp file into the corresponding exe or dll file with the corresponding version of C compiler. Therefore, the path of the C compiler must be specified for the first run, otherwise it cannot be compiled, and the specified compilation information is written into the Matcom\bin\matcom.ini file. However, when installing matcom in this step, it will automatically find the compiler and write it into the matcom.ini file. Matcom version uses TeeChart3.0 OCX control, so it supports graphical operation.

Let's still use the testwin.m file above, and don't comment out the graphics function. We use Mideva to generate signal processing programs that Vc can call.

Run Mideva and directly open the M file in the main interface. Select Compile into dll in the menu and enter testwin ... You can find several files similar to TestWin. c,TestWin。 h,TestWin。 CPP,TestWin。 Lib,TestWin。 DLL,TestWin。 Exp et al. In the Matcom debugging directory.

Add the above testwin.cpp and testwin.h to the project, project/add to project/files, and add "stdafx.h" to the corresponding file.

Add the link library: Tools\option\directory\, select the include option, and add e:\matcom45\lib (including matcom.h).

Library option, add e:\matcom45\lib.

4) Project \ Add to Project \ File File Type Option (. Lib) Add e: \ matcom45 \ lib \ v4501.lib to the project for compilation and running. The corresponding code is as follows:

void CtestmatcomDlg::OnpsdButton(){

Double data p[5 12], data f [512];

initM(MATCOM _ VERSION); //Initialize the matcom library

Mm Fs, nfft, noverlap// Create a matrix.

Mm x = zero (1,1024);

fs = 1024; nfft = 1024; nover lap = 128;

dMm(Pxx _ o); dMm(f _ o); //Create and name the matrix

datax[]; //The collected data is stored in this array.

for(int I = 1; I<= 1024; i++)

{

x.r( 1,I)= datax[I+ 1]; //Assign a value to the X array

}

testwin(Fs,nfft,noverlap,x,i_o,Pxx,f _ o); //Functions generated by //matcom

for(I = 0; I<5 13; I++){// Take out the power spectral density analysis results.

dataf[i]=f_o.r(i+ 1, 1);

datap[i]=Pxx_o.r(i+ 1, 1); }

exit m();

Return;

}

It can be seen that it is very easy to convert M files with Matcom, and the generated code is very readable. The above transformation also generates a dynamic link library for Vc to call as a general dynamic library. At the same time, it should be pointed out that Matcom can not only convert independent M files that do not depend on other M files, but also convert M file nesting that calls other M files. Assuming that this M file is in the same directory, the psd.m mentioned above can be directly converted by the above method to generate multiple overloaded psd functions.

Conclusion: Using Mtlab engine to call the functions in the toolbox can save a lot of system resources, and the overall performance of the application is good, but it can't run without Matlab. When using Matlab compiler to call toolbox functions, it is necessary to convert the corresponding M file into an independent M file, which does not support the graphic function, so the readability of the converted code is not very good. It is very convenient to convert with Matcom, and the generated code is very readable, which supports graphic function, and the code execution speed is more than 1.5 times faster than that without conversion. The above programs have been debugged in VC++6.0, MATLAB 5.2 and MATCOM 4.5, and the above methods have been well applied in engineering practice.

For reference only, please learn by yourself.

I hope it helps you.