function avgvalue = dtw_avg(test_number)

shortest = 100;
counter = 0;

%find out which of the 4 samples per number is the shortest
%the location of the shortest one is saved in the index vector
for test_number1 = 0:9
        for test_number2 = 1:4
            file = sprintf('%d_%d.wav',test_number1, test_number2);
            test_energy = energy_per_frame(file);
            counter = counter + 1;
            [h w] = size(test_energy);
            
            if h<shortest
                
                shortest = h;
                index(test_number1+1) = counter;
                 
            end

        end
        counter = 0;
        shortest = 100;
end

%cycle through four files of each number and make them all have the 
%same number of frames as the shortest one (calculated above) using DTW
        
    shortest = index(test_number+1);
    file = sprintf('%d_%d.wav', test_number, shortest);
    reference = energy_per_frame(file);
    [h w] = size(reference);
    
    for test_number2 = 1:4 %change upper limit to 4 after testing
       
        if test_number2 == shortest
            test_3d_temp(:,:,test_number2) = reference;
            test_number2 = test_number2 + 1;
        end
        
        if test_number2 ~= shortest
        file2 = sprintf('%d_%d.wav',test_number,test_number2);
        test = energy_per_frame(file2);
        [d dmatrix] = dtw_part2(test, reference);
        [h2 w2] = size(dmatrix);
        r = 1;
        c = 1;
            
 
            while c<w2 && r<h2        
              [tempmin min_index] = min([dmatrix(r+1,c+1), dmatrix(r+1,c), dmatrix(r,c+1)]);
                if min_index == 1
                    r = r+1;
                    c = c+1;
                end

                if min_index == 2
                    r = r+1;
                    c;
                end
                
                if min_index == 3
                    r;
                    c = c+1;
                    mod_ans = mod(c,2);
                    if mod_ans == 0
                        
                        [h3 w3] = size(test);
                        if h3 == h
                            r = h2;
                            c = w2;
                            test_3d_temp(:,:,test_number2) = test;
                        end
                        if h3~= h
                        new_row = (test(c, :) + test(c-1, :))/2;
                        test(c-1,:)= new_row;
                        test(c,:) = [];
                        [h3 w3] = size(test);
                        if h3 == h
                            r = h2;
                            c = w2;
                            test_3d_temp(:,:,test_number2) = test;
                        end
                        end
                    end
                end         
            end
            
            [l w d] = size(test_3d_temp);
            tempsum = 0;
            for i=1:d
               tempsum = tempsum + test_3d_temp(:,:,i);
            end
            
            avgvalue = tempsum/d;
            
            
        end
    end

 