Matlab Coder Memory usage

7 views (last 30 days)
Trickster Tricksterer
Trickster Tricksterer on 1 Aug 2019
Hello everyone.
I have just downloaded this app (MATLAB Coder) and I am having problems while embedding the code. Due to my RAM memory limitations I can't go on with my project.
I would like to know how can I see how much memory my code needs, and also how can I improve memory efficiency.
Thanks in advance.

Answers (2)

Ryan Livingston
Ryan Livingston on 1 Aug 2019
Without specifics of your project it's hard to give precise suggestions. The MATLAB Coder documentation has a performance section:
that contins a memory usage sub-section with some suggestions.
The code generation report with Embedded Coder will show you stack usage estimates:
Finally if too much heap memory is being used consider using a memory profiler, like Massif, on the generated code to see where the memory usage is coming from. Once you find that you can use the Embedded Coder code traceability features:
to map the problematic C or C++ back to the corresponding MATLAB code. That will give you insight into what part(s) of your MATLAB algorithm are causing high memory usage.

Trickster Tricksterer
Trickster Tricksterer on 4 Aug 2019
Thank you so much for your answer.
I'm trying to upload a signal processing algorithm to a embedded device: C2000 Piccolo LAUNCHXL-F280049C (Texas Instruments). This algorithm is quite complex and not every device can handle it:
/*
* Student License - for use by students to meet course requirements and
* perform academic research at degree granting institutions only. Not
* for government, commercial, or other organizational use.
*
* zhang_definitivo_C2000.c
*
* Code generation for function 'zhang_definitivo_C2000'
*
*/
/* Include files */
#include "zhang_definitivo_C2000.h"
#include "matlabCodegenHandle.h"
#include "any.h"
#include "sqrt.h"
#include "sum.h"
#include "power.h"
#include "abs.h"
#include "SystemCore.h"
#include "fft.h"
#include "zhang_definitivo_C2000_rtwutil.h"
/* Function Definitions */
double zhang_definitivo_C2000(void)
{
double z;
int C4;
static const double dv0[320] = { 0.0, 0.0196336924606283, 0.03925981575906861,
0.05887080365118904, 0.078459095727844944, 0.0980171403295606,
0.11753739745783766, 0.13701234168196802, 0.15643446504023087,
0.17579627993435454, 0.19509032201612825, 0.21430915306505074,
0.23344536385590542, 0.252491577015158, 0.27144044986507426,
0.29028467725446233, 0.3090169943749474, 0.32763017956169355,
0.346117057077493, 0.36447049987914965, 0.15307337294603593,
0.16029953324125643, 0.16746389501497125, 0.17456369626933685,
0.18159619989581874, 0.1885586947303991, 0.19544849659878202,
0.20226294935119382, 0.20899942588637954, 0.21565532916440008,
0.22222809320784087, 0.22871518409104491, 0.23511410091698925,
0.24142237678142864, 0.24763757972393363, 0.25375731366545823,
0.25977921933207354, 0.26570097516451269, 0.27152029821317669,
0.27723494501825435, 0.282842712474619, 0.28834143868116752,
0.29372900377427424, 0.29900333074503876, 0.30416238624001241,
0.30920418134509475, 0.314126772352298, 0.31892826150908288,
0.323606797749979, 0.32816057741020549, 0.33258784492101812,
0.33688689348651468, 0.34105606574163694, 0.34509375439111678,
0.34899840282911887, 0.35276850573934204, 0.35640260967534715,
0.35989931362088862, 0.36325726953003251, 0.36647518284685443,
0.36955181300451473, 0.37248597390352145, 0.37527653436899366,
0.37792241858675124, 0.38042260651806142, 0.3827761342928836,
0.384982094581459, 0.38703963694410393, 0.38894796815907068,
0.390706352528349, 0.39231411216129219, 0.39377062723595668,
0.39507533623805513, 0.3962277361774309, 0.39722738278197056,
0.39807389066887877, 0.39876693349325121, 0.39930624407388637,
0.3996916144962892, 0.39992289619282595, 0.4, 0.39992289619282595,
0.3996916144962892, 0.39930624407388637, 0.39876693349325121,
0.39807389066887877, 0.39722738278197056, 0.3962277361774309,
0.39507533623805513, 0.39377062723595668, 0.39231411216129219,
0.39070635252834895, 0.38894796815907068, 0.38703963694410393,
0.38498209458145893, 0.38277613429288354, 0.38042260651806142,
0.37792241858675124, 0.37527653436899366, 0.37248597390352145,
0.36955181300451473, 0.36647518284685437, 0.36325726953003251,
0.35989931362088856, 0.35640260967534709, 0.35276850573934204,
0.34899840282911887, 0.34509375439111678, 0.34105606574163694,
0.33688689348651463, 0.33258784492101806, 0.32816057741020543,
0.323606797749979, 0.31892826150908288, 0.31412677235229797,
0.30920418134509475, 0.30416238624001246, 0.29900333074503871,
0.29372900377427419, 0.28834143868116746, 0.28284271247461906,
0.27723494501825435, 0.27152029821317669, 0.26570097516451274,
0.25977921933207354, 0.25375731366545823, 0.24763757972393355,
0.2414223767814285, 0.23511410091698931, 0.22871518409104491,
0.22222809320784087, 0.21565532916440003, 0.20899942588637943,
0.20226294935119382, 0.19544849659878194, 0.188558694730399,
0.1815961998958186, 0.17456369626933685, 0.16746389501497119,
0.16029953324125631, 0.15307337294603579, 0.1457881999516597,
0.13844682283099716, 0.13105207182467735, 0.12360679774997885,
0.11611387090178496, 0.10857617994602985, 0.10099663080606328,
0.09337814554236222, 0.0857236612260203, 0.078036128806451277,
0.070318511973741893, 0.0625737860160924, 0.05480493667278722,
0.04701495898313502, 0.039206856131824161, 0.031383638291138029,
0.02354832146047562, 0.015703926303627404, 0.0078534769842512377,
4.8985871965894131E-17, -0.0078534769842513157, -0.015703926303627484,
-0.023548321460475696, -0.031383638291138105, -0.039206856131824237,
-0.0470149589831351, -0.0548049366727873, -0.062573786016092467,
-0.070318511973741976, -0.19509032201612836, -0.21430915306505094,
-0.23344536385590572, -0.25249157701515834, -0.27144044986507476,
-0.29028467725446211, -0.30901699437494728, -0.32763017956169349,
-0.34611705707749263, -0.36447049987914942, -0.38268343236508967,
-0.40074883310314091, -0.41865973753742819, -0.43640924067334186,
-0.45399049973954669, -0.47139673682599764, -0.488621241496955,
-0.50565737337798478, -0.52249856471594869, -0.53913832291100017,
-0.55557023301960229, -0.57178796022761247, -0.58778525229247336,
-0.60355594195357143, -0.61909394930983408, -0.6343932841636456,
-0.64944804833018388, -0.66425243791128208, -0.67880074553294178,
-0.693087362545636, -0.70710678118654768, -0.720853596702919,
-0.73432250943568556, -0.74750832686259694, -0.76040596560003115,
-0.77301045336273722, -0.78531693088074528, -0.79732065377270755,
-0.8090169943749479, -0.820401443525514, -0.83146961230254524,
-0.84221723371628665, -0.8526401643540924, -0.86273438597779206,
-0.8724960070727974, -0.88192126434835494, -0.89100652418836779,
-0.8997482840522214, -0.90814317382508136, -0.916187957117136,
-0.92387953251128685, -0.93121493475880368, -0.93819133592248427,
-0.94480604646687794, -0.95105651629515353, -0.95694033573220882,
-0.96245523645364728, -0.96759909236025987, -0.97236992039767667,
-0.9767658813208725, -0.98078528040323054, -0.98442656808989171,
-0.98768834059513766, -0.99056934044357725, -0.99306845695492629,
-0.99518472667219693, -0.996917333733128, -0.99826561018471582,
-0.9992290362407229, -0.99980724048206482, -1.0, -0.99980724048206482,
-0.9992290362407229, -0.99826561018471593, -0.996917333733128,
-0.99518472667219682, -0.99306845695492629, -0.99056934044357725,
-0.98768834059513777, -0.98442656808989171, -0.98078528040323043,
-0.97676588132087239, -0.97236992039767656, -0.96759909236025965,
-0.96245523645364717, -0.95694033573220894, -0.95105651629515364,
-0.944806046466878, -0.93819133592248416, -0.93121493475880357,
-0.92387953251128663, -0.91618795711713574, -0.90814317382508114,
-0.89974828405222118, -0.89100652418836757, -0.881921264348355,
-0.87249600707279706, -0.86273438597779173, -0.85264016435409207,
-0.84221723371628632, -0.831469612302545, -0.82040144352551325,
-0.809016994374947, -0.79732065377270667, -0.785316930880745,
-0.77301045336273688, -0.76040596560003082, -0.74750832686259661,
-0.73432250943568522, -0.72085359670291838, -0.707106781186547,
-0.6930873625456353, -0.67880074553294112, -0.664252437911281,
-0.64944804833018344, -0.63439328416364527, -0.61909394930983364,
-0.603555941953571, -0.58778525229247258, -0.57178796022761158,
-0.55557023301960218, -0.539138322911001, -0.52249856471594947,
-0.50565737337798511, -0.48862124149695535, -0.47139673682599792,
-0.45399049973954697, -0.43640924067334219, -0.41865973753742808,
-0.40074883310314086, -0.38268343236508956, -0.3644704998791502,
-0.34611705707749341, -0.32763017956169382, -0.30901699437494762,
-0.2902846772544625, -0.27144044986507426, -0.25249157701515784,
-0.2334453638559052, -0.21430915306505044, -0.19509032201612786,
-0.17579627993435484, -0.15643446504023112, -0.13701234168196816,
-0.11753739745783766, -0.0980171403295605, -0.078459095727844749,
-0.05887080365118872, -0.039259815759068187, -0.01963369246062777 };
creal_T vnk[320];
double C1;
dspcodegen_ZeroCrossingDetector zcd;
unsigned long RN;
int C3;
double dv1[158];
double dv2[158];
double lhd;
double thd;
int C5;
double dv3[4];
double dv4[4];
/* CLASSIFIER */
/* Entradas: 5 features ( C1, C2, C3, C4 y C5) */
/* Salidas: Numero entero indicando el tipo de clase */
/* (classResult) */
/* CONSTANTES */
/* umbral de ruido, para distinguirlo de otra perturbación */
/* clases de salida */
/* normal */
/* sag */
/* swell */
/* interruption */
/* oscillatory transient */
/* harmonics */
/* harmonics+sag */
/* harmonics+swell */
/* flicker */
/* numero de ciclos (periodos de onda) en cada muestra */
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
/* EXTRACCIÓN DE FEATURES */
/* C1=zeros(1,n);%vector de n elementos */
/* vector de n elementos */
/* C3=0; %numero entero que puede valer o 0 o 1 */
C4 = 0;
/* vector de n elementos */
/* C5=zeros(1,n);%vector de n elementos */
/* senyal de entrada (que vamos a clasificar) */
/* puntos en un ciclo */
/* frecuencia fundamental */
/* frecuencia de muestreo = 320 (samples per cycle) * 50 (base signal frecuency) */
/* vector de tiempos, tiene que tener tamanyo PointsPerSignal */
/* %%%%%%%%%%%%%%%%%%%%%FUNCIÓN DE PRUEBA: out%%%%%%%%%%%%%%%%%%%%%%%% */
/* offset del primer escalón (arbitrario) */
/* offset del segundo escalón */
/* Difference function, 0 all except in a certain interval. */
/* The points of the sag will be multiplied by the factor 0.6 (their amplitude will decrease). The rest, no. */
/* Final distorted signal. Ojo el .* significa multiplicacion elemento a elemento */
/* FEATURES */
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%C1 */
/* vector de n elementos */
/* matriz de n*pointsPerCycle elementos */
/* eje de frecuencias de la DFT (Discrete Fourier Transform), vector */
/* encontramos el indice del vector (index) correspondiente a la frecuencia fundamental */
/* valor eficaz de la señal de red de baja tensión. */
fft(dv0, vnk);
/* transformada de fourier de un ciclo */
/* sólo interesa el módulo de la frecuencia fundamental (hinchado o hundido). */
C1 = 1.4142135623730951 * rt_hypotd(vnk[1].re, vnk[1].im) / 320.0 /
0.70710678118654746;
/* valor de C1, vector */
/* C2 */
/* parametro que fija en el paper */
/* vector de n elementos */
/* vector de n elementos */
/* valor eficaz de cada ciclo */
/* C3 */
zcd.isInitialized = 0L;
/* System object Constructor function: dsp.ZeroCrossingDetector */
zcd.matlabCodegenIsDeleted = false;
/* funcion que calcula el numero de cruces por cero https://es.mathworks.com/help/dsp/ref/dsp.zerocrossingdetector-system-object.html */
/* restamos al valor eficaz, su valor medio. diffVector es un vector */
/* pone a cero todos los elementos entre 0.01 y -0.01 para evitar el ruido. MIO */
RN = SystemCore_step(&zcd);
/* obtiene el numero de cruces por cero de diffVector traspuesto, RN es un numero natural */
if (RN >= 3UL) {
C3 = 1;
/* feature */
} else {
C3 = 0;
/* feature */
}
/* C4 */
/* vector de n elementos */
/* vnk=zeros(n,pointsPerCycle);% matriz de n*pointsPerCycle elementos */
/* umbral */
/* vnk(ii,:)=fft(out((ii-1)*pointsPerCycle+1:ii*pointsPerCycle));%IMPORTANTE: transformada de fourier, aqui Sergio deberias coger los valores de antes, no volverla a hacer (yo aqui la vuelvo a hacer porque no optimizo el tiempo de computo) */
b_abs(*(creal_T (*)[158])&vnk[2], dv1);
power(dv1, dv2);
lhd = sum(dv2);
b_sqrt(&lhd);
thd = lhd / rt_hypotd(vnk[1].re, vnk[1].im);
/* calculo de la thd de un ciclo */
if (thd > 0.05) {
/* si se sobrepasa el umbral, se pone C4 a uno, sino se queda a cero */
C4 = 1;
/* feature */
}
/* C5 */
/* vector de n elementos */
C5 = 0;
/* vector de n elementos */
c_abs(*(creal_T (*)[4])&vnk[2], dv3);
b_power(dv3, dv4);
lhd = b_sum(dv4);
b_sqrt(&lhd);
lhd /= rt_hypotd(vnk[1].re, vnk[1].im);
/* calculo lhd */
if ((lhd > 0.07) && (lhd >= thd - lhd)) {
/* si se complen las dos condiciones, se pone C5 a uno, sino se queda a cero */
C5 = 1;
}
/* FIN DE FEATURES */
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
/* ARBOL DE DECISION */
if ((!any(1.01 < C1)) && (!any(0.99 > C1)) && (!b_any(C4))) {
/* normal signal */
C4 = 1;
/* salida */
} else if (C3 == 1) {
/* flicker */
C4 = 10;
} else if ((!b_any(C5)) && (!any(0.91 > C1)) && (!any(1.09 < C1))) {
/* oscll transient */
C4 = 6;
/* salida */
} else {
/* si todos los valores de C2 son iguales a cero */
/* harmonics */
C4 = 7;
/* salida */
}
z = C4;
c_matlabCodegenHandle_matlabCod(&zcd);
return z;
}
/* End of code generation (zhang_definitivo_C2000.c) */
This is the C code. My device has 100KB of RAM, so I can't belive that this code doesn't fit in.
I'll check your links, and thank you again,
Sergio

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by