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}];(*generace vstupnich dat*)
nxnom=(1/(Δt*f));
Length[vstupnidata]
vstupnidata//ListPlot
Print["doporuceny level je ",Round[Mean[vstupnidata],0.01]]
(1/(Δt))

(*parametry jsou Vstupni data, uroven, pocet bodu pro interpolaci *)
Metoda2[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;

k=40;(* nastaveni koeficienta nahoru dolu*)

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=Metoda2[vstupnidata,15,3](*na vystupu rovnice dvou primek, koreny rovnic, perioda, frekvence*)