实现内容:
1) 通过Matlab将生成的随机小数扩大为20位有符号位
2) 将拓展后的数据变为补码格式: 负数变为补码,正数保持不变
3)将补码格式的十进制小数转换为有符号十六进制数
4)将生成的十六进制数写入.coe文件中
代码实现
% generate the random number
for i = 1: 10
M(i,1)= (-1)^i *rand(1);
end
N = length(M);
Radix = 16;
Coeff_width = 20;
% convert the fraction number to integer
% 20 bits with 1 signed bit
M_mul = round(M.*2^(Coeff_width-1));
% convert the coefficient to complement format
for i = 1: N
if(M_mul(i) < 0)
comple_M(i) = 2^Coeff_width + M_mul(i);
else
comple_M(i) = M_mul(i);
end
end
%% write the real part .coe file
fid = fopen('M.coe','w');%File storage path
fprintf(fid,'Radix = %d; \n',Radix);
fprintf(fid,'Coefficient_Width = %d; \n',Coeff_width);
fprintf(fid,'CoefData = ');
% convert the complem