Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc
- clear all
- napis = 'cabacabacabarrraaasddawkgaiikallwororoewiiieekisiikeaikgg#';
- [a,~,~] = unique(napis);
- for i = 1:length(a)
- prob(i) = length(findstr(a(i), napis));
- end
- prob = prob./sum(prob);
- [~,ind] = sort(prob,'descend');
- prob = prob(ind);
- a = a(ind);
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %prob = [0.45,0.3,0.2,0.05];
- %a = ['abc#'];
- tab = zeros(length(prob),2);
- startValue = 0;
- for i = 1:length(prob)
- tab(i,1) = startValue;
- tab(i,2) = sum(prob(1:i));
- startValue = tab(i,2);
- end
- tab2 = tab;
- tab_new = hpf(0,[15,0]);
- for i = 1:length(napis)
- indx = find(napis(i)==a);
- tab_new = hpf(tab(indx,:));
- p = tab_new(1);
- k = tab_new(2);
- for j = 1:length(prob)-1
- tab_new(end+1,2) = tab_new(end,2);
- tab_new(end-1,2) = (k-p)*sum(prob(1:j))+p;
- tab_new(end,1) = tab_new(end-1,2);
- end
- tab = tab_new;
- end
- %%%%%%%%%%%%%dekodowanie
- value = (tab(end,1) + tab(end,2))/2;
- tab = tab2;
- s = '0';
- wynik = [];
- while(s~='#')
- indx = find(tab(:,1)<value);
- indx = indx(end);
- s = a(indx);
- wynik = [wynik,s];
- tab_new = hpf(tab(indx,:));
- p = tab_new(1);
- k = tab_new(2);
- for j = 1:length(prob)-1
- tab_new(end+1,2) = tab_new(end,2);
- tab_new(end-1,2) = (k-p)*sum(prob(1:j))+p;
- tab_new(end,1) = tab_new(end-1,2);
- end
- tab = tab_new;
- end
- napis
- wynik
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement