Skip to content

Instantly share code, notes, and snippets.

@satlavida
Last active April 17, 2019 16:24
Show Gist options
  • Save satlavida/b5a2bf2d8587faf37c3125ee1c1c2046 to your computer and use it in GitHub Desktop.
Save satlavida/b5a2bf2d8587faf37c3125ee1c1c2046 to your computer and use it in GitHub Desktop.
Cyclic Encoding using Shift Registers
clc;
close all;
clear;
n = input('Enter n for cyclic code');
k = input('Enter k for cyclic code');
msg = input('Enter message bits of the length n-k');
N = n-k; %No of shift register%
poly = cyclpoly(n,k,'max');
S = zeros(1,N);
SP = S; %Previous state of shift register%
g = zeros(1,N-1);
for i = 2:N
g(i-1) = poly(i);
end
disp(S);
for i=1:k
%disp(S);
%sprintf('%d + %d %d', S(N), msg(i), i)
S(1) = xor(S(N),msg(i));
for j = 2:N
%sprintf('%d + %d %d', g(N-j+1)*S(1), SP(j-1), j)
S(j) = xor(g(N-j+1)*S(1), SP(j-1));
end
%disp(S);
SP = S;
end
encoded = [msg, fliplr(S)];
sprintf('Encoded message using hardware is')
disp(encoded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment