function rozpoznanitonu(recObj)

if ~exist('recObj','var')
    load nahravka.mat
    recObj=rec;
    clear rec;
end

close all
% Store data in double-precision array.
myRecording = getaudiodata(recObj);

% Plot the waveform.

close all;
f=figure;
set(f,'units','centimeters');
s=2*[21 10.5];
set(f,'position',[1.5 1.2 s]);
set(f,'papersize',s);

subplot(3,2,1)
plot(myRecording);
title('Nezpracovany zvukovy signal')
xlabel('Vzorkovani v case');
ylabel('Zvukovy signal');

subplot(3,2,2)
wind=1e3;
S=spectrogram(myRecording,wind);
Y=linspace(0,5000,size(S,1));
time=recObj.TotalSamples/recObj.SampleRate;
X=linspace(0,time,size(S,2));
pcolor(X,fliplr((Y)),flipud(log(abs(S))))
ylim([0 3*1500])

title('Spektrogram, skupiny po 1000 vzorcich, frekvenèní báze')
xlabel('cas, s')
ylabel('frekvence, 10^y Hz')
shading interp

% zv
S(Y<100,:)=0;

[T tony]=tonebaze(Y');

for ii=1:size(S,2)
    sp=abs(S(:,ii));
    sp(sp<1.8)=0;
    if sum(sp)>0
        m=min(Y(sp>0));
        sp(Y>m+100)=0;
        [M,I]=max(sp);
        fr=Y(I);
        
        subplot(3,2,4)
        hold on
        plot(X(ii),fr,'xr')
        title('Dominantni frekvence')
        xlim([0 time])
        
        tt=T'*sp;
        [~,kk]=max(tt);
        fr=tony(kk);
        
        subplot(3,2,[5 6])
        klaviatura(fr)
%         pause(0.1)
    else
        subplot(3,2,[5 6])
        klaviatura
    end
    subplot(3,2,3)
    plot(Y,abs(S(:,ii)))
    ylim([0,max(max(abs(S)))])
    title('Lokalni spektrum zvuku')
    drawnow
end


function [T tony]=tonebaze(Y)

N=55;
T=zeros(size(Y,1),N);

F=174.6; %Frekvence F3
tony=F*2.^((0:N)/12);
fmax=max(Y);

for ii=1:50
    for jj=1:ceil(fmax/tony(ii))
        T(:,ii)=T(:,ii)+exp(-jj/10)*exp(-(Y-jj*tony(ii)).^2/1000);
    end
    T(:,ii)=T(:,ii)/sum(T(:,ii));
end






