]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/fibodeco.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / fibodeco.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} { } fibodeco (@var{code})
18 ## 
19 ## Returns the decoded fibonacci value from the binary vectors @var{code}.
20 ## Universal codes like fibonacci codes Have a useful synchronization property,
21 ## only for 255 maximum value we have designed these routines. We assume
22 ## user has partitioned the code into several unique segments based on
23 ## the suffix property of unique strings "11" and we just decode the
24 ## parts. Partitioning the stream is as simple as identifying the
25 ## "11" pairs that occur, at the terminating ends. This system implements
26 ## the standard binaary Fibonacci codes, which means that row vectors
27 ## can only contain 0 or 1. Ref: @url{http://en.wikipedia.org/wiki/Fibonacci_coding}
28 ## 
29 ## @example
30 ## @group
31 ##     fibodeco(@{[0 1 0 0 1 1]@}) %decoded to 10
32 ##     fibodeco(@{[1 1],[0 1 1],[0 0 1 1],[1 0 1 1]@}) %[1:4]
33 ## @end group
34 ## @end example
35 ## @end deftypefn
36 ## @seealso{fiboenco}
37   
38 function num=fibodeco(code)
39   ##
40   ## generate fibonacci series table.
41   ##
42   ## f(1)=1;
43   ## f(2)=1;
44   ##
45   ## while ((f(end-1)+f(end)) < 256)
46   ##    val=(f(end-1)+f(end));
47   ##    f=[f val];
48   ## end
49   ## f=f(2:end);
50   ##
51   ##all numbers terminate with 1 except 0 itself.
52   ##
53   ##
54   ##f= [75025   46368   28657   17711   10946    6765    4181    2584 \
55   ##     1597     987    610     377     233     144      89      55 \
56   ##     34      21      13   8       5       3       2       1];
57   ##
58   ##f= [ 233   144    89    55    34    21    13     8     5     3  2     1];
59
60   f= [  1     2     3     5     8    13    21    34    55    89   144   233];     
61   L_C=length(code);
62
63   if (nargin < 1)
64     error("Usage:fibodec(cell-array vectors), where each vector is +ve sequence of numbers ...
65               either 1 or 0");
66   end
67
68   for j=1:L_C
69     word=code{j};
70     ##discard the terminating 1.
71     word=word(1:end-1);
72     L=length(word);
73     num(j)=sum(word.*f(1:L));
74   end
75   
76   return
77 end
78 %!
79 %! assert(fibodeco({[1 1],[0 1 1],[0 0 1 1],[1 0 1 1]}),[1:4])
80 %! assert(fibodeco({[0 1 0 0 1 1]}),10)
81 %!