## 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: BITRATE = gds_bitrate(GDSSTRUCT)
##
## Estimate bitrate of a baseband digital binary signal.
##
## Example:
##
## gs = gds_csvread("DS0084.CSV");
## gs_bitrate(gs);
##
## See also: gds_csvread, gds_zcoss

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

function bitrate = gds_bitrate(gs)

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

	x_zero = __gds_zcross(gs.Waveform_Data - mean(gs.Waveform_Data));
	mindiff = min(diff(x_zero));
	if(mindiff == 0.0)
		##
		## Should never happen
		##
		error("Signal speed appears to be infinite.");
	endif

	dt = __gds_sampling_period(gs);
		
	bitrate = 1 / mindiff / dt;
endfunction
