clear
clc
clf


dataFilename = 'NBII068.DAT';  %input filename
wavOutputFilename = '/user/rls/Desktop/audioFile1.wav'; %output wav filename
%makeWavFile=1;                   %do you want to make a wav file?
makePlots=1;                     %do you want the plot outputted?


fileID = fopen(dataFilename,'r');   %Open file for reading
RawData = fread(fileID,'uint16');   %Read file into array of 16 bit cells

correctedData=RawData;
numMarkers=0;

%Data marker cataloging and correction
for i=1:length(correctedData)
    if(correctedData(i)>1024)
        correctedData(i)=correctedData(i)-4096;
        numMarkers=numMarkers+1;
        markerPoints(numMarkers)=i;
    end
end


voltageData = 3.3*correctedData ./ 1024;

j = 1;
k = 1;
for i=1:length(voltageData)
 if(mod(i,2))
     voltageDataR(j) = voltageData(i);
     j = j+1;
 else
     voltageDataL(k) = voltageData(i);
     k = k+1;
 end

end

frequencyDataL=fft(voltageDataL-mean(voltageData));
L1=length(frequencyDataL);

frequencyDataR=fft(voltageDataR-mean(voltageDataR));
L2=length(frequencyDataR);

if (makePlots~=0)
    
    subplot(2,2,1)
    plot(voltageDataL); 
    title('Left Microphone Voltage');
    xlabel('Sample Number');
    ylabel('Relative Intensity');
    
    subplot(2,2,2)
    plot(voltageDataR); 
    title('Right Microphone Voltage');
    xlabel('Sample Number');
    ylabel('Relative Intensity');
    
    subplot(2,2,3)
    plot(((0:L1-1)/L1)*199450,abs(frequencyDataL));
    title('Left Microphone Frequency Spectrum');
    xlim([0 10^5]);
    xlabel('Frequency (Hz)');
    ylabel('Intensity');
    
    subplot(2,2,4)
    plot(((0:L2-1)/L2)*199450,abs(frequencyDataR));
    title('Right Microphone Frequency Spectrum');
    xlim([0 10^5]);
    xlabel('Frequency (Hz)');
    ylabel('Intensity');
end

normalizedData=(2*(correctedData-mean(correctedData)))./1024; %-1.0 to +1.0

% if (makeWavFile~=0)
%     audiowrite(wavOutputFilename,normalizedData,199450);
% end