]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/fiboenco.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / fiboenco.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} {} fiboenco (@var{num})
18 ## 
19 ## Returns the cell-array of encoded fibonacci value from the column vectors @var{num}.
20 ## Universal codes like fibonacci codes have a useful synchronization
21 ## property, 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 elements [1 1] and we just decode the
24 ## parts. Partitioning the stream is as simple as identifying the [1 1]
25 ## 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: http://en.wikipedia.org/wiki/Fibonacci_coding
28 ## Ugly O(k.N^2) encoder.Ref: Wikipedia article accessed March, 2006.
29 ## @url{http://en.wikipedia.org/wiki/Fibonacci_coding},  UCI Data Compression
30 ## Book, @url{http://www.ics.uci.edu/~dan/pubs/DC-Sec3.html}, (accessed 
31 ## October 2006)
32 ## 
33 ## @example
34 ## @group
35 ##      fiboenco(10) #=  code is @{[ 0 1 0 0 1 1]@}
36 ##      fiboenco(1:4) #= code is @{[1 1],[0 1 1],[0 0 1 1],[1 0 1 1]@}
37 ## @end group
38 ## @end example
39 ## @end deftypefn
40 ## @seealso{fibodeco}
41
42 function op_num=fiboenco(num)
43      %
44      % generate fibonacci series table.
45      %
46      % f(1)=1;
47      % f(2)=1;
48      %
49      % while ((f(end-1)+f(end)) < 256)
50      %     val=(f(end-1)+f(end));
51      %     f=[f val];
52      % end
53      % f=sort(f(2:end),"descend");
54      %
55
56      %f= [75025   46368   28657   17711   10946    6765    4181    2584 \
57      %   1597     987    610     377     233     144      89      55 \
58      %   34      21      13   8       5       3       2       1];
59      
60      if(nargin < 1) || (min(num) <= 0 || max(num) > 608)
61        error("Usage:fiboenco(num), where num is +ve sequence of numbers ...
62        and less than equal to 608");
63      end
64      f= [ 233   144    89    55    34    21    13     8     5     3     2     1];
65      
66      onum=num;
67      LEN_F=length(f);
68      LEN_N=length(num);
69      
70      for j=1:LEN_N
71        N=num(j);
72        rval=[];
73
74        %create Fibonacci encoding of a number
75        for i=find(f<=N):LEN_F
76          if(N >= f(i))
77            N=N-f(i);
78            rval=[1 rval];
79          else
80            rval=[0 rval];
81          end
82        end      
83        op_num{j}=[rval 1];
84      end
85      return
86 end
87 %!
88 %!assert(fibodeco(fiboenco(1:600)),[1:600])
89 %!