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

ClearAll[vydejNx5];
vydejNx5[{{listDat_,sr_},{ksr2_,ksr1_}}]:=Module[{k1=0.5,aclist,x1,y1,x2,y2,x3,y3,mozne,start,zkrot,krok,fce,nx,f,sd2,nSampl,ykvref,varmin,pomer,fix,sortFix,ndat,flnx,cenx,hfl,hce,ynx},
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[[#+nOkno]]-aclist[[#]]&/@rn);
sigma=(1./1)sd2[soucty];(*zde zmena, bylo (1./(nOkno^2))*)
{nOkno,sigma}
];
mozne=Range[Round[(ksr1^(-1))*sr],Round[(ksr2^(-1))*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]]&]
];

fix={{x1,y1},{x2,y2},{x3,y3}}=FixedPoint[krok,start];
sortFix=Sort[fix,#1[[2]]<#2[[2]]&];
nx=If[y1>y2&&y3>y2,((y1+2 x2 y1-4 x2 y2-y3+2 x2 y3)/(2 (y1-2 y2+y3))),sortFix[[1,1]]];

varmin=If[y1>y2&&y3>y2,-(((y1^2)+(((-4 y2+y3))^2)-2 y1 (4 y2+y3))/(8 (y1-2 y2+y3))),sortFix[[1,2]]];
pomer=(nx/sr);
ndat=1.*Length[listDat];
flnx=Floor[nx];
cenx=Ceiling[nx];
hfl=listDat[[flnx]];
hce=listDat[[cenx]];
ynx=-((flnx hce-cenx hfl)/cenx)+((hce nx)/cenx);
ykvref=(1/nx)*(Plus@@(((#)^2)&/@Take[listDat,flnx])+(ynx^2)*(nx-flnx));

{((Abs[varmin])/(Abs[ykvref]+(10^(-8)))),N@pomer}
];

(*FSCT Fourier Sine cosine transform  returns coeficicents*)
FSCT[function_,f_,k_,T1_,T2_]:={2*f*Total@(function*Cos[2*Pi*f*k*t]*0.0001/.t->#&/@Range[T1,T2,0.0001]),2*f*Total@(function*Sin[2*Pi*f*k*t]*0.0001/.t->#&/@Range[T1,T2,0.0001])}
(*{2*f*NIntegrate[function*Cos[2*Pi*f*k*t],{t,T1,T2},AccuracyGoal->5],2*f*NIntegrate[function*Sin[2*Pi*f*k*t],{t,T1,T2},AccuracyGoal->5]}*)
PhaseShiftOfFundamental[function_,f_,Tmesurement_]:=Module[{Tperiod,timeIntervals,fourierCoeficients,phaseshift},
Tperiod=(1./f);
timeIntervals=Table[{Tperiod*i,Tperiod(1+i)},{i,0,(Tmesurement/Tperiod)-1}];
fourierCoeficients=FSCT[function,f,1,#[[1]],#[[2]]]&/@timeIntervals;
(*ted je to spravne( podminka b > 0) *)
phaseshift[{a_,b_}]:=N[ArcTan[a/b]*(180/Pi)]/;b≥0;
phaseshift[{a_,b_}]:=N[180+ArcTan[a/b]*(180/Pi)]/;b<0;
phaseshift/@fourierCoeficients
]

PhaseShiftOfFundamental[Sin[2*Pi*50*t-7Pi/6.+0.01],50.,0.1]

Plot[Sin[2*Pi*50*t-7Pi/6.+0.01]-Sin[2*Pi*50*t+%[[1]]*Pi/180.],{t,0,0.1}]

samplerate=0.02*12500;

PhaseShiftOfSignal[dataInput_,samplerate_,freqVariance_,fsampling_]:=Module[{int,Tperiod,Kperiod,data},
data=dataInput/.{a_,b_}:>{a-dataInput[[1,1]],b};
int=Interpolation[data][t];
{variance,Kperiod}=vydejNx5[{{data[[Round[Length[data]/2];;Round[Length[data]/2]+Round[2*samplerate],2]],samplerate},{0.95,1.05}}];
Tperiod=Kperiod*samplerate/fsampling;
PhaseShiftOfFundamental[int,((1.*freqVariance)/Tperiod),data[[-1,1]]]
]

PlotPhaseShiftForDifferentFrequencies[ARCdata_,samplerate_,fsampling_]:=Module[{res},
res=PhaseShiftOfSignal[ARCdata,samplerate,#,fsampling]&/@Range[0.95,1.05,0.01];
Show[{ListLinePlot[res,PlotLegends->(ToString[#]<>"*f"&/@Range[0.95,1.05,0.01])],
ListPlot[res[[6]],PlotStyle->{Black,PointSize[0.02]}]}]
]

data=Import["./Data/scope_1.csv"][[3;;]];
data=data/.{a_,b_,c_,d_,f_}:>{a-data[[1,1]],b,c,d,f};
hfdata=data[[;;,{1,2}]];
hfdata=Select[hfdata,NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];
lowpassdata=data[[;;,{1,5}]];
lowpassdata=Select[lowpassdata,NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];

ListLinePlot[data[[;;,5]]]

fsample=1/Differences@data[[{2,3},1]]

phaseshiftsalll=PhaseShiftOfSignal[lowpassdata,0.02*fsample[[1]],1.,fsample[[1]]];

ListLinePlot[Flatten@phaseshiftsalll,PlotRange->Full,PlotStyle->Blue,Frame->True,FrameStyle->Directive[13],GridLines->Full,FrameLabel->{"(Subscript[n, period]) (-) ","φ(°) "},ImageSize->Medium]