function [time,singlesignal,freq,plotcalspecdb,calmicropascalpervolt]=InfiniumFFT(params);

%********************************************************************************
% CLEAR FIGURES AND SETUP PARAMETERS
%********************************************************************************
figure(1)
clf
figure(2)
clf

directoryname=params.directoryname;
filename=params.filename;
numdatafiles=params.numdatafiles;
preampgain=params.preampgain;
hydrophoneID=params.hydrophoneID;
identifier=params.identifier;

%********************************************************************************
% DETERMINE MAXIMUM RECORD LENGTH USED BY ALL RECORDED DATAFILES
%********************************************************************************
recordsize=1e10;
for k=1:numdatafiles,
   if numdatafiles==1,
      filepath=[directoryname filename];
   else,
      m=length(filename);
      filepath=[directoryname filename(1:m-4) num2str(k) '.csv'];
      disp(['Checking Sample Record Length In:  ' filepath])
   end;
   infstruct=ReadInfiniumCSVFile(filepath);
   recordsize=min(recordsize, length(infstruct.x));
end;

%********************************************************************************
% READ AND PROCESS DATAFILES:
% ARRANGE FOR DIFFERENT PROCESSING OF MULTIPLE VS SINGLE FILE RECORDS
%********************************************************************************
for k=1:numdatafiles,
   if numdatafiles==1,
      timeplottitle=['Gain Adj Data:  ' filename];
      freqplottitle='Uncal Spect Mag';
      caltimeplottitle=['Gain/Win/Cal Adj Data:  ' filename];
      calfreqplottitle='Cal Spect Mag';
      filepath=[directoryname filename];
   else,
      m=length(filename);
      if k==1,
         timeplottitle=['Gain Adj Data: ' filename(1:m-4) num2str(k) '.csv'];
			caltimeplottitle=['Gain/WIn/Cal Adj Data: ' filename(1:m-4) num2str(k) '.csv'];
      end;
      freqplottitle=['Uncal Avg Spect Mag: ' num2str(numdatafiles) ' File Avg'];
      calfreqplottitle=['Calib Avg Spect Mag: ' num2str(numdatafiles) ' File Avg'];
      filepath=[directoryname filename(1:m-4) num2str(k) '.csv'];
      disp(['Averaging Spectra for Data Contained In:  ' filepath])
   end;
   
   %**********************************************************************
   % READ NEXT DATAFILE:
   %**********************************************************************
   infstruct=ReadInfiniumCSVFile(filepath);
   
   %**********************************************************************
   % CHECK TO ENSURE PROPER UNITS:
   %**********************************************************************
   defaultxunits='second';
   defaultyunits='Volt';
   if infstruct.xunits(1:6)~=defaultxunits,
      disp('WARNING:  SCOPE X AXIS UNITS HAVE CHANGED')
   end;
   if infstruct.yunits(1:4)~=defaultyunits,
      disp('WARNING:  SCOPE Y AXIS UNITS HAVE CHANGED')
   end;
   
   %**********************************************************************
   % PREPARE WORKING VECTORS AND ADJUST FOR PRE-AMPLIFIER GAIN SETTING:
   %**********************************************************************
   zeropad=0;
   if zeropad==1,
      numsamp=10000; %Data Sets will be zero padded to this size
   else,
      numsamp=recordsize;
   end;
   
   time=infstruct.x(1:recordsize); %units = seconds
   signal=infstruct.y(1:recordsize)./(10^(preampgain/20));  %units = volts
   origdt=time(2)-time(1);
   orignumsamp=length(time);
	[time,signal]=ConditionData(time,signal,numsamp);
   newdt=time(2)-time(1);
   newnumsamp=length(time);
   dtlabel=['dt: Orig=' num2str(origdt) ', Adj=' num2str(newdt)];
   samplabel=['N: Orig=' num2str(orignumsamp) ', Adj=' num2str(newnumsamp)];
   
   % Save Copy of Raw Signal Before Windowing and Calc its FFT:  Will be plotted later
   if k==1,
      singlesignal=signal;
	   [freq,singletrans] = Time2Freq(time,singlesignal);  % xunits=Hz, yunits=Sample Averaged Volts
   end;
   
   %**********************************************************************
   % WINDOW DATA, CALCULATE FFT, AVG FFT MAG
   %**********************************************************************
   %Window Data
   signal=blackman(newnumsamp).*signal;
   
   %Calc FFT
   [freq,trans] = Time2Freq(time,signal);  % xunits=Hz, yunits=Sample Averaged Volts
   
   %Sum Spectral Data for Average
   if k==1,
      sumtrans=trans;
   else,
      sumtrans=abs(sumtrans)+abs(trans);
   end;
   
end; %for k

%Calculate Spectral Data Average
if numdatafiles>1,
   trans=(sumtrans./numdatafiles);
end;

%********************************************************************************
% DISPLAY USEFUL HEADER INFO
%********************************************************************************
dispinfhead=0; %Set == 1 to display Infinium Header Data
if dispinfhead==1,
   infstruct %Dislpay Infinium Header Data
end;
disp(['Preamp Gain Setting During Measurement = ' num2str(preampgain) ' dB'])
disp(['Measurement Made Using Hydrophone ' hydrophoneID])

dt=time(2)-time(1);
df=freq(2)-freq(1);
disp(['dt = ' num2str(dt) ' (sec)'])
disp(['df = ' num2str(df) ' (Hz)'])

%********************************************************************************
% CONVERT AVERAGE SPECTRAL MAG DATA TO dB
%********************************************************************************
specmag=20*log10(abs(trans)); %dB Re: 1 Volt

%**********************************************************************
% PLOT RAW DATA FROM FIRST DATAFILE IN DATA SET
%**********************************************************************
figure(1)
orient 'tall'
sh=subplot(3,2,1);
set(sh,'FontSize',6);
plot(time,singlesignal);

axvals=axis;
axvals(1)=min(time);
axvals(2)=max(time);
axis(axvals);

title(timeplottitle,'FontSize',6);
xlabel('Time (sec)','FontSize',6);
ylabel('Volts','FontSize',6);
text(0.01,0.95,identifier,'sc','FontSize',6)
text(0.6,0.95,dtlabel,'sc','FontSize',6)
text(0.6,0.90,samplabel,'sc','FontSize',6)
dtlabel=['dt (sec) = ' num2str(dt)];
text(0.6,0.05,dtlabel,'sc','FontSize',6)

%********************************************************************************
% PLOT AVERAGE SPECTRAL MAG DATA NORMALIZED TO 1 Hz BW
%********************************************************************************

plotspecmag=specmag - 10*log10(df); %Normalizes to 1 Hz Bandwidth
sh=subplot(3,2,2);
set(sh,'FontSize',6);
plot(freq,plotspecmag)

axvals=axis;
axvals(1)=min(freq);
axvals(2)=max(freq);
axis(axvals);

title(freqplottitle,'FontSize',6)
xlabel('Frequency (Hz)','FontSize',6)
ylabel('(dB re: 1 Volt)','FontSize',6)
dflabel=['df (Hz)= ' num2str(df)];
text(0.6,0.05,dflabel,'sc','FontSize',6)

%********************************************************************************
% READ, RESAMPLE, PLOT, AND APPLY HYDROPHONE CALIBRATION DATA
%********************************************************************************
%Read Hydrophone Calibration Data
eval(['calib=hydrocal' hydrophoneID ';']); %xunits = Hz, yunits = dB re: 1V/uPa
[m,n]=size(calib);

%Plot Raw Calibration Data
figure(2)
orient 'landscape'
sh=subplot(2,2,1);
[m,n]=size(calib);
set(sh,'FontSize',6);
plot(log10(calib(2:m,1)),calib(2:m,2))
axis([log10(5e3) log10(5e5) -240 -190]);
title('Raw Calibration Data','FontSize',6)
xlabel('Freq (Log(Hz))','FontSize',6)
ylabel('dB re: 1V/uPa','FontSize',6)
text(0.6,0.95,hydrophoneID,'sc','FontSize',6)

sh=subplot(2,2,2);
set(sh,'FontSize',6);
plot(calib(:,1),calib(:,2))
axis([0 max(calib(:,1)) -240 -190]);
title('Zero Extended Calib Data','FontSize',6)
xlabel('Freq (Hz)','FontSize',6)
ylabel('dB re: 1V/uPa','FontSize',6)

%Resample Calibration Data to Match Frequency Axis of Measured Data
[calx,caldb,caldx]=ResampleData(freq,calib(:,1),calib(:,2));

%Plot Resampled Calibration Data
sh=subplot(2,2,3);
set(sh,'FontSize',6);
plot(freq,caldb)
axis([0 max(freq) -240 -190]);
title('Resampled Calibration Data','FontSize',6)
xlabel('Freq (Hz)','FontSize',6)
ylabel('dB re: 1V/uPa','FontSize',6)

%Convert Calibration Data to Units of uPa/Vp and Plot
calmicropascalpervolt=10.^((-caldb)./20); %uPa/Vp

sh=subplot(2,2,4);
set(sh,'FontSize',6);
plot(freq,calmicropascalpervolt)
axvals=axis;
axvals(1)=0;
axvals(2)=max(freq);
axis(axvals);
title('Resampled Calibration Data','FontSize',6)
xlabel('Freq (Hz)','FontSize',6)
ylabel('uPa/Volt','FontSize',6)

%********************************************************************************
% APPLY CALIBRATION DATA
%********************************************************************************
caltrans=trans.*calmicropascalpervolt;
singlecaltrans=singletrans.*calmicropascalpervolt;

%********************************************************************************
% CALCULATE CALIBRATED TIME DOMAIN SIGNAL FOR FIRST DATAFILE
%********************************************************************************
[caltime,singlecalsignal]=Freq2Time(freq,singlecaltrans);

%********************************************************************************
% CONVERT CALIBRATED SPECTRAL DATA TO dB UNITS
%********************************************************************************
calspecdb=20*log10(abs(caltrans)); %units dB:re 1 uPa

%********************************************************************************
% PLOT RESULTS
%********************************************************************************
figure(1)
sh=subplot(3,2,3);
set(sh,'FontSize',6);
plot(time,real(singlecalsignal));

axvals=axis;
axvals(1)=min(time);
axvals(2)=max(time);
axis(axvals);
disp(['Min Time = ' num2str(min(time)) ':  Max Time = ' num2str(max(time))]);

title(caltimeplottitle,'FontSize',6);
xlabel('Time (sec)','FontSize',6);
ylabel('Pressure (uPa)','FontSize',6);
maxlabel=['Max=' num2str(max(real(singlecalsignal)),'%0.5g') ' uPa'];
text(0.6,0.95,maxlabel,'sc','FontSize',6)


plotcalspecdb=calspecdb - 10*log10(df); %Normalizes to 1 Hz Bandwidth
sh=subplot(3,2,4);
set(sh,'FontSize',6);
plot(freq,plotcalspecdb)

axvals=axis;
axvals(1)=min(freq);
axvals(2)=max(freq);
axis(axvals);
disp(['Min Freq = ' num2str(min(freq)) ':  Max Freq = ' num2str(max(freq))]);

title(calfreqplottitle,'FontSize',6)
xlabel('Frequency (Hz)','FontSize',6)
ylabel('(dB re: uPa)','FontSize',6)
maxlabel=['Max (dB)    = ' num2str(max(plotcalspecdb))];
text(0.6,0.95,maxlabel,'sc','FontSize',6)

sh=subplot(3,2,5);
set(sh,'FontSize',6);
plot(time,real(singlecalsignal));

axvals=axis;
axvals(1)=params.mintime;
axvals(2)=params.maxtime;
axis(axvals);

title(caltimeplottitle,'FontSize',6);
xlabel('Time (sec)','FontSize',6);
ylabel('Pressure (uPa)','FontSize',6);

sh=subplot(3,2,6);
set(sh,'FontSize',6);
plot(freq,plotcalspecdb)

axvals=axis;
axvals(1)=params.minfreq;
axvals(2)=params.maxfreq;
axis(axvals);

title(calfreqplottitle,'FontSize',6)
xlabel('Frequency (Hz)','FontSize',6)
ylabel('(dB re: uPa)','FontSize',6)
minind=max([1 min(find(freq>=params.minfreq))]);
maxind=min([length(freq) max(find(freq<=params.maxfreq))]);
maxlabel=['Max (dB)    = ' num2str(max(plotcalspecdb(minind:maxind)))];
text(0.6,0.95,maxlabel,'sc','FontSize',6)
