This loop is wrong for the real part sigr:
for (i = 0; i<N; i++) {
signal[i] = sigr[i]; // should be signal[2*i] = etc.
signal[2*i+1] = sigi[i];
}
You ignored my previous advice about the size of gft. Since it is the interleaved complex data, it needs to be allocated to 2*N, not N (this is what is causing the seg fault):
gft = mxCalloc(N,sizeof(*gft)); // should be 2*N, not N
Also, as an efficiency suggestion, I would not allocate a 0 block for sigi up front. That is wasted effort and memory and slows down the code. I know this makes the downstream code simpler, but I would still opt for a bit of extra code downstream to handle it vice extra memory & copying. E.g., something like this:
if (mxIsComplex(prhs[0])) {
sigi = mxGetPi(prhs[0]);
for (i = 0; i<N; i++) {
signal[2*i ] = sigr[i];
signal[2*i+1] = sigi[i];
}
} else {
for (i = 0; i<N; i++) {
signal[2*i ] = sigr[i];
signal[2*i+1] = 0.0; // might not need this, see below
}
}
This also avoids the need to conditionally free sigi downstream (which you did not do btw). All of the mxCreateEtc API functions 0 out the data areas, which is why you don't need to explicitly do this in your code. Additionally, it appears that mxMalloc also 0's out the memory it returns from all tests I have done. If you don't want to rely on that then use mxCalloc or explicitly 0 out the imaginary elements in your code.
Best Answer