summaryrefslogtreecommitdiff
path: root/inst/wrapTo180.m
blob: c54ba95aff1a4b2c8b6d02ef6d25a56db6eb301d (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
## Copyright (C) 2015 Oscar Monerris Belda
## 
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {Function File} {@var{xwrap} =} wrapTo180 (@var{x})
##
## Wraps X into the [-180 to 180] interval.  
##
## @var{x}: angle(s) in degrees (single value, vector or ND-matrix).  
##
## @var{xwrap}: output value(s) in the range [-180 .. 180] degrees.
## The interval [-180 .. 180] is a closed interval: values equal to
## negative odd multiples of -180 are mapped to -180, values equal
## to an odd multiple of 180 are mapped to 180.
##
## @example
##  wrapTo180 ([-181, -180, -50; 180, 200, 460])
##  ans =
##   179  -180   -50
##   180  -160   100
## @end example
##
## @seealso{unwrap, wrapToPi, wrapTo2Pi}
## @end deftypefn

function xwrap = wrapTo180 (x)

  xwrap = rem (x, 360);
  idx = find (abs (xwrap) > 180);
  xwrap(idx) -= 360 * sign (xwrap(idx));

endfunction


%!test
%! x = -800:0.1:800;
%! xw = wrapTo180 (x);
%! assert (sind (x), sind (xw), 16 * eps)
%! assert (cosd (x), cosd (xw), 16 * eps)
%! assert (! any (xw < -180))
%! assert (! any (xw > 180))

%!test
%! c = [-721.1, -718.9, -481.3, -479.99, -361, -359, -200, -180-(1e-14), -180, ...
%!      -180-(2e-14), -160, -eps, 0, eps, 160, 180, 180+(1e-14), 180+(2e-14), 200];
%! assert (wrapTo180 (c), [-1.10, 1.10, -121.30, -119.99, -1.0, 1.0, 160.0, ...
%!                         -180.0, -180.0, 180.0, -160.0, -0.0, 0.0, 0.0, ...
%!                         160.0, 180.0, 180.0, -180.0, -160.0], 1e-13);