Diff of /inst/convenc.m [53900c] .. [658f98]  Maximize  Restore

Switch to side-by-side view

--- a/inst/convenc.m
+++ b/inst/convenc.m
@@ -1,4 +1,4 @@
-## Copyright (C) 2012 Tony Richardson <richardson.tony@gmailcom>
+## Copyright (C) 2013 Mike Miller <mtmiller@ieee.org>
 ##
 ## This program is free software; you can redistribute it and/or modify it under
 ## the terms of the GNU General Public License as published by the Free Software
@@ -14,58 +14,106 @@
 ## this program; if not, see <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {@var{x} =} convenc (@var{m}, @var{G}, @var{k})
-## Compute output of an (n, @var{k}, L) convolutional encoder with vector input
-## @var{m} and matrix of generator polynomials @var{G}.
+## @deftypefn  {Function File} {@var{y} =} convenc (@var{msg}, @var{t})
+## @deftypefnx {Function File} {@var{y} =} convenc (@var{msg}, @var{t}, @var{punct})
+## @deftypefnx {Function File} {@var{y} =} convenc (@var{msg}, @var{t}, @var{punct}, @var{s0})
+## @deftypefnx {Function File} {[@var{y}, @var{state_end}] =} convenc (@dots{})
+## Encode the binary vector @var{msg} with the convolutional encoder
+## described by the trellis structure @var{t}.
 ##
-## The input vector @var{m} can be of arbitrary length. @var{G} is a matrix with n rows
-## and @var{k}*(L+1) columns. The rows of @var{G} are the generator polynomials for each
-## of the n output bits (per @var{k} input bits).
+## The rate @math{k/n} convolutional encoder encodes @math{k} bits at a
+## time from the input vector and produces @math{n} bits at a time into the
+## output vector.  The input @var{msg} must have a length that is a multiple
+## of @math{k}.
 ##
-## The output is a vector whose length is n*floor([length(@var{m})+@var{k}*(L+1)-1]/@var{k}).
-## If unspecified, @var{k} defaults to 1.
+## If the initial state @var{s0} is specified, it indicates the internal
+## state of the encoder when the first @math{k} input bits are fed in.  The
+## default value of @var{s0} is 0.
 ##
-## Example 1: Compute the output from a (2, 1, 2) convolutional encoder
-## @example
-## @group
-## m  = [ 1 1 0 1 1 1 0 0 1 0 0 0];
-## g1 = [1 1 1];
-## g2 = [1 0 1];
-## convenc (m, [g1; g2])
-##      @result{} [1 1 0 1 0 1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0]
-## @end group
-## @end example
+## The optional output argument @var{state_end} indicates the internal state
+## of the encoder after the last bits are encoded.  This allows the state of
+## the encoder to be saved and applied to the next call to @code{convenc} to
+## process data in blocks.
 ##
-## Example 2: Compute the output from a (3, 2, 1) convolutional encoder
-## @example
-## @group
-## m  = [0 1 1 0 0 0 1 1 ];
-## g1 = [1 0 1 1];
-## g2 = [1 1 0 1];
-## g3 = [1 0 1 0];
-## convenc (m, [g1; g2; g3], 2)
-##      @result{} [1 1 1 1 1 1 1 1 0 1 0 1]
-## @end group
-## @end example
-##
-## @strong{Caution:}: this function is not compatible with @sc{matlab}'s convenc().
+## @seealso{poly2trellis}
 ## @end deftypefn
 
-function x = convenc (m, G, k = 1)
-  if (nargin < 2 || nargin > 3)
+function [y, state_end] = convenc (msg, t, punct, s0 = 0)
+
+  if (nargin < 2 || nargin > 4)
     print_usage ();
   endif
 
-  # Use conv2 to do repeated 1d convolutions of m with each row of G.
-  # rem is used to transform the standard convolution result to one
-  # which uses modulo-2 addition.  Only cols with index a mult. of k
-  # are in the actual enc. output
+  if (! (isvector (msg) && all (msg == 0 | msg == 1)))
+    error ("convenc: MSG must be a binary vector");
+  endif
 
-  x = rem(conv2(1, m(:)', G),2)(:,!rem(1:numel(m),k))(:)';
+  if (! istrellis (t))
+    error ("convenc: T must be a valid trellis structure");
+  endif
+
+  if (nargin < 3)
+    punct = [];
+  endif
+  if (! isempty (punct))
+    warning ("convenc: ignoring PUNCT, puncturing is not yet implemented");
+  endif
+
+  ## FIXME: Add error check for valid punct binary vector
+
+  if (nargin == 4 && ! (isscalar (s0) && s0 == fix (s0) && s0 >= 0
+                        && s0 < t.numStates))
+    error ("convenc: S must be an integer in the range [0,T.numStates-1]");
+  endif
+
+  k = log2 (t.numInputSymbols);
+  n = log2 (t.numOutputSymbols);
+
+  in_symbols = numel (msg) / k;
+  if (in_symbols != fix (in_symbols))
+    error ("convenc: length of MSG must be a multiple of k");
+  endif
+
+  transpose = (columns (msg) == 1);
+  msg = msg(:).';
+
+  state = s0;
+  y = [];
+
+  ## FIXME: Implement output puncturing
+
+  for idx = 1:k:numel (msg)
+    in_sym = bi2de (msg(idx:idx+k-1), "left-msb");
+    out_sym = t.outputs(state+1,in_sym+1);
+    state = t.nextStates(state+1,in_sym+1);
+    out_bits = de2bi (out_sym, n, "left-msb");
+    y = [y out_bits];
+  endfor
+
+  if (transpose)
+    y = y(:);
+  endif
+
+  if (nargout > 1)
+    state_end = state;
+  endif
 
 endfunction
+
+%!test
+%! t = poly2trellis (1, 1);
+%! m = randi ([0 1], 128, 1);
+%! [y, s] = convenc (m, t);
+%! assert (y, m)
+%! assert (s, 0)
+%!test
+%! t = poly2trellis (3, [7 5]);
+%! m = [1 1 0 1 1 1 0 0 1 0 0 0];
+%! y = [1 1 0 1 0 1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 1 1 0 0];
+%! assert (convenc (m, t), y)
 
 %% Test input validation
 %!error convenc ()
 %!error convenc (1)
-%!error convenc (1, 2, 3, 4)
+%!error convenc (1, 2)
+%!error convenc (1, 2, 3, 4, 5)

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks