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