]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/lz77enco.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / lz77enco.m
1 ## Copyright (C) 2007 Gorka Lertxundi
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} {@var{c} =} lz77enco (@var{m}, @var{alph}, @var{la}, @var{n})
18 ## Lempel-Ziv 77 source algorithm implementation. Where
19 ##
20 ## @table @asis
21 ## @item @var{c}
22 ## encoded message (Mx3).
23 ## @item @var{alph}
24 ## size of alphabet.
25 ## @item @var{la}
26 ## lookahead buffer size.
27 ## @item @var{n}
28 ## sliding window buffer size.
29 ## @end table
30 ## @seealso{lz77deco}
31 ## @end deftypefn
32
33 function c = lz77enco(m, alph, la, n)
34
35   if (la <= 0 || n <= 0)
36     error("lz77enco: Lookahead buffer size and window size must be higher than 0.2");
37   endif
38   if n - la < la
39     error("lz77enco: Unreachable configuration: n - la >= la.");
40   endif
41   if alph < 2
42     error("lz77enco: Alphabet size within less than 2 symbols?");
43   endif
44   if max(m)+1 > alph
45     error("lz77enco: It is not possible to codify the message, too small alphabet size.");
46   endif
47   if rows(m) != 1
48     error("lz77enco: Message to encode must be a 1xN matrix.");
49   endif
50
51   c = zeros(1,3);
52   enco = zeros(1,3);
53   window = zeros(1,n);
54   x = length(m);
55   len = length(m);
56
57   while x ~= 0
58     ## update window
59     window(1:n-la) = window(enco(2)+2:n-la+enco(2)+1);
60     if x < la
61       window(n-la+1:n) = [m(len-x+1:len) zeros(1,la-x)];
62     else
63       window(n-la+1:n) = m(len-x+1:len-x+la);
64     endif
65         
66     ## get a reference (position ,length) to longest match, and next symbol
67     enco = [0 0 0];
68     for y=(n-la):-1:1
69       z = 0;
70       while(z ~= la && (window(y+z) == window(n-la+z+1)))
71         z += 1;
72       endwhile
73                 
74       if enco(2) < z
75         enco(1) = y-1;
76         enco(2) = z;
77         enco(3) = window(n-la+z+1);
78       endif
79     endfor
80
81     ## encoded message
82     if x == len
83       c = enco;
84     else
85       c = [c ; enco];
86     endif
87
88     x -= enco(2)+1;
89   endwhile
90 endfunction
91
92 %!demo
93 %! lz77enco([0 0 1 0 1 0 2 1 0 2 1 0 2 1 2 0 2 1 0 2 1 2 0 0],3,9,18)