]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/rose.m
update packages
[CreaPhase.git] / octave_packages / m / plot / rose.m
1 ## Copyright (C) 2007-2012 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} rose (@var{th}, @var{r})
21 ## @deftypefnx {Function File} {} rose (@var{h}, @dots{})
22 ## @deftypefnx {Function File} {@var{h} =} rose (@dots{})
23 ## @deftypefnx {Function File} {[@var{r}, @var{th}] =} rose (@dots{})
24 ##
25 ## Plot an angular histogram.  With one vector argument @var{th}, plots the
26 ## histogram with 20 angular bins.  If @var{th} is a matrix, then each column
27 ## of @var{th} produces a separate histogram.
28 ##
29 ## If @var{r} is given and is a scalar, then the histogram is produced with
30 ## @var{r} bins.  If @var{r} is a vector, then the center of each bin are
31 ## defined by the values of @var{r}.
32 ##
33 ## The optional return value @var{h} is a vector of graphics handles to the
34 ## line objects representing each histogram.
35 ##
36 ## If two output arguments are requested then, rather than plotting the
37 ## histogram, the polar vectors necessary to plot the histogram are
38 ## returned.
39 ##
40 ## @example
41 ## @group
42 ## [r, t] = rose ([2*randn(1e5,1), pi + 2*randn(1e5,1)]);
43 ## polar (r, t);
44 ## @end group
45 ## @end example
46 ##
47 ## @seealso{polar, compass, hist}
48 ## @end deftypefn
49
50 function [thout, rout] = rose (varargin)
51
52   [h, varargin, nargin] = __plt_get_axis_arg__ ((nargout > 1), "rose",
53                                                 varargin{:});
54
55   if (nargin < 1)
56     print_usage ();
57   endif
58
59   ## Force theta to [0,2*pi] range
60   th = varargin {1};
61   th = atan2  (sin (th), cos (th)) + pi;
62
63   if (nargin > 1)
64     x = varargin {2};
65     if (isscalar (x))
66       x = [0.5/x : 1/x : 1] * 2 * pi;
67     else
68       ## Force theta to [0,2*pi] range
69       x = atan2  (sin (x), cos (x)) + pi;
70     endif
71   else
72     x = [1/40 : 1/20 : 1] * 2 * pi;
73   endif
74
75   [nn, xx] = hist (th, x);
76   xx = xx(:).';
77   if (isvector (nn))
78     nn = nn (:);
79   endif
80   x1 = xx(1:end-1) + diff (xx, 1) / 2;
81   x1 = [x1 ; x1; x1; x1](:);
82   th = [0; 0; x1; 2*pi ; 2*pi];
83   r = zeros (4 * size (nn, 1), size (nn, 2));
84   r(2:4:end, :) = nn;
85   r(3:4:end, :) = nn;
86
87   if (nargout < 2)
88     oldh = gca ();
89     unwind_protect
90       axes (h);
91       newplot ();
92       hlist = polar (h, th, r);
93     unwind_protect_cleanup
94       axes (oldh);
95     end_unwind_protect
96
97     if (nargout > 0)
98       thout = hlist;
99     endif
100   else
101     thout = th;
102     rout = r;
103   endif
104
105 endfunction
106
107
108 %!demo
109 %! clf
110 %! rose ([2*randn(1e5, 1), pi + 2*randn(1e5, 1)]);
111