]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/scatterplot.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / scatterplot.m
1 ## Copyright (C) 2003 David Bateman
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} scatterplot (@var{x})
18 ## @deftypefnx {Function File} {} scatterplot (@var{x},@var{n})
19 ## @deftypefnx {Function File} {} scatterplot (@var{x},@var{n},@var{off})
20 ## @deftypefnx {Function File} {} scatterplot (@var{x},@var{n},@var{off},@var{str})
21 ## @deftypefnx {Function File} {} scatterplot (@var{x},@var{n},@var{off},@var{str},@var{h})
22 ## @deftypefnx {Function File} {@var{h} =} scatterplot (@var{...})
23 ##
24 ## Display the scatter plot of a signal. The signal @var{x} can be either in
25 ## one of three forms
26 ##
27 ## @table @asis
28 ## @item A real vector
29 ## In this case the signal is assumed to be real and represented by the vector
30 ## @var{x}. The scatterplot is plotted along the x axis only.
31 ## @item A complex vector
32 ## In this case the in-phase and quadrature components of the signal are 
33 ## plotted seperately on the x and y axes respectively.
34 ## @item A matrix with two columns
35 ## In this case the first column represents the in-phase and the second the
36 ## quadrature components of a complex signal and are plotted on the x and
37 ## y axes respectively.
38 ## @end table
39 ##
40 ## Each point of the scatter plot is assumed to be seperated by @var{n} 
41 ## elements in the signal. The first element of the signal to plot is 
42 ## determined by @var{off}. By default @var{n} is 1 and @var{off} is 0.
43 ##
44 ## The string @var{str} is a plot style string (example 'r+'),
45 ## and by default is the default gnuplot point style.
46 ##
47 ## The figure handle to use can be defined by @var{h}. If @var{h} is not 
48 ## given, then the next available figure handle is used. The figure handle
49 ## used in returned on @var{hout}.
50 ## @end deftypefn
51 ## @seealso{eyediagram}
52
53 ## 2005-04-23 Dmitri A. Sergatskov <dasergatskov@gmail.com>
54 ##     * modified for new gnuplot interface (octave > 2.9.0)
55
56 function varargout = scatterplot (x, n, _off, str, h)
57
58   if ((nargin < 1) || (nargin > 5))
59     usage (" h = scatterplot (x, n [, off [, str [, h]]]])");
60   endif
61   
62   if (isreal(x))
63     if (min(size(x)) == 1)
64       signal = "real";
65       xr = x(:);
66       xi = zeros(size(xr));
67     elseif (size(x,2) == 2)
68       signal = "complex";
69       xr = x(:,1);
70       xi = x(:,2);
71     else
72       error ("scatterplot: real signal input must be a vector");
73     endif
74   else
75     signal = "complex";
76     if (min(size(x)) != 1)
77       error ("scatterplot: complex signal input must be a vector");
78     endif
79     xr = real(x(:));
80     xi = imag(x(:));
81   endif
82   
83   if (!length(xr))
84     error ("scatterplot: zero length signal");
85   endif
86   
87   if (nargin > 1)
88     if (!isscalar(n) || !isreal(n) || (floor(n) != n) || (n < 1))
89       error ("scatterplot: n must be a positive non-zero integer");
90     endif
91   else
92     n = 1;
93   endif
94   
95   if (nargin > 2)
96     if (!isscalar(_off) || !isreal(_off) || (floor(_off) != _off) || ...
97         (_off < 0) || (_off > length(x)-1))
98       error ("scatterplot: offset must be an integer between 0 and length(x)-1");
99     endif
100     off = _off;
101   else
102     off = 0;
103   endif
104
105   if (nargin > 3)
106     if (isempty(str))
107       fmt = "-r";
108     elseif (ischar(str))
109       fmt = str;
110     else
111       error ("scatterplot: plot format must be a string");
112     endif
113   else
114     fmt = "-r";
115   endif
116
117   if (nargin > 4)
118     hout = figure (h);
119   else
120     hout = figure ();
121   endif
122
123   xr = xr(off+1:n:rows(xr));
124   xi = xi(off+1:n:rows(xi));
125
126   plot(xr,xi,fmt);
127   if (!strcmp(signal,"complex"))
128     ## FIXME: What is the appropriate xrange
129     xmax = max(xr);
130     xmin = min(xr);
131     xran = xmax - xmin
132     xmax =  ceil(2 * xmax / xran) / 2 * xran;
133     xmin = floor(2 * xmin / xran) / 2 * xran;
134     axis([xmin, xmax, -1, 1]);
135   endif
136   title("Scatter plot");
137   xlabel("In-phase");
138   ylabel("Quadrature");
139   legend("off");
140
141   if (nargout > 0)
142     varargout{1} = hout;
143   endif
144
145 endfunction
146
147 %!demo
148 %! n = 200;
149 %! ovsp=5;
150 %! x = 1:n;
151 %! xi = [1:1/ovsp:n-0.1];
152 %! y = randsrc(1,n,[1 + 1i, 1 - 1i, -1 - 1i, -1 + 1i]) ;
153 %! yi = interp1(x,y,xi);
154 %! noisy = awgn(yi,15,"measured");
155 %! hold off;
156 %! h = scatterplot(noisy,1,0,"b",1);
157 %! hold on;
158 %! scatterplot(noisy,ovsp,0,"r+",h);