# A DFT-based phasor estimation

I have the sample set of ideal and real secondary current during CT saturation, need to apply the DFT analysis to samples and estimate the fundamental phasor (magnitude and phase angle) of samples:

I am far from digital signal processing but I suppose the main idea is to perform computation at each sample $$m$$ on the basis of $$N$$ historical samples. I've been trying to apply the following expressions: $$I_{k}(m)=\sqrt{I_{RE}^2(m)+I_{IM}^2(m)}$$ $$\varphi(m)=\arctan\lgroup\frac{I_{IM}(m)}{I_{RE}(m)}\rgroup$$ $$I_{RE}(m)=\frac{2}{N}\sum_{n=0}^{N-1}i(m-n)\cdot \cos\lgroup\frac{2\pi k n}{N}\rgroup$$ $$I_{IM}(m)=\frac{2}{N}\sum_{n=0}^{N-1}i(m-n)\cdot \sin\lgroup\frac{2\pi k n}{N}\rgroup$$ where:
$$m$$ is the current sample
$$n$$ is the sample number in a cycle (window)
$$k$$ is the harmonic number $$I_{k}(m)$$ is the output sample for harmonic $$k$$
$$i(m-n)$$ is the input sample set (stored samples)
$$N$$ is the number of samples in cycle (window)

Here is my c# code:

private DFTsample[] FourierAnalysis(List<double> inputSamples, List<double> inputTime, int k)
{
DFTsample[] dFTsamples = new DFTsample[inputSamples.Count];

for (int m = 0; m < inputSamples.Count; m++)
{
if (inputTime.ElementAt(m) < 0)
{
dFTsamples[m] = new DFTsample() { Abs = 0, Angle = 0, Re = 0, Im = 0 };
continue;
}

if(inputTime.ElementAt(m) >= 0)
{
dFTsamples[m] = new DFTsample();
double Re = 0;
double Im = 0;
for (int n = 0; n < N; n++)
{
Re += 2 / N * inputSamples[m - n] * Math.Cos(2 * Math.PI * k * n / N);
Im += 2 / N * inputSamples[m - n] * Math.Sin(2 * Math.PI * k * n / N);
}
dFTsamples[m].Re = Re;
dFTsamples[m].Im = Im;
dFTsamples[m].Abs = Math.Sqrt(Math.Pow(Re, 2) + Math.Pow(Im, 2));
dFTsamples[m].Angle = Math.Atan2(Re, Im) * 180 / Math.PI;
}
}
return dFTsamples;
}


My result doesn't seem right, something went wrong:

I'd really appreciate it if you'd let me know how to use the DFT and compute the magnitude and phase angle of the secondary current.

• Your pictures appear to be behind a pay wall. At least I can't see them Dec 10, 2022 at 9:08
• @Hilmar Thank you for noticing. I've changed links, hope they'll work well Dec 11, 2022 at 1:14
• Don't you have the $\sin(\cdot)$ and $\cos(\cdot)$ swapped? $$I_{RE}(m)=\frac{2}{N}\sum_{n=0}^{N-1}i(m-n)\cdot \cos\lgroup\frac{2\pi k n}{N}\rgroup$$ $$I_{IM}(m)=\frac{2}{N}\sum_{n=0}^{N-1}i(m-n)\cdot \sin\lgroup\frac{2\pi k n}{N}\rgroup$$ Dec 11, 2022 at 4:39
• And you wanna changed $\arctan(\cdot)$ to $\arg\{\}$. get all four quadrants. Dec 11, 2022 at 4:44
• If you do this right, and use sliding DFTs for each $X[k]$, you can minimize computations, assuming $N$ is a few K. Now, if you do that, your sin and cos will stay synchronous with the harmonics as $m$ increases. That will change the definition of $\varphi(m)$ into a more lowpass form. Like an envelope. Dec 11, 2022 at 4:53