function [sig_bb,f_bb]=baseband(sig,fs);
%
% Call [sig_bb,f_bb]=baseband(sig,fs);
%
% INPUTS:
% sig,fs    = signal sampled at fs for one PRI;
%
% OUTPUTS:
% sig_bb    = complex, basebanded signal
% f_bb      = basebanding frequency
%
% f_bb taken to be at max of FFT of sig
% Frequency spectrum is truncated outside of [0,2*f_bb]
% before inverse FFT is taken.
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
npt=length(sig); dt=1/fs; PRI=length(sig)*dt; df=1/PRI;

S=fft(sig); S_dB=dB(S); f_ax=df*[0:npt-1];
%
[S_max,ii_max]=max(S_dB(1:floor(npt/2)));
f_bb=f_ax(ii_max);
S_bb=zeros(size(S));
S_bb(1:ii_max)=S(ii_max:2*ii_max-1);
S_bb(npt-[ii_max-2:-1:0])=S(1:ii_max-1);
sig_bb=ifft(S_bb);
