ClearAll["Global`*"];
SetDirectory[NotebookDirectory[]];
$HistoryLength=3;

vydejNx5[listDat_]:=Module[{k1=0.5,aclist,x1,y1,x2,y2,x3,y3,Δmozne=0.1,mozne,start,zkrot,krok,fce,nx,f,sd2,nSampl,ykvref,varmin,pomer},
sd2[list_]:=Module[{mean,mean2},mean=Mean[list];mean2=Mean[list*list];mean2-(mean^2)];
aclist=Prepend[Accumulate[listDat],0.];
nSampl=Length[aclist];
fce[nOkno_]:=Module[{rn,soucty,sigma},
rn=Range[nSampl-nOkno];
soucty=(aclist[[rn+nOkno]]-aclist[[rn]]);
sigma=(1./(nOkno^2))sd2[soucty];
{nOkno,sigma}
];
mozne=Range[Round[(1-Δmozne)*sr],Round[(1+Δmozne)*sr]];
start=fce/@{mozne[[1]],Round[0.5*(mozne[[1]]+mozne[[-1]])],mozne[[-1]]};

zkrot[n_]:=If[n<mozne[[1]],mozne[[1]],If[n>mozne[[-1]],mozne[[-1]],Round[n]]];

krok[body:{{x1_,y1_},{x2_,y2_},{x3_,y3_}}]:=Module[{nb,yka,miny},
yka=body/.{a_,b_}:>b;
miny=Min[yka];
nb=Which[miny==y2,{fce[Round[0.5*(x1+x2)]],fce[Round[0.5*(x3+x2)]]},
miny==y3,{fce[zkrot[x3+k1*(x3-x2)]]},miny==y1,{fce[zkrot[x1-k1*(x2-x1)]]}];
Sort[Take[Sort[Union[nb,body],#1[[2]]<#2[[2]]&],3],#1[[1]]<#2[[1]]&]
];

{{x1,y1},{x2,y2},{x3,y3}}=FixedPoint[krok,start];
nx=((y1+2 x2 y1-4 x2 y2-y3+2 x2 y3)/(2 (y1-2 y2+y3)));
ykvref=Total[((Take[listDat,Round[nx]])^2)];
varmin=-(((y1^2)+(((-4 y2+y3))^2)-2 y1 (4 y2+y3))/(8 (y1-2 y2+y3)));
pomer=(nx/sr);
{f=((fnom*sr)/nx),(varmin/ykvref),pomer}
];

FileNames[]

data=Import["13122018.csv"];

fsampling=1/Differences[data[[{1,2},1]]][[1]]
sr=0.02*fsampling;
kperSampl=2;
fnom=50.;
Tnom=(1/fnom);

ω=2.*Pi*fnom;

data[[;;,2]]//Length

res=vydejNx5[data[[;;,2]]]

frek=res[[1]]

simpson[Sin[t],{t,0,2Pi},500]

simpson[func_,{var_,T1_,T2_},n_]:=Module[{data},
data=func/.var->#&/@Range[T1,T2,(T2-T1)/n];
((T2-T1)/n)/3 Plus@@({1,4,1}.#&/@Partition[data,3,2])]

vint=Interpolation[data[[All,{1,2}]]];
iint=Interpolation[data[[All,{1,3}]]];

Clear[a1v,b1v,a1i,b1i,phasorI,phasorV]

a1v[i_]=2*frek*simpson[vint[t]*Cos[2*Pi*i*frek*t],{t,0,1./frek},500.];
b1v[i_]=2*frek*simpson[vint[t]*Sin[2*Pi*i*frek*t],{t,0,1./frek},500.];

phasorV[i_]=I*a1v[i]+b1v[i];

a1i[i_]=2*frek*simpson[iint[t]*Cos[2*Pi*i*frek*t],{t,0,1./frek},500.];
b1i[i_]=2*frek*simpson[iint[t]*Sin[2*Pi*i*frek*t],{t,0,1./frek},500.];
phasorI[i_]=I*a1i[i]+b1i[i];

Plot[{vint[t],Im[phasorV[1]*Exp[I*2*Pi*frek*t]]},{t,0,0.02}]

S=phasorV[#]*Conjugate[phasorI[#]]/2&/@Range[50]//Total;

Print["Cinny vykon: ",Re[S]]
Print["Jalovy ",Im[S]]

frek*simpson[(iint[t]*vint[t]),{t,0./frek,1./frek},1000.]