Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %matrix1 = readInfo('kid.bmp');
- matrix2 = readInfo('homer.bmp');
- %matrix3 = readInfo('homerBin.bmp');
- %matrix4 = readInfo('guitarSolo.wav');
- %matrix5 = readInfo('english.txt');
- %huffman(matrix5);
- %makehistogramTXT(matrix5);
- % minLimit(matrix1);
- % minLimit(matrix2);
- % minLimit(matrix3);
- % minLimit(matrix4);
- %minLimit(matrix5);
- %x = minLimit(matrix2);
- %disp(x);
- %disp(matrix2);
- createPairAlphabet(matrix2);
- function a = readInfo(file)
- [~,~,ext] = fileparts(file);
- if(ext == '.bmp')
- a = imread(file);
- elseif(ext == '.wav')
- [a,~] = audioread(file);
- elseif(ext == '.txt')
- ID = fopen(file,'r');
- c = fscanf(ID,'%s ');
- c = erase(c,',');
- c = erase(c,'.');
- a = [];
- for k = 1:length(c)
- a = [a,c(k)];
- end
- a = uint16(a);
- end
- end
- function makehistogramTXT(a)
- alf = cellstr((horzcat('A':'Z','a':'z'))');
- conv = cellfun(@uint16, alf);
- graf = categorical(a,conv,alf);
- histogram(graf);
- xlim(alf);
- end
- function makehistogram (a)
- xlim auto;
- ylim auto;
- histogram(a);
- end
- function x = minLimit (a)
- [N] = histcounts(a);
- %a = length(N);
- N(N==0) = [];
- % for i=1:a
- % if(N(i) ~= 0)
- % x = x - (N(i)/sum(N))*log2(N(i)/sum(N));
- % end
- % end
- x = -sum((N./sum(N)).*log2((N./sum(N))));
- end
- function huffman(a)
- %b={sort(unique(a)) countmember(sort(unique(a)),a)};
- %[x,y] = histcounts(a);
- b={sort(unique(a)) countmember(sort(unique(a)),a)};
- %disp(b{1});
- %disp(b{2});
- %prob = b{2}/sum(b{2});
- n = hufflen(b{2});
- %disp(prob);
- %[x,y]= huffmandict(b{1},n);
- disp(n);
- end
- function alph = createPairAlphabet(a)
- [l,c] = size(a);
- bar(reshape(a,[l*c,1]));
- end
- %FUNÇOES
- function COUNT = countmember(A,B)
- narginchk(2,2) ;
- if ~isequal(class(A), class(B))
- error('Both inputs should be of the same class.') ;
- end
- if isempty(A) || isempty(B)
- COUNT = zeros(size(A)) ;
- else
- [AUnique, ~, j] = unique(A(:)) ; %Aunique = A sem repetiçoes e ordenado, j = indices do A em AUnique
- [~, Loc] = ismember(B, AUnique) ; %Loc = indices de AUnique em B
- N = histc(Loc(:), 1:length(AUnique)) ; % conta o numero de vezes que os indices comuns aparecem
- COUNT = reshape(N(j),size(A)) ;
- disp(A);
- disp(COUNT)
- end
- end
- function HL = hufflen(S)
- if nargin<1
- error('hufflen: see help.')
- end
- HL=zeros(size(S));
- S=S(:);
- Ip=find(S>0); % index of positive elements
- Sp=S(Ip); % the positive elements of S
- N=length(Sp); % elements in Sp vector
- HLp=zeros(size(Sp));
- C=[Sp(:);zeros(N-1,1)]; % count or weights for each "tree"
- Top=1:N; % the "tree" every symbol belongs to
- [So,Si]=sort(-Sp); % Si is indexes for descending symbols
- last=N; % Number of "trees" now
- next=N+1; % next free element in C
- while (last > 1)
- % the two smallest "trees" are put together
- C(next)=C(Si(last))+C(Si(last-1));
- I=find(Top==Si(last));
- HLp(I)=HLp(I)+1; % one extra bit added to elements in "tree"
- Top(I)=next;
- I=find(Top==Si(last-1));
- HLp(I)=HLp(I)+1; % and one extra bit added to elements in "tree"
- Top(I)=next;
- last=last-1;
- Si(last)=next;
- next=next+1;
- % Si shall still be indexes for descending symbols or nodes
- count=last-1;
- while ((count> 0) & (C(Si(count+1)) >= C(Si(count))))
- temp=Si(count);
- Si(count)=Si(count+1);
- Si(count+1)=temp;
- count=count-1;
- end
- end
- HL(Ip)=HLp;
- return;
- end
- %notas
- %p(p==0) = [];
- %para som: z = x1 * 2^16 + x2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement