Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

Use matlab to generate IEEE floating point COE file

by_ Xiao Qin 2022-01-23 13:10:15 阅读数:12 评论数:0 点赞数:0 收藏数:0

Use MATLAB Generate IEEE Floating point coe file

In this article , Mainly wrote my own generation IEEE Format coe Documents and MATLAB Read IEEE Format coe Knowledge points used in the document process , involves PFGA and MATLAB.

IEEE754 Reading

What is? IEEE754

IEEE Binary floating point arithmetic standard (IEEE 754) yes 20 century 80 The most widely used floating point arithmetic standard since the s , For many CPU And floating-point arithmetic used by . The full name of the standard is IEEE Binary floating point arithmetic standard (ANSI/IEEE Std 754-1985), also called IEC 60559:1989, Binary floating point arithmetic of microprocessor system ( The original number is IEC 559:1989).
IEEE 754 Four ways to represent floating point values are specified : Single precision (32 position )、 Double precision (64 position )、 Extended single precision (43 Bit above , Rarely used ) And Extended double precision (79 Bit above , Usually, the 80 Bit implementation ).

IEEE754 Express

IEEE The expression of can also be said to be IEEE The composition of .
 form among S Is the sign bit of the data ;E Is the number of digits ;M Is a significant digit .
IEEE754 standard :

Reference documents

The actual value of the above data is n = ( − 1 ) s ∗ m ∗ 2 e n = (-1)^{s} *m* 2^e n=(1)sm2e

IEEE Format coe File generation

This process is mainly divided into three parts . One is generation coe Data point of file ; The second is to format the data points ; The third is to store the converted data points in coe file .

Generate coe Data point of file

There are many ways to generate data points , Here are the methods of generating sine wave signals ( If there is something wrong, please correct )

width=32; % Width
depth=128; % The depth is 65536
n=0:depth-1;
yn=sin(2*pi/128*n);

The generated waveform is shown in the figure :
 Generate a sine wave picture

Data format conversion

These data points have been stored in yn In variables . Then convert the data to IEEE Floating point number of format .
What I use here is

yn = single(yn);
num2hex(yn);

Store the converted data into a file

%=============================== Start writing coe file ===============================
addr=0:depth-1;
% str_width=strcat('WIDTH=',num2str(width));
str_depth=strcat('DEPTH=',num2str(depth));
fid=fopen('IEEE_sin.coe','wt'); % Open or create a new coe, The storage location and file name are arbitrary
%********************************************************************************/
fprintf(fid,'MEMORY_INITIALIZATION_RADIX=16;\n');
fprintf(fid,'MEMORY_INITIALIZATION_VECTOR=\n');
fprintf(fid,'%tx,\n',yn) % Start writing data
fclose(fid);

Pay special attention here , Is the format in which the data is written ,IEEE The format is "%tx". I've been tortured in this place for a long time , I didn't find this problem at first , Generated coe There has been a problem with the file . This place is my reference MATLAB Official help file .
Enter at the command line :

help fprintf

Find the help shown in the figure
 Insert picture description here I am MATLAB The small white , If there is any mistake, please correct it

ahhhh ! There was a problem

The above is about data conversion , Actually %tx This directly converts floating-point numbers to single precision hexadecimal , There is no need for an intermediate conversion
 Insert picture description here In the picture above fprintf The following is lowercase x, If you change it to uppercase X, The output of the coe Floating point number of data bits of the file .

Copyright statement
In this paper,the author:[by_ Xiao Qin],Reprint please bring the original link, thank you