Home > NoiseTools > nt_double2int.m

# nt_double2int

## PURPOSE

nt_double2int() - recode/decode double as integer to save space

## SYNOPSIS

function z=nt_double2int(x,param)

## DESCRIPTION

    nt_double2int() - recode/decode double as integer to save space

     z=nt_double2int(x,intsize): recode double as integer
       z: coded data
       x: data to code (matrix or array of matrices)
       intsize: [default: 'int16']

     x=nt_double2int(z,indices): decode back to double
       x: decoded data
       z: coded data
       indices: indices of rows/columns [default: all]
         indices{1}: rows to keep
         indices{2}: columns to keep

     Data are coded as a structure including a matrix of integers together with
     min and max of each column:
       z.ints: data scaled and coded as ints
       z.min: min of original data
       z.max: max of original data
       z.intsize: integer type (e.g. 'int16')

     They can also consist of a cell array of such structures.

     NoiseTools

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_double2int nt_double2int() - recode/decode double as integer to save space
- nt_greetings nt_greetings - display message the first time the toolbox is used

This function is called by:

- nt_double2int nt_double2int() - recode/decode double as integer to save space
- nt_idxx nt_idxx(fname,iname,bsize,chunksize,nfft) - create an index file to summarize large data file
- nt_plotxx nt_plotxx(fname,bounds,chans) - plot using index file

## SOURCE CODE

    0001 function z=nt_double2int(x,param)
    0002 %nt_double2int() - recode/decode double as integer to save space
    0003 %
    0004 % z=nt_double2int(x,intsize): recode double as integer
    0005 %   z: coded data
    0006 %   x: data to code (matrix or array of matrices)
    0007 %   intsize: [default: 'int16']
    0008 %
    0009 % x=nt_double2int(z,indices): decode back to double
    0010 %   x: decoded data
    0011 %   z: coded data
    0012 %   indices: indices of rows/columns [default: all]
    0013 %     indices{1}: rows to keep
    0014 %     indices{2}: columns to keep
    0015 %
    0016 % Data are coded as a structure including a matrix of integers together with
    0017 % min and max of each column:
    0018 %   z.ints: data scaled and coded as ints
    0019 %   z.min: min of original data
    0020 %   z.max: max of original data
    0021 %   z.intsize: integer type (e.g. 'int16')
    0022 %
    0023 % They can also consist of a cell array of such structures.
    0024 %
    0025 % NoiseTools
    0026
    0027 nt_greetings;
    0028
    0029
    0030 if iscell(x)
    0031     for iChunk=1:numel(x)
    0032         z{iChunk}=nt_double2int(x{iChunk},param);
    0033     end
    0034     return
    0035 end
    0036
    0037 if isfloat(x) % code to integer
    0038
    0039     if nargin<2; param='int16'; end
    0040     intsize=param;
    0041     sz=size(x);
    0042     if ndims(x)>2; x=x(:,:); end
    0043
    0044     z.ints=zeros(size(x),intsize);
    0045     z.min=zeros(1,size(x,2));
    0046     z.max=zeros(1,size(x,2));
    0047     z.intsize=intsize;
    0048
    0049     % recode columns individually
    0050     for iCol=1:size(x,2);
    0051         z.min(iCol)=min(x(:,iCol));
    0052         z.max(iCol)=max(x(:,iCol));
    0053         z.ints(:,iCol)= double(intmin(intsize)) + ...
    0054             (x(:,iCol) - z.min(iCol)) * ...
    0055             ( double(intmax(intsize))-double(intmin(intsize)) ) /...
    0056             ( z.max(iCol)-z.min(iCol) ); % automatically coerced to intsize
    0057     end
    0058     z.ints=reshape(z.ints,sz);
    0059
    0060 else % decode back to double
    0061
    0062     assert(isstruct(x), '!');
    0063     intsize=x.intsize;
    0064     sz=size(x.ints);
    0065     if ndims(x.ints)>2; x.ints=x.ints(:,:); end
    0066
    0067     if nargin<2; param=[]; end
    0068     indices=param;
    0069
    0070     if isempty(indices);
    0071
    0072         % default: decode all
    0073         z=zeros(size(x.ints));
    0074         for iCol=1:size(x.ints,2)
    0075             z(:,iCol) = x.min(iCol) + ...
    0076                 (x.max(iCol)-x.min(iCol)) / (double(intmax(intsize))-double(intmin(intsize))) * (double(x.ints(:,iCol))-double(intmin(intsize)));
    0077         end
    0078         z=reshape(z,sz);
    0079
    0080     else
    0081         % select rows and/or columns
    0082         if numel(indices)<2; indices{2}=[]; end
    0083         if isempty(indices{1}); indices{1}=1:size(x.ints,1); end
    0084         if isempty(indices{2}); indices{2}=1:size(x.ints,2); end
    0085
    0086         z=zeros(numel(indices{1}),numel(indices{2}));
    0087
    0088         for idx=1:numel(indices{2})
    0089             iCol=indices{2}(idx);
    0090             z(:,idx) = x.min(iCol) + ...
    0091                 (x.max(iCol)-x.min(iCol)) / (double ( double(intmax(intsize))-double(intmin(intsize)))) * (double(x.ints(indices{1},iCol))-double(intmin(intsize)));
    0092         end
    0093
    0094
    0095         if numel(sz)>2 && numel(indices{2})<size(x.ints,2)
    0096             warning('output data has not been reshaped');
    0097         else
    0098             z=reshape(z,[numel(indices{1}),numel(indices{2})]);
    0099         end
    0100
    0101     end
    0102
    0103 end
    0104
    0105
    0106
    0107
    0108