pom=pom;
Remove["Global`*"];
SetDirectory[NotebookDirectory[]];
$HistoryLength=3;
v:=(Subscript[α, #])&;
params:=Union[Cases[#,(Subscript[α, _]),{0,∞}]]&;

vydejNx5[listDat_]:=Module[{k1=0.5,aclist,x1,y1,x2,y2,x3,y3,Δmozne=0.2,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=Accumulate[listDat];
nSampl=Length[aclist];
fce[nOkno_]:=Module[{rn,soucty,sigma},
rn=Range[nSampl-nOkno];
soucty=Prepend[(aclist[[#+nOkno]]-aclist[[#]]&/@rn),aclist[[nOkno]]];
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),(10^6)(sr^2)(varmin/ykvref),pomer}
];

resampluj[list_List,nNorm_,nx_]:=Module[{ourInt,index,indexs,meritko},
ourInt[x_,dats_]:=Module[{p,a,b,ya,yb,yx},p={a,b}={Floor[x],Ceiling[x]};{ya,yb}=dats[[p]];yx=ya+(x-a)*((yb-ya)/(b-a))];
meritko=((nx-1)/(nNorm-1));
index[x_]:=1.+x*meritko;
indexs=index/@Range[1,nNorm-1];
Prepend[ourInt[#,list]&/@indexs,list[[1]]]
];

fazorNharm[list_,nHarm_]:=Module[{a1,b1,int,vahy,vazky},
vahy[n_Integer]:=Prepend[Append[((((-1))^(#+1))+3)&/@Range[2n+1],1],1];
vazky=vahy[-1+(sr/2)];
int[listHodnot_,listVah_]:=(2./(3sr))vazky.(listHodnot*listVah);
a1=int[list,cosTabS[[nHarm]]];b1=int[list,sinTabS[[nHarm]]];
b1+I*a1
];


vydejFazorS[list_]:=Module[{f,δ,var,pomer,newDat,fazors},
{f,var,pomer}=vydejNx5[list];
newDat=resampluj[list,sr+1,(sr+1)*pomer];
fazors={#*2.*Pi*f,fazorNharm[newDat,#]}&/@Range[nFmax];
{{f,var},fazors}
];

(*dobre je nFile=7*)
nFmax=17;
nFile=7;
data=Drop[Import["scope_"<>ToString[nFile]<>".csv"],2];
ukaz[data_]:=Module[{pl1,pl2,pl3},
pl1=ListPlot[data/.{a_,b_,c_,d_}:>{a,b},PlotStyle->Red,Joined->True];
pl2=ListPlot[data/.{a_,b_,c_,d_}:>{a,70c},PlotStyle->Green,Joined->True,PlotRange->All];
pl3=ListPlot[data/.{a_,b_,c_,d_}:>{a,d},PlotStyle->Blue,Joined->True];
{pl1,pl2,pl3}
];
plA=ukaz[data];
Δt=data[[2,1]]-data[[1,1]];
tStart=data[[1,1]];
fnom=50.;
Tnom=(1/fnom);
sr=Round[(Tnom/(Δt))];
sinTabS=Table[Sin[i*#]&/@Range[0.,2.Pi,2.(Pi/sr)],{i,1,nFmax}];
cosTabS=Table[Cos[i*#]&/@Range[0.,2.Pi,2.(Pi/sr)],{i,1,nFmax}];
kperSampl=1.6;
vezmi=Module[{nBeru,dats},
nBeru=Round[sr*kperSampl];
dats=(Drop[#,1]&/@Take[data,nBeru])\[Transpose];
vydejFazorS/@dats
];
fcesAVars=vezmi\[Transpose][[1]];
datPhasors=Drop[#,1]&/@vezmi;
fmer=Mean[fcesAVars][[1]];
(*zabyvat se jednotnou frekvenci fazoru*)
fmer=((fcesAVars/.{f_,var_}:>{f,(1/var)})\[Transpose])/.{f_List,vahy_List}:>((f.vahy)/(Plus@@vahy));
Tmer=(1/fmer);
harm[{ω_,fazor_}]:=Abs[fazor]*Sin[ω*t+Arg[fazor]];
rek[dats_]:=Plus@@(harm/@dats);
{r1[t_],r2[t_],r3[t_]}=rek[datPhasors[[#,1]]]&/@Range[3];

tok[t_]=-Re[Integrate[r3[t],t]];
Plot[tok[t],{t,0,Tmer}];
plb=ParametricPlot[{r2[t],tok[t]},{t,0,Tmer},AspectRatio->1,PlotRange->All,PlotStyle->{Black,Thickness[0.006]},GridLines->Automatic,AxesLabel->{"i [A]","ϕ [Wb]"}];

Show[plA,Plot[{r1[t],70r2[t],r3[t]},{t,tStart,tStart+3Tmer},PlotRange->All],AxesLabel->{"t [s]","vals[V]"},PlotLabel->"(*1 Red, 2 Green, 3 Blue*)"];

Manipulate[tpoc=k*Tmer;
pocBod={r2[tpoc],tok[tpoc]};
Show[plb,Graphics[{PointSize[Large],Red,Point[pocBod]}]],
{k,0,1}];

tpoc=0.3Tmer;
proud=r2;
pocBod={r2[tpoc],tok[tpoc]};
Show[plb,Graphics[{PointSize[Large],Red,Point[pocBod]}]]

(*dfKudi[t_]:=Re[((tok'[t])/(proud[t]))/. Re'[a_]:>Re[a]];
Plot[dfKudi[t],{t,0,Tmer},AxesLabel->{"t [s]","((dϕ)/di) [Wb/A]"}];*)

n=100;
ΔtLin=(Tmer/(n-1));
rn=Range[0,Tmer,ΔtLin];
datLin={proud[#],tok[#]}&/@rn;
ListPlot[datLin];
Mlin=Fit[datLin,{ii},{ii}]/.ii->1;
resLin=NDSolve[{r3[t]==-Mlin*iLin'[t],iLin[tpoc]==proud[tpoc]},{iLin},{t,tpoc,tpoc+2Tmer}][[1]];
Plot[{proud[t],iLin[t]/.resLin},{t,tpoc,tpoc+2Tmer}];