%ASR2.M
%this script qualifies the training data into 17 bark spectrum normalized
%regions. 

clear all;
clc;
close all;
disp('Welcome to the Automated Speech Recognizer');
disp('This program should automatically recognize what digit is said in a .wav file');
disp('');
disp('It requires a set of training data, which is .wav files of digits zero through nine being said');
disp('Multiple sets of training can be loaded.');
disp('');
numSetsRefData=input('How many sets of reference data would you like to load?');
refDataLoc=cell(numSetsRefData,10);
refData=cell(numSetsRefData,10);
for k=1:numSetsRefData
    for m=0:9
        [Filename Pathname]=uigetfile('*.wav', ['Pick reference data for set ' num2str(k) ' digit ' num2str(m)]);
        if (Pathname == 0)
            error('Error.. No File Name Input');
        end;
        refDataLoc{k,m+1}=cat(2,Pathname,Filename);
    end;
end;
for k=1:numSetsRefData
    for m=0:9
        %Loads .wav files in
        [refData{k,m+1},fs(k,m+1),bits(k,m+1)]=wavread(refDataLoc{k,m+1});
    end;
end;
sameSampleRate=find(fs ~= fs(1,1));
if (length(sameSampleRate) ~= 0)
    error('Error... Please Make Sure Reference Data Sample Rates Are Consistent.');
end;
sameBitRate=find(bits ~= bits(1,1));
if (length(sameBitRate) ~= 0)
    error('Error... Please Make Sure Reference Data Bit Rates Are Consistent.');
    return;
end;
fs=fs(1,1);
if (fs < 8000)
    error('Error... Sampling rate must be at least 8kHz');
end;
bits=bits(1,1);
%Make each frame approx. 20ms with 50% overlap
windowSize=ceil(fs*20*10^-3);
win=window(@hamming,windowSize);
%Take the FFT of each frame
for k=1:numSetsRefData
    for m=0:9
        [refDataFFT{k,m+1},F,T]=spectrogram(refData{k,m+1},win,ceil(length(win)/2),windowSize,fs);
        refDataE{k,m+1}=abs(refDataFFT{k,m+1}).^2;
        totalEnergy(k,m+1)=sum(sum(refDataE{k,m+1},1));
        %Normalize the Power in Each Reference Set
        refDataE{k,m+1}=refDataE{k,m+1}./totalEnergy(k,m+1);
    end;
end;
%Create Bark Spectrum Bands
barkUpperLims=[100 200 300 400 510 630 770 920 1080 1270 1480 1720 2000 2320 2700 3150 3700];
for k=1:17
    indexLimBark(k) = find(F>=barkUpperLims(k),1);
end;
%Combine frequencies into 17 level bark spectrum
%initialize cells for combined energy data
refDataENew=cell(numSetsRefData,10);
for k=1:numSetsRefData
    for m=0:9
        refDataENew{k,m+1}=zeros(17,size(refDataE{k,m+1},2));
    end;
end;
for k=1:numSetsRefData
    for m=0:9
        indexLowerBark=1;
        for p=1:17
            while (indexLowerBark <= indexLimBark(p))
                refDataENew{k,m+1}(p,:)=refDataENew{k,m+1}(p,:)+refDataE{k,m+1}(indexLowerBark,:);
                indexLowerBark=indexLowerBark+1;
            end;
        end;
    end;
end;
clear refDataE;
refDataE=refDataENew;
disp('');
saveData=input('Would You Like To Save Your Analyzed Reference Set For Simpler Startup Using ASR1_Preload? [y/n]','s');
switch lower(saveData)
    case 'y'
        fileName=input('Please type in filename you would like to save to: ','s');
        save(fileName,'refDataE','fs','bits','-MAT');
    otherwise
end;
ASR1_main(refDataE,fs,bits);

    
   
    