annotate main/comm/inst/encode.m @ 9666:67d4cfc5eeb3 octave-forge

comm: update license to GPLv3+
author carandraug
date Tue, 13 Mar 2012 04:31:21 +0000
parents 2de537641f94
children fe6da9839797
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2382
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
1 ## Copyright (C) 2003 David Bateman
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
2 ##
9666
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
3 ## This program is free software; you can redistribute it and/or modify it under
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
4 ## the terms of the GNU General Public License as published by the Free Software
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
5 ## Foundation; either version 3 of the License, or (at your option) any later
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
6 ## version.
2382
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
7 ##
9666
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
11 ## details.
2382
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
12 ##
9666
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
13 ## You should have received a copy of the GNU General Public License along with
67d4cfc5eeb3 comm: update license to GPLv3+
carandraug
parents: 4404
diff changeset
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
2382
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
15
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
16 ## -*- texinfo -*-
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
17 ## @deftypefn {Function File} {@var{code} =} encode (@var{msg},@var{n},@var{k})
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
18 ## @deftypefnx {Function File} {@var{code} =} encode (@var{msg},@var{n},@var{k},@var{typ})
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
19 ## @deftypefnx {Function File} {@var{code} =} encode (@var{msg},@var{n},@var{k},@var{typ},@var{opt})
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
20 ## @deftypefnx {Function File} {[@var{code}, @var{added}] =} encode (@var{...})
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
21 ##
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
22 ## Top level block encoder. This function makes use of the lower level
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
23 ## functions such as @dfn{cyclpoly}, @dfn{cyclgen}, @dfn{hammgen}, and
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
24 ## @dfn{bchenco}. The message to code is pass in @var{msg}, the
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
25 ## codeword length is @var{n} and the message length is @var{k}. This
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
26 ## function is used to encode messages using either:
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
27 ##
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
28 ## @table @asis
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
29 ## @item A [n,k] linear block code defined by a generator matrix
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
30 ## @item A [n,k] cyclic code defined by a generator polynomial
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
31 ## @item A [n,k] Hamming code defined by a primitive polynomial
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
32 ## @item A [n,k] BCH code code defined by a generator polynomial
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
33 ## @end table
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
34 ##
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
35 ## The type of coding to use is defined by the variable @var{typ}. This
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
36 ## variable is a string taking one of the values
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
37 ##
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
38 ## @table @code
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
39 ## @item 'linear' or 'linear/binary'
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
40 ## A linear block code is assumed with the coded message @var{code} being in
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
41 ## a binary format. In this case the argument @var{opt} is the generator
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
42 ## matrix, and is required.
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
43 ## @item 'cyclic' or 'cyclic/binary'
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
44 ## A cyclic code is assumed with the coded message @var{code} being in a
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
45 ## binary format. The generator polynomial to use can be defined in @var{opt}.
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
46 ## The default generator polynomial to use will be
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
47 ## @dfn{cyclpoly(@var{n},@var{k})}
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
48 ## @item 'hamming' or 'hamming/binary'
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
49 ## A Hamming code is assumed with the coded message @var{code} being in a
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
50 ## binary format. In this case @var{n} must be of an integer of the form
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
51 ## @code{2^@var{m}-1}, where @var{m} is an integer. In addition @var{k}
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
52 ## must be @code{@var{n}-@var{m}}. The primitive polynomial to use can
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
53 ## be defined in @var{opt}. The default primitive polynomial to use is
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
54 ## the same as defined by @dfn{hammgen}.
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
55 ## @item 'bch' or 'bch/binary'
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
56 ## A BCH code is assumed with the coded message @var{code} being in a binary
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
57 ## format. The generator polynomial to use can be defined in @var{opt}.
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
58 ## The default generator polynomial to use will be
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
59 ## @dfn{bchpoly(@var{n},@var{k})}
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
60 ## @end table
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
61 ##
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
62 ## In addition the argument 'binary' above can be replaced with 'decimal',
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
63 ## in which case the message is assumed to be a decimal vector, with each
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
64 ## value representing a symbol to be coded. The binary format can be in two
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
65 ## forms
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
66 ##
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
67 ## @table @code
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
68 ## @item An @var{x}-by-@var{k} matrix
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
69 ## Each row of this matrix represents a symbol to be coded
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
70 ## @item A vector
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
71 ## The symbols are created from groups of @var{k} elements of this vector.
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
72 ## If the vector length is not divisble by @var{k}, then zeros are added
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
73 ## and the number of zeros added is returned in @var{added}.
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
74 ## @end table
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
75 ##
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
76 ## It should be noted that all internal calculations are performed in the
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
77 ## binary format. Therefore for large values of @var{n}, it is preferable
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
78 ## to use the binary format to pass the messages to avoid possible rounding
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
79 ## errors. Additionally, if repeated calls to @dfn{encode} will be performed,
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
80 ## it is often faster to create a generator matrix externally with the
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
81 ## functions @dfn{hammgen} or @dfn{cyclgen}, rather than let @dfn{encode}
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
82 ## recalculate this matrix at each iteration. In this case @var{typ} should
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
83 ## be 'linear'. The exception to this case is BCH codes, whose encoder
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
84 ## is implemented directly from the polynomial and is significantly faster.
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
85 ##
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
86 ## @end deftypefn
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
87 ## @seealso{decode,cyclgen,cyclpoly,hammgen,bchenco,bchpoly}
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
88
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
89 function [code, added] = encode(msg, n, k, typ, opt)
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
90
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
91 if ((nargin < 3) || (nargin > 5))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
92 usage ("[code, added] = encode (msg, n, k [, typ [, opt]])");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
93 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
94
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
95 if (!isscalar(n) || (n != floor(n)) || (n < 3))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
96 error ("encode: codeword length must be an integer greater than 3");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
97 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
98
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
99 if (!isscalar(k) || (k != floor(k)) || (k > n))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
100 error ("encode: message length must be an integer less than codeword length");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
101 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
102
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
103 if (nargin > 3)
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
104 if (!ischar(typ))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
105 error ("encode: type argument must be a string");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
106 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
107 ## Why the hell did matlab decide on such an ugly way of passing 2 args!
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
108 if (strcmp(typ,"linear") || strcmp(typ,"linear/binary"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
109 coding = "linear";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
110 msgtyp = "binary";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
111 elseif (strcmp(typ,"linear/decimal"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
112 coding = "linear";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
113 msgtyp = "decimal";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
114 elseif (strcmp(typ,"cyclic") || strcmp(typ,"cyclic/binary"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
115 coding = "cyclic";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
116 msgtyp = "binary";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
117 elseif (strcmp(typ,"cyclic/decimal"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
118 coding = "cyclic";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
119 msgtyp = "decimal";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
120 elseif (strcmp(typ,"bch") || strcmp(typ,"bch/binary"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
121 coding = "bch";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
122 msgtyp = "binary";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
123 elseif (strcmp(typ,"bch/decimal"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
124 coding = "bch";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
125 msgtyp = "decimal";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
126 elseif (strcmp(typ,"hamming") || strcmp(typ,"hamming/binary"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
127 coding = "hamming";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
128 msgtyp = "binary";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
129 elseif (strcmp(typ,"hamming/decimal"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
130 coding = "hamming";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
131 msgtyp = "decimal";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
132 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
133 error ("encode: unrecognized coding and/or message type");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
134 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
135 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
136 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
137 coding = "hamming";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
138 msgtyp = "binary";
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
139 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
140
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
141 added = 0;
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
142 if (strcmp(msgtyp,"binary"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
143 vecttyp = 0;
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
144 if ((max(msg(:)) > 1) || (min(msg(:)) < 0))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
145 error ("encode: illegal value in message");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
146 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
147 [ncodewords, k2] = size(msg);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
148 len = k2*ncodewords;
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
149 if (min(k2,ncodewords) == 1)
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
150 vecttyp = 1;
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
151 msg = vec2mat(msg,k);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
152 ncodewords = size(msg,1);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
153 elseif (k2 != k)
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
154 error ("encode: message matrix must be k columns wide");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
155 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
156 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
157 if (!isvector(msg))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
158 error ("encode: decimally encoded message must be a vector");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
159 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
160 if ((max(msg) > 2^k-1) || (min(msg) < 0))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
161 error ("encode: illegal value in message");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
162 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
163 ncodewords = length(msg);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
164 msg = de2bi(msg(:),k);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
165 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
166
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
167 if (strcmp(coding,"bch"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
168 if (nargin > 4)
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
169 code = bchenco(msg,n,k,opt);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
170 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
171 code = bchenco(msg,n,k);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
172 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
173 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
174 if (strcmp(coding,"linear"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
175 if (nargin > 4)
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
176 gen = opt;
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
177 if ((size(gen,1) != k) || (size(gen,2) != n))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
178 error ("encode: generator matrix is in incorrect form");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
179 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
180 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
181 error ("encode: linear coding must supply the generator matrix");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
182 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
183 elseif (strcmp(coding,"cyclic"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
184 if (nargin > 4)
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
185 [par, gen] = cyclgen(n,opt);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
186 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
187 [par, gen] = cyclgen(n,cyclpoly(n,k));
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
188 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
189 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
190 m = log2(n + 1);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
191 if ((m != floor(m)) || (m < 3) || (m > 16))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
192 error ("encode: codeword length must be of the form '2^m-1' with integer m");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
193 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
194 if (k != (n-m))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
195 error ("encode: illegal message length for hamming code");
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
196 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
197 if (nargin > 4)
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
198 [par, gen] = hammgen(m, opt);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
199 else
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
200 [par, gen] = hammgen(m);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
201 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
202 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
203 code = mod(msg * gen, 2);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
204 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
205
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
206 if (strcmp(msgtyp,"binary") && (vecttyp == 1))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
207 code = code';
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
208 code = code(:);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
209 elseif (strcmp(msgtyp,"decimal"))
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
210 code = bi2de(code);
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
211 endif
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
212
5056d2d5eb72 Changed the directory structure of comm to match the package system
hauberg
parents:
diff changeset
213 endfunction