Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] FFT in MATLAB gets wrong results when exported to C/C++ by MATLAB Coder

Status
Not open for further replies.

gabhades

Newbie level 4
Newbie level 4
Joined
Jan 21, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,330
Hi guys,

this is almost exactly the same problem as this already solved one:

https://www.mathworks.de/matlabcent...n-directly-called-in-matlab-and-called-in-exp

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)

The result in MATLAB is
Code:
 0.0480, 0.0543, 0.0703, 0.0917, 0.1164, 0.1440, 0.1746, 0.2087, 0.2471 ...

But in C++ it is
Code:
 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!
 

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.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top