⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mgenbin.m

📁 OFDMA 物理层开发的matlab 源码.飞思卡尔提供.对物理层开发的工程师有帮助!
💻 M
字号:
function mGenBin(in,fname,type,format,endian)
% Writes integers into a binary file.
%
%     mGenBin(in,fname,type,format,endian)
%
% in     : vector or matrix (real or complex)
%          Matrices are written column by column. 
%
% fname  : name of the binary output file 
%
% type   : defines how the input data is interpreted
%          'int8'   signed 8-bit integers (-127 .. 128)
%          'int16'  signed 16-bit integers (-32768 .. 32767)
%          'int32'  signed 32-bit integers (-2147483648 .. 2147483647)
%          'uint8'  unsigned 8-bit integers (0 .. 255)
%          'uint16' unsigned 16-bit integers (0 .. 65535)
%          'uint32' unsigned 32-bit integers (0 .. 4294967295)
%          If the input values exceed the range defined by type
%          the input values are saturated.
%
% format : defines how the data is arranged in the binary file
%          'R'    real values only
%          'I'    imaginary values only
%          'RI'   first all real, second all imaginary
%          'IR'   first all imaginary, second all real
%          '1R1I' 1 real and 1 imaginary value, alternating
%          '4R4I' 4 real and 4 imaginary values, alternating
%          '8R8I' 8 real and 8 imaginary values, alternating
%
% endian : 'big','b' or 'little','l' , default is 'big'
%
% Within this function a float input is always rounded to convert the
% float values into integers.
%
% Examples:    in = [  2  -2  56  12 132 255   0 384  85 170]
%              (for simplicity reasons the imag component is zero)
%
%   mGenBin(in,'file.bin','int8','R') generates the output
%     02 fe 38 0c 7f 7f 00 7f 55 7f  
%
%   mGenBin(in,'file.bin','uint8','R') generates the output
%     02 00 38 0c 84 ff 00 ff 55 aa  
%
%   mGenBin(in,'file.bin','uint8','I') generates the output
%     00 00 00 00 00 00 00 00 00 00
%
%   mGenBin(in,'file.bin','uint8','IR') generates the output
%     00 00 00 00 00 00 00 00 00 00 02 00 38 0c 84 ff
%     00 ff 55 aa  
%
%   mGenBin(in,'file.bin','uint8','1R1I') generates the output
%      R  I  R  I  R  I  R  I  R  I  R  I  R  I  R  I
%     02 00 00 00 38 00 0c 00 84 00 ff 00 00 00 ff 00
%     55 00 aa 00  
%
%   mGenBin(in,'file.bin','int16','R') generates the output
%       R     R     R     R     R     R     R     R
%     00 02 ff fe 00 38 00 0c 00 84 00 ff 00 00 01 7f
%     00 55 00 aa
%
%   mGenBin(in,'file.bin','uint16','R','little') generates the output
%       R     R     R     R     R     R     R     R
%     02 00 00 00 38 00 0c 00 84 00 ff 00 00 00 7f 01
%     55 00 aa 00
%
%   mGenBin(in,'file.bin','int16','1R1I') generates the output
%       R     I     R     I     R     I     R     I
%     00 02 00 00 ff fe 00 00 00 38 00 00 00 0c 00 00
%     00 84 00 00 00 ff 00 00 00 00 00 00 01 7f 00 00
%     00 55 00 00 00 aa 00 00
%
%   mGenBin(in,'file.bin','int16','8R8I') generates the output
%     00 02 ff fe 00 38 00 0c 00 84 00 ff 00 00 01 7f  R
%     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  I
%     00 55 00 aa 00 00 00 00 00 00 00 00 00 00 00 00  R
%     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  I
%
% See also mGetBin 
%
% Matlab 7 Release 14 SP2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Property of Freescale
%  Freescale Confidential Proprietary
%  Freescale Copyright (C) 2005 All rights reserved
%  ----------------------------------------------------------------------------
%  $RCSfile: mGenBin.m.rca $
%  Tag $Name:  $ 
%  $Revision: 1.1 $
%  $Date: Wed Sep 27 11:19:01 2006 $
%  Target: Matlab
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Author: Karsten Mohr
% Date  : Aug 2005

%check input dimension
if ndims(in)~=2
    error('Error: Input must be a vector or 2-dim matrix.');
end
InLen=prod(size(in));

%set default endianess to big endian
if ~exist('endian','var')
    endian='b';
end

%make row vector and round values to get integers 
if isfloat(in)
  in=reshape(round(in),1,InLen);
else
  in=reshape(in,1,InLen);
end
  
%check type and set parameters
switch type
    case 'int8'
        bytes=1; min_val=-128; max_val=127; base=256;
    case 'int16'
        bytes=2; min_val=-32768; max_val=32767; base=65536;
    case 'int32'
        bytes=4; min_val=-2147483648; max_val=2147483647; base=4294967296;
    case 'uint8'
        bytes=1; min_val=0; max_val=255; base=256;
    case 'uint16'
        bytes=2; min_val=0; max_val=65535; base=65536;
    case 'uint32'
        bytes=4; min_val=0; max_val=4294967295; base=4294967296;
    otherwise
        error('Error: Type not supported.');
