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 ISOOSI ( “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, DataLink, 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 } 
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)
1 2 3 4 5 6 7 8 9 10 11 
[time,output,Fs]=Line_Encoder([1 1 0 1 1 0 0 0 1 0 0],2e6,3,'Manchester'); plotHandle=plot(time,output); xlabel('Time'); ylabel('Amplitude'); title('Manchester Encoder'); set(plotHandle,'LineWidth',2.5); grid on; maxTime=max(time); maxAmp=max(output); minAmp=min(output); axis([0,maxTime,minAmp1,maxAmp+1]); 
File 3:— PSD_of_Line_Codes.m—> To generate PSD of Line codes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 
inputBits=randi([0 1],1,1000); % R2008a syntax => randsrc(1,10000,[0 1]); colordef white; Rb=2e6; % Bit Rate amplitude=2; % PeakPeak Amplitude [time,xiPolar,Fs]=Line_Encoder(inputBits,Rb,amplitude,'polar'); [pyy,fy]=psd(xiPolar); figure(1); plotHandle=plot(fy*Fs/2,10*log10((pyy)),'k'); set(plotHandle,'LineWidth',2.5); hold on; [time,xiUnipolar,Fs]=Line_Encoder(inputBits,Rb,amplitude,'unipolar'); [pyy,fy]=psd(xiUnipolar); plotHandle=plot(fy*Fs/2,10*log10((pyy)),'r'); set(plotHandle,'LineWidth',2.5); hold on; [time,xiManchester,Fs]=Line_Encoder(inputBits,Rb,amplitude,'Manchester'); [pyy,fy]=psd(xiManchester); plotHandle=plot(fy*Fs/2,10*log10((pyy)),'g'); set(plotHandle,'LineWidth',2.5); legend('polar','unipolar','Manchester'); title('PSD of Line Codes'); grid on; hold off; figure(2); subplot(4,1,1); plot(inputBits); xlabel('Time (Seconds)'); ylabel('Amplitude (Voltage)'); title('Input bit stream'); maxTime=max(time); maxAmp=max(inputBits); minAmp=min(inputBits); axis([0,1000,minAmp0.5,maxAmp+0.5]); subplot(4,1,2); plot(time,xiPolar); xlabel('Time (Seconds)'); ylabel('Amplitude (Voltage)'); title('Polar NRZ coded'); maxTime=max(time); maxAmp=max(xiPolar); minAmp=min(xiPolar); axis([0,maxTime,minAmp0.5,maxAmp+0.5]); subplot(4,1,3); plot(time,xiUnipolar); xlabel('Time (Seconds)'); ylabel('Amplitude (Voltage)'); title('Unipolar NRZ coded'); maxTime=max(time); maxAmp=max(xiUnipolar); minAmp=min(xiUnipolar); axis([0,maxTime,minAmp0.5,maxAmp+0.5]); subplot(4,1,4); plot(time,xiManchester); xlabel('Time (Seconds)'); ylabel('Amplitude (Voltage)'); title('Manchester Coded'); maxTime=max(time); maxAmp=max(xiManchester); minAmp=min(xiManchester); axis([0,maxTime,minAmp0.5,maxAmp+0.5]); 
Sample Output :
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 )
Pingback: QPSK modulation and Demodulation  GaussianWaves()
Pingback: BPSK modulation and Demodulation  GaussianWaves()
Pingback: How does codes fit in channel?()