Quantize Deep Learning Network for Battery State of Charge Estimation
R2026bThis example shows how to compress a neural network for predicting the state of charge (SOC) of a battery using quantization. After compression, this example analyzes the quantized network through simulation in MATLAB, simulation in Simulink, and software-in-the-loop (SIL) testing.
Battery state of charge is the level of charge of an electric battery relative to its capacity, measured as a percentage. For more information about the task, see Battery State of Charge Estimation Using Deep Learning.
For an example that shows the same network compressed using pruning and projection, see Compress Deep Learning Network for Battery State of Charge Estimation.
Load Data and Network
Load the test data. This example prepares the data as shown in Prepare Data for Battery State of Charge Estimation Using Deep Learning.
requiredVars = ["XTrain","YTrain","XVal","YVal"]; if any(~ismember(requiredVars,who)) [XTrain,YTrain,XVal,YVal] = prepareBSOCData; end
Load the pretrained network. This network has been trained using the steps shown in Train Deep Learning Network for Battery State of Charge Estimation.
if ~exist("recurrentNet","file") load("pretrainedBSOCNetwork.mat") end
Analyze Floating-Point Network
Analyze the structure and parameter memory of the original floating-point network.
analyzeNetwork(recurrentNet) networkMetricsFloatingPoint = estimateNetworkMetrics(recurrentNet)
networkMetricsFloatingPoint = 3×8 table
"lstm_1" "LSTM" 266240 269312 1.0156 768 265216 0.9961
"lstm_2" "LSTM" 197120 198656 0.7520 384 196608 0.9961
"fc" "Fully Connected" 129 256 4.9210e-04 0 128 0.4981
Compute the root-mean-square-error (RMSE) of the floating-point network on the test data.
RMSEFloatingPoint = testnet(recurrentNet,XVal,YVal,"rmse")RMSEFloatingPoint = 0.0347
Quantize Network
Create a dlquantizer object and specify the network to quantize. Set the execution environment to MATLAB. When you use the MATLAB execution environment, quantization is performed using the fi fixed-point data type. Using this data type requires a Fixed-Point Designer™ license.
quantObj = dlquantizer(recurrentNet,ExecutionEnvironment="MATLAB");Prepare the network for quantization using prepareNetwork. One benefit of prepareNetwork is the removal of the dropoutLayer layers from the original network. These layers do not impact the inference behavior of the network and the remaining layers of the network are all supported for quantization. For more information on supported layers for quantization, see Supported Layers for Quantization.
prepareNetwork(quantObj)
Use the calibrate function to exercise the network with the calibration data and collect range statistics for the weights, biases, and activations at each layer. Transpose the training data, XTrain, to a single column cell array so it is in a valid format for calibration. For more information on valid data formats for calibration, see Prepare Data for Quantizing Networks.
calResults = calibrate(quantObj,XTrain');
Use the quantize function to quantize the network object and return a simulatable quantized network.
qRecurrentNet = quantize(quantObj);
Analyze Quantized Network
Analyze the structure and parameter memory of the quantized network.
analyzeNetwork(qRecurrentNet) networkMetricsQuantized = estimateNetworkMetrics(qRecurrentNet)
networkMetricsQuantized = 3×8 table
"lstm_1" "LSTM" 266240 269312 0.2568 768 265216 0.9961
"lstm_2" "LSTM" 197120 198656 0.1895 384 196608 0.9961
"fc" "Fully Connected" 127 252.0156 1.2398e-04 0 126 0.4941
Compare the parameter memory of layers of the floating-point and quantized networks. The estimateLayerMemory function, defined at the bottom of this example, uses the estimateNetworkMetrics function to measure the memory of supported layers.
memoryNetFloatingPoint = estimateLayerMemory(recurrentNet); memoryNetQuantized = estimateLayerMemory(qRecurrentNet); figure bar([memoryNetFloatingPoint memoryNetQuantized]) xticklabels(["Floating-Point","Quantized"]) ylabel("Memory (KB)") title("Memory of Supported Layers in Floating-Point and Quantized Networks")

The parameter memory of the LSTM and fully connected layers in the network reduced by about 75%.
Compute the RMSE of the quantized network on the test data.
RMSEQuantized = testnet(qRecurrentNet,XVal,YVal,"rmse")RMSEQuantized = 0.0350
Compare the error of the floating-point and quantized networks
figure bar([RMSEFloatingPoint RMSEQuantized]) xticklabels(["Floating-Point","Quantized"]) ylabel("RMSE") title("RMSE of Floating-Point and Quantized Networks")

Quantizing the network has negligible impact on the RMSE of the predictions.
Simulate Quantized Network in MATLAB
The quantized network qRecurrentNet is a simulatable network. Simulate the original floating-point network and the quantized network to compare their performance to each other and to the ground truth.
Extract one sequence from the test data.
testDataPoint =
17;
xSample = XVal{:,testDataPoint};
ySample = YVal{:,testDataPoint};Simulate the floating-point and quantized networks with the test data sequence.
samplePredictionFloatingPoint = predict(recurrentNet,xSample); samplePredictionQuantized = predict(qRecurrentNet,xSample);
Plot the ground truth, floating-point network prediction, and quantized network prediction.
plotBSOCPrediction(ySample,samplePredictionFloatingPoint,samplePredictionQuantized,testDataPoint)

Export Quantized Network to Simulink
Export the model with quantization details to Simulink using the exportNetworkToSimulink function. This function converts the network into layer blocks. You can use layer blocks to:
Visualize the operations of each layer in a network.
Easily debug issues with a Simulink model and generated code.
Customize generated code using built-in Simulink features.
mdlInfo = exportNetworkToSimulink(qRecurrentNet, ... ModelName="QuantizedBSOCNetwork", ... ExpandNetworkSubsystem=true, ... Stateful=true, ... FrameBased=true, ... InputDataType="double");

To see the internal operations of each layer block, click the down arrow.
Simulate Quantized Network in Simulink
Set model parameters.
sampleTime = 1; N = size(xSample,1); timeSteps = (0:sampleTime:(N-1)*sampleTime)'; ts = timeseries(xSample,timeSteps,"Name",'Battery_Signals'); load_system([mdlInfo.ModelName '.slx']); set_param(mdlInfo.ModelName,Solver="FixedStepDiscrete",FixedStep=num2str(sampleTime)); set_param(mdlInfo.ModelName,SaveOutput="on",OutputSaveName="YSimPred");
Simulate the network.
in = Simulink.SimulationInput(mdlInfo.ModelName);
in = in.setExternalInput(ts);
in = in.setModelParameter(SimulationMode="normal",StopTime=num2str(timeSteps(end)));
simOut = sim(in);
samplePredictionSimulink = simOut.YSimPred{1}.Values.Data;Plot the ground truth, floating-point network prediction from MATLAB, and quantized network prediction from Simulink.
plotBSOCPrediction(ySample,samplePredictionFloatingPoint,samplePredictionSimulink,testDataPoint)

Generate Code
Generate and test code for the model through software-in-the-loop (SIL) simulation. Then use Embedded Coder to trace elements of the Simulink model to the generated code.
Perform Software-in-the-Loop Testing
Run SIL simulation. A SIL simulation generates source code from the model, compiles and builds the source code, and executes an application as a separate process on your host computer.
inSIL = Simulink.SimulationInput(mdlInfo.ModelName);
inSIL = inSIL.setExternalInput(ts);
inSIL = inSIL.setModelParameter(SimulationMode="software-in-the-loop",StopTime=num2str(timeSteps(end)));
simOutSIL = sim(inSIL);### Skipped unpacking from Simulink cache file "QuantizedBSOCNetwork_2.slxc" because the file does not have the relevant build artifacts.
### Searching for referenced models in model 'QuantizedBSOCNetwork_2'.
### Total of 1 models to build.
### Starting build procedure for: QuantizedBSOCNetwork_2
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw
### Invoking Target Language Compiler on QuantizedBSOCNetwork_2.rtw
### Using System Target File: Y:\12\user.Bdoc.j3060568\runnable\matlab\rtw\c\ert\ert.tlc
### Loading TLC function libraries
..........
### Initial pass through model to cache user defined code
....
### Caching model source code
...............................................................................
..............
### Writing header file QuantizedBSOCNetwork_2_types.h
### Writing source file QuantizedBSOCNetwork_2.c
### Writing header file QuantizedBSOCNetwork_2_private.h
.
### Writing header file QuantizedBSOCNetwork_2.h
### Writing header file rtwtypes.h
### Writing source file QuantizedBSOCNetwork_2_data.c
### Writing source file ert_main.c
.
### TLC code generation complete (took 13.456s).
......### Saving binary information cache.
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\QuantizedBSOCNetwork_2.mk' ...
### Building 'QuantizedBSOCNetwork_2': "%MINGW_ROOT%\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2.mk buildobj
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw>call "setup_mingw.bat"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw>set "MINGW_ROOT=F:\3rdparty\internal\12933511\win64\MinGW\bin"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw>cd .
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw>if "buildobj" == "" ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2.mk all ) else ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2.mk buildobj )
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "QuantizedBSOCNetwork_2.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HO946A~1.J30/DEEPLE~1/QuantizedBSOCNetwork_2_ert_rtw/QuantizedBSOCNetwork_2.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "QuantizedBSOCNetwork_2_data.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HO946A~1.J30/DEEPLE~1/QuantizedBSOCNetwork_2_ert_rtw/QuantizedBSOCNetwork_2_data.c"
"### Successfully generated all binary outputs."
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw>exit /B 0
### Successful completion of build procedure for: QuantizedBSOCNetwork_2
### Simulink cache artifacts for 'QuantizedBSOCNetwork_2' were created in 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2.slxc'.
Build Summary
Top model targets:
Model Build Reason Status Build Duration
=========================================================================================================================
QuantizedBSOCNetwork_2 Information cache folder or artifacts were missing. Code generated and compiled. 0h 1m 38.153s
1 of 1 models built (0 models already up to date)
Build duration: 0h 1m 42.023s
### Preparing to start SIL simulation ...
Building with 'MinGW64 Compiler (C)'.
MEX completed successfully.
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\coderassumptions\lib\QuantizedBSOCNetwork_2_ca.mk' ...
### Building 'QuantizedBSOCNetwork_2_ca': "%MINGW_ROOT%\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2_ca.mk all
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\coderassumptions\lib>call "setup_mingw.bat"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\coderassumptions\lib>set "MINGW_ROOT=F:\3rdparty\internal\12933511\win64\MinGW\bin"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\coderassumptions\lib>cd .
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\coderassumptions\lib>if "all" == "" ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2_ca.mk all ) else ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2_ca.mk all )
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DINTEGER_CODE=0 -DCA_CHECK_FLOATING_POINT_ENABLED=1 -DCA_CHECK_LONG_LONG_ENABLED=0 -DCA_CHECK_DYNAMIC_MEMORY=0 -DCA_MODEL_SPECIFIC_CHECKS_ENABLED=1 -DCA_NONTUNABLE_ENUM_CHECKS_ENABLED=1 -DCA_CHECK_DAZ_ENABLED=1 @QuantizedBSOCNetwork_2_ca_comp.rsp -o "coder_assumptions_flt.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_flt.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DINTEGER_CODE=0 -DCA_CHECK_FLOATING_POINT_ENABLED=1 -DCA_CHECK_LONG_LONG_ENABLED=0 -DCA_CHECK_DYNAMIC_MEMORY=0 -DCA_MODEL_SPECIFIC_CHECKS_ENABLED=1 -DCA_NONTUNABLE_ENUM_CHECKS_ENABLED=1 -DCA_CHECK_DAZ_ENABLED=1 @QuantizedBSOCNetwork_2_ca_comp.rsp -o "QuantizedBSOCNetwork_2_ca.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HO946A~1.J30/DEEPLE~1/QuantizedBSOCNetwork_2_ert_rtw/coderassumptions/QuantizedBSOCNetwork_2_ca.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DINTEGER_CODE=0 -DCA_CHECK_FLOATING_POINT_ENABLED=1 -DCA_CHECK_LONG_LONG_ENABLED=0 -DCA_CHECK_DYNAMIC_MEMORY=0 -DCA_MODEL_SPECIFIC_CHECKS_ENABLED=1 -DCA_NONTUNABLE_ENUM_CHECKS_ENABLED=1 -DCA_CHECK_DAZ_ENABLED=1 @QuantizedBSOCNetwork_2_ca_comp.rsp -o "coder_assumptions_hwimpl.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_hwimpl.c"
"### Creating static library "./QuantizedBSOCNetwork_2_ca.lib" ..."
"F:\3rdparty\internal\12933511\win64\MinGW\bin/ar" ruvs ./QuantizedBSOCNetwork_2_ca.lib @QuantizedBSOCNetwork_2_ca.rsp
F:\3rdparty\internal\12933511\win64\MinGW\bin/ar: creating ./QuantizedBSOCNetwork_2_ca.lib
a - coder_assumptions_hwimpl.obj
a - coder_assumptions_flt.obj
a - QuantizedBSOCNetwork_2_ca.obj
"### Created: ./QuantizedBSOCNetwork_2_ca.lib"
"### Successfully generated all binary outputs."
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\coderassumptions\lib>exit /B 0
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\sil\QuantizedBSOCNetwork_2.mk' ...
### Building 'QuantizedBSOCNetwork_2': "%MINGW_ROOT%\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2.mk all
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\sil>call "setup_mingw.bat"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\sil>set "MINGW_ROOT=F:\3rdparty\internal\12933511\win64\MinGW\bin"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\sil>cd .
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\sil>if "all" == "" ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2.mk all ) else ("F:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 8 -l 8 -Oline -f QuantizedBSOCNetwork_2.mk all )
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "xil_data_stream.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/xil_data_stream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "xil_interface.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HO946A~1.J30/DEEPLE~1/QuantizedBSOCNetwork_2_ert_rtw/sil/xil_interface.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "sil_main.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HO946A~1.J30/DEEPLE~1/QuantizedBSOCNetwork_2_ert_rtw/sil/sil_main.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "xil_interface_lib.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/xil_interface_lib.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "xilcomms_rtiostream.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/xilcomms_rtiostream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "xil_rtiostream.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/xil_rtiostream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "xil_services.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/xil_services.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "rtiostream_utils.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/coder/rtiostream/src/utils/rtiostream_utils.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "target_io.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/target_io.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "coder_assumptions_app.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_app.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "coder_assumptions_data_stream.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_data_stream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "coder_assumptions_rtiostream.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/rtw/targets/pil/c/coder_assumptions_rtiostream.c"
"F:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -O0 -msse2 -fno-predictive-commoning -DCODER_ASSUMPTIONS_ENABLED=1 -DXIL_SIGNAL_HANDLER=1 -DSIL_DISABLE_SUBNORMAL_SUPPORT=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DRTIOSTREAM_RX_BUFFER_BYTE_SIZE=50000 -DRTIOSTREAM_TX_BUFFER_BYTE_SIZE=50000 -DMEM_UNIT_BYTES=1 -DMemUnit_T=uint8_T -DMODEL=QuantizedBSOCNetwork_2 -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @QuantizedBSOCNetwork_2_comp.rsp -o "rtiostream_tcpip.obj" "Y:/12/HEMQ38~U/runnable/matlab/toolbox/coder/rtiostream/src/rtiostreamtcpip/rtiostream_tcpip.c"
"### Creating standalone executable "./QuantizedBSOCNetwork_2.exe" ..."
"F:\3rdparty\internal\12933511\win64\MinGW\bin/g++" -static -m64 -o ./QuantizedBSOCNetwork_2.exe @QuantizedBSOCNetwork_2.rsp -Wl,--start-group @QuantizedBSOCNetwork_2_ref.rsp C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HO946A~1.J30/DEEPLE~1/QuantizedBSOCNetwork_2_ert_rtw/coderassumptions/lib/QuantizedBSOCNetwork_2_ca.lib -Wl,--end-group -lws2_32
"### Created: ./QuantizedBSOCNetwork_2.exe"
"### Successfully generated all binary outputs."
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3060568\deeplearning_shared-ex56344754\QuantizedBSOCNetwork_2_ert_rtw\sil>exit /B 0
### Starting SIL simulation for component: QuantizedBSOCNetwork_2
### Application stopped
### Stopping SIL simulation for component: QuantizedBSOCNetwork_2
Plot the ground truth, floating-point network prediction from MATLAB, and quantized network prediction from SIL.
samplePredictionSIL = simOutSIL.YSimPred{1}.Values.Data;
plotBSOCPrediction(ySample,samplePredictionFloatingPoint,samplePredictionSIL,testDataPoint)
Compare the predictions of the quantized network in Simulink and in SIL simulation.
figure timeSteps = 1:length(ySample); plot(timeSteps,[samplePredictionSimulink,samplePredictionSIL]) xlabel("Time (Seconds)") ylabel("State of Charge (%)") legend({"Predicted Simulink","Predicted SIL"}, ... 'Location','best') grid on

For an example of running SIL with the SIL/PIL Manager app in Simulink, see Generate Code for Battery State of Charge Estimation Using Deep Learning.
Trace Simulink Model Elements in Generated Code
Code tracing (traceability) uses hyperlinks to navigate between a line of generated code and its corresponding elements in a model. You can use this to trace which parts of the code relate to each layer.
To trace the generated code to blocks in the Simulink model:
Open the model.
Open the Embedded Coder® app. Build the model. On the C Code tab, click Build.The HTML code generation report opens by default. To use the report, see Verify Generated Code by Using Code Tracing (Embedded Coder).
View the generated code in the Code view in the Code perspective. The source code contains traceability information such as hyperlinked comments, line numbers, variables, and operators.
In the model element, click a layer block.
In the generated code in the Code view window, you see the first instance of highlighted code that is generated for the layer block. At the top of the Code view, numbers that appear to the right of generated file names indicate the total number of highlighted lines in each file. This figure shows the result of tracing the Fully Connected Layer block in the
QuantizedBSOCNetworkmodel.

Helper Functions
Calculate Memory of Supported Layers
The estimateLayerMemory function uses the estimateNetworkMetrics function to measure the memory of each supported layer. Not all layers are supported for estimateNetworkMetrics, so the resulting number is often less than the total network memory.
function layerMemoryInKB = estimateLayerMemory(net) info = estimateNetworkMetrics(net); layerMemoryInMB = sum(info.("ParameterMemory (MB)")); layerMemoryInKB = layerMemoryInMB * 1000; end
Plot Battery State of Charge Prediction
The plotBSOCPrediction function plots a comparison of the ground truth, the prediction of the floating-point network, and the prediction of the quantized network for one test sequence.
function plotBSOCPrediction(yTrue,yFloatingPoint,yQuantized,seqIdx) % Convert data to column vectors yTrue = squeeze(extractdata(yTrue))'; yFloatingPoint = squeeze(extractdata(yFloatingPoint))'; yQuantized = squeeze(extractdata(yQuantized))'; % Create time steps vector timeSteps = 1:length(yTrue); % Plot predictions figure plot(timeSteps,yTrue,LineWidth=1.5) hold on plot(timeSteps,yFloatingPoint) plot(timeSteps,yQuantized) hold off xlabel("Time (Seconds)") ylabel("State of Charge (%)") legend({"True","Predicted (Floating-Point)","Predicted (Quantized)"}, ... Location="best") title(sprintf("Battery SOC Predictions vs Ground Truth (Sequence %d)",seqIdx),FontWeight="bold") grid on end