Mercurial > forge
annotate main/comm/inst/rsencof.m @ 9666:67d4cfc5eeb3 octave-forge
comm: update license to GPLv3+
author | carandraug |
---|---|
date | Tue, 13 Mar 2012 04:31:21 +0000 |
parents | 2de537641f94 |
children |
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 | 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. | |
2382
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
7 ## |
9666 | 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. | |
2382
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
12 ## |
9666 | 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/>. | |
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} {} rsencof (@var{in},@var{out}) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
18 ## @deftypefnx {Function File} {} rsencof (@var{in},@var{out},@var{t}) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
19 ## @deftypefnx {Function File} {} rsencof (@var{...},@var{pad}) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
20 ## |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
21 ## Encodes an ascii file using a Reed-Solomon coder. The input file is |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
22 ## defined by @var{in} and the result is written to the output file @var{out}. |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
23 ## The type of coding to use is determined by whether the input file is 7- |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
24 ## or 8-bit. If the input file is 7-bit, the default coding is [127,117]. |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
25 ## while the default coding for an 8-bit file is a [255, 235]. This allows |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
26 ## for 5 or 10 error characters in 127 or 255 symbols to be corrected |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
27 ## respectively. The number of errors that can be corrected can be overridden |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
28 ## by the variable @var{t}. |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
29 ## |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
30 ## If the file is not an integer multiple of the message size (127 or 255) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
31 ## in length, then the file is padded with the EOT (ascii character 4) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
32 ## characters before coding. Whether these characters are written to the |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
33 ## output is defined by the @var{pad} variable. Valid values for @var{pad} |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
34 ## are "pad" (the default) and "nopad", which write or not the padding |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
35 ## respectively. |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
36 ## |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
37 ## @end deftypefn |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
38 ## @seealso{rsdecof} |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
39 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
40 function rsencof(in, out, varargin) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
41 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
42 if ((nargin < 2) || (nargin > 4)) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
43 usage("rsencof (in, out [, t [, pad]])"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
44 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
45 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
46 if (!ischar(in) || !ischar(out)) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
47 error ("rsencof: input and output filenames must be strings"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
48 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
49 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
50 t = 0; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
51 pad = 1; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
52 for i=1:length(varargin) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
53 arg = varargin{i}; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
54 if (ischar(arg)) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
55 if (strcmp(arg,"pad")) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
56 pad = 1; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
57 elseif (strcmp(arg,"nopad")) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
58 pad = 0; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
59 else |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
60 error ("rsencof: unrecognized string argument"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
61 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
62 else |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
63 if (!isscalar(t) || (t != floor(d)) || (t < 1)) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
64 error ("rsencof: t must be a postive, non-zero integer"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
65 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
66 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
67 end |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
68 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
69 try fid = fopen(in, "r"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
70 catch |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
71 error ("rsencof: can not open input file"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
72 end |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
73 [msg, count] = fread(fid, Inf, "char"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
74 fclose(fid); |
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 is8bit = (max(msg) > 127); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
77 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
78 if (is8bit) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
79 m = 8; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
80 n = 255; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
81 if (t == 0) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
82 t = 10; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
83 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
84 else |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
85 m = 7; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
86 n = 127; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
87 if (t == 0) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
88 t = 5; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
89 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
90 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
91 k = n - 2 * t; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
92 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
93 ncodewords = ceil(count / k); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
94 npad = k * ncodewords - count; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
95 msg = reshape([msg ; 4 * ones(npad,1)],k,ncodewords)'; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
96 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
97 code = rsenc(gf(msg,m), n, k,"beginning")'; |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
98 code = code(:); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
99 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
100 try fid = fopen(out, "w"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
101 catch |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
102 error ("rsencof: can not open output file"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
103 end |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
104 if (pad) |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
105 fwrite(fid, code, "char"); |
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 fwrite(fid, code(1:(ncodewords*n-npad)), "char"); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
108 endif |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
109 fclose(fid); |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
110 |
5056d2d5eb72
Changed the directory structure of comm to match the package system
hauberg
parents:
diff
changeset
|
111 endfunction |