Does MATLAB code retain its accuracy when is executed in dsp kit? Ladies, Gantlemen,
Please let me ask your help on my problem. MATLAB code (at https://metrw-pitch.blogspot.com/ you can see code in .mlx (for run it you want delete comments) and .m format) executing in MATLAB online R2020b, outputs good accuracy even for low fundamental frequencies (fundFreq) but code (originally in my answer to a question of mine) of similar algorithm in C++ (acctually I have translated this code into Matlab's language), executing in Visual Studio 2019, outputs bad accuracy in low (lower than 150 Hz) frequencies.
Question is whether MATLAB's code executing in dsp kit retains its accuracy. I'm homeless (in France) and get internet access in Social Services and Post Offices like this I'm just now. So it's impossible for me to run code in dsp kit. You are kindly requested test MATLAB's code in your dsp kits and say me its output for fundFreq 50, 60, 70 Hz. Be aware when change FundFreq, change, if necessary, FFTfundFreq and/or GridDemiSpan so that grid's span covers FundFreq.
I have asked similar question on 8 Feb 2021 at 13:25, at MATLAB but nobody answered yet.
Copy of Matlab's code without comments is following.
format long;
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+...
150000*SinThirdHarmAngles;

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;
end

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

With regards and friendship, Georges Theodosiou
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.