Home > FMAToolbox > Data > GetAngles.m

GetAngles

PURPOSE ^

GetAngles - Get angles (from position samples).

SYNOPSIS ^

function angles = GetAngles(varargin)

DESCRIPTION ^

GetAngles - Get angles (from position samples).

  Angles are returned in radians (in [-pi,pi]). This function requires two
  or more LEDs (only the first two are used).

  USAGE

    angles = GetAngles(<options>)

    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'mode'         'all' gets all position samples, 'clean' discards bad
                    position samples (lights out of boundaries or too close
                    - see SETTINGS) (default 'clean')
    =========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function angles = GetAngles(varargin)
0002 
0003 %GetAngles - Get angles (from position samples).
0004 %
0005 %  Angles are returned in radians (in [-pi,pi]). This function requires two
0006 %  or more LEDs (only the first two are used).
0007 %
0008 %  USAGE
0009 %
0010 %    angles = GetAngles(<options>)
0011 %
0012 %    <options>      optional list of property-value pairs (see table below)
0013 %
0014 %    =========================================================================
0015 %     Properties    Values
0016 %    -------------------------------------------------------------------------
0017 %     'mode'         'all' gets all position samples, 'clean' discards bad
0018 %                    position samples (lights out of boundaries or too close
0019 %                    - see SETTINGS) (default 'clean')
0020 %    =========================================================================
0021 
0022 % Copyright (C) 2012 by Michaƫl Zugaro
0023 %
0024 % This program is free software; you can redistribute it and/or modify
0025 % it under the terms of the GNU General Public License as published by
0026 % the Free Software Foundation; either version 3 of the License, or
0027 % (at your option) any later version.
0028 
0029 global DATA SETTINGS;
0030 if isempty(DATA),
0031     error('No session defined (did you forget to call SetCurrentSession? Type ''help <a href="matlab:help Data">Data</a>'' for details).');
0032 end
0033 
0034 % Default values
0035 mode = 'clean';
0036 
0037 if mod(length(varargin),2) ~= 0,
0038     error('Incorrect number of parameters (type ''help <a href="matlab:help GetAngles">GetAngles</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+1) ' is not a property (type ''help <a href="matlab:help GetAngles">GetAngles</a>'' for details).']);
0045     end
0046     switch(lower(varargin{i})),
0047         case 'mode',
0048             mode = lower(varargin{i+1});
0049             if ~isastring(mode,'clean','all'),
0050                 error('Incorrect value for property ''mode'' (type ''help <a href="matlab:help GetAngles">GetAngles</a>'' for details).');
0051             end
0052         otherwise,
0053             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help GetAngles">GetAngles</a>'' for details).']);
0054     end
0055 end
0056 
0057 angles = [];
0058 positions = DATA.positions;
0059 if isempty(positions), return; end
0060 
0061 % Make sure we have at least two LEDs
0062 if size(positions,2) < 5,
0063     error('Insufficient number of LEDs (type ''help <a href="matlab:help GetAngles">GetAngles</a>'' for details).');
0064 end
0065 
0066 % Discard positions with incorrect distance between lights
0067 if strcmp(mode,'clean'),
0068     distance = sqrt((positions(:,4)-positions(:,2)).^2+(positions(:,5)-positions(:,3)).^2);
0069     selected = (distance >= SETTINGS.minDistance & distance <= SETTINGS.maxDistance);
0070     positions = positions(selected,:);
0071 end
0072 
0073 % Discard partial/complete undetects (by convention, values of -1)
0074 discard = any(positions(:,2:end) == -1,2);
0075 positions(discard,:) = [];
0076 
0077 % Clip if necessary (but issue warning)
0078 m = min(positions(:,2:end));
0079 M = max(positions(:,2:end));
0080 if any(m<0)||any(M(1:2:end)>DATA.maxX|M(2:2:end)>DATA.maxY),
0081     warning(['Position data should stay within [0,' num2str(DATA.maxX) ']x[0,' num2str(DATA.maxY) ']. The data will now be clipped accordingly.']);
0082     positions(:,2) = Clip(positions(:,2),0,DATA.maxX);
0083     positions(:,3) = Clip(positions(:,3),0,DATA.maxY);
0084 end
0085 
0086 positions(:,2:end) = Smooth(positions(:,2:end),[5 0]);
0087 angles(:,1) = positions(:,1);
0088 angles(:,2) = angle( positions(:,4)-positions(:,2)+j*(positions(:,5)-positions(:,3)) );

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