Info

This question is closed. Reopen it to edit or answer.

Problems with Mex file execution

3 views (last 30 days)
Aniruddh
Aniruddh on 3 Jun 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
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
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
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
Aniruddh
Aniruddh on 4 Jun 2011
Hi,I am really sorry for the trouble.The code wasn't well written at all.I have modified the code to a large extent but it still gives the same problem.I am attaching the modified code.
#include "mex.h"
#include "matrix.h"
#include <math.h>
double xsquare(double x)
{
return (x * x);
}
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_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++)
{
lf_mean=0;
lf_std=0;
output_index = j;
lf_ent_term = lf_phvar_grad;
for (i = 0; i < sli_win_len; i++)
{
lf_mean += *(lf_ent_term);
lf_ent_term++;
}
lf_mean = (lf_mean * win_len_inv);
lf_ent_term = lf_phvar_grad;
for (i = 0; i < sli_win_len; i++)
{
lf_std += xsquare(*(lf_ent_term)-lf_mean);
lf_ent_term++;
}
lf_std=sqrt(lf_std * win_len_inv);
lf_output[output_index] = 1 / (1 + lf_mean + lf_std);
lf_phvar_grad=lf_phvar_grad + 1 ;
}
mxFree(lf_ent_term);
mxDestroyArray(mx_phvar_grad);
}

This question is closed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!