extracting ultra accurate phase with FFT - Page 1
Forum section: electronics / projects

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 02, 2018, 12:45:18 pm
content: Hi,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 with in -+0.01 Degree phase info from samples of an ADC, the matlab FFT function says Sure we can! But it's wired some how!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.Quote

author: timpattinson
author_rank: Contributor
timestamp: June 02, 2018, 01:20:08 pm
content: Hi,This is completely possible. In reality it depends on many parameters, i'll list a few of the more obvious ones.Sample rate of the ADCNoise in the system - both from your source and generated by the ADC or any amplifiers/buffers before itClocking issues ( if there is variation in the sample frequency due to jitter or drift)Resolution of the ADCAvailable processing power and time requirements (do you need a real-time measurement? How long can your FFT be?)In practice I don't know what level of equipment/circuit you would need for 0.01 degree accuracy, someone else may be able to comment on that.Sent from my SM-G930F using Tapatalk

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 02, 2018, 01:55:36 pm
content: Thanks for the update, I will test my code on my Cortex M device and compare the results to see how they are matched, But i wonder if anybody else has done something similar?

author: Dave
author_rank: Super Contributor
timestamp: June 02, 2018, 02:00:38 pm
content: You need to round the sine samples to simulate quantization and then you need to figure out how much noise is going to be introduced by your system and add that to the samples. Only then can you estimate how many samples are going to be needed for the desired phase angle accuracy. My guess is significantly more than 64.

author: JS
author_rank: Frequent Contributor
timestamp: June 02, 2018, 04:43:02 pm
content: Do you nees the phase/freq data or you need the phase of a particular frequency sinewave?If the later there are better ways of doing so, fitting the parameters of a ainewave would be my first guess.Do you have a reference signal or trigger as the phase reference or its just relative to a random sampling window?JSEnviado desde mi LG-M250 mediante Tapatalk

author: ejeffrey
author_rank: Super Contributor
timestamp: June 02, 2018, 05:11:16 pm
content: If you have the CPU power and your signal is free from interfering signals at other frequencies, fitting is probably a better bet. If your SNR is high enough you don't even need a complete cycle. But it will be hard to do that in realtime on a microcontroller. If you have strong signals at other frequencies, you will probably want to go back to the FFT since it acts as a narrow band filter. If your signal is not synchronous with the clock (including any unwanted signal components) you will need to add a window function which will degrade SNR somewhat as well.Are you using the ADC in your microcontroller? If so, I suspect sample clock jitter will likely be an issue. The clocks on those chips are optimized for low power, not low jitter.

author: Kleinstein
author_rank: Super Contributor
timestamp: June 02, 2018, 06:48:32 pm
content: If the frequency is known and thus fitting is only amplitude and phase (or numerically better sin and cos component), the FFT is nothing more than an efficient method to do the fit. Though for just those two parameters the direct fit by calculating just that one Fourier-component of interest is possibly even fast. If there is an efficient way to "calculate" the sine values, there is no need for that much computations - so a µC might well be enough.There is no need to have that many samples to get a good phase resolution. More samples or better amplitude resolution are kind of alternatives and unless with very few samples, more points help has much as oversampling to get increased amplitude resolution. I did some similar tasks (fitting amplitude, frequency and decay rate) and did not need that many point. Even with just 8 Bit amplitude resolution something like 0.01 degree phase accuracy does not sound impossible: it needs amplitude resolution in the 30 ppm range, and thus something like 20000 Samples with 8 bit resolution (or correspondingly more/less with better SNR).The tricky part can be the start and end and windowing function if the sampling rate is not in sync with the frequency of interest.

author: coppice
author_rank: Super Contributor
timestamp: June 02, 2018, 07:51:03 pm
content: If you have 3 perfect samples from a perfect sine wave you can characterise that wave. If the waveform is not a perfect sine wave the number of samples you need grows rapidly. If there is noise (which, of course, there will be) the required samples grows rapidly. Imperfection in the sampling, either in amplitude or in time (e.g. clock jitter), makes the required samples grow rapidly.What do your signal, and sampling scheme look like?

