Quiet@Remove["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 reterns coeficicents*)
FSCT[function_,f_,k_,T1_,T2_]:={2*f*NIntegrate[function*Cos[2*Pi*f*k*t],{t,T1,T2}],2*f*NIntegrate[function*Sin[2*Pi*f*k*t],{t,T1,T2}],(Sqrt[2*f*NIntegrate[function^2,{t,T1,T2}]])}
PhaseShiftOfFundamental[function_,f_,Tmesurement_]:=Module[{},
Tperiod=(1./f);
timeIntervals=Table[{Tperiod*i,Tperiod(1+i)},{i,0,(Tmesurement/Tperiod)-1}];
fourierCoeficients=FSCT[function,f,1,#[[1]],#[[2]]]&/@timeIntervals;
N[ArcTan[#[[1]]/#[[2]]]*(180/Pi)]&/@fourierCoeficients
]

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

SignalReconstruction[function_,f_]:=Module[{fourierCoeficients,Tperiod},
Tperiod=(1./f);
fourierCoeficients=(1/3.)FSCT[function,f,#,0,3*Tperiod]&/@Range[1,100];
Total@Table[(Sqrt[fourierCoeficients[[i,1]]^2+fourierCoeficients[[i,2]]^2])*Sin[2*Pi*i*f*t+ArcTan[fourierCoeficients[[i,1]]/fourierCoeficients[[i,2]]]],{i,1,Length[fourierCoeficients]}]+(1/Tperiod)*NIntegrate[function,{t,0,Tperiod}]
]

RMS50toRMS[data_,lowFreqdata_,samplingFrequency_]:=Module[{Tperiod,ss,samplerate,IntegralHFSQ,HFSQint,coefHFSQ,HFSquare,kperiod,DCcomp,boundary,sigmaHFdata,stdHFdata,dataHF,CalcSTD,medianHFdata,sigmaHFdatanorm,rmes,rms,dataraw,ampl,absVal,Len,intV,dataVoltage,VltvsAbs},
samplerate=0.02*samplingFrequency;(*samlerate for 50 hz signal *)
{ss,kperiod}=vydejNx5[{{lowFreqdata[[Round[Length[lowFreqdata]/2];;,2]],samplerate},{0.95,1.05}}];
Tperiod=kperiod*samplerate/samplingFrequency;
dataraw=Select[data[[;;,{1,5}]],NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];
ampl=Coef[dataraw,Tperiod];
absVal=(Sqrt[#[[1]]^2+#[[2]]^2])/(#[[3]]*(Sqrt[2]))&/@ampl;
dataVoltage=Select[data[[;;,{1,4}]],NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];
dataVoltage=dataVoltage/.{a_,b_}:>{a-dataVoltage[[1,1]],b};
intV=Interpolation[dataVoltage][t];
rmes=RMSCalc[intV,(1./Tperiod),dataVoltage[[-1,1]]];
(*rms=RMSvalues[datarow[[;;,2]],kperiod*0.02*samplingFrequency];
Len= Min[{Length[rms],Length[absVal]}];*)
VltvsAbs=Thread[{rmes[[2;;]],Abs@Differences[absVal]}];
{ListLinePlot[absVal,PlotRange->Full,PlotStyle->Blue,Frame->True,FrameStyle->Directive[13],GridLines->Full,ImageSize->Medium,FrameLabel->{"(Subscript[n, period])(-)","(Subscript[RMS, 50])/RMS"}],
ListLinePlot[Differences@absVal,PlotRange->Full,PlotStyle->Blue,Frame->True,FrameStyle->Directive[13],GridLines->Full,ImageSize->Medium,FrameLabel->{"(Subscript[n, period])(-)","diff((Subscript[RMS, 50])/RMS)"}],
ListLinePlot[rmes,PlotRange->Full,PlotStyle->Blue,Frame->True,FrameStyle->Directive[13],GridLines->Full,ImageSize->Medium],Show[ListPlot[VltvsAbs,PlotRange->{{0,0.2},Full},PlotRange->Full,PlotStyle->{PointSize[0.02],Blue},Frame->True,FrameStyle->Directive[13],GridLines->Full,ImageSize->Medium,FrameLabel->{"(Subscript[V, arc])(V)","diff((Subscript[RMS, 50])/RMS)"}],ListLinePlot[{{0,0.2},{0.2,0.2}},PlotStyle->Red]]}
]

(*The function calculates RMS valuse using rectangular integration*)
RMSvalues[data_,SamplesPerPeriod_]:=(Sqrt[((Total[ data[[#;;#+Round[SamplesPerPeriod]]]^2])/(SamplesPerPeriod*1.))])&/@Range[1,Length[data]-Round[SamplesPerPeriod],Round[SamplesPerPeriod]]

PlotRMSvalues[data_,SamplesPerPeriod_]:=ListLinePlot[RMSvalues[data,SamplesPerPeriod],PlotRange->Full,PlotStyle->Blue,Frame->True,FrameStyle->Directive[13],GridLines->Full,ImageSize->Medium]

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]),(Sqrt[f*Total@(function^2*0.0001/.t->#&/@Range[T1,T2,0.0001])])}

rms[function_,f_,T1_,T2_]:=(Sqrt[f*Total@(function^2*0.0001/.t->#&/@Range[T1,T2,0.0001])])

FourCoef[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) *)
fourierCoeficients
]

Coef[dataInput_,Tperiod_]:=Module[{int,Kperiod,data},
data=dataInput/.{a_,b_}:>{a-dataInput[[1,1]],b};
int=Interpolation[data][t];

FourCoef[int,(1./Tperiod),data[[-1,1]]]
]

RMSCalc[function_,f_,Tmesurement_]:=Module[{Tperiod,timeIntervals,fourierCoeficients,phaseshift},
Tperiod=(1./f);
timeIntervals=Table[{Tperiod*i,Tperiod(1+i)},{i,0,(Tmesurement/Tperiod)-1}];
fourierCoeficients=rms[function,f,#[[1]],#[[2]]]&/@timeIntervals;
(*ted je to spravne( podminka b > 0) *)
fourierCoeficients
]

Scope 0: vrtacka bez oblouku (rozbeh)

data0=Import["./Data/scope_0.csv"][[3;;]];
data0=data0/.{a_,b_,c_,d_,f_}:>{a-data0[[1,1]],b,c,d,f};
hfdata0=data0[[;;,{1,2}]];
hfdata0=Select[hfdata0,NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];
lowpassdata0=data0[[;;,{1,3}]];
lowpassdata0=Select[lowpassdata0,NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];

ListLinePlot[data0[[;;,{1,3}]],PlotRange->Full]

samplingFreq0=1/(data0[[5,1]]-data0[[4,1]])

{ss0,kPer0}=vydejNx5[{{lowpassdata0[[Round[Length[lowpassdata0]/2];;,2]],0.02*samplingFreq0},{0.95,1.05}}]

PlotRMSvalues[data0[[;;,4]],kPer0*0.02*samplingFreq0]

RMS50toRMS[data0,lowpassdata0,samplingFreq0]

Scope 1: vrtacka bez oblouku

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

ListLinePlot[data1[[;;,{1,4}]],PlotRange->Full]

samplingFreq1=1/(data1[[5,1]]-data1[[4,1]])

{ss1,kPer1}=vydejNx5[{{lowpassdata1[[Round[Length[lowpassdata1]/2];;,2]],0.02*samplingFreq1},{0.95,1.05}}]

PlotRMSvalues[data1[[;;,4]],kPer1*0.02*samplingFreq1]

RMS50toRMS[data1,lowpassdata1,samplingFreq1]

Scope 2: vrtacka + oblouk na konci

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

lowpassdata2=Select[data2[[;;,{1,3}]],NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];

ListLinePlot[data2[[-1000;;,{1,4}]],PlotRange->Full]

ListLinePlot[data2[[;;,{1,4}]],PlotRange->Full]

samplingFreq2=1/(data2[[5,1]]-data2[[4,1]])

{ss2,kPer2}=vydejNx5[{{lowpassdata2[[Round[Length[lowpassdata2]/2];;,2]],0.02*samplingFreq2},{0.95,1.05}}]

PlotRMSvalues[data2[[;;,4]],kPer2*0.02*samplingFreq2]

RMS50toRMS[data2,lowpassdata2,samplingFreq2]

Scope 4: LEDka bez oblouku

data4=Import["./Data/scope_4.csv"][[3;;]];
data4=data4/.{a_,b_,c_,d_,f_}:>{a-data4[[1,1]],b,c,d,f};
hfdata4=Select[data4[[;;,{1,2}]],NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];
lowpassdata4=Select[data4[[;;,{1,3}]],NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];

ListLinePlot[data4[[;;,{1,4}]],PlotRange->Full]

samplingFreq4=1/(data4[[5,1]]-data4[[4,1]])

{ss4,kPer4}=vydejNx5[{{lowpassdata4[[Round[Length[lowpassdata4]/2];;,2]],0.02*samplingFreq4},{0.95,1.05}}]

PlotRMSvalues[data4[[;;,4]],kPer4*0.02*samplingFreq4]

RMS50toRMS[data4,lowpassdata4,samplingFreq4]

Scope 6: LED s obloukem

data6=Import["./Data/scope_6.csv"][[3;;]];
data6=data6/.{a_,b_,c_,d_,f_}:>{a-data6[[1,1]],b,c,d,f};
hfdata6=Select[data6[[;;,{1,2}]],NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];
lowpassdata6=Select[data6[[;;,{1,3}]],NumericQ[#[[1]]]&&NumericQ[#[[2]]]&];

ListLinePlot[data6[[49000;;50000,{1,4}]],PlotRange->Full]

samplingFreq6=1/(data6[[5,1]]-data6[[4,1]])

{ss6,kPer6}=vydejNx5[{{lowpassdata6[[Round[Length[lowpassdata6]/2];;,2]],0.02*samplingFreq6},{0.95,1.05}}]

PlotRMSvalues[data6[[;;,4]],kPer6*0.02*samplingFreq6]

RMS50toRMS[data6,lowpassdata6,samplingFreq6]