Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. %matrix1 = readInfo('kid.bmp');
  2. matrix2 = readInfo('homer.bmp');
  3. %matrix3 = readInfo('homerBin.bmp');
  4. %matrix4 = readInfo('guitarSolo.wav');
  5. %matrix5 = readInfo('english.txt');
  6. %huffman(matrix5);
  7. %makehistogramTXT(matrix5);
  8. % minLimit(matrix1);
  9. % minLimit(matrix2);
  10. % minLimit(matrix3);
  11. % minLimit(matrix4);
  12. %minLimit(matrix5);
  13. %x = minLimit(matrix2);
  14. %disp(x);
  15. %disp(matrix2);
  16. createPairAlphabet(matrix2);
  17.  
  18. function a = readInfo(file)
  19. [~,~,ext] = fileparts(file);
  20. if(ext == '.bmp')
  21. a = imread(file);
  22. elseif(ext == '.wav')
  23. [a,~] = audioread(file);
  24. elseif(ext == '.txt')
  25. ID = fopen(file,'r');
  26. c = fscanf(ID,'%s ');
  27. c = erase(c,',');
  28. c = erase(c,'.');
  29. a = [];
  30. for k = 1:length(c)
  31. a = [a,c(k)];
  32. end
  33. a = uint16(a);
  34. end
  35. end
  36. function makehistogramTXT(a)
  37. alf = cellstr((horzcat('A':'Z','a':'z'))');
  38. conv = cellfun(@uint16, alf);
  39. graf = categorical(a,conv,alf);
  40. histogram(graf);
  41. xlim(alf);
  42. end
  43.  
  44. function makehistogram (a)
  45. xlim auto;
  46. ylim auto;
  47. histogram(a);
  48. end
  49.  
  50.  
  51. function x = minLimit (a)
  52. [N] = histcounts(a);
  53. %a = length(N);
  54. N(N==0) = [];
  55. % for i=1:a
  56. % if(N(i) ~= 0)
  57. % x = x - (N(i)/sum(N))*log2(N(i)/sum(N));
  58. % end
  59. % end
  60. x = -sum((N./sum(N)).*log2((N./sum(N))));
  61. end
  62.  
  63.  
  64. function huffman(a)
  65. %b={sort(unique(a)) countmember(sort(unique(a)),a)};
  66. %[x,y] = histcounts(a);
  67. b={sort(unique(a)) countmember(sort(unique(a)),a)};
  68. %disp(b{1});
  69. %disp(b{2});
  70. %prob = b{2}/sum(b{2});
  71. n = hufflen(b{2});
  72. %disp(prob);
  73. %[x,y]= huffmandict(b{1},n);
  74. disp(n);
  75. end
  76.  
  77.  
  78.  
  79. function alph = createPairAlphabet(a)
  80. [l,c] = size(a);
  81. bar(reshape(a,[l*c,1]));
  82.  
  83. end
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. %FUNÇOES
  91.  
  92. function COUNT = countmember(A,B)
  93. narginchk(2,2) ;
  94. if ~isequal(class(A), class(B))
  95. error('Both inputs should be of the same class.') ;
  96. end
  97.  
  98. if isempty(A) || isempty(B)
  99. COUNT = zeros(size(A)) ;
  100. else
  101. [AUnique, ~, j] = unique(A(:)) ; %Aunique = A sem repetiçoes e ordenado, j = indices do A em AUnique
  102. [~, Loc] = ismember(B, AUnique) ; %Loc = indices de AUnique em B
  103. N = histc(Loc(:), 1:length(AUnique)) ; % conta o numero de vezes que os indices comuns aparecem
  104. COUNT = reshape(N(j),size(A)) ;
  105. disp(A);
  106. disp(COUNT)
  107. end
  108. end
  109.  
  110. function HL = hufflen(S)
  111. if nargin<1
  112. error('hufflen: see help.')
  113. end
  114. HL=zeros(size(S));
  115. S=S(:);
  116. Ip=find(S>0); % index of positive elements
  117. Sp=S(Ip); % the positive elements of S
  118.  
  119. N=length(Sp); % elements in Sp vector
  120. HLp=zeros(size(Sp));
  121. C=[Sp(:);zeros(N-1,1)]; % count or weights for each "tree"
  122. Top=1:N; % the "tree" every symbol belongs to
  123. [So,Si]=sort(-Sp); % Si is indexes for descending symbols
  124. last=N; % Number of "trees" now
  125. next=N+1; % next free element in C
  126. while (last > 1)
  127. % the two smallest "trees" are put together
  128. C(next)=C(Si(last))+C(Si(last-1));
  129. I=find(Top==Si(last));
  130. HLp(I)=HLp(I)+1; % one extra bit added to elements in "tree"
  131. Top(I)=next;
  132. I=find(Top==Si(last-1));
  133. HLp(I)=HLp(I)+1; % and one extra bit added to elements in "tree"
  134. Top(I)=next;
  135. last=last-1;
  136. Si(last)=next;
  137. next=next+1;
  138. % Si shall still be indexes for descending symbols or nodes
  139. count=last-1;
  140. while ((count> 0) & (C(Si(count+1)) >= C(Si(count))))
  141. temp=Si(count);
  142. Si(count)=Si(count+1);
  143. Si(count+1)=temp;
  144. count=count-1;
  145. end
  146. end
  147.  
  148. HL(Ip)=HLp;
  149. return;
  150. end
  151.  
  152.  
  153.  
  154. %notas
  155.  
  156. %p(p==0) = [];
  157. %para som: z = x1 * 2^16 + x2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement