I want Huffman coding

(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 Comments

What is your question about MATLAB?
Sir check how the linear block encoding and decoding is implemented in
blockcode52. The code also includes a fragment which simulates errors occurring during
the propagation as a parameter “p”. It’s the example on pages 20-23 of Error-Coding
notes. Use smaller probabilities of error parameter “p” in the code to observe zero errors
on the output. Gradually increase the probability to achieve about 50% of output error
rate “err”. Plot the curve “err” vs “p” and explain the result.
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
We do not have "blockcode52" or "page 20-23 of Error-Coding notes".
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')
No error when I run your code.
Perhaps you had left-over variables in the workspace.

Sign in to comment.

Asked:

on 26 Nov 2022

Edited:

on 10 Aug 2024

Community Treasure Hunt

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

Start Hunting!