## Copyright (C) 2009, 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: [GDSSTRUCT_AC] = gds_ac(GDSSTRUCT)
## usage: gds_ac(GDSSTRUCT)
##
## Returns the AC component of the waveform. That is the DC component
## is removed from the signal.
##
## If no return value is requested then the data is plotted instead.
##
## Example:
##
## gs = gds_csvread("DS0008.CSV");
## ac = gds_ac(gs);
## subplot (2,1,1);
## gds_plot(gs);
## subplot (2,1,2);
## gds_plot(ac);
##
## See also: gds_csvread, gds_csvwrite.

## Author: Thomas Weidenfeller
## Created: 2009-07-08
## Version: $Id: gds_ac.m,v 1.8 2010/12/31 13:05:40 tw Exp $
## Keywords: Oscilloscope, GDS-2000, gds2000tools, AC, DC

function [gsout] = gds_ac(gs)

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

	dc = mean(gs.Waveform_Data);		   # extract DC component
	gs.Waveform_Data = gs.Waveform_Data - dc;  # remove DC component

	dcv = gds_wd2val(dc, gs.Vertical_Scale);

	gs.Trigger_Level = gs.Trigger_Level - dcv; # as-if trigger level
	gs.Vertical_Position = gs.Vertical_Position - dcv; # TODO: not needed?
	gs.Source = [ gs.Source "-AC" ];

	if(nargout < 1)
	gds_plot(gs);
	else
		gsout = gs;
	endif
endfunction

%!demo
%! ##gs = gds_csvread("DS0008.CSV"); ## read waveform from file
%! gs = __gds_demo_gs();  ## for now get a demo gds waveform data structure
%! ac = gds_ac(gs);
%! subplot (2,1,1);
%! gds_plot(gs);
%! subplot (2,1,2);
%! gds_plot(ac);
