📄 mgenheader.m
字号:
function mGenHeader(in,fname,type,arrayname,format)
% Writes integers into a Header file.
%
% mGenHeader(in,fname,type,arrayname,format)
%
% 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
% '1R1I' 1 real and 1 imaginary value, alternating
%
% Matlab 7 Release 14 SP2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Property of Freescale
% Freescale Confidential Proprietary
% Freescale Copyright (C) 2005 All rights reserved
% ----------------------------------------------------------------------------
% $RCSfile: mGenHeader.m.rca $
% Tag $Name: $
% $Revision: 1.1 $
% $Date: Wed Sep 27 11:19:00 2006 $
% Target: Matlab
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%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 format to '1R1I'
if ~exist('format','var')
format='1R1I';
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
%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
%open Header file
fid = fopen(fname,'w');
if fid==-1
error('Error: Can not open file "%s".',fname);
end
switch format
case 'R'
fprintf(fid,'%s[%d] = {\n',arrayname,InLen);
for i=1:InLen
if (i == InLen)
fprintf(fid,'%d\n};\n',in_real(i));
else
fprintf(fid,'%d,\n',in_real(i));
end
end
case '1R1I'
fprintf(fid,'%s[%d] = {\n',arrayname,InLen*2);
for i=1:InLen
if (i == InLen)
fprintf(fid,'%d,%d\n};\n',in_real(i),in_imag(i));
else
fprintf(fid,'%d,%d,\n',in_real(i),in_imag(i));
end
end
end
%close Header file
fclose(fid);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -