]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/eyediagram.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / eyediagram.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} {} eyediagram (@var{x},@var{n})
18 ## @deftypefnx {Function File} {} eyediagram (@var{x},@var{n},@var{per})
19 ## @deftypefnx {Function File} {} eyediagram (@var{x},@var{n},@var{per},@var{off})
20 ## @deftypefnx {Function File} {} eyediagram (@var{x},@var{n},@var{per},@var{off},@var{str})
21 ## @deftypefnx {Function File} {} eyediagram (@var{x},@var{n},@var{per},@var{off},@var{str},@var{h})
22 ## @deftypefnx {Function File} {@var{h} =} eyediagram (@var{...})
23 ##
24 ## Plot the eye-diagram of a signal. The signal @var{x} can be either in one
25 ## 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}. A single eye-diagram representing this signal is plotted.
31 ## @item A complex vector
32 ## In this case the in-phase and quadrature components of the signal are 
33 ## plotted seperately.
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.
37 ## @end table
38 ##
39 ## Each line of the eye-diagram has @var{n} elements and the period is assumed
40 ## to be given by @var{per}. The time axis is then [-@var{per}/2 @var{per}/2].
41 ## By default @var{per} is 1.
42 ##
43 ## By default the signal is assumed to start at -@var{per}/2. This can be
44 ## overridden by the @var{off} variable, which gives the number of samples
45 ## to delay the signal.
46 ##
47 ## The string @var{str} is a plot style string (example 'r+'),
48 ## and by default is the default gnuplot line style.
49 ##
50 ## The figure handle to use can be defined by @var{h}. If @var{h} is not 
51 ## given, then the next available figure handle is used. The figure handle
52 ## used in returned on @var{hout}.
53 ## @end deftypefn
54 ## @seealso{scatterplot}
55
56 ## 2005-04-23 Dmitri A. Sergatskov <dasergatskov@gmail.com>
57 ##     * modified for new gnuplot interface (octave > 2.9.0)
58
59 function varargout = eyediagram (x, n, _per, _off, str, h)
60
61   if ((nargin < 2) || (nargin > 6))
62     usage (" h = eyediagram (x, n [, per [, off [, str [, h]]]])");
63   endif
64   
65   if (isreal(x))
66     if (min(size(x)) == 1)
67       signal = "real";
68       xr = x(:);
69     elseif (size(x,2) == 2)
70       signal = "complex";
71       xr = x(:,1);
72       xr = x(:,2);
73     else
74       error ("eyediagram: real signal input must be a vector");
75     endif
76   else
77     signal = "complex";
78     if (min(size(x)) != 1)
79       error ("eyediagram: complex signal input must be a vector");
80     endif
81     xr = real(x(:));
82     xi = imag(x(:));
83   endif
84   
85   if (!length(xr))
86     error ("eyediagram: zero length signal");
87   endif
88   
89   if (!isscalar(n) || !isreal(n) || (floor(n) != n) || (n < 1))
90     error ("eyediagram: n must be a positive non-zero integer");
91   endif
92
93   if (nargin > 2)
94     if (isempty(_per))
95       per = 1;
96     elseif (isscalar(_per) && isreal(_per))
97       per = _per;
98     else
99       error ("eyediagram: period must be a real scalar");
100     endif
101   else
102     per = 1;
103   endif
104
105   if (nargin > 3)
106     if (isempty(_off))
107       off = 0;
108     elseif (!isscalar(_off) || !isreal(_off) || (floor(_off) != _off) || ...
109                   (_off < 0) || (_off > (n-1)))
110       error ("eyediagram: offset must be an integer between 0 and n");
111     else
112       off = _off;
113     endif
114   else
115     off = 0;
116   endif
117
118   if (nargin > 4)
119     if (isempty(str))
120       fmt = "-r";
121     elseif (ischar(str))
122       fmt = str;
123     else
124       error ("eyediagram: plot format must be a string");
125     endif
126   else
127     fmt = "-r";
128   endif
129
130   if (nargin > 5)
131     if (isempty(h))
132       hout = figure ();
133     else
134       hout = figure (h);
135     endif
136   else
137     hout = figure ();
138   endif
139
140   horiz = (per*[0:n]/n - per/2)';
141   if (2*floor(n/2) != n)
142     horiz = horiz - per / n / 2;
143   endif
144   lx = length(xr);
145   off = mod(off+ceil(n/2),n);
146   Nn = ceil((off + lx) / n);
147   post = Nn*n - off - lx;
148   xr = reshape([NaN * ones(off,1); xr; NaN * ones(post,1)],n,Nn);
149   xr = [xr ; [xr(1,2:end), NaN]];
150   xr = [xr; NaN*ones(1,Nn)];
151   if (all(isnan(xr(2:end,end))))
152     xr(:,end) = [];
153     horiz = [repmat(horiz(1:n+1),1,Nn-1);NaN*ones(1,Nn-1)](:);
154   else
155     horiz = [repmat(horiz(1:n+1),1,Nn);NaN*ones(1,Nn)](:);
156   endif
157
158   if (strcmp(signal,"complex"))
159     xi = reshape([NaN * ones(off,1); xi; NaN * ones(post,1)],n,Nn);
160     xi = [xi ; [xi(1,2:end), NaN]];
161     xi = [xi; NaN*ones(1,Nn)];
162     if (all(isnan(xi(2:end,end))))
163       xi(:,end) = [];
164     endif
165   endif
166
167   if (strcmp(signal,"complex"))
168     subplot(2,1,1);
169     plot(horiz,xr(:),fmt);
170     title("Eye-diagram for in-phase signal");
171     xlabel("Time");
172     ylabel("Amplitude");
173     subplot(2,1,2);
174     plot(horiz,xi(:),fmt);
175     title("Eye-diagram for quadrature signal");
176     xlabel("Time");
177     ylabel("Amplitude");
178   else
179     plot(horiz,xr(:),fmt);
180     title("Eye-diagram for signal");
181     xlabel("Time");
182     ylabel("Amplitude");
183   endif
184
185   if (nargout > 0)
186     varargout{1} = hout;
187   endif
188
189 endfunction
190
191 %!demo
192 %! n = 50;
193 %! ovsp=50;
194 %! x = 1:n;
195 %! xi = [1:1/ovsp:n-0.1];
196 %! y = randsrc(1,n,[1 + 1i, 1 - 1i, -1 - 1i, -1 + 1i]) ;
197 %! yi = interp1(x,y,xi);
198 %! noisy = awgn(yi,15,"measured");
199 %! eyediagram(noisy,ovsp);