end

%check format
switch format
    case {'R','I','RI','IR','1R1I'}
        %do nothing
    case {'4R4I'}
        padded_zeros=4-mod(InLen,4);
        if padded_zeros~=4
          in=[in zeros(1,padded_zeros)];
          InLen=InLen+padded_zeros;
          disp('Info: input data has been appended with zeros to match output format.');
        end
    case {'8R8I'}
        padded_zeros=8-mod(InLen,8);
        if padded_zeros~=8
          in=[in zeros(1,padded_zeros)];
          InLen=InLen+padded_zeros;
          disp('Info: input data has been appended with zeros to match output format.');
        end
    otherwise
        error('Error: Format not supported.');
end

%split input into real and imag part
in_real = real(in);
in_imag = imag(in);

%check whether the input values (real) are in the selected range
if (min(in_real)<min_val) | (max(in_real)>max_val) 
    warning('At least one real input element exceeds the range of the selected type. Values are saturated ...');
    %in = mod(in + base/2,base)-base/2; %limit signed values
    in_real = (in_real<=max_val).*in_real + (in_real>max_val).*max_val; %upper saturation
    in_real = (in_real>=min_val).*in_real + (in_real<min_val).*min_val; %lower saturation
end

%check whether the input values (imag) are in the selected range
if ~isreal(in)
  %if complex then process imaginary part also
  if (min(in_imag)<min_val) | (max(in_imag)>max_val) 
    warning('At least one imaginary input element exceeds the range of the selected type. Values are saturated ...');
    %in = mod(in + base/2,base)-base/2; %limit signed values
    in_imag = (in_imag<=max_val).*in_imag + (in_imag>max_val)*max_val; %upper saturation
    in_imag = (in_imag>=min_val).*in_imag + (in_imag<min_val)*min_val; %lower saturation
  end
end

%make data unsigned
switch type
    case {'int8'}
      %if signed convert to unsigned
      in_real = mod(int16(in_real)+base,base);
%      if ~isreal(in)
        in_imag = mod(int16(in_imag)+base,base);
%      end
    case {'int16'}
      %if signed convert to unsigned
      in_real = uint16(mod(int32(in_real)+base,base));
%      if ~isreal(in)
        in_imag = uint16(mod(int32(in_imag)+base,base));
%      end
    case {'int32'}
      %if signed convert to unsigned
      in_real = uint32(mod(double(in_real)+base,base));
%      if ~isreal(in)
        in_imag = uint32(mod(double(in_imag)+base,base));
%      end
    otherwise
    %do nothing
end

%open binary file
fid = fopen(fname,'wb');
if fid==-1
    error('Error: Can not open file "%s".',fname);
end

switch type
    case {'int16','uint16'}
        %big endian
        in_real = [ bitand(bitshift(in_real,-8),255); ...
                    bitand(in_real,255) ];
        in_imag = [ bitand(bitshift(in_imag,-8),255); ...
                    bitand(in_imag,255) ];
    case {'int32','uint32'}
        in_real = [ bitshift(in_real,-24); ...
                    bitand(bitshift(in_real,-16),255); ...
                    bitand(bitshift(in_real,-8),255); ...
                    bitand(in_real,255) ];
        in_imag = [ bitshift(in_imag,-24); ...
                    bitand(bitshift(in_imag,-16),255); ...
                    bitand(bitshift(in_imag,-8),255); ...
                    bitand(in_imag,255) ];
    otherwise
        %do nothing
end

%swap bytes for little enidan
switch endian
    case {'little','l'}
      in_real=flipud(in_real);
      in_imag=flipud(in_imag);
    case {'big','b'}
      %do nothing
    otherwise
      error('Error: Endianess type not supported.');
end

%write data to file
switch format
    case 'R'
        %real only
        fwrite(fid,in_real,'uchar');
    case 'I'
        %imag only
        fwrite(fid,in_imag,'uchar');
    case 'RI'
        %first all real, second all imag
        fwrite(fid,in_real,'uchar');
        fwrite(fid,in_imag,'uchar');
    case 'IR'
        %first all imag, second all real
        fwrite(fid,in_imag,'uchar');
        fwrite(fid,in_real,'uchar');
    case '1R1I'
        fwrite(fid,[in_real;in_imag],'uchar');
    case '4R4I'
        fwrite(fid,[reshape(in_real,bytes*4,InLen/4);reshape(in_imag,bytes*4,InLen/4)],'uchar');
    case '8R8I'
        fwrite(fid,[reshape(in_real,bytes*8,InLen/8);reshape(in_imag,bytes*8,InLen/8)],'uchar');
end

%close binary file
fclose(fid);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -