Home > FMAToolbox > IO > ChangeBinaryGain.m

ChangeBinaryGain

PURPOSE ^

ChangeBinaryGain - Change gain in a multiplexed binary file.

SYNOPSIS ^

function ChangeBinaryGain(filename,varargin)

DESCRIPTION ^

ChangeBinaryGain - Change gain in a multiplexed binary file.

  USAGE

    ChangeBinaryGain(filename,<options>)

    filename       file to read
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'output'      output file name (default = append '-gains')
     'nChannels'   number of data channels in the file (default = 1)
     'gains'       list of gains for each channel, or one value for all
     'precision'   sample precision (default = 'int16')
     'skip'        number of bytes to skip after each value is read
                   (default = 0)
    =========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ChangeBinaryGain(filename,varargin)
0002 
0003 %ChangeBinaryGain - Change gain in a multiplexed binary file.
0004 %
0005 %  USAGE
0006 %
0007 %    ChangeBinaryGain(filename,<options>)
0008 %
0009 %    filename       file to read
0010 %    <options>      optional list of property-value pairs (see table below)
0011 %
0012 %    =========================================================================
0013 %     Properties    Values
0014 %    -------------------------------------------------------------------------
0015 %     'output'      output file name (default = append '-gains')
0016 %     'nChannels'   number of data channels in the file (default = 1)
0017 %     'gains'       list of gains for each channel, or one value for all
0018 %     'precision'   sample precision (default = 'int16')
0019 %     'skip'        number of bytes to skip after each value is read
0020 %                   (default = 0)
0021 %    =========================================================================
0022 
0023 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0024 %
0025 % This program is free software; you can redistribute it and/or modify
0026 % it under the terms of the GNU General Public License as published by
0027 % the Free Software Foundation; either version 3 of the License, or
0028 % (at your option) any later version.
0029 
0030 % Default values
0031 nChannels = 1;
0032 precision = 'int16';
0033 skip = 0;
0034 gains = [];
0035 output = [filename '-gains'];
0036 
0037 if nargin < 1 | mod(length(varargin),2) ~= 0,
0038   error('Incorrect number of parameters (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).');
0039 end
0040 
0041 % Parse options
0042 for i = 1:2:length(varargin),
0043     if ~ischar(varargin{i}),
0044         error(['Parameter ' num2str(i+3) ' is not a property (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).']);
0045     end
0046     switch(lower(varargin{i})),
0047         case 'output',
0048             output = varargin{i+1};
0049             if ~isastring(output),
0050                 error('Incorrect value for property ''output'' (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).');
0051             end
0052         case 'nchannels',
0053             nChannels = varargin{i+1};
0054             if ~isiscalar(nChannels,'>0'),
0055                 error('Incorrect value for property ''nChannels'' (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).');
0056             end
0057         case 'gains',
0058             gains = varargin{i+1};
0059             if ~isdvector(gains,'>=0'),
0060                 error('Incorrect value for property ''gains'' (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).');
0061             end
0062         case 'precision',
0063             precision = varargin{i+1};
0064             if ~isastring(precision),
0065                 error('Incorrect value for property ''precision'' (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).');
0066             end
0067         case 'skip',
0068             skip = varargin{i+1};
0069             if ~isiscalar(skip,'>=0'),
0070                 error('Incorrect value for property ''skip'' (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).');
0071             end
0072         otherwise,
0073             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).']);
0074     end
0075 end
0076 
0077 % Check output file
0078 f = fopen(output,'r');
0079 if f ~= -1,
0080     error(['Output file ''' output ''' exists.']);
0081 end
0082 
0083 % Check gains
0084 gains = gains(:)';
0085 nGains = length(gains);
0086 if nGains == 1,
0087     gains = repmat(gains,1,nChannels);
0088     nGains = nChannels;
0089 elseif nGains ~= nChannels,
0090     error('Incorrect number of gains (type ''help <a href="matlab:help ChangeBinaryGain">ChangeBinaryGain</a>'' for details).');
0091 end
0092 
0093 % Read in chunks, apply gains and save
0094 offset = 0;
0095 nSamples = floor(100000/nChannels);
0096 while true,
0097     data = LoadBinary(filename,'nChannels',nChannels,'offset',offset,'samples',nSamples,'precision',precision,'skip',skip);
0098     data = data .* repmat(gains,size(data,1),1);
0099     SaveBinary(output,data,'precision',precision,'mode','append');
0100     offset = offset + nSamples;
0101     if size(data,1) < nSamples, break; end
0102 end

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005