Simulink model converts floating-point numbers to fixed-point numbers
조회 수: 5 (최근 30일)
이전 댓글 표시
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1299565/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1300570/image.png)
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
댓글 수: 0
채택된 답변
Andy Bartlett
2023년 2월 21일
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)
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)
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)
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)
nt = numerictype(X4)
댓글 수: 0
추가 답변 (1개)
Andy Bartlett
2023년 2월 21일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!