## Copyright (C) 2010 Thomas Weidenfeller
## 
## This file is part of gds2000tools/gds-octave.
## 
## gds2000tools/gds-octave is free software: you can redistribute it
## and/or modify it under the terms of the GNU General Public License
## version 3 as published by the Free Software Foundation.
## 
## gds2000tools/gds-octave is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with gds2000tools/gds-octave.  If not,
## see <http://www.gnu.org/licenses/>.
##

## usage: = gds_eye(GDSSTRUCT)
##
## Plots an eye diagram of periodic signal, e.g. a baseband digital signal.
##
## The clock of the signal is reconstructed by calculating the
## Zero crossings of the AC component of the signal and using the
## smallest difference between such zero crossings as the baud rate.
##
## Example:
##
## gs = gds_csvread("DS0084.CSV");
## subplot (2,1,1);
## gds_plot(gs);
## subplot (2,1,2);
## gds_eye(gs);
##
## See also: gds_csvread, gds_zcross, gds_ac.

## Author: Thomas Weidenfeller
## Created: 2010-12-31
## Version: $Id: gds_eye.m,v 1.1 2010/12/31 02:28:33 tw Exp $
## Keywords: Oscilloscope, GDS-2000, gds2000tools, sign

function gds_eye(gs)
	if(nargin != 1 || !isstruct(gs))
		print_usage();
	endif

	baud = gds_bitrate(gs);
	dt = __gds_sampling_period(gs);
	n  = __gds_memory_length(gs);
	Tt = dt .* n;
	N  = floor(Tt .* baud);
	nn = round(N ./ baud ./ dt);
	X = resize(gds_struct2val(gs), nn, 1);
	PER = nn ./ N .* dt;
	OFF = 0; ## TODO
	H = gcf();
	eyediagram(X, N, PER, OFF, [], H);
	title(["Eye-diagram for " gs.Source " " gs.Horizontal_Mode " " gs.Time "\n" num2str(baud) " Bd; N: " num2str(N) ]);
	if(strcmp(gs.Horizontal_Units, "s"))
		xlabel("Time / s");
	else
		xlabel(gs.Horizontal_Units);
	endif
	ylabel([ "Amplitude / " gs.Vertical_Units ]);
	axis("on");
	grid("on");
endfunction
