function [xx, yy] = eye_plot(fname,col_to_plot,N_bits,Eye_width)
% Function  [xx, yy] = eye_plot(fname, col_to_plot, N_bits, Eye_width)
% 
% This function takes in time data from the file fname and plots it as 
% an eye diagram.  The user must input how many bits are in the time 
% sequence in the input file, and how many bits wide the eye diagram 
% should be.  The first column of fname is assumed to be the time mesh.
%  Input arguments:
%     fname          : Input file name
%     col_to_plot    : Which column of fname will be used to plot the eye
%     N_bits         : How many bits of data are in the time mesh in fname
%     Eye_width      : How many bits wide should the eye diagram be
%  Output arguments:
%     xx             : Vector of time data
%     yy             : Matrix consisting of all the traces in the eye 
%                      diagram


% As before, read in the data from the file fname and determine its size.
data = load(fname);
[m n] = size(data);

% As before, the first column is assumed to define the x-axis and 
% the second column is the time domain data of the signal, 
% to be plotted as an eye diagram.
x = data(:,1);
y = data(:,col_to_plot);

% First determine how many time mesh points there are per bit.
% This is needed so we know how much to time shift the data to plot
% the eye.
Pts_per_bit = m/N_bits;

% One trace in the eye diagram is the time data itself.  Then 
% all possible time-shifted copies are considered as well.  This 
% for-loop concatenates all the possible bit shifts in one big
% matrix.  The circular (periodic) shifts are done with the 
% Matlab command wshift.
yplot = y;
for j = 1:N_bits
  yplot = [yplot wshift(1,y,Pts_per_bit*j)];
end

% We then plot the data in black, and use the xlim command to 
% restrict the view of the eye diagram to a certain number of 
% bits.
plot(x,yplot,'k'); xlim([0 x(end)*Eye_width/N_bits]);

% Also note that in this code, the output [x y] is the x and y data 
% defined by the first column of fname and the columns defined
% by cols_to_plot, respectively.  
% So, if there are two output arguments present, 
% assign x and y to them:
if nargout == 2
  xx = x;
  yy = yplot;
end
