PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/R2-1-x/octave-forge/main/comm/eyediagram.m

#
MATLAB | 270 lines | 247 code | 23 blank | 0 comment | 47 complexity | 89d8261ae41455c1ceb231d897da3a7e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. ## Copyright (C) 2003 David Bateman
  2. ##
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2 of the License, or
  6. ## (at your option) any later version.
  7. ##
  8. ## This program is distributed in the hope that it will be useful,
  9. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ## GNU General Public License for more details.
  12. ##
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this program; if not, write to the Free Software
  15. ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  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. ## 2005-04-23 Dmitri A. Sergatskov <dasergatskov@gmail.com>
  56. ## * modified for new gnuplot interface (octave > 2.9.0)
  57. function hout = eyediagram (x, n, _per, _off, str, h)
  58. if ((nargin < 2) || (nargin > 6))
  59. usage (" h = eyediagram (x, n [, per [, off [, str [, h]]]])");
  60. endif
  61. if (isreal(x))
  62. if (min(size(x)) == 1)
  63. signal = "real";
  64. xr = x(:);
  65. elseif (size(x,2) == 2)
  66. signal = "complex";
  67. xr = x(:,1);
  68. xr = x(:,2);
  69. else
  70. error ("eyediagram: real signal input must be a vector");
  71. endif
  72. else
  73. signal = "complex";
  74. if (min(size(x)) != 1)
  75. error ("eyediagram: complex signal input must be a vector");
  76. endif
  77. xr = real(x(:));
  78. xi = imag(x(:));
  79. endif
  80. if (!length(xr))
  81. error ("eyediagram: zero length signal");
  82. endif
  83. if (!isscalar(n) || !isreal(n) || (floor(n) != n) || (n < 1))
  84. error ("eyediagram: n must be a positive non-zero integer");
  85. endif
  86. if (nargin > 2)
  87. if (isempty(_per))
  88. per = 1;
  89. elseif (isscalar(_per) && isreal(_per))
  90. per = _per;
  91. else
  92. error ("eyediagram: period must be a real scalar");
  93. endif
  94. else
  95. per = 1;
  96. endif
  97. if (nargin > 3)
  98. if (isempty(_off))
  99. off = 0;
  100. elseif (!isscalar(_off) || !isreal(_off) || (floor(_off) != _off) || ...
  101. (_off < 0) || (_off > (n-1)))
  102. error ("eyediagram: offset must be an integer between 0 and n");
  103. else
  104. off = _off;
  105. endif
  106. else
  107. off = 0;
  108. endif
  109. if (nargin > 4)
  110. if (isempty(str))
  111. fmt = "w l 1";
  112. elseif (ischar(str))
  113. fmt = __pltopt__ ("eyediagram", str);
  114. else
  115. error ("eyediagram: plot format must be a string");
  116. endif
  117. else
  118. fmt = "w l 1";
  119. endif
  120. if (nargin > 5)
  121. if (isempty(h))
  122. if (!gnuplot_has_frames)
  123. hout = figure ();
  124. else
  125. hout = 0;
  126. endif
  127. elseif (!isscalar(h) || !isreal(h) || (floor(h) != h) || (h < 0))
  128. error ("eyediagram: figure handle must be a positive integer");
  129. else
  130. if (!gnuplot_has_frames)
  131. error ("eyediagram: gnuplot must have frames for figure handles");
  132. endif
  133. hout = figure (h);
  134. endif
  135. else
  136. if (!gnuplot_has_frames)
  137. hout = figure ();
  138. else
  139. hout = 0;
  140. endif
  141. endif
  142. neven = (2*floor(n/2) == n);
  143. if (neven)
  144. horiz = (per*[0:n]/n - per/2)';
  145. else
  146. horiz = (n-2)/(n-1)*(per*[0:n-1]/(n-1) - per/2)';
  147. endif
  148. lx = length(xr);
  149. off = mod(off+ceil(n/2),n);
  150. ## Need to save data to a temporary file, since we use line breaks
  151. ## in the file to specify a discontinous plot to gnuplot plot. This
  152. ## is much faster than multiple plot commands.....
  153. tmpfile = tmpnam();
  154. try mark_for_deletion(tmpfile);
  155. catch
  156. warning("eyediagram: temporary file %s will not be deleted!\n",tmpfile)
  157. end
  158. fid = fopen(tmpfile,"w+t");
  159. if (fid == -1)
  160. error ("eyediagram: error opening temporary file");
  161. endif
  162. if (strcmp(signal,"complex"))
  163. if (off != 0)
  164. fprintf(fid, "%10.3e %10.3e %10.3e\n", [horiz(n-off+1:n), xr(1:off), ...
  165. xi(1:off)]');
  166. if (neven && (lx != off))
  167. fprintf(fid, "%10.3e %10.3e %10.3e\n", [horiz(n+1), xr(off+1), ...
  168. xi(off+1)]');
  169. endif
  170. fprintf(fid, "\n");
  171. indx = off;
  172. else
  173. indx = 0;
  174. endif
  175. while ((lx-indx) >= n)
  176. fprintf(fid, "%10.3e %10.3e %10.3e\n", [horiz(1:n), xr(indx+1:indx+n), ...
  177. xi(indx+1:indx+n)]');
  178. if (neven && (lx != indx+n))
  179. fprintf(fid, "%10.3e %10.3e %10.3e\n", [horiz(n+1), xr(indx+n+1), ...
  180. xi(indx+n+1)]');
  181. endif
  182. fprintf(fid, "\n");
  183. indx = indx + n;
  184. endwhile
  185. if ((lx-indx) != 0)
  186. fprintf(fid, "%10.3e %10.3e %10.3e\n", [horiz(1:lx-indx), ...
  187. xr(indx+1:lx), xi(indx+1:lx)]');
  188. endif
  189. else
  190. if (off != 0)
  191. fprintf(fid, "%10.3e %10.3e\n", [horiz(n-off+1:n), xr(1:off)]');
  192. if (neven && (lx != off))
  193. fprintf(fid, "%10.3e %10.3e\n", [horiz(n+1), xr(off+1)]');
  194. endif
  195. fprintf(fid, "\n");
  196. indx = off;
  197. else
  198. indx = 0;
  199. endif
  200. while ((lx-indx) >= n)
  201. fprintf(fid, "%10.3e %10.3e\n", [horiz(1:n), xr(indx+1:indx+n)]');
  202. if (neven && (lx != indx+n))
  203. fprintf(fid, "%10.3e %10.3e\n", [horiz(n+1), xr(indx+n+1)]');
  204. endif
  205. fprintf(fid, "\n");
  206. indx = indx + n;
  207. endwhile
  208. if ((lx-indx) != 0)
  209. fprintf(fid, "%10.3e %10.3e\n", [horiz(1:lx-indx), xr(indx+1:lx)]');
  210. endif
  211. endif
  212. fclose(fid);
  213. try ar = automatic_replot;
  214. catch ar = 0;
  215. end
  216. unwind_protect
  217. if (strcmp(signal,"complex"))
  218. subplot(2,1,1);
  219. title("Eye-diagram for in-phase signal");
  220. else
  221. oneplot();
  222. title("Eye-diagram for signal");
  223. endif
  224. xlabel("Time");
  225. ylabel("Amplitude");
  226. legend("off");
  227. hold off;
  228. __gnuplot_plot__ tmpfile ;
  229. if (strcmp(signal,"complex"))
  230. subplot(2,1,2);
  231. title("Eye-diagram for quadrature signal");
  232. __gnuplot_plot__ tmpfile using 1:3;
  233. endif
  234. unwind_protect_cleanup
  235. xlabel("");
  236. ylabel("");
  237. title("");
  238. if (strcmp(signal,"complex"))
  239. oneplot();
  240. endif
  241. automatic_replot = ar;
  242. end_unwind_protect
  243. ## XXX FIXME XXX
  244. ## Can't unlink the tmpfile now, since can't guarantee gnuplot finished
  245. ## with it!!!
  246. ## [u, msg] = unlink(tmpfile);
  247. endfunction