HEX文件格式不赘述,写里直接放上代码。请批评改正。
%%convert a signal data into hex file format
% data format:16bit
% signal length: less than ^-
% author: Yang Li [email protected]
% data:2015.01. clear all;
close all;
clc; %% fixed point or binary16 float point
fixed=;
%% generate a signal waveform
%
T=;%time length
fs=1e3;%sample rate
N=;
Period=N/fs/;
f=/Period;
t=linspace(,(N-)/fs,N); %data=:^-;
data= sin(*pi*f*t) +(rand(,N)-0.5)/;
% data=mod(*t,);
% plot(t,data)
dataBits=;
byteBits=; %% convert into bits fixed point number
if fixed ==
maximum=max(abs(data));%maximum of absolute signal
scale=(^-)/maximum;%scale
data=round(data*scale);
for i=::length(data)
if data(i) <
data(i) =^-abs(data(i)) ;
end
end
end
%% write
fh=fopen('data.hex','w');%open a new file (file path, file name)
bytesEveryRow=;%write bytes every row
dataEveryRow=bytesEveryRow/(dataBits/byteBits);%number of data every row
lengthData=length(data);%length of signal data
rowLengths=ceil(lengthData/dataEveryRow);%number of rows of the file to be written for i=:rowLengths % write a row each time
if(mod(i,hex2dec(''))==)
baseaddr=dec2hex(floor(i/hex2dec('')),);
checkSum=+hex2dec(baseaddr(:))+hex2dec(baseaddr(:));
checkSum=(dec2hex(bitand(-mod(checkSum,),),));
fprintf(fh,[':02000004',baseaddr,checkSum,,]);%产生HEX码(end-:end) end
checkSum=;
addr=dec2hex(mod((i-)*,hex2dec('')),);%the start address of this row
checkSum=hex2dec(addr(:))+hex2dec(addr(:));
if i*dataEveryRow <lengthData % numbers of data to be written greater than dataEveryRow
dataRow=dataEveryRow;
dataHex='';
for j=*(i-)+::*(i)
if fixed == %
dataHexCurr=dec2hex(data(j),);% bits fixed point
else
dataHexCurr=num2binary16hex(data(j));%number format: 16bits floatIEEE half precison float point standard,result is a hex format
end
checkSum=checkSum+hex2dec(dataHexCurr(:))+hex2dec(dataHexCurr(:));
dataHex=strcat(dataHex,dataHexCurr);
end else
dataHex='';
dataRow =lengthData-(rowLengths-)*dataEveryRow;
for j=(rowLengths-)*dataEveryRow+::lengthData
if fixed == %
dataHexCurr=dec2hex(data(j),);% bits fixed point
else
dataHexCurr=num2binary16hex(data(j));%number format: 16bits floatIEEE half precison float point standard,result is a hex format
end
checkSum=checkSum+hex2dec(dataHexCurr(:))+hex2dec(dataHexCurr(:));
dataHex=strcat(dataHex,dataHexCurr);
end
end
checkSum=checkSum+dataRow*dataBits/byteBits;
checkSum=(dec2hex(bitand(-mod(checkSum,),),));
% disp([num2str(i),'--',dataHex,'--',checkSum])
fprintf(fh,[':',dec2hex(dataRow*dataBits/byteBits,),addr,'',dataHex,checkSum,,]);%产生HEX码(end-:end)
end fprintf(fh,':00000001FF');
fclose(fh);
代码中有定点浮点两种,其中浮点格式为IEEE754标准中的half-precision-floating point, 16bit, 1bit 浮号位,5bit指数位,10小数位,代码如下:
function [ hex ] = num2binary16he( number )
%The IEEE standard specifies a binary16 as having the following format: %Sign bit: bit
%Exponent width: bits
%Significand precision: bits ( explicitly stored)
%S EEEEE MMMMMMMMMM
%INPUT: number is a random precision number %OUTPUT : var binary is a 's string of the binary16
% var decimail is the value of the number in decimal %Author :Yang Li .
%Email: [email protected]
if real(number) >= ^
error('beyond the maximum :-2^14--+2^24');
end S=number< ;
E=;
M=abs(number);
while((E>- && E<)&&( M>= || M< ))
if(M>=)
E=E+;
M=M/;
else
E=E-;
M=M*;
end
end
if ((E==- || E==)&& M>= || M<)
M=;
else
M=round((M-)*^) ;
end number1=(-)^S*(^ E )*(+M/^);
% binary=strcat(num2str(S),num2str(E),num2str(M))
E =E+;
binary=strcat(num2str(S),dec2bin(E,), dec2bin(M,)) ; sReal=(str2double(binary()));
eReal =-;
for i=:
eReal=eReal+str2double(binary(i))*^(-i);
end
% eReal=eReal*(-)^str2double(binary());
mReal = ;
for i=:
mReal=mReal+str2double(binary(i))*^(-i);
end numberReal=(-)^sReal*^eReal*(+mReal/^);
err=num2str(abs(number-numberReal));
decimal=numberReal;
% disp(['the origin data is : ',num2str(number)]);
% disp(['the float format is : ',binary]);
% disp(['so ,there is a error :',num2str(abs(number-numberReal))]); num=;
for i=:
num=num+str2double(binary(i))*^(-i);
end
hex=dec2hex(num,);
end