ClearAll[vstupnidata]
f=50;
ClearAll[t];
R=5; (*Sum*)
zadani={{1,100,0.543},{2,0,-0.23},{3,0,0.5},{5,0,0.001},{7,0,0.02},{8,0,5},{11,0,-0.3}};(*Obsah harmonickych, kolikata, Amplituda, fazovy posun *)
u[{n_,Aa_,fi_}]:=Aa*Sin[2*Pi*f*n*t+fi];
vstup[tt_]:=Plus@@(u/@zadani)/.t->tt;

Δt=0.0002; (*vzorkovaci frekvence*)
vstupnidata=Table[vstup[t]+0*RandomReal[NormalDistribution[0.0,R]],{t,0,0.032,Δt}];
nxnom=(1/(Δt*f));
Length[vstupnidata]
vstupnidata//ListPlot
Print["doporuceny level je ",Round[Mean[vstupnidata],0.01]]

Vzorkovaci[vstupnidata_,level_,n_]:=Module[{delka,k,leveldolni,levelhorni,bodydolni,bodyhorni,bodyobou,pomList,pos,x1,x3,startpozx1,startpozx3,dataproX1,primkaX1,korenx1,dataproX3,primkaX3,korenx3,vstupnidataPos,listGroup,ll,pos1,pos3,bod1,bod3,Perioda},
delka=Length[vstupnidata];
vstupnidataPos=Table[{i,vstupnidata[[i]]},{i,1,delka-1,1}];
level;
(* nastaveni koeficienta nahoru dolu*)
k=40;

leveldolni=level-k;
levelhorni=level+k;
bodydolni=Select[vstupnidataPos,#[[2]]≥leveldolni&];
bodyhorni=Select[vstupnidataPos,#[[2]]≤ levelhorni&];
bodyobou=Intersection[bodydolni,bodyhorni];

fceNovebody[list_]:=Module[{Novebodyobou,pomlist},
Novebodyobou=list;
pomlist=Novebodyobou/.{x_,y_}-> x;
FindClusters[pomlist]
];

fceSortTakeVstupnidata[list_]:= Replace[list,x_?NumericQ:> {x,Take[vstupnidata[[x]]]},2];

listGroup=fceSortTakeVstupnidata[fceNovebody[bodyobou]];


x1=listGroup[[1]]/.{x_,y_}:>y;
x3=listGroup[[3]]/.{x_,y_}:>y;

startpozx1=Nearest[x1,level]/.{x_}-> x;
 startpozx3=Nearest[x3,level]/.{x_}-> x;

pos1=Position[x1,startpozx1]/.{{x_}}-> x;
pos3=Position[x3,startpozx3]/.{{x_}}-> x;

bod1=listGroup[[1]][[pos1]];
bod3=listGroup[[3]][[pos3]];

dataproX1=Table[listGroup[[1]][[i]],{i,pos1-n,pos1+n}];
dataproX3=Table[listGroup[[3]][[i]],{i,pos1-n,pos1+n}];

primkaX1= Fit[dataproX1, {1,x},x];
korenx1=Solve[primkaX1==level,x]/.{{x_-> y_}}-> y;

primkaX3= Fit[dataproX3, {1,x},x];
korenx3=Solve[primkaX3==level,x]/.{{x_-> y_}}-> y;
Perioda=(korenx3-korenx1)*Δt;

plot1=Plot[{primkaX1,level},{x,0,700},PlotStyle->{Red,Thick}];
plot3=Plot[{primkaX3},{x,0,700},PlotStyle->Red];
{primkaX1,primkaX3,korenx1,korenx3,Perioda,(1/Perioda),Show[vstupnidata//ListPlot,plot1,plot3,PlotRange-> {{0,200},{-100,100}},AxesLabel->{RawBoxes[RowBox[{"n"," ","-"," ",RowBox[{"vzorek"," ","[","-","]"}]}]],HoldForm["Napětí [V]"]},LabelStyle->{FontFamily->"Arial",11,GrayLevel[0]},ImageSize->Medium,GridLines->None]}
]
pom=Vzorkovaci[vstupnidata,10,3](*na vystupu rovnice dvou primek, koreny rovnic, perioda, frekvence*)

Statisticka[vstupnidata_]:=Module[{delka,pom1,pom2,pom3,pom4,pom5,pom6,pom7,pom8,SprOkno},

(*spocita delky oken     -  delky oken   *)
   fceOkna[]:=Module[{start,konec,listOken},
delka=Length[vstupnidata];
start=Round[0.58*delka];
konec=Round[0.67*delka];

listOken=Range[start,konec]
];


(* spocita indexy{Start_, Konec_}, pro danou delku okna   -   indexy*)
fceOkno[x_]:=Module[{pom,start,konec},
delka=Length[vstupnidata];
start=Range[delka-x];
konec=Range[x,delka];

pom=Table[{start[[i]],konec[[i]]},{i,1,delka-x}]
];


(*spocita {Xstr,delkaOkna} pro ruzne kombinace indexy    -   {XStr, delkaOkna}*)
fceXstr[{start_,konec_}]:=Module[{pomlist,delkaOkna,Xstr},
pomlist=Take[vstupnidata,{start,konec}];
(*delka okna, +1 kvuli indexum,  pro okno (1,2,3,4) delka je 4 ale 4-1=3  *)
delkaOkna=konec-start+1;

{Xstr=Mean[pomlist],
delkaOkna}
];


(*spocita Sigmu pro dannou delku okna*)
fceSigma[delkaOkna_]:=Module[{listXstrPatternDelka,Sigma},
listXstrPatternDelka=Cases[pom3,{x_,delkaOkna}] /.{x_,y_}->x;
Sigma=Mean[(listXstrPatternDelka^2)]-((Mean[listXstrPatternDelka])^2);

Sigma
];

(* spocita delky oken*)          
(* pom1 je list s delkami oken*)       
pom1=fceOkna[];

(* spocita indexy{Start_, Konec_}, pro danou delku okna   -   indexy{Start_,Konec_} pro vsechny delky oken*)
(* pom2 je list se vsemi indexy pro vsechny delky oken*)                                
pom2=Flatten[fceOkno/@pom1,1];     
 
(*spocita {Xstr,delkaOkna} pro ruzne kombinace indexy    -   {XStr, delkaOkna}*)
(*pom3 je list s Xstr a delkou okna pro danou delku*)
pom3=fceXstr/@pom2;

(* udela graf {pozice okna, Xstr} pozice okna - pozice vypocetniho okna*)
(*Xstr se stejnou delkou okna do jednoho Case *)
(*pom4 je list ve kterem listy Xstr se stejnou delkou { {},{},[},...,{} }  *)
pom4=Cases[pom3,{x_,#}]&/@fceOkna[] /.{x_,y_}->x;

(*spocita Sigmu pro vsechny delky oken*)
(*pom 5 je list se Sigmami pro vse delky oken*)
pom5=fceSigma[#]&/@pom1;

(* hleda minimum (Sigma^2)  a vezme index - pak z listu s okny vezme okno s danym indexem ->  Spravne okno *)
SprOkno=Take[pom1,Flatten[Position[pom5,Min[pom5]]]];

(* udela list    {{delka Okna, (Sigma^2)},{(Subscript[l, 2]),((Subscript[Sigma, 2])^2)},....,{(Subscript[l, n]),((Subscript[Sigma, n])^2)}}   *)
pom6=Table[{pom1[[i]],pom5[[i]]},{i,Range[1,Length[pom1]]}] ;

(*udela sort pres (Sigma^2)*)
pom7=Sort[pom6,#1[[2]]<#2[[2]]& ];

(*udela list s 3 nejmensi Sigma    *)
(* pom8 je list  {{delka,(Sigma^2)},{},{}}  *)
pom8=Take[pom7,3];



(*Spocita minimum pres 3 body a skoreguje frekvecni *)
fceMinimum[]:=Module[{pomlist,TVr,SigmaVr,x1,x2,x3,y1,y2,y3,frekvence},
   pomlist=Sort[pom8];
x1=Take[pomlist[[1]][[1]]];
x2=Take[pomlist[[2]][[1]]];
x3=Take[pomlist[[3]][[1]]];
y1=Take[pomlist[[1]][[2]]];
y2=Take[pomlist[[2]][[2]]];
y3=Take[pomlist[[3]][[2]]];


TVr=((y1+2x2*y1-4*x2*y2-y3+2*x2*y3)/(2*(y1-2y2+y3)));

SigmaVr=(((y1^2)+(((-4*y2+y3))^2)-2*y1*(4*y2+y3))/(8*(y1-2*y2+y3)));

frekvence=(1/(TVr*0.0002))
];

(*Grafika*)
{ListPlot[pom4,AxesLabel->{"poziceOkna","Xstr"}],
ListPlot[pom6,AxesLabel->{"perioda okna
","(Sigma^2)"}],(1/(fceMinimum[])),
fceMinimum[]
}
];

Statisticka[vstupnidata](*graf stredni hodnoty, graf sigma a perioda okna, perioda, frekvence *)