Info
This question is closed. Reopen it to edit or answer.
Problems with Mex file execution
3 views (last 30 days)
Show older comments
Hi
I am facing problems with execution of a mex file.The purpose of the mex file is to return a array s,which is a statistical measure given by 1/1+mean(A)+std(A),where A corresponds to the first order difference of the variance of a phase matrix having 3 rows,each row corresponding to the phase of an oscillator(extracted by the hilbert transform).
The input arguments to the mex function are A and sliding window length(length in sample points) in that order respectively.I am employing a sliding window length of 10 seconds duration,which slides across the entire length of A by one sample point in each iteration.The original dataset from which phases are extracted is one hour long and the sampling frequency is 256Hz.This is resulting in a large number of iterations which is compelling me to use the mex file.
The mex file I have written is attached below.The c code is getting compile to a mex file.But during execution there is a segmentation fault.Since this is the first time I am writing a mex file,I needed some guidance.
#include "mex.h"
#include "matrix.h"
#include <math.h>
void mexFunction(
int nlhs,
mxArray* plhs[],
int nrhs,
const mxArray* prhs[]
)
{
mxArray* mx_phvar_grad = mxDuplicateArray(prhs[0]);
double *lf_phvar_grad = mxGetPr( mx_phvar_grad ),
win_len_inv,
*lf_moutput,
*lf_soutput,
lf_mean,
lf_std,
*lf_output,
*lf_ent_term;
int phvargrad_len = (int) mxGetNumberOfElements( prhs[0] ),
sli_win_len = (int) *mxGetPr( prhs[1] ),
output_len,
win_index,
output_index,
i,
j;
mwSize output_dims[2];
nlhs = 1;
output_dims[0] = 1;
output_dims[1] = phvargrad_len - sli_win_len + 1 ;
plhs[0] = mxCreateNumericArray(2,output_dims,mxDOUBLE_CLASS,mxREAL);
lf_output = mxGetPr(plhs[0]);
output_len = mxGetNumberOfElements(plhs[0]);
lf_ent_term = (double *) mxCalloc ( sli_win_len,
sizeof( double ) );
win_len_inv = 1 / (sli_win_len * 1.0);
win_index = phvargrad_len - sli_win_len + 1;
for(j = 0; j < win_index; j++)
{
output_index = j;
lf_phvar_grad[output_index];
for (i = 0; i < sli_win_len; i++)
{
lf_ent_term[i] = lf_phvar_grad[i];
lf_moutput[0] += lf_phvar_grad[i];
}
lf_mean = (lf_moutput[0] * win_len_inv);
for (i = 0; i < sli_win_len; i++)
{
lf_ent_term[i] = lf_phvar_grad[i];
lf_soutput[0] += ((lf_phvar_grad[i] - lf_mean) * (lf_phvar_grad[i] - lf_mean));
}
lf_std = sqrt(lf_soutput[0] * win_len_inv);
lf_output[output_index] = 1 / 1 + lf_mean + lf_std;
}
mxFree(lf_ent_term);
mxDestroyArray(mx_phvar_grad);
}
1 Comment
Kaustubha Govind
on 3 Jun 2011
Have you already tried debugging the function to see what line causes the issue? See http://www.mathworks.com/help/toolbox/simulink/sfg/bq2rjeu-1.html for guidance.
Answers (1)
Jan
on 3 Jun 2011
- Line 51: This statement has no effect.
- Line 59: lf_moutput is not defined
- Line 71: lf_soutput is not defined
- Line 77: "1 / 1 + lf_mean + lf_std" will most likely not do, what you expect. Some parenthesis are needed.
EDITED: Answer to your comment:
lf_ent_term = (double *) mxCalloc(sli_win_len, sizeof(double));
...
lf_ent_term = lf_phvar_grad;
// Now the original allocated memory for lf_ent_term
// cannot be accessed anymore!
...
mxFree(lf_ent_term);
// Now lf_phvar_grad is freed - crash
1 Comment
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!