matlab生成HEX文件-任意信号 大于64K长度

HEX文件格式不赘述,写里直接放上代码。请批评改正。

 1 %%convert a signal data into hex file format

 2 % data format:16bit 

 3 % signal length: less than 2^24-1

 4 % author: Yang Li [email protected]

 5 % data:2015.01.27

 6 

 7 clear all;

 8 close all;

 9 clc;

10 

11 %% fixed point or binary16 float point

12 fixed=1;

13 %% generate a signal waveform

14 %

15 T=10;%time length

16 fs=1e3;%sample rate

17 N=10000;

18 Period=N/fs/10;

19 f=1/Period;

20 t=linspace(0,(N-1)/fs,N);

21 

22 %data=1:2^5-1; 

23 data= sin(2*pi*f*t) +(rand(1,N)-0.5)/10;

24 % data=mod(50000*t,1000);

25 % plot(t,data)

26 dataBits=16;

27 byteBits=8;

28 

29 %% convert into 16 bits fixed point number

30 if fixed == 1

31     maximum=max(abs(data));%maximum of absolute signal 

32     scale=(2^15-1)/maximum;%scale 

33     data=round(data*scale);

34     for i=1:1:length(data)

35         if data(i) <0 

36             data(i) =2^16-abs(data(i)) ;

37         end

38     end

39 end

40 %% write

41 fh=fopen('data.hex','w');%open a new file (file path, file name)

42 bytesEveryRow=16;%write 16 bytes every row

43 dataEveryRow=bytesEveryRow/(dataBits/byteBits);%number of data every row

44 lengthData=length(data);%length of signal data

45 rowLengths=ceil(lengthData/dataEveryRow);%number of rows of the file to be written

46 

47 for i=1:rowLengths % write a row each time

48     if(mod(i,hex2dec('1000'))==1)

49         baseaddr=dec2hex(floor(i/hex2dec('1000')),4);

50         checkSum=6+hex2dec(baseaddr(1:2))+hex2dec(baseaddr(3:4));

51          checkSum=(dec2hex(bitand(256-mod(checkSum,256),255),2));

52         fprintf(fh,[':02000004',baseaddr,checkSum,13,10]);%产生HEX码(end-1:end)

53     

54     end

55     checkSum=0;

56     addr=dec2hex(mod((i-1)*16,hex2dec('10000')),4);%the start address of this row

57     checkSum=hex2dec(addr(1:2))+hex2dec(addr(3:4));

58     if i*dataEveryRow <lengthData %  numbers of data to be written greater than dataEveryRow

59         dataRow=dataEveryRow;

60         dataHex='';

61         for j=8*(i-1)+1:1:8*(i)            

62             if fixed == 1%                

63                 dataHexCurr=dec2hex(data(j),4);%16 bits fixed point

64              else

65                 dataHexCurr=num2binary16hex(data(j));%number format: 16bits floatIEEE 754 half precison float point standard,result is a hex format

66              end

67             checkSum=checkSum+hex2dec(dataHexCurr(1:2))+hex2dec(dataHexCurr(3:4));

68             dataHex=strcat(dataHex,dataHexCurr);

69         end    

70             

71     else

72          dataHex='';

73          dataRow =lengthData-(rowLengths-1)*dataEveryRow;

74         for j=(rowLengths-1)*dataEveryRow+1:1:lengthData            

75              if fixed == 1% 

76                 dataHexCurr=dec2hex(data(j),4);%16 bits fixed point

77              else

78                 dataHexCurr=num2binary16hex(data(j));%number format: 16bits floatIEEE 754 half precison float point standard,result is a hex format

79              end  

80             checkSum=checkSum+hex2dec(dataHexCurr(1:2))+hex2dec(dataHexCurr(3:4));

81             dataHex=strcat(dataHex,dataHexCurr);

82         end

83     end

84    checkSum=checkSum+dataRow*dataBits/byteBits;

85     checkSum=(dec2hex(bitand(256-mod(checkSum,256),255),2)); 

86 %     disp([num2str(i),'--',dataHex,'--',checkSum])

87     fprintf(fh,[':',dec2hex(dataRow*dataBits/byteBits,2),addr,'00',dataHex,checkSum,13,10]);%产生HEX码(end-1:end)

88 end

89 

90 fprintf(fh,':00000001FF');    

91 fclose(fh);

代码中有定点浮点两种,其中浮点格式为IEEE754标准中的half-precision-floating point, 16bit, 1bit 浮号位,5bit指数位,10小数位,代码如下:

 1 function [ hex ] = num2binary16he( number )

 2 %The IEEE 754 standard specifies a binary16 as having the following format:

 3 

 4 %Sign bit: 1 bit

 5 %Exponent width: 5 bits

 6 %Significand precision: 11 bits (10 explicitly stored)

 7 %S EEEEE MMMMMMMMMM

 8 %INPUT: number is a random precision number

 9 

10 %OUTPUT : var binary is a 2's string of the binary16

11 %         var decimail is the value of the number in decimal

12 

13 %Author :Yang Li .

14 %Email: [email protected]

15 if real(number) >= 2^14  

16     error('beyond the maximum :-2^14--+2^24');

17 end

18 

19 S=number<0 ;

20 E=0; 

21 M=abs(number);

22 while((E>-15 && E<15)&&( M>=2 || M<1 ))

23     if(M>=2)

24      E=E+1;

25      M=M/2;

26     else

27         E=E-1;

28         M=M*2;

29     end    

30 end

31 if ((E==-15 || E==15)&& M>=2 || M<1)

32     M=0;

33 else

34     M=round((M-1)*2^10) ; 

35 end

36 

37 

38   number1=(-1)^S*(2^ E   )*(1+M/2^10);

39 % binary=strcat(num2str(S),num2str(E),num2str(M)) 

40 E =E+15;

41 binary=strcat(num2str(S),dec2bin(E,5), dec2bin(M,10)) ;

42 

43 

44 sReal=(str2double(binary(1)));

45 eReal =-15; 

46  for i=2:6

47     eReal=eReal+str2double(binary(i))*2^(6-i);

48  end    

49 %  eReal=eReal*(-1)^str2double(binary(2));

50  mReal = 0; 

51 for i=7:16

52     mReal=mReal+str2double(binary(i))*2^(16-i);

53 end 

54  

55 numberReal=(-1)^sReal*2^eReal*(1+mReal/2^10);

56 err=num2str(abs(number-numberReal));

57 decimal=numberReal;

58 % disp(['the origin data is : ',num2str(number)]);

59 % disp(['the float format is : ',binary]); 

60 % disp(['so ,there is a error :',num2str(abs(number-numberReal))]);

61 

62 num=0;

63 for i=1:16

64     num=num+str2double(binary(i))*2^(16-i);

65 end

66 hex=dec2hex(num,4);

67 end

 

你可能感兴趣的:(matlab)