Simulink model converts floating-point numbers to fixed-point numbers

2 visualizaciones (últimos 30 días)
According to the image encryption algorithm of MATLAB code, several data are obtained from the workspace in the Simulink model. Input MATLAB Function, I want to carry out fixed-point to generate Verilog HDL, but the output matrix is always floating point number. The algorithm first reads the image information, obtains the data type as unit8, and then converts it to double. I annotated the conversion to double when modeling. img is displayed as uni8 in the model, but the output matrix is still double. I change the output to unit8, and Simulink expects double when an error is reported. Does this have anything to do with double when other input parameters are input? How should I solve this problem?
I changed the output data type to unit8 and the error was This assignment writes a 'double' value into a 'uint8' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches. Function 'unit8Fcn' (#24.408.411), line 24, column 1: "img" Launch diagnostic report.
function [img] = system3(lambd, m, n)
U = lambd;
V = lambd;
W = lambd;
X = zeros(1, m*n);
for i = 1:m*n
U = mod(12*U, 251);
V = mod(13*V, 241);
W = mod(14*W, 239);
X(i) = mod(U+V+W, 256);
end
img = reshape(X, m, n);
end

Respuesta aceptada

Andy Bartlett
Andy Bartlett el 21 de Feb. de 2023
By default zeros() gives double
Your output is double because of how it is initialized with the zeros() function
n = 3;
m = 4;
X1 = zeros(1, m*n);
class_X1 = class(X1)
class_X1 = 'double'
Use 'like' to control data type
To force the output of the zeros to be the same as another variable you can use the 'like' argument. This works with floating-point, integer, fixed-point, and boolean.
lambd = uint16(7);
X2 = zeros(1, m*n,'like',lambd);
class_X2 = class(X2)
class_X2 = 'uint16'
Use class name argument if desired data type is known in advance and a MATLAB built-in
If the data type is known in advance and a built-in type, then you can just give the name of the class as an argument.
X3 = zeros(1, m*n,'logical');
class_X3 = class(X3)
class_X3 = 'logical'
This does not work with fixed-point but there is a simple workaround. Create a dummy "prototype variable" and use the 'like' syntax.
prototypeVar = fi([],1,16,7);
X4 = zeros(1, m*n,'like',prototypeVar);
class_X4 = class(X4)
class_X4 = 'embedded.fi'
nt = numerictype(X4)
nt = DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 7

Más respuestas (1)

Andy Bartlett
Andy Bartlett el 21 de Feb. de 2023
Suggestion consider using modByConstant
Since the "denominator" of your modulos are constants, you my wish to use the modByConstant function or similar Simulink block. Directly using mod may be implemented using a divide which can be "costly" if targeting an embedded microcontroller or FPGA.
modByConstant will optimize the implementation to use only a multiply and shift which can be much faster than divide based approaches.

Categorías

Más información sobre FPGA, ASIC, and SoC Development en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by