FFT is getting wrong result when exported to C/C++ by MATLAB Coder

9 views (last 30 days)
Hi guys,
this is almost exactly the same problem as I got in this question, which is already solved:
In the last case, I explicitly converted 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:
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)
for (i=1:1000)
x(i)=sin(2*3.14*(i-1)/50);
end
(C++)
for (int i=0;i<1000;i++)
x_in->data[i]=sin(2*3.14*i/50)
The result in MATLAB is
0.0480, 0.0543, 0.0703, 0.0917, 0.1164, 0.1440, 0.1746, 0.2087, 0.2471 ...
But in C++ it is
2.16108, 0.621004, 0.339259, 0.305906, 0.453614, 0.696989, 0.773992, 0.648152...
Is there any possible solution for this problem? Thanks alot!

Accepted Answer

Bin Han
Bin Han on 8 May 2013
Edited: Bin Han on 8 May 2013
Problem solved.
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.
  3 Comments
Ryan Livingston
Ryan Livingston on 14 May 2015
Error reporting is removed in standalone code so there is no error at runtime. When generating code, there is no error because it appears that the inputs were configured to be variable size so MATLAB Coder cannot be certain that the length is disallowed.
However, if a MEX file were generated and run with a non-power-of-2 length input, an error stating that as a requirement would be given. It is always a good idea to generate and test a MEX function before generating standalone code to verify your initial design. The documentation describes this in the suggested workflow.
liticer john
liticer john on 15 May 2015
Thank you very much for your help! I do agree with your idea to verify a function. But now I need to do fft2 with a non-power-of-2 length input, in matlab, it works right. However, when generating C++ code with Matlab Coder, it doesn't work. What should I do to solve this?

Sign in to comment.

More Answers (0)

Categories

Find more on Code Generation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!