clc;clear;close all;

file = input('Enter name of wav file  ', 's');

counter = 0;

    for test_number2 = 0:9
        for test_number3 = 1:4
            counter = counter + 1;
            file2 = sprintf('%d_%d.wav',test_number2, test_number3);
            test_energy = energy_per_frame(file);
            test_against = energy_per_frame(file2);
            distance(counter) = dtw(test_energy,test_against);

        end

    end

matches = zeros(1,length(distance));    
flag = 0;
counter = 0;
    
for k=1:length(matches)
    
   if distance(k)<495
       matches(k) = 1;
       flag = flag + 1;
   end
    
end

if flag == 0
    sprintf('the entered word is not similar enough to any in the database')
end

counter2 = 0;

if flag > 1
    
    for x=1:length(matches)
            if (matches(x)==1)
                counter2 = counter2 + 1;
                match_values(counter2) = floor(x/4);
            end   
    end
    
if max(match_values)~= 0
    
    for y=1:length(match_values)
           
                counter = counter + 1;
                match_values2(counter) = match_values(y);
           
    end
    
    match_values2 = match_values2/max(match_values2);
        
        if min(match_values2)<1
           
            sprintf('the entered word is similar to more than one word in the database')
            
        end
        
        if min(match_values2)==1
           
            flag = 1;
        end 
end

if max(match_values) == 0
    flag = 1;
end
    
end

if flag==1
    [y index] = max(matches);
    output = floor(index/4);
    sprintf('the dectected number is %d',output)
end

