フィルターのクリア

I want Huffman coding

7 ビュー (過去 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 Center および File ExchangeDenoising and Compression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by