% This script demonstrates encoding a binary sequence to a baseband
% line signal, and the effects of adding noise and distortion. Plots are
% generated to help understand the impact of noise and bandwidth limits on
% the received waveform and potential for bit errors in the decoded
% sequence. The script uses the function MODULB (must be downloaded
% from EE422G web site as well) to generate various line codes. The
% script initializes with critical parameters such as data rate, bit
% sequence, line code, channel bandwidth and signal to noise level. It
% then plots the line coded sequence (Figure 1 of Lab Assignment), the
% undistorted sequence with noise (Figure 2), and the noise plus distortion
% sequence (Figure 3). If very long sequences are used it may be better to
% comment out the plotting code, which is located at the end of the script.
% Beyond 12 bits it becomes difficult to observe details from the plots.
%
% Written by Kevin D. Donohue (donohue@engr.uky.edu) October 2007
clear
fs = 10000; % samples/sec, sampling rate on output waveform
dr = 1000; % bits/sec, bit rate, should be less than fs/10 for a good simulation
snr = 10; % Signal to noise ratio in dB for AWGN
% Channel bandwidth model
bw = 1500; % Hz
[b,a] = butter(4, bw/(fs/2)); % use a Butterworth LPF as channel Model
% Line code options in cell array:
lncds = {'unipolar_nrz'; ... % 1
'bipolar_nrz'; ... % 2
'4level_nrz'; ... % 3
'bipolar_rz'; ... % 4
'unipolar_rz'; ... % 5
'ami'; ... % 6
'manchester'; ... % 7
'miller'; ... % 8
'unipolar_nyquist'; ... % 9
'bipolar_nyquist'}; % 10
linecode = 2; % Select line code index from above cell array
% Binary sequence:
%seq = round(rand(1,1000)); % Uncommment this for a random sequence
seq = [1 0 1 0 0 0 1 1 0 1 0 1 0]; % Uncomment this for a fixed sequence
%seq = [0 0 1 0 0];
%
% Create line signal
%
[y,t] = modulb(seq,dr,fs,lncds{linecode});
%
% Distort signal based on limited bandwidth
%
yd = filtfilt(b,a,y); % Band-Limited Channel
%
% Add noise at specified SNR
%
sigrms = sqrt(mean(y.^2)); % Signal power in rms
% scale power in noise relative to signal
npow = sigrms*10^(-snr/20); % Noise power in rms
nossig = randn(size(yd)); % Create Noise signal
yn = y+npow*nossig; % Add scaled noise to undistorted signal
ynd = yd+npow*nossig; % Add scaled noise to distorted signal
% Plot results
% Line signal plot
figure(1)
plot(t,y)
xlabel('Seconds')
ylabel('Volts')
title([lncds{linecode},' line signal representing binary sequence'],'Interpreter','none')
% Extend y-axis to see signal ends and put text on top
ylow = min(y); % Find signal low points
yhigh = max(y); % Find signal high points
set(gca,'Ylim', [ylow(1)-.15, yhigh(1)+.15]) % Add a little to both ends of y-axis
% Write binary sequence text above signal in the center of time interval
% Create a signal bit template to get bit length
template0 = modulb([0],dr,fs,lncds{linecode});
for k=1:length(seq)
xpoint = length(template0)/(2*fs)+(k-1)/dr; % X location to write text
text(xpoint, max(y)+.2, int2str(seq(k)),'Color','r') % Write bit
end
% Write lines at boundaries of each bit signal
hold on
for k =1:length(seq)
xpoint = length(template0)/(2*fs)+ (k-1.5)/dr; % X location to write text
plot([xpoint, xpoint], [ylow(1)-.15, yhigh(1)+.15], 'g:')
end
hold off
% Plot of line signal with noise
figure(2)
plot(t,yn)
xlabel('Seconds')
ylabel('Volts')
title([lncds{linecode},' line signal representing binary sequence with ' num2str(snr) ...
'dB snr'],'Interpreter','none');
% Extend y-axis to see signal ends and put text on top
ylow = min(yn); % Find signal low points
yhigh = max(yn); % Find signal high points
set(gca,'Ylim', [ylow(1)-.15, yhigh(1)+.15]) % Add a little to both ends of y-axis
% Write binary sequence above signal in the center of time interval
for k=1:length(seq)
xpoint = length(template0)/(2*fs)+(k-1)/dr;
text(xpoint, max(y)+.2,int2str(seq(k)),'Color','r')
end
% Write lines at boundaries of each bit signal
hold on
for k =1:length(seq)
xpoint = length(template0)/(2*fs)+ (k-1.5)/dr;
plot([xpoint, xpoint], [ylow(1)-.15 yhigh(1)+.15], 'g:')
end
hold off
% Plot of line signal with noise and channel distortion
figure(3)
plot(t,yd)
xlabel('Seconds')
ylabel('Volts')
title([lncds{linecode},' line signal representing binary sequence with channel bandwidth of ' ...
num2str(bw), ' Hz'],'Interpreter','none')
% Extend y-axis to see signal ends and put text on top
ylow = min(ynd); % Find signal low points
yhigh = max(ynd); % Find signal high points
set(gca,'Ylim', [ylow(1)-.15, yhigh(1)+.15]) % Add a little to both ends of y-axis
% Write binary sequence above signal in the center of time interval
for k=1:length(seq)
xpoint = length(template0)/(2*fs)+(k-1)/dr;
text(xpoint, max(y)+.2,int2str(seq(k)),'Color','r')
end
% Write lines at boundaries of each bit signal
hold on
for k =1:length(seq)
xpoint = length(template0)/(2*fs)+ (k-1.5)/dr;
plot([xpoint, xpoint], [ylow(1)-.15 yhigh(1)+.15], 'g:')
end
hold off