/tags/R2008-02-16/main/info-theory/inst/tunstallcode.m

# · MATLAB · 92 lines · 70 code · 12 blank · 10 comment · 7 complexity · c41bc571c60f8636154d45ef0d87ed2e MD5 · raw file

  1. ## Copyright (C) 2006 Muthiah Annamalai <muthiah.annamalai@uta.edu>
  2. ##
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2 of the License, or
  6. ## (at your option) any later version.
  7. ##
  8. ## This program is distributed in the hope that it will be useful,
  9. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ## GNU General Public License for more details.
  12. ##
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this program; If not, see <http://www.gnu.org/licenses/>.
  15. ## -*- texinfo -*-
  16. ## @deftypefn {Function File} {@var{code_dictionary} =} tunstallcode (@var{probability_list})
  17. ##
  18. ## Implementation of a @code{|A|}-bit tunstall coder given the source
  19. ## probability of the @code{|A|} symbols from the source with @code{2^|A|}
  20. ## code-words involved. The variable @var{probability_list} ordering of
  21. ## symbols is preserved in the output symbol/code dictionary.
  22. ## Tunstall code is a variable to fixed source coding scheme,
  23. ## and the arrangement of the codeword list order corrseponds to
  24. ## to the regular tunstall code ordering of the variable source
  25. ## word list, and the codes for each of them are enumerations
  26. ## from @code{1:2^N}. Return only the ordering (grouping) of source symbols
  27. ## as their index of match is the corresponding code word. The
  28. ## probabilites of the various symbols are also stored in here.
  29. ## for example
  30. ##
  31. ## @example
  32. ## [cw_list, prob_list] = tunstallcode([0.6 .3 0.1])
  33. ## @end example
  34. ##
  35. ## essentially you will use the cw_list to parse the input
  36. ## and then compute the code as the binary value of their index
  37. ## of match, since it is a variable to fixed code.
  38. ##
  39. ## Reference: "Synthesis of noiseless compression codes", Ph.D. Thesis
  40. ## of B.P. Tunstall, Georgia Tech, Sept 1967
  41. ## @end deftypefn
  42. function [cw_list,prob_list]=tunstallcode(prob_list)
  43. if nargin < 1
  44. error('usage: tunstallcode(probability_list)');
  45. end
  46. N=length(prob_list);
  47. S=1:N;
  48. CWMAX=2**N;
  49. porig_list=prob_list;
  50. prob_op=prob_list;
  51. for idx=1:N
  52. cw_list{idx}=sprintf("%d",idx);
  53. end
  54. while ((length(cw_list)+N-1) <= CWMAX)
  55. L=length(cw_list);
  56. %
  57. %assuming maximum of list is on top.
  58. %i.e: all lists are in descending order.
  59. %
  60. base=cw_list{1};
  61. base_prob=prob_list(1);
  62. prob_list=[prob_list(2:end) base_prob];
  63. cw_list={cw_list{2:end}};
  64. for idx=1:N
  65. prob_list(L+idx-1)=base_prob*porig_list(idx);
  66. w=sprintf("%s%d",base,idx);
  67. cw_list{L+idx-1}=w;
  68. end
  69. idx=find(prob_list==max(prob_list));
  70. prob_list=[prob_list(idx) prob_list(1:idx-1) prob_list(idx+1:end)];
  71. cw_list={cw_list{idx} cw_list{1:idx-1} cw_list{idx+1:end}};
  72. end
  73. return
  74. end
  75. %!assert(tunstallcode([0.7 0.3]),{"111","2","12","112"})
  76. %
  77. %P=[ 0.500000 0.250000 0.125000 0.125];
  78. %tunstallcode(P)
  79. %tunstallcode([0.6 .3 0.1])
  80. %