% % spectrum of a 'Vzor'
% % [Features] = ...
% %     kNN_model_load('Spectr.mat');
% load('Ceps.mat')
%  [Features] = ...
%      kNN_model_load('Ceps.mat');
% 
% % 
% % 
% % 
% %  
% %  
% %  fs = 44100;
% %  T = 1/fs;
% %  
% % 
% %  
% % 
% % 
% % 
% %  
% %  
% %  
% %   %cepstrum
% %  Featuresceps{1} = rceps(Featuresceps{1}(:,1));
% %  Lengthofmodel = length(Featuresceps{1});
% %  frequency_bin = fs*(0:Lengthofmodel-1)/Lengthofmodel;
% % 
% %  frequency_bin = frequency_bin ./ max(abs(frequency_bin));
% %  Time_axes_points = (0:Lengthofmodel-1)*T;
% %  subplot(4,2,2)
% %  plot(frequency_bin(1:end/25),Featuresceps{1}(1:end/25))
% % 
% %  xlabel('Qfreq[sec]');
% %  ylabel('Power');
% %  %sample
% % [signal1] = audioread('gunshot.wav');
% %  if (size(signal1,2)>1)
% %     signal1 = (sum(signal1,2)/2); 
% %  end
% %  Length_of_sample = length(signal1);
% %  
% %  Time_axes_pints_of_sample = (0:Length_of_sample-1)*T;
% %  coefs = rceps(signal1);
% %  coefs = coefs(:,1);
% %  frequency_bin_sample = fs*(0:Length_of_sample-1)/Length_of_sample;
% %  subplot(4,2,4)
% %  plot(frequency_bin_sample(1:end/2),coefs(1:end/2))
% %  xlabel('Qfreq[sec]');
% %  ylabel('power');
%  [Featuresaudio] = audioread('dogg.wav');
%  if (size(Featuresaudio,2)>1)
%      Featuresaudio = (sum(Featuresaudio,2)/2); 
%   end
% Featuresaudio 
%  [Features] = getDFT(Featuresaudio, 44100);

% % Enter a sound
[signal,fs] = audioread('dog_barking_11.wav');
 if (size(signal,2)>1)
     signal = (sum(signal,2)/2); 
  end
 %spectrum model
% %  fs = 44100; 
% %  T = 1/fs;
% %  L = length(Features);
% %  f = fs*(0:L-1)/L;
% %  t = (0:L-1)*T;
% % 
win = 0.31;
%win = 0.310090703;  %% length of a single bark match with the window and step
step = 0.1;
curPos = 1;
windowLength = (win * fs); 
Stepf = round(step * fs); 
d = zeros(1,1);
Length_signal = length(signal);
numberOfFrames =  floor((Length_signal-windowLength)/Stepf) + 1;
[Featuresaudio] = audioread('dogg.wav');
 if (size(Featuresaudio,2)>1)
     Featuresaudio = (sum(Featuresaudio,2)/2); 
 end
  Featuresaudio = Featuresaudio(1:windowLength);
% % Featuresaudio 
 [Features] = getDFT(Featuresaudio, 44100);
 
Ham = window(@hamming, windowLength);
for i = 1:numberOfFrames
    frame  = (signal(curPos:curPos+windowLength-1));
  %  frame  = frame .* Ham;
    Curframe{i} = getDFT(frame, fs);
    %Curframe{i} = mean(Curframe{i},2);
    Curframe{i} = Curframe{i};   % in case of transposing
   
    d(i) =  corr(Features,Curframe{i});
    curPos = curPos + Stepf;
   
end
 
 
     if all(d(:) < 0.39)
        fprintf('Dog wasnt found in any frame')
     else 
         idx  = find(d(:) > 0.40);
         %fprintf('Dog detected in frame number %d\n',idx(:))
         for i = 1:length(idx)
            idx(i) = idx(i) *0.1;
         end 
             idx = round(idx,2);
             fprintf('Dog detected at second %0.2f\n',idx(:))  
         end
 
 
 
 
% %  Features{1} =  Features{1}./max(abs(Features{1}))
% %  subplot(4,2,1)
% %  plot(f(1:end/20),Features{1}(1:end/20))
% %  xlabel('Frekvencia[Hz]');
% %  ylabel('Power');
% %  title('Vzor(Priemer 5 psıch štekotov)')
% %  hold on
% % 
% %  % % sample
% %  [signal] = audioread('gunshot.wav');
% %  % % convert to mono
% %  
% % if (size(signal,2)>1)
% %     signal = (sum(signal,2)/2); 
% % end
% % 
% % 
% %  Length_of_sample = length(signal);
% % 
% %  time_on_x = (0:Length_of_sample-1)*T;
% %  b = abs(fft(signal));
% %  b = b./ max(abs(b));
% % 
% %  freq_bin = fs*(0:Length_of_sample-1)/(Length_of_sample);
% %  subplot(4,2,3)
% %  plot(freq_bin(1:end/20),b(1:end/20))
% %  xlabel('Frekvencia[Hz]');
% %  ylabel('Power');
% %  
% %  title('Vzorka na porovnanie')
% % Similarityspectrum = corr(b,Features{1})
% % %SimilarityCepstrum = corr(coefs,Featuresceps{1})
% % 
% % 
% % 
% % 
% % if Similarityspectrum > 0.4
% % disp('Dog_detected');
% % else
% %     disp('Dog_was_not_ found');
% %  
% % end
 
 
 
 
 
 