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