clear all; close all;
[NameHarv1,PathHarv1] = uigetfile('*.bin','Vyber soubor s daty z obvodu harvesteru', 'D:\Dropbox\Bakalarka\mereni\vibrace\PROUD');
if( ~isequal(NameHarv1,0))                                                                                  
    [cas1, procPlusV, zn1, procOut, zn2, procReady, zn3] = textread(strcat(PathHarv1, NameHarv1),'{%u: %f%1s, %f%1s, %f%1s}, ');
    cas11 = unique(uint16(cas1./10));
    ind = 1;
    souc = zeros(3,1);
    prum = zeros(3,1);
    inc = 1;
    for inc = 1:numel(cas11)
        for inc1 = 1:numel(cas1(uint16(cas1./10 - 0.5) == cas11(inc)))               % do mean from values during one second
            souc(1) = souc(1) + procPlusV(ind)*0.036;
            souc(2) = souc(2) + procOut(ind)*0.036;
            souc(3) = souc(3) + procReady(ind)*0.036;
            ind = ind + 1;
        end;
        prum(1) = souc(1)/numel(cas1(uint16(cas1./10) == cas11(inc)));
        prum(2) = souc(2)/numel(cas1(uint16(cas1./10) == cas11(inc)));
        prum(3) = souc(3)/numel(cas1(uint16(cas1./10) == cas11(inc)));
        VoltOut(inc) = prum(1);
        Out(inc) = prum(2);
        Ready(inc) = prum(3);
        souc = zeros(3,1);
        prum = zeros(3,1);
    end;    
  
%        % 30Hz1g15min / in this case discharge capacitor was forgotten,
%         for i = 1:numel(cas11)                                                           % offset must be compensated
%             VoltOut(i) = VoltOut(i) - 0.9;
%             Out(i) = Out(i) - 0.9;
%             Ready(i) = Ready(i) - 0.9;
%         end
    medVolt = medfilt1(VoltOut,20);                      % median filter to get rid of spikes
    medOut = medfilt1(Out,10);
    medReady = medfilt1(Ready,10);
    
    medVolt = medVolt(50:(end - 20));               % eliminate starting peaks
    medOut = medOut(50:(end - 20));
    medReady = medReady(50:(end - 20));
    cas11 = cas11(50:(end - 20));
    
    %***************Voltage charging during measurement
    Wt = double(zeros(numel(medVolt) - 1, 1));
    P = double(zeros(numel(medVolt) - 1, 1));
    matSum = double(zeros(numel(medVolt) - 1, 1));
    sumP = 0;
    for inc = 1:(numel(medVolt)-1)
        Wt(inc) = 0.5*(300e-6)*(medVolt(inc)^2);
        if inc > 1
            P(inc) = abs(Wt(inc) - Wt(inc-1))/double(cas11(inc) - cas11(inc-1));
            sumP = sumP + P(inc);
        end
    end
    'Prumerny vykon'
    sumP/(numel(medVolt) - 2)
    matSum(:) = sumP/(numel(medVolt) - 2);
    
    figure(100)
    [AX,AY] = plotyy(double(cas11(1:(numel(medVolt)-1)))./60.0, medVolt(1:numel(medVolt)-1),double(cas11(1:(numel(medVolt)-1)))./60.0, P.*1e6, 'plot');
%     graphical modifications
    set(get(AX(1),'Ylabel'),'String','U_{NAB}[V]');
    set(get(AX(1),'Ylabel'),'FontSize',16);
    set(AX(1), 'FontSize',14)
    set(get(AX(2),'Ylabel'),'String','P [\muW]');
    set(get(AX(2),'Ylabel'),'FontSize',16);
    set(AX(2), 'FontSize',14)
    %title('Uout from EH300A [%]');
    axes(AX(2))
    hold on;
    plot(matSum.*1e6, 'Color',[1 0 0]);
    grid on;
    xl = xlabel('t (min)');
    set(xl, 'FontSize', 16);
    %********************
    figure(101)
    [AX] = plot(double(cas11)./60.0, medOut);
    %title('Uout from EH300A [%]');
    set(get(get(AX, 'Parent'), 'XLabel'),'FontSize',16);
    set(get(get(AX, 'Parent'), 'YLabel'), 'FontSize',16);
    set(get(AX, 'Parent'), 'FontSize',14);
    xlabel('t (min)');
    ylabel('U_{OUT} [V]');
    grid on

    figure(102)
    [AX] = plot(double(cas11)./3600, medReady);
    %title('Uready from EH300A [%]');
    set(get(get(AX, 'Parent'), 'XLabel'),'FontSize',16);
    set(get(get(AX, 'Parent'), 'YLabel'), 'FontSize',16);
    set(get(AX, 'Parent'), 'FontSize',14);
    xlabel('t (min)');
    ylabel('U_{READY} [V]');
    grid on;
else
    'Data harvesteru nebyla otevrena...'
end