From: skyworld on
Hi,

I wrote a mex file like this:

#include <stdio.h>
#include "math.h"
#include "mex.h"

#define DATA_NUM 512
#define COEF_NUM 7

void mexFunction(int nlhs,
mxArray *plhs[],
int nrhs,
const mxArray *prhs[]
)
{

mxArray *x_data_array;
double *x_data;
mxArray *coef_data_array;
double *coef_data;
double *y_data_array;

int i,j;
double tmp_x[DATA_NUM];

x_data_array = prhs[0];
x_data = mxGetPr(x_data_array);
coef_data_array = prhs[1];
coef_data = mxGetPr(coef_data_array);

plhs[0] = mxCreateDoubleMatrix(1,DATA_NUM,mxREAL);
y_data_array = mxGetPr(plhs[0]);


for (i=0;i<COEF_NUM;i++)
{
mexPrintf("%d,data=%d,coef=%d\n",i,x_data[i],coef_data[i]);
}

for (i=0;i<DATA_NUM;i++)
{
tmp_x[i] = 12;
mexPrintf("tmp_x=%d\n",tmp_x[i]);
tmp_x[i] = x_data[i];
mexPrintf("i=%d, x=%d, tmp_x=%d\n",i,x_data[i],tmp_x[i]);
}

for (i=0;i<DATA_NUM;i++)
{
for (j=0;j<COEF_NUM;j++)
{
y_data_array[i] += x_data[i+j]*coef_data[COEF_NUM-j-1];
}
}

return;

}

my purpose is to finish a fir function. I use this command to compile
it:
mex fir_check.c

and I infer it in matlab as this:

b = firhalfband(N,fc/fs);
x=cos(2*pi*t*f0)+cos(2*pi*t*f1)+cos(2*pi*t*f2)+cos(2*pi*t*f3);
y_dut = fir_check(x,b);

It is very interesting that I can't get right data from x_data,
coef_data and tmp_x. The display shows these data are very huge
numbers, which I think should be within +/- 4. And sometime when I run
the m file, the matlab will report run-time error. Could anybody help
me to find what is wrong with this code? thanks.


From: Jan Simon on
Dear Yong,

> double tmp_x[DATA_NUM];
> tmp_x[i] = 12;
> mexPrintf("tmp_x=%d\n",tmp_x[i]);

Your tmp_x is a double vector. Display doubles with the format "%f" or "%g", because "%d" formats integers.

Please post just the relevant part of the code and omit all lines, which are not involved in the runtime error.

Good luck, Jan
From: James Tursa on
skyworld <chenyong20000(a)gmail.com> wrote in message <9eb6e874-c2c3-4de1-ad17-446a3c028234(a)k8g2000prh.googlegroups.com>...
>
> I wrote a mex file like this:
>
(snip)
>
> It is very interesting that I can't get right data from x_data,
> coef_data and tmp_x. The display shows these data are very huge
> numbers, which I think should be within +/- 4. And sometime when I run
> the m file, the matlab will report run-time error. Could anybody help
> me to find what is wrong with this code? thanks.
>

You are probably exceeding valid indexes for the arrays. Impossible to tell for sure because you didn't post enough information about size of the inputs. e.g., I would suspect this line:

> y_data_array[i] += x_data[i+j]*coef_data[COEF_NUM-j-1];

Does x_data really contain 511+6+1 = 518 elements?

Try putting in some checks to see that your sizes and indexing are all consistent.

James Tursa
From: skyworld on
On Jul 8, 11:17 pm, "Jan Simon" <matlab.THIS_Y...(a)nMINUSsimon.de>
wrote:
> Dear Yong,
>
> >     double  tmp_x[DATA_NUM];
> >         tmp_x[i] = 12;
> >         mexPrintf("tmp_x=%d\n",tmp_x[i]);
>
> Your tmp_x is a double vector. Display doubles with the format "%f" or "%g", because "%d" formats integers.
>
> Please post just the relevant part of the code and omit all lines, which are not involved in the runtime error.
>
> Good luck, Jan

Hi Jan,

thanks for your kind information. You just point out one of my errors.
Thanks. A very annoying problem is that when I revised %d to %f, there
is no messages on command window output. When I change back to %d,
also no output. totally different from yesterday. That is my life....


regards
yong
From: skyworld on
On Jul 9, 2:35 am, "James Tursa"
<aclassyguy_with_a_k_not_...(a)hotmail.com> wrote:
> skyworld<chenyong20...(a)gmail.com> wrote in message <9eb6e874-c2c3-4de1-ad17-446a3c028...(a)k8g2000prh.googlegroups.com>...
>
> > I wrote a mex file like this:
>
>     (snip)
>
> > It is very interesting that I can't get right data from x_data,
> > coef_data and tmp_x. The display shows these data are very huge
> > numbers, which I think should be within +/- 4. And sometime when I run
> > the m file, the matlab will report run-time error. Could anybody help
> > me to find what is wrong with this code? thanks.
>
> You are probably exceeding valid indexes for the arrays. Impossible to tell for sure because you didn't post enough information about size of the inputs. e.g., I would suspect this line:
>
> >             y_data_array[i] += x_data[i+j]*coef_data[COEF_NUM-j-1];
>
> Does x_data really contain 511+6+1 = 518 elements?
>
> Try putting in some checks to see that your sizes and indexing are all consistent.
>
> James Tursa

Hi James,

thanks for your message. In fact I'm trying to write a code for FIR
check so that it could be used in HW co-simulation. My inputs to this
function are two arguments: x_data with 512 samples and coefficients
with 7 taps. In order to match matlab filter outputs, I guess I need
to pad 6 zeros before x samples. That is the reason I defined tmp_x.
My purpose would like to do this:
for (i=0 to i=5) tmp_x(i) = 0;
for (i=6 to i=518) tmp_x(i) = x_data(i-6);
then I will do fir multiply-addition as
y_data_array[i] += x_data[i+j]*coef_data[COEF_NUM-j-1];

I met two problems when doing above:
1) I can't get right data in tmp_x from x_data. I use code like these
to check if tmp_x got data from x_data
for (i=0;i<DATA_NUM;i++)
{
tmp_x[i] = 12;
mexPrintf("tmp_x=%d\n",tmp_x[i]);
tmp_x[i] = x_data[i];
mexPrintf("i=%d, x=%d, tmp_x=%d\n",i,x_data[i],tmp_x[i]);
}
the display shows two data are different.

2) If I use only 512 x sample to do fir calculation, the result seems
ok. But if I run this program several times, there maybe run-time
error. I don't know what kind of hidden issues in my code.

thanks.