author: JS
author_rank: Frequent Contributor
timestamp: June 03, 2018, 10:06:01 am
content: Quote from: Kleinstein on June 02, 2018, 06:48:32 pmFFT is fitting for periodic signals, sinewaves with freq not a rational multiple of the sampling frequency aren't periodics in discrete time, even when they were before being sampled. With DFT the parameters of such signals you'd get would be an approximation even when not quantized.Freq can be estimated pretty efficiently, it's important to have a good starting point to do so. If the frequency is roughly known to a good precision great, even if not exact. If not doing a zero crossing detection can be useful to set the starting point and have a limited range to search while fitting.OP still didn't said if he needs the phase of a single and known (or unknown) freq or the phase/freq data, in which case we are making nonsense.Another way would be to use a PLL, to extract the instantaneous phase and average to get the overall with the highest precision. This could probably be real time and do a moving average. Pretty small stuff is able to do this and play music at the same time, or even show tv programs. (Look for fm demodulators) there muss be done nice usable code out there to get started.JSEnviado desde mi LG-M250 mediante Tapatalk

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 03, 2018, 10:35:09 am
content: Thanks guys for your feedback, I have used an 16bit external ADC next to my 200MHz Cortex M4 part,I have used linear interpolation with samples near zero, to estimate the exact zero crossing and thus figuring out the phase info for the required 8 channels of ADC, the first channel assumed to be the reference wave, going in that way has some draw backs, that it depends on the amplitude of the signal, the lower the amplitude the lower the accuracy, and even with high amplitudes I could only go to 1-2 phase degree in my system, please do not worry about the Calculation time for the FFT, for the 64 sample FFT, my cortex would do it in under 100uS, and I have about 5mS room for my calculations.

author: timpattinson
author_rank: Contributor
timestamp: June 03, 2018, 10:45:21 am
content: Is there any DC in your signal, even a small amount? Adding a DC offset to an otherwise identical signal would cause it to have a different calculated phase difference when looking at zero crossings. This could explain your situation, as a fixed DC offset is proportionally less important when the AC signal is bigger.If you are having trouble implementing the FFT you could look at the ARM CMSIS DSP library which appears to have FFT functionshttp://www.keil.com/pack/doc/CMSIS/DSP/html/group__groupTransforms.html

author: Yansi
author_rank: Super Contributor
timestamp: June 03, 2018, 10:59:50 am
content: FFT is not efficient at all, if you are only interested in a single frequency. Use the Goertzel algorithm (which computes the single DFT term).And btw, this is the way how many if not most high end impedance (RLCG) bridges work.

author: coppice
author_rank: Super Contributor
timestamp: June 03, 2018, 11:06:57 am
content: Quote from: Yansi on June 03, 2018, 10:59:50 amGoertzel will only give you an accurate measurement if its set for the exact frequency of the signal of interest. If you are looking at the return from a generated signal you will know the exact frequency, and that works out nicely. If you are analysing a signal from some arbitrary generator you're going to need to nail its frequency precisely before you can set the coefficient of the Goertzel filter, and apply it.

author: Yansi
author_rank: Super Contributor
timestamp: June 03, 2018, 11:09:22 am
content: Well, I am not sure what you can do with a phase from a single FFT either. You need to have something as a reference phase, as the phase is relative, isn't it?I do not know of any application, that uses absolute phase extracted from a single signal FFT.

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 03, 2018, 12:04:27 pm
content: QuoteAs I said I have 8 channels of DATA from 8 different signals, and I assumed the first one to be Reference

author: coppice
author_rank: Super Contributor
timestamp: June 03, 2018, 12:14:11 pm
content: Quote from: ali_asadzadeh on June 03, 2018, 12:04:27 pmIf you have a bunch of signals, all at exactly the same frequency, and want to know their relative phases, you can use correlation to get very precise phase measurements.

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 03, 2018, 12:16:27 pm
content: QuoteThe sampling frequency is not sync with the frequency of interest, also if I change frequency of the signal, it would give way more error in the phase, suppose I only increase the frequency of the signal by 2% I would get about 3.5 degree error!Code:t=0:2*pi/64:2*pi-2*pi/64;R=2.3*sin(1.02*t+pi*178/180);FFT=fft(R);angle(FFT(2))*180/pi

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 03, 2018, 12:20:04 pm
content: QuoteHow should I use correlation? Do you have any code snippet?

author: JS
author_rank: Frequent Contributor
timestamp: June 03, 2018, 03:33:48 pm
content: Quote from: Yansi on June 03, 2018, 11:09:22 amYes, it's relative, everything is, but no, phase is absolute, once you know the phase of a sinewave at a given point in time you can know what phase it will have at any point in time. It's as relative as time itself, for our purposes standing still in or mother earth surface is absolute.It's easy to miss as we don't usually care about them but relative to something more, besides as it's periodic it repeats itself so at 4π los exactly te sabe as at 9776846π, si it's arbitrar what we call 0 phase or we agree too normalize it between 0 and 2π or between -π and π.Now, as any "absolute" thing youcan compare two and here is the case, being one of the channels the reference and the others will be leading our lagging a certain ammount.JSEnviado desde mi LG-M250 mediante Tapatalk

author: Yansi
author_rank: Super Contributor
timestamp: June 03, 2018, 04:01:28 pm
content: I do not understand much of what you have tried to express, however I still think that taking a signal, windowing it and calculating the FFT of that, the phase does not give any useful information, unless you know the EXACT time the window of samples was taken.The phase values from the FFT are referenced to the window of samples taken. You can take as many different sample windows from a sinewave, to get as many different phase results, as in each sample set, the sinewave will be in different phase to the window of samples itself. Hence the absolute phase of FFT does not make any sense.Taking FFT both of a reference signal and measured signal, then taking a difference of phase between the two concurently sampled signals makes sense.

author: JohnnyMalaria
author_rank: Super Contributor
timestamp: June 03, 2018, 05:58:32 pm
content: If your measurement is for a single, fixed frequency then I'd not bother with FFT, curve fitting etc. There's a much simpler way.In the following, you will have to decide whether you can perform the measurements on all the channels at once or one at a time (+ reference channel) to allow for time delays if your ADC does not perform true simulataneous sampling. In the latter case, timing differences between channels may vary depending on how many channels you are sampling.1. Use your first ADC channel as the reference for the rest2. Capture the reference channel and other channel(s)3. Convert the data to simple +1 or -1 (i.e., the sign of the data) (or, for speed, assign +1 = True, -1 = False and calculate NOT XOR)4. Multiply a test channel's sign data by the reference sign data5. Integrate (i.e., simple summation)From the integral, it is trivial to get the phase. Why?If the reference and test channel are exactly in phase, the multiplication of the signs will always be +1. If the reference and the test channel are exactly out of phase, the multiplication of the signs will always be -1. If you sample atnsamples per period, the integral will be +nif in phase and -nif 180° out of phase. There's a direct linear relationship between the integral, sampling rate and the phase. The more periods you integrate this over, the higher the precision. This allows you to use a reasonably low.One limitation of this is that you cannot determine which quadrant your phase is in. If you are looking at small phase differences it wouldn't be a problem. Otherwise you need to do the same thing with a second reference that is 90° out of phase with the first one.Because MATLAB is matrix-based, the above calculations are trivial and fast. I've used this approach with great success to measure phase differences of less than 0.1° for a very noisy signal. This is basically a software implementation of a phase sensitive detector.This leads to a question: are you trying to measure phase differences or the time delay between channels at a variety of single frequrencies? If the latter, I agree with the suggestion of correlation. I'm pretty sure MATLAB can do it (cross-correlation).

author: Yansi
author_rank: Super Contributor
timestamp: June 03, 2018, 06:04:17 pm
content: Will not work correctly this way, as you have quantized the zero crossings to nearest sample position. Which is incorrect and will yield more phase measurement error.The correct way is to multiply both signals by cos(wt) and sin(wt) of desired frequency of interest, integrate and then get the phase difference from the arctan of it. (Which in fact is calculation of the single term of the DFT).By no means I am a DSP expert, but your "+- sign algortihm" sounds as dodgy as it can get.

author: JohnnyMalaria
author_rank: Super Contributor
timestamp: June 03, 2018, 06:09:42 pm
content: Quote from: ali_asadzadeh on June 02, 2018, 12:45:18 pmDo you want high accuracy or high precision? (both, I'm sure)If you want high accuracy then I suggest to try different values for the frequency in your MATLAB code. It may be precise but the accuracy may vary slightly. e.g, if you go from 178.03° to 177.00°, you may get 178.143° and 176.344° - good precision, not so good accuracy.

author: JohnnyMalaria
author_rank: Super Contributor
timestamp: June 03, 2018, 06:17:02 pm
content: Quote from: Yansi on June 03, 2018, 06:04:17 pmIt works - I've done it - 30 years ago on 286 PC and today on this laptop (though I now do quadrature detection). Companies have made lots of money doing this in some lock-in amplifiers. Like I say, I've measured phase differences of doppler-shifted laser light using this very approach. I got me my PhD and has been commercialized by a number of companies. I've done a similar dodgy thing with autocorrelation (reducing data to single bit) again with great success. The zero crossing is a red herring. Choose your sampling period to be a non-integer factor of the signal period, average over enough periods and the zero crossing issue goes away.Like I say below, tell me it can't be done and I'll do it - which I did.

author: rhb
author_rank: Super Contributor
timestamp: June 03, 2018, 07:02:39 pm
content: You might want to set it up as an L1 pursuit solution to Ax=y using the simplex solver in GLPK and arbitrary precision arithmetic. It's compute intensive, but quite amazing.The A matrix would have as many rows as data samples and one column for each frequency. L1 pursuits have the interesting property that they don't smear the solution the way L2 does. The ordinary FFT is an L2 solution.The references in this will get you started:https://arxiv.org/pdf/1709.01071.pdfDon't worry about the ugly math. You can treat it the same way you do an FFT. Just do it.

author: Yansi
author_rank: Super Contributor
timestamp: June 03, 2018, 07:09:38 pm
content: Quote from: JohnnyMalaria on June 03, 2018, 06:17:02 pmThat something was done or is being done does not mean it is the best way to be done. And I do not want to be mean, but am really not sure, how integrating just the sign of the signal will yield better results than calculating the full DFT term. Especially, when the frequency of interest approaches the nyquist frequency. Taking just the sign will yield very inaccurate results in that case I guess.I think it is not that hard to imagine what accuracy can be obtained, if you integrate just the sign of a signal, where you have like just a few samples per period.Otherwise I really did not get how your algorithm works. Of course, cheaper RLC bridges /impedance analyzers/lockins may just only integrate the input signal with the polarity switched according to the reference. But is it the best approach? I'd say certainly not.

author: coppice
author_rank: Super Contributor
timestamp: June 03, 2018, 07:11:11 pm
content: Quote from: Yansi on June 03, 2018, 06:04:17 pmQuadrature correlation is a faster approach, but if you can let the +- approach run for long enough it will converge on the right answer. You throw away a lot of information, and then need to recover it through the massive damping that integration provides. Its good for simplicity, and in RF applications the cycles go past so rapidly that the settling time is fine for many uses.

author: JS
author_rank: Frequent Contributor
timestamp: June 03, 2018, 07:49:54 pm
content: Quote from: coppice on June 03, 2018, 07:11:11 pmNot fast enough if your signal keeps changing, you need quadrature information to demodulate fm, lucky enough those usb synchronizes gives you rude quadrature information in base band already, if you syntonize them correctly you just need to use the phase in them. If the frequency isn't prefect a ramp in phase will appear and screw the quantization.Too bad I didn't have the time to play around longer with the toy as it might be a very nice and cheap HF tool. A few bux for GHz analog data to the computer. I don't do much HF but as soon as I come across needing to measure something I order one to be my first tool. Really nice spectrum analysis right out of the box with the included software, front end required to be a usable lab tool but some attenuators and adapters might be good enough to start.JSEnviado desde mi LG-M250 mediante Tapatalk

author: JohnnyMalaria
author_rank: Super Contributor
timestamp: June 03, 2018, 07:51:22 pm
content: Quote from: Yansi on June 03, 2018, 07:09:38 pmI didn't say it was. But for measuring a fixed time delay between two identical signals (which I assume is the OP's need) doesn't require DFT processing. What I described works. It doesn't mean it's better. It simply works if done right and is trivial to implement. My experience over many years is that people dismiss an idea as being impossible because either they have never tried or, if they have tried, didn't do it right.

author: Kleinstein
author_rank: Super Contributor
timestamp: June 03, 2018, 08:09:22 pm
content: The simple FFT / DFT approach can become tricky if the signal frequency is not related to the sampling frequency. In the FFT the signal is not in one frequency bin. So it gets really tricky to determine a phase from the FFT. Using date with an integer number (or simple fraction) of samples per period is kind of the simple case only.Doing a fit of a sine function to the signals might be the easier way. It is not as complicated as it might sound. Even if the frequency is only approximately known, the right functions can be close to a linear fit. It would thus only need a very few (e.g. 2) iterations for a good fit if done right (choosing the right base functions).If computational speed is an issue, e.g. with real time data using the Goertzel algorithm (or similar) might be an option, that is probably hard to beat and may be good enough.The frequency of the signal or ADC is another important parameter to determine on how much time can be spend on calculations.

author: Yansi
author_rank: Super Contributor
timestamp: June 03, 2018, 08:15:49 pm
content: Quote from: JohnnyMalaria on June 03, 2018, 07:51:22 pmWell tbh I have toyed with Goertzel and the quadrature correlation quite a bit, in an attempt to make a decent diy RLCG bridge, and later had some fun with DTMF decoding. Hence the bit of my dislike for the simple methods, as none of them seemed intereseting enough to try, as they were widely used in cheap instrumentation and I wanted to attack something better and different. (I wasn't fully satisfied either, but due to many other things mostly).

author: JohnnyMalaria
author_rank: Super Contributor
timestamp: June 03, 2018, 08:41:11 pm
content: Quote from: Yansi on June 03, 2018, 08:15:49 pmThat's strikes me as an odd reason. If a solution exists, is proven for your needs and inexpensive, why would you dismiss because it isn't interesting? If you're developing a commercial product, it seems rather luxurious and a waste of resources.An attitude I see today is that people never look back at old solutions to the same problems. In particular, some people (I'm guessing half my age) would completely discount the option of performing some precision mathematical signal processing in the analog domain. My experience is that it can be more accurate and precise, faster (obviously), cheaper and quicker to implement. For example, I use two $30 high-precision multiplier/divider ICs to do quadrature detection plus a couple of standard op-amps to do some filtering before digitizing. I can get away with a much lower ADC sampling rate which, in turn, saves $1000s for what I would otherwise need.

author: rhb
author_rank: Super Contributor
timestamp: June 03, 2018, 09:11:58 pm
content: What @JohnnyMalaria suggested several posts back is a variation of sign bit recording. This will actually recover full amplitude broadband data from records which are only a single bit. Sam Allen did this 40 years ago with Vibroseis data. The price tag is some extra compute. But analog crosscorrelation works quite well.If you go the FFT route, use a linear fit to the phase difference between the signals.If you really want to be good at signal processing (or anything else), you need to know all the ways ti can and has been done. My dissertation supervisor's main claim to fame was an analog filter using a revolving drum and adjustable playback heads to cancel water bottom reflections in seismic data in the late 50's and early 60's when seismic data was still recorded in analog format on 2" wide tape.I filed and lapped a 0.75" x 1.0" x 3.125" block of aluminum plane and parallel to within 0.0015" this morning. I'm about to fix the remaining error by scraping. All this is *very* old school. But for what I wanted, it was quicker and easier than setting up to use my Clausing milling machine.

author: Yansi
author_rank: Super Contributor
timestamp: June 03, 2018, 09:38:27 pm
content: Quote from: JohnnyMalaria on June 03, 2018, 08:41:11 pmI didn't say I was designing a commercial product, did I? It was a diy, mainly for the educational purpose, which it fulfilled at the satisfactory level. Yes, there are solutions, like the synchronous detection using just polarity switching, which has obvious flaws I did not want to have in my design.

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 04, 2018, 09:53:10 am
content: Thanks guys for your feadbackQuoteI'm doing true simulataneous sampling on the signals,JohnnyMalaria would you please give me a pesudo code? I do not get your Idea fully,Also I want to get the Phase info from the signals not the time delay between channles.QuoteKleinstein,How the sine fitting would work? I have no Idea? do you have any refrence or pesudo code?

author: JS
author_rank: Frequent Contributor
timestamp: June 04, 2018, 10:34:11 am
content: Sine fitting is a recursive algorithm, first you try to find the frequency, then the phase and if you are interested then you can find the amplitude. The more efficient approach h is just to find the freq using a sin and a cos and then extract the phase from there, as it would be a coef from the fft.To find the freq you should start with a good approximation and then maximize the intercorrelation between n the signal and the sin and cos. The efficiency of the algorithm will depend on the efficiency of that search. Then with the relation between the intercorrelation of the sin and the cos you can extract the phase.Matlab has a function to do fitting, maybe you can try to understand in the pc to later build up in the embedded.JSEnviado desde mi LG-M250 mediante Tapatalk

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 04, 2018, 10:52:50 am
content: Thanks JS for the update, what's the name of the MATLAB function?

author: JS
author_rank: Frequent Contributor
timestamp: June 04, 2018, 04:14:25 pm
content: Quote from: ali_asadzadeh on June 04, 2018, 10:52:50 amYou are welcome.The function name is "fit".JSEnviado desde mi LG-M250 mediante Tapatalk

author: JohnnyMalaria
author_rank: Super Contributor
timestamp: June 04, 2018, 05:27:47 pm
content: Quote from: ali_asadzadeh on June 04, 2018, 09:53:10 amPlease see that attached spreadsheet.If your signal is steady over 100 cycles then this will work.The calculations in the spreadsheet are very easy to do in MATLAB.You can change the period and the phase to see how the calculation is affected. The calculated phase using the algorithm is shown. The key is to sample at a frequency that is not an exact multiple of your signal frequency. This is why the period is 99.9 in the example.The example uses 3,000 samples. In practice, you could go much higher and increase the precision of the phase estimate.Even with 3,000 samples, it is possible to get better than 1 degree precision for a signal sampled only 5 times per cycleYou should extend the number of rows significantly since this forum limits the size of the spreadsheet.Note, this will only give phase results from 0 to 180 degrees (i.e., 270 = 90, 315 = 45 etc). If you need to discriminate then you have to employ the same algorithm but using a second reference signal that is 90 degrees out-of-phase with the first reference signal.

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 05, 2018, 08:24:11 am
content: Thanks JohnnyMalaria, can the calculations be done on a single cycle of samples?

author: JohnnyMalaria
author_rank: Super Contributor
timestamp: June 05, 2018, 12:19:33 pm
content: Quote from: ali_asadzadeh on June 05, 2018, 08:24:11 amNo.

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 10, 2018, 12:46:40 pm
content: Guys I have made some measurements with my System, 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 sin 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 with in 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 over head, also I should extract all the phase and frequency of the waves from this 128 samples, roughly 5ms Regarding that I have a ton of other things to do! fill 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

author: JS
author_rank: Frequent Contributor
timestamp: June 10, 2018, 05:25:06 pm
content: But you don't have 0.01Hz data on 5ms sampling time, so you cant have that resolution from an FFT. Or you could adding enough padding or reconstruction from the signal you have but not a task for a limited overhead system.Sorry if I missed, what resolution does your ADC have? Trying to extract some data that isn't there is possible but not useful, if your sampled data doesn't have it you can't get it.I might take a look at matlab but as said, I'd like to know if is theoretically possible to do so. For the 50Hz phase it probably is, depending on the bit depth of the samples.JSEnviado desde mi LG-M250 mediante Tapatalk

author: coppice
author_rank: Super Contributor
timestamp: June 10, 2018, 06:57:45 pm
content: Quote from: ali_asadzadeh on June 10, 2018, 12:46:40 pmIt sounds like you are measuring 3 phase power. It would have been helpful to say this from the start. Is there a special reason why you asked about measuring from a single sample of the waveform (e.g. you are trying to do something tricky, like load disaggregation), or are longer term measurements OK? Per cycle measurements would be fine with pure sine wave signals, but you never see pure sine waves in power measurements, and that complicates things.Most people trying to get an accurate phase measurement for mains power don't need a fast response. They synthesize a pure sine fundamental voltage waveform, and lock it to the voltage signal in both amplitude and phase. This will lock it to the fundamental component of the voltage waveform if you filter hard enough in the tracking loop. Its trivial to produce the sine wave with both in phase and quadrature components. Now you quadrature correlate the pure sine wave voltage signal with the current waveform. Only the fundamental of the current will correlate with the pure voltage signal, so you don't need a separate pure sine current waveform. From the correlation you can get both the phase angle and the fundamental component of the power (if that is of interest to you). You can't get fast updates, like per cycle results, though. The filters take time to settle.Using the above approach its easy to get 0.01 degree accuracy in the phase measurement using the hardware and sampling rates you are using.

author: jbb
author_rank: Super Contributor
timestamp: June 10, 2018, 07:16:06 pm
content: Yes, if you had said three-phase power we could have gotten here sooner. It’s theXY problem. We’ve all done it.Over a 5ms window, the term ‘instantaneous frequency’ doesn’t mean much, because it’s not a full line cycle. Zero crossing techniques are not recommended because noise in the voltage of current waveforms will lead to the measurement going all over the place. Furthermore, the frequency of a typical AC power grid doesn’t change that fast because it’s set by large rotating generators that don’t change speed instantly.In addition, your line currents are likely to be distorted by the presence of nonlinear loads.So for accurate line frequency I thoroughly recommmend the use of a PLL. Because you have three phases you can use phasor extraction (Park’s Transformation) to get largely ripple-free measurements of VDand VQ, which allows for more aggressive tuning of the PLL loop.If you want to measure power, VARs etc at high rates, might I suggest looking into Akagi’s instantaneous power theory?

author: coppice
author_rank: Super Contributor
timestamp: June 10, 2018, 09:22:00 pm
content: Quote from: jbb on June 10, 2018, 07:16:06 pmUsing a PLL is good. Relying on the presence of all three phases is bad. People usually expect measurements to proceed correctly on the remaining phases if one or two phases are down.

author: CopperCone
author_rank: Super Contributor
timestamp: June 11, 2018, 12:55:41 am
content: good equipment will have a phase missing detect circuit

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: June 11, 2018, 12:29:16 pm
content: QuoteMy ADC resolution is 16bitQuoteThanks coppice, I'm not measuring the 3 phase power, I'm doing Directional protection for a 3phase system , Since I'm doing protection, I need a cycle by cycle protection,So I need phase and frequency info with in each cycle, So based on those facts do you recommend an algorithm?

author: JS
author_rank: Frequent Contributor
timestamp: June 11, 2018, 03:36:39 pm
content: Ok, even if you need cycle by cycle data, there is useful info in previous data to help ease things a bit. Frequency shouldn't change much, so you could have a better aproimation to start with, if you sync the sampling window with the input, phase shouldn't change much either.So, with that in mind, seems easier to do the fit, as you know a good starting point for frequency and phase. If you know how much it could change from once cycle to the other (phase and freq) even better, as you have a limited range to work. All those things make the fitting of a curve more efficient, knowing where to look.JSEnviado desde mi LG-M250 mediante Tapatalk