Here I am going to post MATLAB script for different line coding schemes including RZ, NRZ, Manchester coding, 2B1Q, Differential Manchester and a few other.
It should be noted that
dec2binary() is a separate function created in MATLAB to convert the digital input into its binary counterpart
================================================================================================================================================
MATLAB CODE
================================================================================================================================================
clear all
close all
clc
scrsize = get(0,'screensize');
first = [10, (scrsize(4)/2)+40, (1265/1280)*scrsize(3), (280/800)*scrsize(4)];
second = [10, 50, (625/1280)*scrsize(3), (305/800)*scrsize(4)];
third = [(second(3)+20), 50, second(3:4)];
fprintf('Line coding assignment by Mohammad Ibrahim\n TE-47D\n');
b = input('Number of bits: '); % Number of bits.
N = input('Number of samples in final signal: '); % Number of samples in final signal.
n = 0:(N-1); %Index
% Choose the input type.
input = questdlg(' Choose input', 'Input',...
'Sine', 'Sawtooth', 'More options', 'Sine');
% Create the input data sequence.
switch input
case 'Sine'
x = sin(2*pi*n/N);
case 'Sawtooth'
x = sawtooth(2*pi*n/N);
case 'More options'
input = questdlg(' More options', 'Input','Tangent','Sinc^2','Random','Random');
switch input
case 'Tangent'
x = tan(2*pi*n/N);
case 'Sinc^2'
x = (sin(2*pi*n/N)./(2*pi*n/N)).^2;
case 'Random'
x = randn(1,N); % Random data
x = x/max(abs(x)); % Scale to +/- 1
end
end
fprintf('Bits = %g, levels = %g, signal = %s.\n', b, 2^b, input);
% Signal is restricted to between -1 and +1.
x(x>=1)=(1-eps); % Make signal from -1 to just less than 1.
x(x<-1)=-1;
% Quantize a signal to "b" bits.
xq=floor((x+1)*2^(b-1)); % Signal is one of 2^n int values (0 to 2^n-1)
xq=xq/(2^(b-1)); % Signal is from 0 to 2 (quantized)
xq=xq-(2^(b)-1)/2^(b); % Shift signal down (rounding)
figure(1)
subplot(1,2,1)
stem(n,x,'b');
title('Original signal')
grid on
subplot(1,2,2)
stem(xq,'r');
title('Quantized signal')
grid on
set(1,'Position', first)
levels=floor((x+1)*2^(b-1));
L = length(levels);
bits = zeros(1,L*b);
for(i = 1:L)
z = ((i-1)*b)+1;
bits(z:z+b-1) = dec2binary(levels(i),b);
end
n1 = 0:1/b:N-1/b;
%to show proper pulses
i1 = 1:30;
i1 = i1./i1;
bits_1 = bits;
for(i = length(bits_1)-1:-1:1)
if(bits_1(i) == 1)
bits_1 = [bits_1(1:i), i1, bits_1(i:end)];
else
bits_1 = [bits_1(1:i), i1.*0,bits_1(i:end)];
end
end
bits_1 = [bits_1, i1.*bits_1(end), bits_1(end)];
n2 = 0:1/(b*32):(N - (1/(b*32)));
figure
plot(n2,bits_1)
axis([n2(1) n2(end) -2 2])
title('Bitstream')
xlabel('Time')
ylabel('Amplitude')
grid on
set(2,'Position', second)
%choose line coding method
lc = questdlg(' Choose method', 'method',...
'RZ', 'NRZ-L', 'More options', 'RZ');
switch lc
case 'More options'
lc = questdlg(' Choose method', 'method',...
'NRZ-I', 'Manchester', 'More options', 'NRZ-I');
switch lc
case 'More options'
lc = questdlg(' Choose method', 'method',...
'Differential Manchester', '2B1Q', 'Differential Manchester');
end
end
switch lc
case 'RZ'
%RZ ------------------------------------------------------
bits_rz = bits_1;
for(i = 1:length(bits_rz))
if(bits_rz(i) == 0)
bits_rz(i) = -1;
end
end
for(i = 1:32:(length(bits_1)))
bits_rz(i+16:i+31) = zeros(1,16);
end
figure
plot(n2,bits_rz)
axis([n2(1) n2(end) -2 2])
title('RZ (1 = high; 0 = low)')
xlabel('Time')
ylabel('Amplitude')
grid on
case 'NRZ-L'
%NRZ-L ---------------------------------------------------
bits_nrzl = bits_1;
for(i = 1:length(bits_nrzl))
if(bits_nrzl(i) == 0)
bits_nrzl(i) = 1;
else
bits_nrzl(i) = -1;
end
end
figure
plot(n2,bits_nrzl)
axis([n2(1) n2(end) -2 2])
title('NRZ-L (1 = low; 0 = high) ')
xlabel('Time')
ylabel('Amplitude')
grid on
case 'NRZ-I'
% NRZ-I ---------------------------------------------------
bits_nrzi = ones(1,length(bits_1));
for(i = 1:length(bits))
if(bits(i) == 1)
bits_nrzi((i*32 - 31):end) = -1.*bits_nrzi((i*32 - 31):end);
end
end
figure
plot(n2,bits_nrzi)
axis([n2(1) n2(end) -2 2])
title('NRZ-I (1 = transition; 0 = no transition')
xlabel('Time')
ylabel('Amplitude')
grid on
case 'Manchester'
%Manchester -----------------------------------------------
bits_man = ones(1,length(bits_1));
for(i = 1:length(bits))
if(bits(i) == 1)
bits_man((i*32 - 31):(i*32)) = [ones(1,16), -1.*ones(1,16)];
else
bits_man((i*32 - 31):(i*32)) = [-1.*ones(1,16), ones(1,16)];
end
end
figure
plot(n2,bits_man)
axis([n2(1) n2(end) -2 2])
title('Manchester (1 = transition high to low; 0 = transition low to high')
xlabel('Time')
ylabel('Amplitude')
grid on
case 'Differential Manchester'
%Differential Manchester ----------------------------------
bits_dman = ones(1,length(bits_1));
if(bits(1) == 1)
bits_dman((i*32 - 31):(i*32)) = [ones(1,16), -1.*ones(1,16)];
else
bits_dman((i*32 - 31):(i*32)) = [-1.*ones(1,16), ones(1,16)];
end
for(i = 2:length(bits))
if(bits(i) == 1)
prev = -1;
else
prev = 1;
bits_dman((i*32 - 31):(i*32)) = [-1.*ones(1,16), ones(1,16)];
end
bits_dman((i*32 - 31):(i*32)) = prev.*bits_dman(((i-1)*32 - 31):((i-1)*32));
end
figure
plot(n2,bits_dman)
axis([n2(1) n2(end) -2 2])
title('Differential Manchester (1 = transition; 0 = no transition)')
xlabel('Time')
ylabel('Amplitude')
grid on
case '2B1Q'
% 2B1Q ----------------------------------------------------
bits_2b1q = bits_1;
for(i = 1:64:length(bits_2b1q))
x = bits_2b1q(i)*10 + bits_2b1q(i+32);
switch x
case 00
bits_2b1q(i:i+63) = -2;
case 01
bits_2b1q(i:i+63) = -1;
case 10
bits_2b1q(i:i+63) = 1;
case 11
bits_2b1q(i:i+63) = 2;
end
end
figure
plot(n2,bits_2b1q)
axis([n2(1) n2(end) -3 3])
title('2B1Q (two bits coupled, 00 = -2; 01 = -1; 10 = 1; and 11 = 2)')
xlabel('Time')
ylabel('Amplitude')
grid on
end
set(3,'Position', third)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function [c] = dec2binary(a,b)
c = zeros(1,b);
j = 0;
for(i = (b-1):-1:0)
j = j+1;
if (a-(2^i) == floor(a-(2^i)) && a-(2^i) >= 0)
c(j) = 1;
a = a - (2^i);
else
c(j) = 0;
end
end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------