# Matlab Simulation – PSD of Line Codes

Line codes are used to map binary information sequence into analog signal which has properties suitable for the physical media that is being utilized to send the data.

Consider the case of a networking equipment like a router or a switch. According to ISO-OSI ( “International Standard Organisation – Open System Interconnection”[1]) reference model , any communication device or network can be conceptualized into seven layers of protocols namely Application, Presentation, Session, Transport, Network, Data-Link, and Physical Layer. Physical Layer dictates the electrical, physical and mechanical properties of the communication network or device. It defines properties like voltage levels, timing , maximum data rate allowable through the media without data loss,bandwidth efficiency, power consumption, physical connectors ( details like 50Ω or 75Ω termination, SMA, SMC or BNC connectors etc…,).

Line codes are chosen in such a way that they are optimized for the media that is being used to transmit data. Some line codes provide excellent timing synchronization (Timing synchronization is important in the context of sampling and clock recovery at the receiver. Incorrect timing sync may cause unrecoverable errors at the receiver due to wrong sampling instants) but may posses an undesirable DC content ( Why ?? Desired operating frequency range for a particular application may be between 500KHz and 1MHz, presence of DC content in such an application causes wastage of power.)

Commonly used line codes are NRZ (Non Return to Zero), RZ (Return to Zero), AMI (Alternate Mark Inversion), Manchester code, 8b/10b code,2B1Q code, Miller code, etc..,

Power Spectrum Density (PSD) is a common method employed in choosing a line code suitable for the physical media under consideration. Lets discuss how to convert a bit stream into NRZ unipolar, NRZ polar and Manchester coded data and plot their PSD.

### Procedure to plot Power Spectral Density of line codes in Matlab :

Step 1: Generate random binary sequence of sufficient length .Too small number of bits will give a poor PSD plot , whereas , too large number of bits consumes more computation time.

Step 2: Map the generated bits into NRZ unipolar or NRZ polar or Manchester line code (see Algorithm).

Step 3: Plot PSD using “psd” function in matlab.

### Algorithm:

 Manchester Coding: NRZ Unipolar NRZ Polar First half of bit period : signal = data Second half of bit period: signal = !data For the entire bit period: signal=0 { if data =0 } signal=A volts { if data =1 } For the entire bit period: signal=-A volts { if data =0 } signal=A volts { if data =1 }

### File 1: %———Line_Encoder.m—————%

Check this ebook for the full Matlab code Simulation of Communication Systems using Matlab by Mathuranathan Viswanathan

Sample Output :

### Recommended Books:

• Pingback: How does codes fit in channel?()

• Hi
For getting back the original data just xor the Manchester coded data with a clock that run at 1/2 of bit rate.

Following code illustrates this:

%Manchester Coded data recovery
Tb=1/Rb;
%Generate Clock at 1/2 bit rate – Tb
clock=[];
numberOfDataBits = length(data);
bit=1;
for j=1:numberOfDataBits*2,
for i=0:1/Fs:Tb/2-1/Fs,
clock = [clock bit];
end
bit=~bit;
end
%XOR clock with Manchester coded output
recoveredStream = xor(clock,(output+max(output))/max(output));

%Decimate to get the recovered Data
for i=1:numberOfDataBits,
recoveredBits(i) = recoveredStream(i*Fs/Rb);
end
%print original data and recovered bits
recoveredBits %recovered data
data %original data

Regards
Mathuranathan

• you are doing very good,

but can i have the line decoding.

I mean from Manchester coding to its original form.

for example, I generated 1000 binary numbers and i coded them with your Line_code function to be of Manchester shape, how can i decode them to have their original format?

Thank you very much.