Simulink model converts floating-point numbers to fixed-point numbers
Show older comments
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
Accepted Answer
More Answers (1)
Andy Bartlett
on 21 Feb 2023
0 votes
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.
Categories
Find more on Get Started with HDL Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!