function [ extrema] = FindLocalExtrema( data, threshold, scale )
% Hlada lokalne extremy v matici data
% data - vstupne data
%  threshold je prah pri hladani lokalnych extremov
% scale - urcuje velkost klzaveho okna - sliding window

% znormujem data
rdataMax = data / max(data); %matica znormovana na max
rdataMin = (data / min(data)); %matica znormovana na min

winmax = zeros(size(data));
winmin = zeros(size(data)); %dve nulove matice

%hlada extremy v znormovanej matici cez sliding window
for i = 1+scale:size(data,1)-scale %cyklus od zaciatok +scale az do koniec -scale
    winmax(i) = max(rdataMax(i-scale:i+scale));
    winmin(i) = max(rdataMin(i-scale:i+scale)); 
end

maxima = zeros(size(data)); 
minima = zeros(size(data));

% najde lokalne minimum a maximum
ii = (1+scale:size(data,1)-scale)';
maxima(ii) = rdataMax(ii) >= threshold & rdataMax(ii) >= winmax(ii);
minima(ii) = rdataMin(ii) >= threshold & rdataMin(ii) >= winmin(ii);
%vysledok su matice nul a jednotiek

% skombinuju sa maxima a minima
extrema = maxima + minima; %spocita mi to tie matice 
minima=find(minima)
maxima=find(maxima)