-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfloat2integer.m
More file actions
42 lines (38 loc) · 1.46 KB
/
float2integer.m
File metadata and controls
42 lines (38 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function U = float2integer(X,divisor,offset,inttype,varargin)
% U = float2integer(X,divisor,offset,inttype,varargin)
% convert float to scaled integer of specified type (to make files smaller)
% such that X can be recovered by X = offset + U/divisor
%
% input
% X - floating point value (single or double) to be converted
% by X = offset + U/divisor
% divisor
%` offset (in most cases, offset=0)
% inttype - e.g. 'uint16', 'int16', 'uint8', or 'int8'
%
% optional input
% minimum and maximum - either as 2 variables, or as vector of length 2
%
% Example -- scale aspects, whos values range from -180 to +180, as int8
% I = float2integer(aspect,(2^7-1)/180,0,'int8',-180,180)
noptarg = size(varargin,2);
if noptarg>0
if noptarg==1
minmaxV = varargin{1};
elseif noptarg==2
minmaxV = [varargin{1} varargin{2}];
else
error('just 1 or 2 optional arguments, no more!')
end
X = truncateLimits(X,minmaxV);
end
X = double(X);
t = isnan(X);
% make sure stays within bounds of this integer type
limits = offset+[double(intmin(inttype))/divisor double(intmax(inttype))/divisor];
assert(nanmin(X(:))>=limits(1) && nanmax(X(:))<=limits(2),...
'after conversion, some X values (%f<%f or %f>%f) fall outside integer %s''s range [%d %d]',...
nanmin(X(:)),limits(1),nanmax(X(:)),limits(2),intmin(inttype),intmax(inttype))
U = cast(round((X-offset)*divisor),inttype);
U(t) = intmin(inttype);
end