I want Huffman coding
Afficher commentaires plus anciens
(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 commentaires
Walter Roberson
le 27 Nov 2022
What is your question about MATLAB?
Muhammad
le 27 Nov 2022
Steven Lord
le 27 Nov 2022
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".
Muhammad
le 27 Nov 2022
Modifié(e) : Walter Roberson
le 27 Nov 2022
Walter Roberson
le 27 Nov 2022
Réponses (1)
Sulaymon Eshkabilov
le 27 Nov 2022
0 votes
Have you visited to this doc: Encode sequence of symbols by Huffman encoding - MATLAB huffmanenco (mathworks.com)
Catégories
En savoir plus sur Source Coding dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
