In the linked case, it can be resolved through explicitly converting every integer variable into double format to force it being calculated exactly enough through all the process. However, as I called the FFT function, this has become more difficult. It seems that the original MATLAB code for FFT is meeting the same problem, but obviously I have no chance to edit the fft source code - even if I had it would be no good solution neither coz I cannot do this for every matlab function in my programming.
For example, a matlab function fft_test is implemented as following:
Code:
function X = fft_test(x)
X = abs(fft(x));
end
And after the export in C++ I have a function
extern void fft_test(const emxArray_real_T *x, emxArray_real_T *X);
Given a same input series:
(MATLAB)
Code:
for (i=1:1000)
x(i)=sin(2*3.14*(i-1)/50);
end
(C++)
Code:
for (int i=0;i<1000;i++)
x_in->data[i]=sin(2*3.14*i/50)
Different from what I thought, this time it is another problem other than the data type transformation.
In Matlab, if you want do to FFT in an arbitrary length like 1000, which is not an integer power of 2, Matlab will fix it for you (by doing an interpolation or zero-padding, I am not sure about it).
However, if you have some command like X=fft(x,1000) in your code, you'll get an error report when exporting it to C/C++, informing you that only a length of an integer power of 2 can be accepted.
Further, if you get some vector with a length of 1000 points and call the FFT function without explicitly giving the target length, like X=fft(x), it CAN pass the compiling WITHOUT any error or warning! However, the result will be completely different from what you mean to get
After manually fixing the target transformation length, the reported problem got solved.