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.



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 }


Matlab Code:

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

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

File 2:———— Basic_Wave_gen.m——–>To generate line codes and plot them (optional file)

File 3:— PSD_of_Line_Codes.m—-> To generate PSD of Line codes

Sample Output :

Power Spectral Density of Line Codes
Power Spectral Density of Line Codes

Reference :

[1] – Cisco Systems, Inc – Internetworking Basics – click here

See also:

[1] Colored Noise Generation in Matlab
[2] Sampling Theorem – Baseband Sampling
[3] Sampling Theorem – Bandpass or Intermediate or Under Sampling
[4] Window Functions – An Analysis
[5] FFT and Spectral Leakage
[6] Raised Cosine Filter
[7] Moving Average Filter ( MA filter )

Recommended Books:

More Recommended Books at our Integrated Book Store

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

  • Pingback: BPSK modulation and Demodulation | GaussianWaves()

  • Pingback: QPSK modulation and Demodulation | GaussianWaves()

  • 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
    %Generate Clock at 1/2 bit rate – Tb
    numberOfDataBits = length(data);
    for j=1:numberOfDataBits*2,
    for i=0:1/Fs:Tb/2-1/Fs,
    clock = [clock bit];
    %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);
    %print original data and recovered bits
    recoveredBits %recovered data
    data %original data


  • 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.