Нравится? Делимся информацией!

среда, 28 ноября 2012 г.

MatLAB script конвертация float из десятичной системы счисления в hex для Code Composer Studio

  1. % Create scrip that convert float numbers to hexidecimal.
  2. % This file is useful for example if we need to load *.dat file to Code
  3. % Composer Studio. But this dat-file can understand only hexidecimal format
  4. % of number types such as int, long, float.
  5.  
  6. % For start to work You must to set the input/output file name and path
  7. %and data type working with
  8. filename = 'dspInputSamples.dat';
  9. outFileName = 'dspInputSamplesHEX.dat';
  10. dataType = 'single'; %data of this type we read from input file with
  11. % "filename" name
  12.  
  13. %descriptor of the file that must be opened
  14. %fid = fopen('my_file.dat', 'wb'); % открытие файла на запись
  15. inputFID = fopen( filename, 'rb');
  16. if inputFID == -1
  17. error('input File is not opened');
  18. end
  19.  
  20. %descriptor of teh output file
  21. outputFID = fopen(outFileName, 'wt');
  22. if outputFID == -1
  23. error('output File is not opend');
  24. end
  25.  
  26. %initialize the 1-D row vector. In this value we will save unput data
  27. rowVec = 0;
  28.  
  29. %READ FROM FILE
  30. %samplesCnt - the number of elements read
  31. [rowVec, samplesCnt ] = fscanf( inputFID, '%f', inf );
  32.  
  33. %preallocate 1D column vector
  34. rowVecHEX = zeros( 1, samplesCnt, dataType);
  35.  
  36. %CONVERTION
  37. rowVecHEX = num2hex( single( rowVec ) );
  38.  
  39. %WRITE TO FILE
  40. A = rowVecHEX';
  41. for k = 1:samplesCnt
  42. lastSignificant_16_bits = A(5:8, k);
  43. mostSignificant_16_bits = A(1:4, k);
  44. fprintf( outputFID, '0x%c%c%c%cn', lastSignificant_16_bits );
  45. fprintf( outputFID, '0x%c%c%c%cn', mostSignificant_16_bits );
  46. end
  47.  
  48.  
  49. %display the row vector
  50. disp( rowVecHEX );
  51.  
  52. fclose( inputFID );
  53. fclose( outputFID );

Комментариев нет:

Отправить комментарий