You are on page 1of 2
ovns2017 ‘codage human avec matlab function [code, conpression] =huFfnians(p) SHUFFRANS XHUFFMAN CODER FOR VS % Format [CODE, COMPRESSION % 1UFFMANS (P) % ? is the probability (or nunber of occurences) of each alphabet synbol % CODE gives the huffman code in a string format of ones and zeros % COMPRESSION gives the compression rate x % HuffmanS works by First building up a binary tree (eg p .2.15 .15]) x % al aa x yoy xy / % bs bn we VOL \ % e\a/ ay % be a8 x \ % On x az % % Such that the tree always terminates at an alphabet symbol and the % symbols furthest away from the root have the lowest probability. % The branches at each level are labeled @ and 1. x ‘example CODE would be x % x e18 % el % and the compression rate 1.1121 % Sean Danaher University of Northumbria at Necastle UK 98/6/4 Anput(‘entrez le vecteur des probabilité’); (:)/sum(p);_-Rnormalises probabilities c=nuFt5(p); code=char(getcodes(c, 1ength(p))); conpression=ceil(log(length(p))/0g(2))/ (sum(code’ == ' ')*p)3 x function ¢=huF#5(p); X HUFFS Creates Huffman Tree % Simulates a tree structure using a nested cell structure XP is a vector with the probability (number of occurences) % of each alphabet sysbol % C As the Huffman tree. Note Matlab 5 version % Sean Danaher 98/6/4 University of Northumbria, Newcastle UK “ell (length(p),4); % Generate cell structure for int:length(p) % Fill cell structure with 1,2,3...n e{i}aks % (nsnunber of symbols in alphabet) end while size(e)-2 % Repeat till only two branches [p, {]esort(p)s % Sort to ascending probabilities c(i) % Reorder tree. e(2)=f6(1) (2) % join branch 1 to 2 and prune 1 (2)=p(1)49(2) 3902) % Herge Probabilities end x function y= getcodes(a,n) % Y=GETCODES(A,N) % Pulls out Huffman Codes for V5 % a is the nested cell structure created by huffcodes % nis the number of synbols % Sean Danaher $8/6/4 University of Northumbria, Newcastle UK global y e11(n,1)3 sgetcodes2(a,[]) x itp: eveloppez, nator umsi¢107188Slenvirormemants-developpemerdmataticadage-hufimar-mataty 1 ovns2017 ‘codage human avec matlab function getcodes2(a, cum) % GETCODES(A,DUM) Xgetcodes2 % called by getcodes % uses Recursion to pull out codes % Sean Danaher 98/6/4 University of Northumbria, Newcastle UK global y Af isa(a,‘cell’) ‘getcodes2(a(1), [dun @])5 getcodes2(a{2}, [dun 1])3 else yla}=setste(4s+dum) ; end itp: eveloppez, nator umsi¢107188Slenvirormemants-developpemerdmataticadage-hufimar-mataty

You might also like