(*Impedance a dalsi nedelam vazen. frekvenci*)
pom=pom;
Remove["Global`*"];
SetDirectory[NotebookDirectory[]];

Zf=1+0.5I;(*Zf -impedance*)
Uf=100;(*Fazor napeti*)
Iif=(Uf/Zf)*1.;
Ru=0.00001;(*Sum*)
Ri=Ru/Abs[Zf];  
f=50;
ω=2*Pi*f;
u[t_]:=Im[Uf*(E^(I*ω*t))]+RandomReal[NormalDistribution[0.0,Ru]]
i[t_]:=Im[Iif*(E^(I*ω*t))]+RandomReal[NormalDistribution[0.0,Ri]]
(*Vstupni Data*)

sr=Round[500]; (*pocet vzorku*)
Δtnom=(1/(sr*f))//N ;(*krok*)
nxnom=(1/(Δtnom*f));
U=u[#]&/@Range[0,0.03,Δtnom];
Ii=i[#]&/@Range[0,0.03,Δtnom];

{sr,Δtnom,nxnom,ListPlot[U,PlotLabel->"Napeti U",AxesLabel->{"k [-]   ","U [V]"}],ListPlot[Ii,PlotLabel->"Proud I"],Uf,Zf,Iif}

ClearAll[MetodaBez];
MetodaBez[vstupnidata_] := Module[{delka, listDelek, SigmaOkno, TriSigmy, Acc, SigmaNamerema, Frekvence, Perioda, 
     PocetVzorku, fceXstr, fceOkna, fceSigma, fceFcek, FceFazorCosSin}, 
    fceOkna[] := Module[{start, konec, listOken}, delka = nxnom; start = Round[0.9*delka]; konec = Round[1.1*delka]; 
        listOken = Range[start, konec]]; Acc = Prepend[Accumulate[vstupnidata], 0]; 
     fceSigma[Okno_] := Module[{start, konec, listIndexy, listXstr, Sigma}, delka = Length[vstupnidata]; 
        start = Range[delka - Okno]; konec = Range[Okno, delka]; listIndexy = Table[{start[[i]], konec[[i]]}, 
          {i, 1, delka - Okno}]; fceXstr[{start_, konec_}] := Module[{pomlist, Xstr}, 
          Xstr = (Acc[[konec + 1]] - Acc[[start]])/Okno]; listXstr = fceXstr /@ listIndexy; 
        Sigma = Mean[listXstr^2] - Mean[listXstr]^2; {Sigma, Okno}]; 
     SigmaOkno = ParallelMap[fceSigma[#1] & , fceOkna[]]; TriSigmy = SortBy[Take[Sort[SigmaOkno], 3], Last]; 
     fceFcek[TriSigmy_] := Module[{x1, x2, x3, y1, y2, y3, TVr, SigmaVr, frekvence}, 
       {{y1, x1}, {y2, x2}, {y3, x3}} = TriSigmy; TVr = (y1 + 2*x2*y1 - 4*x2*y2 - y3 + 2*x2*y3)/(2*(y1 - 2*y2 + y3)); 
        SigmaVr = (y1^2 + (-4*y2 + y3)^2 - 2*y1*(4*y2 + y3))/(8*(y1 - 2*y2 + y3)); frekvence = (50.*x2)/TVr]; (*!!*)
     SigmaNamerema = TriSigmy[[2]][[1]]; Frekvence = fceFcek[TriSigmy]; Perioda = 1/Frekvence; 
     PocetVzorku = TriSigmy[[2]][[2]];

 FceFazorCosSin[vstupnidata, frekvence_] := Module[{T, IntegralL, xTable, pomlist, f, Ai, Bi, IntegralCos, fce, cos, fcos, fsin, IntegralSin, sin, Fazor, 
        ycos, Δ, ysin},
 T = 1/frekvence; 
Δ = T/100;
 f = Interpolation[Table[{(i - 1)*Δtnom, vstupnidata[[i]]},{i, 1, Length[vstupnidata]}]];
 ycos[t_] := f[t]*Cos[2*Pi*frekvence*t]; 
              ysin[t_] := f[t]*Sin[2*Pi*frekvence*t]; 
fcos=Total[Table[(ycos[t]+4*ycos[t+0.5*Δ]+ycos[t+Δ]),{t,0,T-Δ,Δ}]];
fsin=Total[Table[(ysin[t]+4*ysin[t+0.5*Δ]+ysin[t+Δ]),{t,0,T-Δ,Δ}]];

              Ai = (Δ/6)*fcos;
Bi = (Δ/6)*fsin;
 Fazor = (Ai*I + Bi)*100]; 

     {Frekvence, FceFazorCosSin[vstupnidata, Frekvence], TriSigmy, SigmaNamerema, PocetVzorku}];

Quiet[MetodaBez[U]][[1]] (*frekvence napeti spocitana*)
Quiet[MetodaBez[Ii]][[1]](*frekvence proudu spocitana*)
FazorU=Quiet[MetodaBez[U]][[2]];
FazorI=Quiet[MetodaBez[Ii]][[2]];
Pcinny=Re[FazorU*Conjugate[FazorI]];
Qjavoly=Im[FazorU*Conjugate[FazorI]];
Zdanlivy=(Sqrt[(Pcinny^2)+(Qjavoly^2)]);
Ucinnik=(Pcinny/Zdanlivy);
PsCinny=Re[Uf*Conjugate[Iif]];
QsJalovy=Im[Uf*Conjugate[Iif]];
ZdanlivyS=(Sqrt[(PsCinny^2)+(QsJalovy^2)]);
UcinnikS=(PsCinny/ZdanlivyS);
Column[{FazorU  " Fazor Napeti Spocitany",FazorI "  Fazor Proudu Spocitany",Pcinny  "  Cinny Spocitany",Qjavoly "  Jalovy Spocitany",Zdanlivy  "  Zdanlivy Spocitany",Ucinnik  "  Ucinnik Spocitany","  ",Uf  "  Fazor Napeti",Iif "  Fazor Proudu",PsCinny  "  Cinny ",QsJalovy "  Jalovy",ZdanlivyS "  Zdanlivy",UcinnikS "  Ucinnik",Zf "  Impedance"}]
Column[{Abs[((PsCinny-Pcinny)/PsCinny)]*100 "  Relativni Chyba P_Cinny",Abs[((QsJalovy-Qjavoly)/QsJalovy)]*100"   Relativni Chyba Q_Jalovy",Abs[((ZdanlivyS-Zdanlivy)/ZdanlivyS)]*100 "   Relativni Chyba S_Zdanlily"}]