function[output] = FindMyClusters(file)
% Funkce slouzi k prirazeni jednotlivych cyklu zaznamu do shluku.
%
% Vstup: file - cesta k jednomu objektu obsahujici data
%               napr.: 'C:\Users\zanet\Documents\BP\data\00000A.json'
%
% Vystup: output - vektor se zarazenim jednotlivych cyklu do shluku
%
%% load
 load('clusters_new.mat')
 load('center.mat')
 load('scale.mat')
 
 file = jsondecode(fileread(file));
 
 attributes = FindAttributes(file);

 eucleid_dist  = []; 
 output = [];
 
 %% Means of attributes instead of NaN attributes 
 
 for i = 1 : size(attributes, 1) %11 rows - attributes
    for j = 1 : size(attributes, 2) % 100 columns - 100 files
        if  isnan(attributes(i, j))
            attributes(i, j) =  mean(attributes(i, :), 'omitnan');
            if isnan(attributes(i, j)) % if there is no values for mean
                attributes(i, j) = 0;
            end
        end
    end
 end
 
%% standardization
% used for clustering:
% C - center
% S - scale

attributes = normalize(attributes','center',C,'scale',S);
attributes = attributes';

%% clusters for beats
% minimal eucledian distance
 
 for j = 1 : size(attributes, 2)
     for i = 1 : size(clusters, 1)
           eucleid_dist(i) = sum((attributes(: ,j)' - clusters(i, :)).^2);
     end
     [~, idx] = min(eucleid_dist);
     output(j) = idx;
 end
file.diagnosis.name

 CS5 = file.data(13).content';
 CS4 = file.data(14).content';
 CS3 = file.data(15).content';
 CS2 = file.data(16).content';
 CS1 = file.data(17).content';
 all_CS = [CS5; CS4; CS3; CS2; CS1]';

fvz = file.measurement.samplingFrequency; %sampling frequency
 pom = 0;
 t = [];
 for k = 1 : length(all_CS) 
    pom = pom + (1/fvz);
    t = [t, pom];   %timeline
end

all_CS = all_CS - mean(all_CS); % subtraction of the effect of the DC component
CS3 = CS3 - mean(CS3);

[~,~,beginning,ending]  = Extraction(file);

vector = zeros(1, size(CS3, 2));
for i = 1:length(beginning)-4 % first five beats are not clasiffied
    vector(beginning(i+4) : ending(i+4)) = output(i);
end

list_of_ones = zeros(1, size(CS3, 2));
list_of_ones(vector == 1) = 1;

list_of_twos = zeros(1, size(CS3, 2));
list_of_twos(vector == 2) = 1;

list_of_threes = zeros(1, size(CS3, 2));
list_of_threes(vector == 3) = 1;

list_of_fours = zeros(1, size(CS3, 2));
list_of_fours(vector == 4) = 1;

list_of_fives = zeros(1, size(CS3, 2));
list_of_fives(vector == 5) = 1;

figure 
plot(t, CS3, 'Color',[0.3 0.3 0.3])
hold on
plot(t, list_of_ones*max(CS3), 'b')
plot(t, list_of_twos*max(CS3), 'r')
plot(t, list_of_threes*max(CS3), 'g')
plot(t, list_of_fours*max(CS3), 'k')
plot(t, list_of_fives*max(CS3), 'c')

title ('Signál s vyznačenou síňovou aktivitou')
legend('CS3', '1', '2', '3', '4', '5')
xlabel('Čas(s)')
ylabel('Amplituda(mV)')


