function [y_cosinuses,rr,times, extrems,amplitudes,oscillations_min,oscillations_max]=serier2rr(y,window,doplot);
% находит минимумы и максимумы ряда и формирует рр-интервалы
if nargin<3
    doplot=1;
end
y_old=y;
y=sglazh(y,floor(window/10),1);
n=length(y);
i=1;
maximumindexes=[];
while (i<n-window)
    ywindow=y(i:i+window);
    curmax=max(ywindow);
    indexmax=find(ywindow==curmax);
    indexmax=indexmax(1)+i;
    if (abs((indexmax-i)-window/2)<window/6)
        maximumindexes=[maximumindexes,indexmax];
        i=i+floor(window/2);
    end
    i=i+1;
end

lenmaxtimes=length(maximumindexes);
minimumindexes=[];
i=1;
while (i<n-window)
    ywindow=y(i:i+window);
    curmin=min(ywindow);
    indexmin=find(ywindow==curmin);
    indexmin=indexmin(1)+i;
    if (abs((indexmin-i)-window/2)<window/6)
        minimumindexes=[minimumindexes,indexmin];
        i=i+floor(window/2);
    end
    i=i+1;
end

lenmintimes=length(minimumindexes);

[oscillations_min,oscillations_max]=create_all_extremums_data(y,maximumindexes,minimumindexes,window);

[times,extrems,amplitudes,mascenters2]=create_extremum_sequence2(y,maximumindexes,minimumindexes,window);

problems=find(extrems(2:length(extrems))+extrems(1:length(extrems)-1)~=0);
%for i=problems
    %times=[times(1:i-1),times(i+1:length(times))];
    %extrems=[extrems(1:i-1),extrems(i+1:length(extrems))];
    %amplitudes=[amplitudes(1:i-1),amplitudes(i+1:length(amplitudes))];
%end

    
rr_max=maximumindexes(2:lenmaxtimes)-maximumindexes(1:lenmaxtimes-1);
rr_min=minimumindexes(2:lenmintimes)-minimumindexes(1:lenmintimes-1);
lentimes=length(times);
rr_all=times(2:lentimes)-times(1:lentimes-1);
[amplitudes,times, extrems]=oscilations_linpred(amplitudes,times, extrems,rr_all,4);
if (doplot)
    figure;
    plot(y);
    set(gca,'XTick',maximumindexes);
    title('maximum');
    
    figure;
    plot(y);
    set(gca,'XTick',minimumindexes);
    title('minimum');
    
    figure;
    plot(y);
    set(gca,'XTick',times);
    title('all');
end
%var_rr=std(rr_all)

%figure;
%plot(rr_max,'r');
%hold on;
%plot(rr_min);
%plot(rr_all,'y');
%legend('rr max','rr min','rr all');


%rr=(sum(rr_max)+sum(rr_min))/(length(rr_max)+length(rr_min));
times=[1,times];
extrems=[0,extrems];
phase=0;
masphases=[];
masamplitudes=[];
mascenters=[];
amplitudes=[amplitudes(1),amplitudes];
lentimes=length(times);
for i=2:lentimes
    time_beg=times(i-1);
    time_end=times(i);
    extrem_beg=extrems(i);
    amplitude=amplitudes(i)/2; %abs(y(time_end)-y(time_beg));
    if isempty(mascenters)
        center=y(1)+amplitude*extrem_beg/2;
    else
        center=mascenters(length(mascenters))+amplitude*extrems(i)+amplitudes(i-1)*extrems(i-1)/2;%(y(time_end)+y(time_beg))/2;
    end
    timerr=time_end-time_beg;
    if extrem_beg<0
        phasebeg=phase;
        phaseend=phase+pi;
    else
        phasebeg=phase+pi;
        phaseend=phase+2*pi;
    end
    phasestep=pi/timerr;
    phaseotresok=phasebeg:phasestep:phaseend;
    if (length(phaseotresok)<timerr)
        phaseotresok=[phaseotresok ones(size(phaseotresok))*phaseend];
    end
    
    if (length(phaseotresok)>timerr)
        phaseotresok=phaseotresok(1:timerr);
    end
    
    amplotresok=ones(size(phaseotresok))*amplitude;
    if (i<=length(mascenters2))
        centerotresok=ones(size(phaseotresok))*mascenters2(i);%center;
    else
        centerotresok=ones(size(phaseotresok))*center;
    end
    masphases=[masphases,phaseotresok];
    masamplitudes=[masamplitudes,amplotresok];    
    mascenters=[mascenters,centerotresok];
    %length(masphases)
    %times(i)
    %mistaketime=length(masphases)-    times(i)
    
    
end

    %figure;
    %plot(y);
    %hold on;
    %plot(mascenters+masamplitudes);
    %plot(mascenters-masamplitudes);

if extrem_beg>0
    phasebeg=phase;
    phaseend=phase+pi;
else
    phasebeg=phase+pi;
    phaseend=phase+2*pi;
end

phasestep=pi/timerr;
phaseotresok=phasebeg:phasestep:phaseend;
amplotresok=ones(size(phaseotresok))*amplitude;
centerotresok=ones(size(phaseotresok))*center;
masphases=[masphases,phaseotresok];
masamplitudes=[masamplitudes,amplotresok];    
mascenters=[mascenters,centerotresok];


y_cosinuses=masamplitudes.*cos(masphases)+mascenters;

if (doplot)
    figure;
    plot(y);
    hold on;
    plot(y_cosinuses);
    %plot(y_old);
    title('cosinus approx');
    n_cut=min(length(y_old),length(y_cosinuses));
    %remains2=y_old(1:n_cut)-y_cosinuses(1:n_cut);
    %figure;
    %plot(remains2);
end
rr=rr_all;