]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/fibosplitstream.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / fibosplitstream.m
1 ## Copyright (C) 2006 Muthiah Annamalai <muthiah.annamalai@uta.edu>
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} { } fibosplitstream (@var{code})
18 ##
19 ## Returns the split data stream at the word boundaries.
20 ## Assuming the stream was originally encoded using @code{fiboenco}
21 ## and this routine splits the stream at the points where '11'
22 ## occur together & gives us the code-words which
23 ## can later be decoded from the @code{fibodeco} This however doesnt
24 ## mean that we intend to verify if all the codewords are correct,
25 ## and infact the last symbol in th return list can or can-not be
26 ## a valid codeword.
27 ##
28 ## A example use of @code{fibosplitstream} would be
29 ## @example
30 ## @group
31 ##
32 ## fibodeco(fibosplitstream([fiboenco(randint(1,100,[0 255]))@{:@}]))
33 ## fibodeco(fibosplitstream([fiboenco(1:10)@{:@}]))
34 ##
35 ## @end group
36 ## @end example
37 ## @seealso{fiboenco,fibodeco}
38 ## @end deftypefn
39
40 function symbols=fibosplitstream(stream)
41   if nargin < 1
42      error('usage: fibosplitstream(stream); see help')
43   end
44
45   symbols={};
46   itr=1;
47   L=length(stream);
48
49   %
50   % Plain & Simple Algorithm. O(N)
51   % Walk till marker '11' or find it.
52   % Then split & save. A little tricky to
53   % handle the edge case_ without tripping over.
54   %
55   idx=[];
56   mark=1;
57   prev_bit=stream(1);
58   just_decoded=0;
59
60   for i=2:L
61     if(~just_decoded && (stream(i)+prev_bit)==2 )
62         symbols{itr}=[stream(mark:i)];
63         mark=i+1;
64         prev_bit=0;
65         just_decoded=1;
66         itr=itr+1;
67     else
68       prev_bit=stream(i);
69       just_decoded=0;
70     end
71     
72   end
73   if(mark < L)
74     symbols{itr}=stream(mark:end);
75   end
76   
77   return
78 end
79 %!
80 %!assert(fibodeco(fibosplitstream([fiboenco(1:10){:}])),[1:10])
81 %!