clear all

Fs = 48000;
dT = 1/Fs;
Vref = 1.5;
LSB = Vref / (2^15 - 1);
overlapPercent = 50;

dirName = 'H:\DeploymentOct12-09\R2009-285-08-00-26\R00000\';
startFileNum = 5;
endFileNum = 307;
numFiles = endFileNum - startFileNum + 1;
fileSize = 20 * 60 * Fs; 
sectionSize = 2^20;
numSections = floor(fileSize / sectionSize);

segLength = 2^10; 

ADCData = zeros( fileSize, 1, 'int16');

hSpectrum = spectrum.welch('Hamming', segLength, overlapPercent);

tic
for fileNum=startFileNum:endFileNum
    fileName = 'D';
    temp = num2str(fileNum);
    for i=1:(6-length(temp))
        fileName = strcat(fileName, '0');
    end
    fileName = strcat(dirName, fileName, temp);
    
    fid = fopen(fileName);
    fprintf(1, 'Reading %s \n', fileName);
    ADCData = fread(fid, 'int16=>int16');
    fclose(fid);

    index = fileNum - startFileNum + 1;
    rms(index) = sqrt(mean(single(ADCData).^2));
    [histData(index,:), histBins] = hist(single(ADCData),11);
        
    if( 0 )
        for i=1:numSections
            pxxIndex = ((fileNum - startFileNum) * numSections) + i;
            dataStart = ((i - 1) * sectionSize) + 1;
            dataEnd = dataStart + sectionSize - 1;
            fprintf('Processing section %d of %d    Index = %d \n', i, numSections, pxxIndex);
            VData(i,:) = single(LSB * single(ADCData(i,:)));
            pxx = psd(hSpectrum, single(VData(dataStart:dataEnd)), 'Fs', Fs);
            Pxx(pxxIndex,:) = 10 * log10(pxx.Data);
        end
    end
end
toc

Vref = 1.5;
LSB = Vref / (2^15 - 1);

