Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 2.42 KB | None | 0 0
  1. % Program Name : mHuffman.m
  2. % Decription : Create code dictionary based on HUFFMAN Coding
  3. % Author : Mainak Ghoshhajra
  4. % Remarks : 1. Generate and verify in-built MATLAB program for ....
  5. % HUFFMAN Coding
  6. % 2. Take input symbols and calculate probabbilities
  7. % 3. Create the code book
  8. %--------------------------------------------------------------------------
  9. clear all; clc;
  10. % Getting charecter probabilities from file
  11. [filename,datapath] = uigetfile('*.*', 'select the file');
  12. if isequal(filename,0)
  13. disp('User selected Cancel');
  14. else
  15. disp(['User selected ', fullfile(datapath,filename)]);
  16. end
  17. fid = fopen(filename);
  18. ftell(fid)
  19. tline1 = fgetl(fid) % read the first line
  20. % str = string(tline1)
  21. sym_dict=unique(tline1);
  22. In_s = sym_dict; %Input symbols
  23. % Calculate probabilities of the symbols
  24. for k1=1:length(sym_dict)
  25. prob(k1) = (sum(tline1==sym_dict(k1)))/length(tline1);
  26. end
  27. [z i]=sort(prob,'ascend');
  28. sort_u =sym_dict(i);
  29. In_p = z;
  30. org_len = length(In_p);
  31. %We have sorted array of probabilities in ascending order with track of symbols
  32. ind=1;
  33. len_tr = [org_len];
  34. pos_tr = [0];
  35. total_array(ind,:)=In_p;
  36. append1=[];
  37. lp_j=1;
  38. while(ind<org_len-1)
  39. firstsum = In_p(lp_j)+In_p(lp_j+1); %sum the lowest probabilities
  40. append1 = [append1,firstsum]; %appending sum in array
  41. In_p = [In_p((lp_j+2):length(In_p)),firstsum]; % reconstrucing prob array
  42. In_p = sort(In_p);
  43. ind = ind+1;
  44. total_array(ind,:) = [In_p,zeros(1,org_len-length(In_p))]; %setting track of probabilities
  45. len_tr = [len_tr,length(In_p)]; %lengths track
  46. for i=1:length(In_p)
  47. if(In_p(i)==firstsum)
  48. pos = i; %position after swapping of new sum
  49. end
  50. end
  51. pos_tr = [pos, pos_tr];
  52. end
  53. main_arr = total_array';
  54. %columns indicates no.of times we have done sorting which length-1;
  55. %rows have the prob values with zero padded at the end.
  56. code = cell(org_len,org_len-1); % create cell array
  57. col=org_len-1;
  58. row=1;
  59. % Assigning 0 and 1 to 1st and 2nd row of last column
  60. code{row,col}='0';
  61. code{row+1,col}='1';
  62. while col~=1
  63. i=1;
  64. x=1;
  65. z=0;
  66. if (main_arr(row,col-1) + main_arr(row+1,col-1))==main_arr(row,col)
  67. code{row,col-1}=[code{row,col} '0'];
  68. code{row+1,col-1}=[code{row,col} '1'];
  69. while ~isempty(code{row+i,col})
  70. code{row+1+i,col-1}=code{row+i,col};
  71. i=i+1;
  72. end
  73. else
  74. code{row,col-1}=[code{row+1,col} '0'];
  75. code{row+1,col-1}=[code{row+1,col} '1'];
  76. while ~isempty(code{row+x,col})
  77. code{row+1+x,col-1}=code{row+z,col};
  78. x=x+1;
  79. z=z+2;
  80. end
  81. end
  82. col=col-1;
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement