# extracting ultra accurate phase and frequency with FFT with real world data

I was doing some experiments in Matlab, and write this code to experiment with it.

I want to know if it's possible to extract accurate within -+0.01° phase info from samples of an ADC, the matlab FFT function says that we can.

But it's weird somehow: It would get me very accurate results, is it doable in a real-world scenario?

I assumed a 64 Sample per Cycle in my Wave under test.

Here is the matlab code

t=0:2*pi/64:2*pi-2*pi/64;
R=sin(t+pi*178.03/180);
FFT=fft(R);
angle(FFT(2))*180/pi


After this code I start to do some measurements on my real hardware, and I have increased my sample rate to 128 samples per cycle.

I have made some measurements with my system, which is a Cortex M4@200MHz device and an external ADC, I have a precision source injecting voltages and currents to my ADC which is connected to CT and PT's, the first 4 channels are CT’s and the last 4 channels are PT's.

I have injected some sine waves to the system from 40Hz to 400Hz and in the 50Hz zone I have incremented the frequency by 0.1Hz, also I have tested the system with various phase angles with the channels, the sampling frequency of my ADC is 6400 sample/sec and my ADC is a true simultaneous one.

Here is the results, when I extract the phases respect to the V0 channel and in the 50Hz region, with different phases the extracted phase is within good ranges, but as the frequency changes the error would increase.

I have only 128 samples in a full 50Hz cycle and I cannot increase my sample rate higher due to my system's overhead. I have to extract all the phase and frequency of the waves from this 128 samples, roughly 5ms, aside of many other things to calculate.

Feel free to play with these data to see if you could suggest a good enough solution to extract phase and frequency info with under 0.1 degree for phases and 0.01Hz for frequency.

And all the extracted waves are ready to be injected into matlab.

Here you can get the wave files

https://ufile.io/5b7y5

• Depends on the signal to noise ratio (noise including quantization noise). Try a least squares curve fit of the 3 free parameters of a sine wave equation. – hotpaw2 Jun 10 '18 at 14:15

You only have 64 samples per FFT. That means your FFT bins are spaced f_sample/64; if your sample rate is 128·50 Hz, then your frequency resolution, without interpolation, is just 100 Hz. To achieve a frequency resolution of $0.01\,\text{Hz}=\frac{50\,\text{Hz}}{50\cdot 100} = \frac{\frac{f_{sample}}{128}}{5000}= \frac{f_{sample}}{640\cdot5000}$, you'd need an FFT of length 3.2 millions. You can't do that on an M4, obviously, so: the FFT is not a tool applicable to your problem here.

Two approaches:

Either, you first reduce the sampling rate (including sufficient analog anti-aliasing filters) or you decimate your signal (including sufficient digital anti-aliasing filters) to a rate that describes a much smaller bandwidth of potential frequencies that you actually care about. Afterwards, do the FFT. Notice that this always always trades length of observation for precision: it's really that simple, you can't get 1/100 Hz of resolution without 100 s of observation. If things change within that period: bad luck.

Or, you don't do that and instead use parametric estimators for frequency and phase. I kind of feel I recommend that here. Parametric estimators can be as simple as zero-crossing detectors, or as complex as Eigenvalue-based estimators, and what you use depends on a lot of factors that you don't discuss – it's hard to guess what you need.

However, I like to point people at Peter K.'s A Review of the Frequency Estimation and Tracking Problems. Many of these algorithms will deliver phase info.

Regarding an accuracy requirement of 0.1° or 0.01 Hz in general: You can't have arbitrary precision with limited observation if you have any noise in your signal. Estimation theory doesn't allow for that. I think you'll be running into theoretical limits rather fast, depending on which algorithm you end up (and using the lowest-variance method might not always be the best choice!).

• Thanks Marcus for the answer,, I'm real new into DSP, I got it that maybe FFT is not the right tool for me in here and also I got it that I need more samples or Cycles to estimate the Phase and frequency info, But I'm not familiar with the algorithms that you suggest also my System needs to detect the phase info with in each cycle, I do not have many cycles to watch over it. – ASiDesigner Jun 11 '18 at 12:12
• I gave you a reference to read :) – Marcus Müller Jun 11 '18 at 12:45