MATLAB code (at https://metrw-pitch.blogspot.com/ you can see improved code) executing in MATLAB online R2020b, outputs good accuracy even for low fundamental frequencies (fundFreq) but the code (originally in the answer to a question of mine) for a similar algorithm in C++ (ported in Matlab), executing in Visual Studio 2019, outputs bad accuracy in low (lower than 150 Hz) frequencies.

My question is whether MATLAB's code executing in a dsp kit retains its accuracy.
I'm homeless (in France) and get internet access only at Social Services offices and Post Offices, so it's impossible for me to run code with a dsp kit. Would you please test MATLAB's code in your dsp kits and tell me its output for fundFreq 50, 60 and 70 Hz? Be aware that when changing fundFreq, also change, if necessary, FFTfundFreq and/or GridDemiSpan so that the grid's span covers fundFreq.

I have asked a similar question on 8 Feb 2021 at 13:25, at MATLAB but nobody answered yet.

Copy of Matlab's code without comments is following.

SampFreq = 16000;
Segm = 1:1600;
FundFreq = 50;
FFTfundFreq = 41;
GridDemiSpan = 10;

FirstHarmAngles = FundFreq*2*pi/SampFreq*Segm+1.9*pi;
SinFirstHarmAngles = sin(FirstHarmAngles);
SecondHarmAngles = FundFreq*2*2*pi/SampFreq*Segm+0.9*pi;
SinSecondHarmAngles = sin(SecondHarmAngles);
ThirdHarmAngles = FundFreq*3*2*pi/SampFreq*Segm+0.3*pi;
SinThirdHarmAngles = sin(ThirdHarmAngles);
Xn = 170000*SinFirstHarmAngles+220000*SinSecondHarmAngles+...

Freqs = FFTfundFreq-GridDemiSpan:0.1:FFTfundFreq+GridDemiSpan;
MagnSqrd = ones(1,201);

for f = 1:201
   Angles = Freqs(f)*2*pi/SampFreq*Segm;
   XnCos = sum(Xn.*cos(Angles));
   XnSin = sum(Xn.*-sin(Angles));
   MagnSqrd(f) = XnCos.^2+XnSin.^2;

[maxMagnSqrd, maxMagnSqrdIndex] = max(MagnSqrd);
GRIDfundFreq = Freqs(maxMagnSqrdIndex);

2 Answers 2


Yes, C\C++ code generated from Matlab/Simulink code should behave the same as the original code.

However there are a few caveats, here are 3 problems I've experienced in real-life.

1 - Simulink\Matlab performs an implicit reset just before starting the simulation/script. Make sure you perform this reset in your C\C++ code, otherwise your results won't be the same and won't be comparable. Simulink coder generates a reset function for you. I've seen a corner case where the reset function would not reset all global variables, so be careful.

2 - Variable types. Matlab/Simulink typically use double-precision floating point variables. If your code uses doubles then great. The behaviour should be the same. However, for performance reasons, you might choose to use single-precision floating point variable aka floats. Then the behaviour could change slightly. There are ways to mitigates the effect of quantization, some algorithms are really sensitive to precision, others less so. Even if you use doubles there are some difference between platforms. So if you use doubles on an ARM µProcessor, the behaviour could be slightly different in corner cases.

3 - Integer saturation. I made the mistake in the past of using a 32-bit integer in Matlab as a free-running counter. It worked well, but after like 10 days the code would freeze. The counter would be stuck at 2^32-1 instead of wrapping back to 0. Obviously, you won't see that behaviour in simulation. So be careful, test your code thoroughly.

  • $\begingroup$ Mr. Ben, please accept my many thanks for you answered my question. Could you please let me know code's output for FundFreq 50, 60, 70 Hz executing in dsp kit? Regards. $\endgroup$ Feb 15, 2021 at 14:47
  • $\begingroup$ Mr. Ben please let me comment your suggestion for double precission. For fundamental frequency 50 Hz, code C++ outputs 53.2 Hz with either double or float precission. Matlab's code (executing in MATLAB online R2020b) with short (float) precission (format) outputs 50.6000 Hz, and with long (double) outputs 50.600000000000001. Default format of MATLAB online R2020b is short (float). Regards. $\endgroup$ Feb 16, 2021 at 9:42
  • $\begingroup$ @GeorgeTheodosiou format short and format long do not change the computation, only the display of values at the end. MATLAB always uses double precision float in computation, unless you specifically cast a matrix to type single. When generating C code, you can choose between float (single precision, 32 bit) and double (64 bit). $\endgroup$ Aug 20, 2022 at 15:53
  • $\begingroup$ @Cris Luengo: Many thanks for your instructive information. I'm beginner in MATLAB and I need it. $\endgroup$ Aug 23, 2022 at 13:10
  • $\begingroup$ @Ben, please let me ask you the time your dsp processor executes 16384 points FFT. Regards. $\endgroup$ Sep 21, 2022 at 7:57

As long as your C++ implementation uses the same data types and the same algorithm structures the results should either be very close or even bit-exact, provided both platforms are compliant with "normal" standards, i.e. IEEE-754.

The link to the Matlab code doesn't work ("forbidden"). A quick look at your C++ code shows that you are using float as your main data type, which has 32-bits of precision. Matlab's default data type is double which is a 64-bit type. Unless your Matlab code specifically uses single, I would expect your Matlab code to perform much better than the C++ code.

  • $\begingroup$ Mr. Hilmar, please accept my many thanks for you answered my question. I have posted link's URL itself. Please let me know whether you see code. Matlab's code output 50.6000 is single number. Regards. $\endgroup$ Feb 10, 2021 at 13:31
  • $\begingroup$ I would have voted up, but most DSP algorithms should be well conditioned. Their performance should not be affected much by using singles vs doubles. $\endgroup$
    – Ben
    Feb 10, 2021 at 13:34
  • $\begingroup$ For example an order-8 IIR filter might be unstable using singles and be stable using doubles. However, a clever DSP engineer would split the order-8 IIR filter in 4 cascaded biquad filters. Biquad filters are less sensitive to quantization. $\endgroup$
    – Ben
    Feb 10, 2021 at 13:37
  • $\begingroup$ Mr. Ben, please accept my many thanks for you commented my question. Both codes output same for single and double numbers. $\endgroup$ Feb 10, 2021 at 13:38
  • $\begingroup$ Ladies, Gentlemen, please let me say my idea. Binaries obtained by Matlab's Coder in .lib or .dll libraries and are executed in dsp kit should output same accuracy, as code when is executed in MATLAB's compiler. Regards. $\endgroup$ Feb 12, 2021 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.