% method of class @signal
% function sig=getpart(sig,from,[to])
%
%   INPUT VALUES:
%		@sig: original signal
%		from: from this time in seconds
%		to: to this time in seconds (default: end of signal)
%   RETURN VALUE:
% returns a signal-object that is a copy of the original signal in the 
% range from-to
% 
% (c) 2003-2008, University of Cambridge, Medical Research Council 
% Maintained by Tom Walters (tcw24@cam.ac.uk), written by Stefan Bleeck (stefan@bleeck.de)
% http://www.pdn.cam.ac.uk/cnbh/aim2006
% $Date: 2008-06-10 18:00:16 +0100 (Tue, 10 Jun 2008) $
% $Revision: 585 $

function sig=getpart(a,from,to)

sr=getsr(a);

if nargin<3
	to=getlength(a);
end

if from < getminimumtime(a)
    error('error: negative beginning in getpart');
end

if to > getmaximumtime(a)+sr
    error('error: getpart wants part behind signal');
end

duration=to-from;
sig=signal(duration,sr);

target_nr_point=getnrpoints(sig);

start=time2bin(a,from)+1;
stop=time2bin(a,to);

% realtarget=stop-start+1;
% if realtarget~=target_nr_point
% 	% seems to happen when sr=44100
% 	target_nr_point=realtarget;
% end

len=length(a.werte);
if start+target_nr_point-1 > len
	target_nr_point=len-start+1;
 	% seems to happen when sr=44100
	sig.werte(1:target_nr_point)=a.werte(start:start+target_nr_point-1);
else	
	sig.werte(1:end)=a.werte(start:start+target_nr_point-1);
end
% sig.werte(1:end)=a.werte(start:stop);

sig=setstarttime(sig,from);
sig=setname(sig,sprintf('Part of Signal: %s',getname(a)));



