]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/apkconst.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / apkconst.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} {} apkconst (@var{nsig})
18 ## @deftypefnx {Function File} {} apkconst (@var{nsig},@var{amp})
19 ## @deftypefnx {Function File} {} apkconst (@var{nsig},@var{amp},@var{phs})
20 ## @deftypefnx {Function File} {} apkconst (@var{...},"n")
21 ## @deftypefnx {Function File} {} apkconst (@var{...},@var{str})
22 ## @deftypefnx {Function File} {@var{y} = } apkconst (@var{...})
23 ##
24 ## Plots a ASK/PSK signal constellation. Argument @var{nsig} is a real vector
25 ## whose length determines the number of ASK radii in the constellation.
26 ## The values of vector @var{nsig} determine the number of points in each 
27 ## ASK radii. 
28 ##
29 ## By default the radii of each ASK modulated level is given by the index of 
30 ## @var{nsig}. The amplitudes can be defined explictly in the variable
31 ## @var{amp}, which  is a vector of the same length as @var{nsig}.
32 ##
33 ## By default the first point in each ASK radii has zero phase, and following
34 ## points are coding in an anti-clockwise manner. If @var{phs} is defined then
35 ## it is a vector of the same length as @var{nsig} defining the initial phase
36 ## in each ASK radii.
37 ##
38 ## In addition @dfn{apkconst} takes two string arguments 'n' and and @var{str}.
39 ## If the string 'n' is included in the arguments, then a number is printed
40 ## next to each constellation point giving the symbol value that would be
41 ## mapped to this point by the @dfn{modmap} function. The argument @var{str}
42 ## is a plot style string (example 'r+') and determines the default gnuplot
43 ## point style to use for plot points in the constellation.
44 ##
45 ## If @dfn{apskconst} is called with a return argument, then no plot is
46 ## created. However the return value is a vector giving the in-phase and
47 ## quadrature values of the symbols in the constellation.
48 ## @end deftypefn
49 ## @seealso{dmod,ddemod,modmap,demodmap}
50
51
52 ## 2005-04-23 Dmitri A. Sergatskov <dasergatskov@gmail.com>
53 ##     * modified for new gnuplot interface (octave > 2.9.0)
54
55
56
57 function yout = apkconst(varargin)
58
59   if ((nargin < 1) || (nargin > 5))
60     error ("apkconst: incorrect number of arguments");
61   endif
62
63   numargs = 0;
64   printnums = 0;
65   fmt = "+r";
66   amp = [];
67   phs = [];
68
69   for i=1:length(varargin)
70     arg = varargin{i};
71     if (ischar(arg))
72       if (strcmp(arg,"n"))
73               try
74                 text();
75                 printnums = 1;
76               catch
77                 printnums = 0;
78               end
79       else
80               fmt = arg;
81       endif
82     else
83       numargs++;
84       switch (numargs)
85               case 1,
86                 nsig = arg; 
87               case 2,
88                 amp = arg; 
89               case 3,
90                 phs = arg; 
91               otherwise
92                 error ("apkconst: too many numerical arguments");
93       endswitch
94     endif
95   end
96
97   if (numargs < 1)
98     error ("apkconst: must have at least one vector argument");
99   endif
100
101   if (isempty(amp))
102     amp = 1:length(nsig);
103   endif
104
105   if (isempty(phs))
106     phs = zeros(size(amp));
107   endif
108
109   if (!isvector(nsig) || !isvector(amp) || !isvector(phs) || ...
110       (length(nsig) != length(amp)) || (length(nsig) != length(phs)))
111     error ("apkconst: numerical arguments must be vectors of the same length");
112   endif
113
114   if (length(nsig) == 0)
115     error ("apkconst: first numerical argument must have non-zero length");
116   endif
117
118   y = [];
119   for i=1:length(nsig)
120     if (nsig(i) < 1)
121       error ("apkconst: must have at least one point in ASK radii");
122     endif
123     y = [y; amp(i) * [cos(2*pi*[0:nsig(i)-1]'/nsig(i) + phs(i)) + ...
124                                   1i*sin(2*pi*[0:nsig(i)-1]'/nsig(i) + phs(i))]];
125   end
126
127   if (nargout == 0)
128     r = [0:0.02:2]'*pi;
129     x0 = cos(r) * amp;
130     y0 = sin(r) * amp;
131     plot(x0, y0, "b");
132     yy = [real(y), imag(y)];
133     hold on;
134     if (printnums)
135       xd = 0.05 * max(real(y));
136       for i=1:length(y)
137               text(real(y(i))+xd,imag(y(i)),num2str(i-1));
138       end
139     endif
140     plot (real(y), imag(y), fmt);
141
142     title("ASK/PSK Constellation");
143     xlabel("In-phase");
144     ylabel("Quadrature");
145   else
146     yout = y;
147   endif
148
149 endfunction