I want Huffman coding

조회 수: 18 (최근 30일)
Muhammad
Muhammad 2022년 11월 26일
댓글: Walter Roberson 2022년 11월 27일
(Huffman coding example, codex.m) Open the code codex.m using the editor. Note
“m” parameter which indicates the number of simulated symbols in array “x”.
1. Determine the size of encoded binary sequence in array “cx”. Calculate the compression
ratio in comparison with no-encoding scenario, when each symbol is encoded by two
bits.
2. Compute the number of bits per symbol, i.e. the ratio of lengths for arrays “cx” and “x”.
Compare with the theoretical number of bits per symbol as in notes example on pages
10-11 (Intro-Source-Coding)
%codex.m example of Huffman coding and decoding
clear
m=1000; % number of code words
% codex.m step 1: generate a 4-PAM sequence
% with probabilities 0.5, 0.25, 0.125, and 0.125
for i=1:m
r=rand;
if r<0.5, x(i)=+1; end
if (r>=0.5) & (r<0.75), x(i)=-1; end
if (r>=0.75) & (r<0.875), x(i)=+3; end
if r>=0.875, x(i)=-3; end
end
% codex.m step 2: encode the sequence using Huffman code
j=1;
for i=1:m
if x(i)==+1, cx(j:j)=[1]; j=j+1; end
if x(i)==-1, cx(j:j+1)=[0,1]; j=j+2; end
if x(i)==+3, cx(j:j+2)=[0,0,1]; j=j+3; end
if x(i)==-3, cx(j:j+2)=[0,0,0]; j=j+3; end
end
% codex.m step 3: decode the variable length sequence
j=1; i=1;
while i<=length(cx)
if cx(i:i)==[1], y(j)=+1; i=i+1; j=j+1;
elseif cx(i:i+1)==[0,1], y(j)=-1; i=i+2; j=j+1;
elseif cx(i:i+2)==[0,0,1], y(j)=+3; i=i+3; j=j+1;
elseif cx(i:i+2)==[0,0,0], y(j)=-3; i=i+3; j=j+1; end
end
err=sum(abs(x-y))
  댓글 수: 5
Muhammad
Muhammad 2022년 11월 27일
편집: Walter Roberson 2022년 11월 27일
Kindly check this code it gives error "Subscripted assignment dimension mismtach.
g=[1 0 1 0 1; 0 1 0 1 1];
h=[1 0 1 0 0; 0 1 0 1 0; 1 1 0 0 1 ];
x(1,:)=[0 0] ; cw(1,:)=mod(x(1,:)*g,2);
x(2,:)=[0 1] ; cw(2,:)=mod(x(2,:)*g,2);
x(3,:)=[1 0] ; cw(3,:)=mod(x(3,:)*g,2);
x(4,:)=[1 1] ; cw(4,:)=mod(x(4,:)*g,2);
syn=[0 0 0 0 0; 0 0 0 0 1; 0 0 0 1 0; 0 1 0 0 0 ; 0 0 1 0 0 ; 1 0 0 0 0 ; 1 1 0 0 0; 1 0 0 1 0];
err_percentage=[];
for p=[0.001 0.01 0.02 0.05 0.1 0.2 0.5 ] ;
m=10000;
dat=0.5*(sign(rand(1, m) - 0.5) + 1) ;
for i = 1:2:m
c=mod([dat(i) dat(i+1)]*g, 2);
for j = 1:length(c)
if rand<p, c(j)=-c(j)+1;
end
end
y=c;
eh=mod(y*h',2);
ehind=eh (1)*4+eh (2)*2+eh (3)+1;
e=syn(ehind,:);
y=mod(y-e,2);
for j = 1:max(size(x))
if y==cw(j,:), z(i:i+1)=x(j, :);
end
end
end
err=sum(abs (z-dat));
err_percentage=[err_percentage (err/m)*100];
end
plot([0.001 0.01 0.02 0.05 0.1 0.2 0.5 ],err_percentage)
xlabel('percentage of bit flipped');
ylabel('percentage of error');
grid on
title('PLOT')
Walter Roberson
Walter Roberson 2022년 11월 27일
No error when I run your code.
Perhaps you had left-over variables in the workspace.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 11월 27일

카테고리

Help CenterFile Exchange에서 Denoising and Compression에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by