Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mex error involving xtimesy.exp and xtimesy.lib

24 views
Skip to first unread message

Steven

unread,
Nov 9, 2009, 1:33:03 PM11/9/09
to
Hello! I am trying to learn how to write mex files, going through the example *.c files provided, the help manual, etc. I have been able to compile the example *.c files using the command

>> mex filename.c;

and I have also been able to write my own simple *.c files which compiled fine.

The latest step in developing my own code involved a call to a MATLAB function, something simple like "abs()" which takes a single input argument and returns a single output argument. Assuming I've got all my variables properly defined, the call looks like this:

mexCallMATLAB(1,y,1,x,"abs");

where y=abs(x); is the normal MATLAB command.

At this point I started getting errors and now I can't even compile the example *.c files (e.g. xtimesy.c), because I am told:

>>Could Not Find <path>/xtimesy.exp
>>Could Not Find <path>/xtimesy.lib

Furthermore, when I go back to my own code and take out the "abs" function call, sometimes it works fine but usually MATLAB crashes.

Any ideas....???

Thanks very much for the help!

Steven

James Tursa

unread,
Nov 9, 2009, 2:21:03 PM11/9/09
to
"Steven " <slu...@artsci.wustl.edu> wrote in message <hd9n8v$cdn$1...@fred.mathworks.com>...

You need to post your code for more detailed help from us. It *sounds* like you might have memory corruption somewhere as a result of invalid mex code, but it is impossible to tell until you post your code. This type of error will not necessarily manifest itself when you call the mex routine and corrupt memoty, but later when the corrupted memory is accessed. e.g., you run the mex routine and everything looks like it worked, but a few commands later you do something simple and it doesn't work. The only choice is to shut down MATLAB and restart it.

James Tursa

Steven

unread,
Nov 9, 2009, 3:05:18 PM11/9/09
to
> You need to post your code for more detailed help from us. It *sounds* like you might have memory corruption somewhere as a result of invalid mex code, but it is impossible to tell until you post your code. This type of error will not necessarily manifest itself when you call the mex routine and corrupt memoty, but later when the corrupted memory is accessed. e.g., you run the mex routine and everything looks like it worked, but a few commands later you do something simple and it doesn't work. The only choice is to shut down MATLAB and restart it.
>
> James Tursa

Thanks. Here's my code, then. It is ultimately supposed to calculate the spectrogram of a speech signal. The code compiles (i.e. I get no errors or warnings after >> mex spec.c;), but when I run it MATLAB crashes.

I see now at least one problem in my use of the variable y. However, if I comment out the definition of y and the "abs" function call and recompile, MATLAB still crashes, whereas it didn't use to....

I think you're right that it has to involve memory. When MATLAB crashes, it first puts up a long message on the MATLAB desktop, beginning with the words "Segmentation violation".

The code:
****
#include <math.h>
#include "mex.h"

void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// INPUTS
// *s points to the speech signal.
// *window points to a windowing function (e.g. a hamming window)
double *s, *window;

// fs is samping rate, step is the number of points to step between fft frames
mwSize fs, nfft, step;

// STUFF ABOUT THE INPUTS
// windowlength is the length of the hamming window, n is the length of *s
// nCols is the number of fft frames to be calculated
mwSize n, windowlength, nCols;

// INTERNALLY USED VARIABLES
// *ws is the windowed sample of the speech signal
double *ws;

// y is a dummy variable to test mexCallMATLAB(1,y,1,ws,"abs");
// The eventual goal is to successfully call "fft".
double *y;

// These will eventually be the real and imaginary parts of the fft
double *re, *im;

// COUNTERS
// These are simple counters
mwSize offset, k, counter=0, frame = 0;

// OUTPUT
// This will eventually be the magnitude spectrogram
double *S;

if(nrhs!=5) {
mexErrMsgIdAndTxt("MyToolbox:testmex:nrhs","Five inputs required.");
}
if(nlhs!=1) {
mexErrMsgIdAndTxt("MyToolbox:testmex:nrhs","One output required.");
}
if(mxGetN(prhs[0])!=1) {
mexErrMsgIdAndTxt("MyToolbox:testmex:nrhs","Signal not a column vector.");
}
if(mxGetNumberOfElements(prhs[0])<=mxGetNumberOfElements(prhs[4])) {
mexErrMsgIdAndTxt("MyToolbox:testmex:nrhs","Signal is too short.");
}
if(mxGetNumberOfElements(prhs[1])!=1 || mxGetNumberOfElements(prhs[2])!=1 || mxGetNumberOfElements(prhs[3])!=1) {
mexErrMsgIdAndTxt("MyToolbox:testmex:nrhs","fs, nfft, or noverlap are not scalar.");
}

// DEFINE THE VARIABLES
s = mxGetPr(prhs[0]);
n = mxGetNumberOfElements(prhs[0]);
fs = mxGetScalar(prhs[1]);
nfft = mxGetScalar(prhs[2]);
step = mxGetScalar(prhs[3]);
window = mxGetPr(prhs[4]);
windowlength = mxGetNumberOfElements(prhs[4]);
nCols = (n-windowlength)/step;

plhs[0] = mxCreateDoubleMatrix(windowlength,nCols,mxREAL);
S = mxGetPr(plhs[0]);

y = mxCreateDoubleMatrix(windowlength,1,mxREAL);

// FOR EACH SEGMENT OF THE SIGNAL...
for(offset=0;offset<n-windowlength;offset=offset+step){

// Get the windowed signal
for(k=0;k<windowlength;k++){
ws[k] = s[offset+k]*window[k];
}

/* THIS IS WHAT I EVENTUALLY WANT TO DO (in plain MATLAB form)
* y = fft(ws,nfft);
* Y = 20*log10(abs(y));
* spec = Y(1:nfft/2,:);
*/

// TEST CALLING A MATLAB FUNCION: y = abs(ws);
mexCallMATLAB(1,y,1,ws,"abs");

// For now, just let re and im be the windowed signal
re = ws;
im = ws;

// Get the magnitude spectrum using
for(k=0;k<windowlength;k++){
S[k+frame*windowlength]= 20*log10(sqrt(pow(re[k],2)+pow(im[k],2)));
}
// Advance to the next frame
frame++;
}

}

James Tursa

unread,
Nov 9, 2009, 5:16:02 PM11/9/09
to
"Steven " <slu...@artsci.wustl.edu> wrote in message <hd9slu$mev$1...@fred.mathworks.com>...

>
> // INTERNALLY USED VARIABLES
> // *ws is the windowed sample of the speech signal
> double *ws;
>
> // y is a dummy variable to test mexCallMATLAB(1,y,1,ws,"abs");
> // The eventual goal is to successfully call "fft".
> double *y;

You have a basic misunderstanding of the mexCallMATLAB function. The 2nd and 4th arguments need to be of type mxArray *[], not double *. That is the basic cause of your seg faults and crashes. e.g., you need these definitions:

mxArray *ws[1];
mxArray *y[1];

Then set ws[0] to an appropriate mxArray variable. Then you can call mexCallMATLAB like this:

mexCallMATLAB(1,y,1,ws,"abs");

Then you can get at the data of the result y like this:

double *ypr, *ypi;
:
ypr = mxGetPr(y[0]);
ypi = mxGetPi(y[0]);

And when you are done with the data free the memory:

mxDestroyArray(y[0]);

James Tursa

Steven

unread,
Nov 9, 2009, 6:11:02 PM11/9/09
to
James,

Thanks for your help with this.

Meanwhile, even when I have completely commented out any lines of code referring to *y or the the "abs" function call, MATLAB is crashing. And I can't compile the original example mex files anymore (e.g. xtimesy.c). Do I need to reinstall MATLAB to get it working again? I've never been at a complete loss what to do with MATLAB before, but I guess there's a first time for everything.

Thanks,
Steven Lulich

James Tursa

unread,
Nov 9, 2009, 6:32:02 PM11/9/09
to
"Steven " <slu...@artsci.wustl.edu> wrote in message <hda7i6$fmf$1...@fred.mathworks.com>...

Can you post the exact code you are using that is still crashing?

James Tursa

Steven

unread,
Nov 9, 2009, 7:14:02 PM11/9/09
to
> Can you post the exact code you are using that is still crashing?
>
> James Tursa

Two parts to this answer:

1) No need anymore. I have been playing around with it and figured out I was still not using mxArray data correctly. It now compiles and runs without error, and it might actually be returning a correct spectrogram now (haven't tested it for further bugs yet).

2) At issue now is the fact that a) this morning I could compile and run the built-in example functions (e.g. xtimesy.c), before I started having mxArray issues, b) after my first attempt with mxArray data, I could no longer even compile the example functions, and c) after resolving the mxArray issues (thanks to your help), I still cannot even compile the example functions. Here's what happens:

>> pwd

ans =

<...>\MATLAB\R2009b\extern\examples\refbook

>> mex xtimesy.c

Could Not Find C:\Program Files\MATLAB\R2009b\extern\examples\refbook\xtimesy.exp
Could Not Find C:\Program Files\MATLAB\R2009b\extern\examples\refbook\xtimesy.lib

>>


Any ideas what's wrong or how to fix this?

Thanks again,

Steven Lulich

James Tursa

unread,
Nov 10, 2009, 12:18:00 AM11/10/09
to
"Steven " <slu...@artsci.wustl.edu> wrote in message <hdab8a$2uh$1...@fred.mathworks.com>...

>
> 2) At issue now is the fact that a) this morning I could compile and run the built-in example functions (e.g. xtimesy.c), before I started having mxArray issues, b) after my first attempt with mxArray data, I could no longer even compile the example functions, and c) after resolving the mxArray issues (thanks to your help), I still cannot even compile the example functions. Here's what happens:
>
> >> pwd
>
> ans =
>
> <...>\MATLAB\R2009b\extern\examples\refbook
>
> >> mex xtimesy.c
>
> Could Not Find C:\Program Files\MATLAB\R2009b\extern\examples\refbook\xtimesy.exp
> Could Not Find C:\Program Files\MATLAB\R2009b\extern\examples\refbook\xtimesy.lib
>
> Any ideas what's wrong or how to fix this?

Not sure, but are you trying to compile the files in the original directories? You shouldn't do this. Copy the source files to another directory (some working directory) on the MATLAB path and compile them there.

James Tursa

Gadi Reinhorn

unread,
Nov 10, 2009, 9:42:54 AM11/10/09
to
Steven,

You might also try adding the "-v" flag to mex so that you can get more
verbose output to identify what is happening.

i.e.
mex -v xtimesy.c

Gadi

0 new messages