Random process
Clc;
Close all;
Clear all;
Echo on;
N=1000;
M=50;
Rx_av=zeros(1,M+1);
Sx_av=zeros(1,M+1);
For j=1:10
X=rand(1,N)-1/2;
Rx=Rx_est(X,M);
Sx=fftshift(abs(fft(Rx)));
Rx_av=Rx_av+Rx;
Sx_av=Sx_av+Sx;
End
Rx_av=Rx_av/10;
Sx_av=Sx_av/10;
Subplot(2,1,1);
Plot(Rx_av);
Xlabel(‘m’);
Axis([0 30 -0.01 0.1]);
Subplot(2,1,2);
Plot(Sx_av);
Xlabel(‘f’);
Axis([0 30 0.06 0.12]);
Function Rx = Rx_est(X, M)
N = length(X);
Rx = zeros(1, M+1);
For m = 0:M
Rx(m+1) = sum(X(1:N-m) .* X(1+m:N)) / N;
End
End
Huffman coding
Clc;
Clear all;
N = input(‘Enter the number of symbols: ‘);
P = input(‘Enter input probabilities: ‘);
% Sort probabilities in descending order
[p, idx] = sort(p, ‘descend’);
Disp(‘The sorted probabilities are:’);
Disp(p);
% Define message symbols and reorder according to
sorted probabilities
M = 1:n;
M = m(idx);
Disp(‘Message symbols are:’);
Disp(m);
% Create Huffman dictionary
Dict = huffmandict(m, p);
% Example message (you can change it)
Msg = m; % encoding the message symbols themselves
Code = huffmanenco(msg, dict);
Disp(‘The encoded code is:’);
Disp(code);
% Decode the Huffman code
Decode = huffmandeco(code, dict);
Disp(‘The decoded symbols are:’);
Disp(decode);
% Entropy calculation
H = -sum(p .* log2(p));
Disp(‘Entropy is:’);
Disp(H);
Liner block code
Clc;
Clear all;
% Generator matrix (systematic form)
G = [1 0 0 0 0 1 1;
0 1 0 0 1 0 1;
0 0 1 0 1 1 0;
0 0 0 1 1 1 1];
% Parity-check matrix
H = [0 1 1 1 1 0 0;
1 0 1 1 0 1 0;
1 1 0 1 0 0 1];
% --- USER INPUT ---
U = input(‘Enter 4-bit message (e.g. [1 0 1 1]): ‘);
% --- ENCODING ---
C = mod(u * G, 2);
Fprintf(‘Encoded codeword: ‘);
Disp©;
% --- ADD ERROR (optional) ---
Pos = input(‘Enter error bit position (1–7), or 0 for no
error: ‘);
R = c;
If pos ~= 0
R(pos) = mod(r(pos) + 1, 2); % flip bit
End
Fprintf(‘Received bits: ‘);
Disp®;
% --- SYNDROME CALCULATION ---
S = mod(r * H’, 2);
Fprintf(‘Syndrome: ‘);
Disp(s);
% --- ERROR CORRECTION ---
S = [0 0 0;
1 0 1;
0 1 1;
1 1 1;
1 1 0;
0 1 0;
1 0 0]; % known syndromes
For I = 1:7
If isequal(s, S(I,)
R(i) = mod(r(i) + 1, 2); % correct the bit
End
End
Fprintf(‘Corrected codeword: ‘);
Disp®;
% --- DECODE ---
Decoded = r(1:4);
Fprintf(‘Decoded message: ‘);
Disp(decoded);
OUTPUT
Enter 4-bit message (e.g. [1 0 1 1]): [1 1 0 1]
Encoded codeword:
1101001
Enter error bit position (1–7), or 0 for no error: 3
Received bits:1111001
Syndrome:110
Corrected codeword:
1111101
Decoded message:
1111
Cyclic code
% --- Input ---
Msg = input(‘Enter 4-bit message as [ ] (e.g. [1 0 1 1]): ‘);
Gp = [1 0 1 1]; % Generator polynomial g(x) = x^3 + x +
1
% --- Encoding ---
Msg_padded = [msg zeros(1,length(gp)-1)];
[~, remd] = deconv(msg_padded, gp);
Remd = mod(remd,2);
Codeword = [msg remd(end-(length(gp)-2):end)];
Disp(‘Encoded Codeword:’);
Disp(codeword);
% --- Introduce Error Manually ---
Rx = input(‘Enter received 7-bit codeword (or press Enter
to use same): ‘);
If isempty(rx)
Rx = codeword;
End
% --- Syndrome Calculation ---
[~, rem_rx] = deconv(rx, gp);
Syndrome = mod(rem_rx,2);
Disp(‘Syndrome:’);
Disp(syndrome);
% --- Error Detection / Correction ---
If all(syndrome == 0)
Disp(‘No Error Detected.’);
Corrected = rx;
Else
Disp(‘Error Detected! (Single-bit correction not
implemented fully)’);
Corrected = rx; % Placeholder – implement lookup if
needed
End
Disp(‘Corrected Codeword:’);
Disp(corrected);
% --- Extract Original Message ---
Decoded = corrected(1:4);
Disp(‘Decoded Message:’);
Disp(decoded);
OUTPUT
Enter 4-bit message as [ ] (e.g. [1 0 1 1]): [1 0 1 0]
Encoded Codeword:
1010011
Syndrome:
0000000
Corrected Codeword:
1010011
Decoded Message:
1010