Code covered by the BSD License

# Encode & Decode a message

### Ashish Soni (view profile)

Encode & Decode a message using source & channel coding (Huffman and cyclic codes)

Exp.m
```clc;
clear all;
sym=input('Enter the Message--- ')
l=length(sym);
x=sort(sym);

a=seqwordcount(sym,'a');     %counting no.of character
h=seqwordcount(sym,'h');   i=seqwordcount(sym,'i');
m=seqwordcount(sym,'m');   s=seqwordcount(sym,'s');
b=seqwordcount(sym,'_');

prob_a=a./l;                    %Finding probability of each symbol
prob_h=h./l; prob_i=i./l;
prob_m=m./l; prob_s=s./l; prob_b=b./l;

msg=[1,2,3,4,5,6];
prob=[prob_a,prob_h,prob_i,prob_m,prob_s,prob_b];
dict = huffmandict(msg,prob);          %arrange symbols according to probability

code_a = huffmanenco(1,dict);         %Huffman coding of symbols
code_a = (code_a)';
code_h = huffmanenco(2,dict);
code_h= (code_h)';
code_i = huffmanenco(3,dict);
code_i = (code_i)';
code_m = huffmanenco(4,dict);
code_m = (code_m)';
code_s = huffmanenco(5,dict);
code_s = (code_s)';
code_b = huffmanenco(6,dict);
code_b = (code_b)';

for i=1:length(prob)
z(i)=prob(i).*log2(1./prob(i));     %Calculation of entropy
end
entropy=sum(z)

code_array= [code_i code_b code_a code_m code_b code_a code_s code_h code_i code_s code_h]
sz= size(code_array);

n=35;   %code vector to b transmitted
k=29;  %message-code_array
p=n-k;  %parity bit
pol=cyclpoly(n,k);
[parmat,genmat]=cyclgen(n,pol);
msg= code_array;
codeword=mod(msg*genmat,2)
trt = syndtable(parmat);    % Produce decoding table.
recd= input('enter the recieved vector- ');
syndrome = rem(recd*parmat',2);
syndrome_de = bi2de(syndrome,'left-msb');     % Convert to decimal.
errorvect = trt(1+syndrome_de,:);
correctedcode = rem(errorvect+recd,2);

if recd == codeword
disp('No Error- ')
disp(sym)
else
disp('Error in message....')
end```