LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Better estimation of phase difference between two signals with variable frequency!

Hello LabView Gurus, 

 

Being a power engineer and having just a little knowledge of signal processing and labview, I have been pulling my hair out for the last couple of days to get a better estimation of phase difference between two signals.

We have two analog voltage signals; 1. sine wave (50Hz ± 1Hz) and 2. a square wave with exactly half of sine wave frequency at any time.

At the starting point of operation (and simulation/acquisition) both signals will have no phase difference. However, the square wave's frequency changes unpredictably for a just a few millisecond but then it gets synchronized with sine wave's frequency again. This means that the square wave will be phased out from its original position. The task of the labview is to find the phase difference between the two signals continuously.

 

My approach to determine the phase difference is to measure the time when sine wave crosses zero amplitude and the time when the very next square wave changes amplitude from zero volts to +ve voltage (I have a 0.5volts threshold just to avoid any dramas from small line noise). The difference between these times is then divided by the time period and multiplied by 360 to get this phase difference in angles. 

 

As this part is just a small block of a big project, I can only allow 5000Hz sampling rate each for both signals. I read 500 samples (which means I read data from 5 cycles of sine wave and 2.5 cycles of square wave).

 

Now the problem is, as long as the frequency of sine wave stays constant at exactly 50Hz, I get a good estimation of the phase difference but when the frequency changes even a little (and it will happen in the real scenario i.e 50Hz ± 1Hz  and the square wave's frequency is dependent of sine wave's frequency), the estimation error increases.

 

I have attached my labview program. From front panel, you can set the phase of square wave (between -180 and 0) and you should see the labview's calculated phase in the indicator box named 'Phase'. Then you can press 'Real Frequency' switch that would cause the frequency to change like it would in real operation.

You can observe that the estimation error increases after you push the button. 

All I need to do is to reduce this estimation error and make it as close to the actual phase difference as possible. Any help would be greatly appreciated.

I am using LabView 2009 for this task.

The application is for electric machines and the stability/performance of machines under different faults.

 

Thank you for reading this far! 🙂

 

Regards,

 

Awais

 

 

Download All
0 Kudos
Message 1 of 10
(8,088 Views)

Basic math gives me a bit of pause on this approach.  You are sampling at 50 times the frequency of interest so you get 50 samples per cycle.  your phase resolution is 1/50th cycle or 7.2 degrees +/- noise.  You will need to samlpe faster to reduce phase resolution or average multiple readings (at a time cost that is signifigant)


"Should be" isn't "Is" -Jay
0 Kudos
Message 2 of 10
(8,070 Views)

I'm not a signal processing expert, but here my basic understanding.

If you simulate sampling with 5kHz and a frequency of 50 Hz (and both are 'sync' by design), you always get an exact 5 periods. Any variation of your signals frequency gives you a propability to get 4 or 6 'trigger' events. That's an up or down of 20%!

 

The one measure to reduce such problems is using 'window functions'. They don't fit your current approach (counting instead of a DSP algorithm), so this needs to be reworked as well.

 

My approach would be to use the concept of a Locki-In amplifier. You need to phaseshift your ref-signal by 90°. Then multiply your measurement signal with the ref signal and the phase shifted ref signal. The obtained values for x/y coordinates of a complex number. Calculate the theta of the complex number (with the LV prim). Feed this in a low pass filter.

The trick on this is, that the square wave has harmonics in it, in this you are interested in the second harmonic which is the sine wave.

To get rid of the effect that the sync between sampling rate and ref signal frequency gives an error, you then can use the window I mentioned above (place it before the lock-in).

 

For a design that really plays well, use a producer-consumer design pattern to get the calculations done in parallel with the DAQ.

 

I suggest you to check on wikipedia for some of the keywords I mentioned. Go also for the external links which lead to great tutorials and AppNotes on the signal processing basics.

 

Sorry, it's not a simple solution I offer and we will have quite some conversation on this forum if you follow this path. Maybe someone else knows a simpler way.

 

Felix

Message 3 of 10
(8,064 Views)

I think both Jeff and Felix have given some good advice.

 

You really need to define how much phase error you can accept and how fast you need to measure (or estimate it from the measurements).  Note that these may be conflicting requirements.  How fast does the line frequency change?  Are the changes continuous or in discrete jumps?

 

I cannot look at your VI now.  How is the square wave generated? The Tone Frequency VI uses Fourier transform techniques internally.  If the data set is too short or the frequency changes within the data set, its estimation of the frequency may not be very good.

 

Lynn

Message 4 of 10
(8,015 Views)

 


Jeff Bohrer wrote:

Basic math gives me a bit of pause on this approach.  You are sampling at 50 times the frequency of interest so you get 50 samples per cycle.  your phase resolution is 1/50th cycle or 7.2 degrees +/- noise.  You will need to samlpe faster to reduce phase resolution or average multiple readings (at a time cost that is signifigant)

Jeff- (Hardly Working)


I am sampling at 100 times the sine wave's frequency and 200 times the square wave's frequency.  Increasing the sampling rate completely solves my problem. But since I am acquiring several other inputs, I cannot afford a sampling rate higher than 5kHz.

 

 

 


F. Schubert wrote:

I'm not a signal processing expert, but here my basic understanding.

If you simulate sampling with 5kHz and a frequency of 50 Hz (and both are 'sync' by design), you always get an exact 5 periods. Any variation of your signals frequency gives you a propability to get 4 or 6 'trigger' events. That's an up or down of 20%!

 

The one measure to reduce such problems is using 'window functions'. They don't fit your current approach (counting instead of a DSP algorithm), so this needs to be reworked as well.

 

My approach would be to use the concept of a Locki-In amplifier. You need to phaseshift your ref-signal by 90°. Then multiply your measurement signal with the ref signal and the phase shifted ref signal. The obtained values for x/y coordinates of a complex number. Calculate the theta of the complex number (with the LV prim). Feed this in a low pass filter.

The trick on this is, that the square wave has harmonics in it, in this you are interested in the second harmonic which is the sine wave.

To get rid of the effect that the sync between sampling rate and ref signal frequency gives an error, you then can use the window I mentioned above (place it before the lock-in).

 

For a design that really plays well, use a producer-consumer design pattern to get the calculations done in parallel with the DAQ.

 

I suggest you to check on wikipedia for some of the keywords I mentioned. Go also for the external links which lead to great tutorials and AppNotes on the signal processing basics.

 

Sorry, it's not a simple solution I offer and we will have quite some conversation on this forum if you follow this path. Maybe someone else knows a simpler way.

 

Felix

www.aescusoft.de
My latest community nugget on producer/consumer design
My current blog: A journey through uml

 

An interesting view. the sine wave can indeed be looked as a second harmonic of the square wave. I will implement your idea and get back to you as soon as I get some results. But since I have very limited knowledge of signal processing, it might take me a while to get my hear around the solution you mentioned.

 

 

 

 

0 Kudos
Message 5 of 10
(7,998 Views)




@johnsold wrote:

I think both Jeff and Felix have given some good advice.

 

You really need to define how much phase error you can accept and how fast you need to measure (or estimate it from the measurements).  Note that these may be conflicting requirements.  How fast does the line frequency change?  Are the changes continuous or in discrete jumps?

 

I cannot look at your VI now.  How is the square wave generated? The Tone Frequency VI uses Fourier transform techniques internally.  If the data set is too short or the frequency changes within the data set, its estimation of the frequency may not be very good.

 

Lynn


 

The sine wave is acquired from the terminals of an electric machine that is synchronized to 50Hz. Our frequency controlling instrument keeps the frequency locked between 49.94 and 50.06 however the variations are intermittent. The square wave is just an output of a sensor that either gives 0 or 6 volts at the terminals. This sensor gives 6 volts output when a fixed point passes in front of it. That fixed point is attached to the shaft of the machine and the machine runs at 1500rpm. The output signal is looked as a pulse signal with a constant duty cycle and a frequency exactly half of frequency that the machine is synchronized at (since it is a 4 pole machine). 

Now under normal operation the phase difference between the two signals stays constant (it should if the measured phase has no error) and when the machine is subjected to a transient fault, the frequency of the pulse signal increases. The frequency during fault time is = 0.5*sine wave's frequency + unknown variable. (this unknown variable is again dependent on how severe the fault is which cannot be predicted). 

The fault is always cleared within 20-100 milliseconds and then the pulse signal's frequency get's sync with sine wave's frequency again but displaced from its original location by a certain 'phase'. We know from machine design that this phase difference will be between 0 and 12 degrees. Anything above that will trip the protective relays.

 

Using the VI, I have posted above, I save the initial phase into an excel file and then after introducing the faults, I measure the new phase and subtract it from the initial phase that gives me the displaced phase of the square wave. (Although, I haven't included the excel part in the VI posted above).

 

Currently, my phase accuracy is +/- 2.5 degrees that is 20% error since the maximum phase is 12 degrees.

I cannot allow faster sampling since this phase would be used along with other measurements from the machine to control the reactive power compensator.

I would be happy if I can reduce this error to 10%.

How fast do I need to measure this phase? I am happy to get two readings per second (for my control system) but again the faster the better of course.

 

Many Thanks,

 

Awais

0 Kudos
Message 6 of 10
(7,979 Views)

Awais,

 

It seems that you have several conflicting requirements. You would like to be able to measure the phase difference between the square wave and the sine wave with a resolution of < 1.2 degrees (referred to the sine wave).  You also do not want to sample faster than 5000 samples per second, although your reasons are not clear.

 

It appears that you are not greatly concerned with what happens during the transient faults.

 

Let's look at the phase error requirements.  At the highest steady state frequency (50.6 Hz) to sample with 1.2 degree resolution means that the sampling frequency must be >= (360/1.2)*50.6 = 15180 Hz.  For the simplest phase measurements it is convenient to sample at an integer multiple of the reference frequency.  Since you frequency can vary that means that a phase locked loop or other means of synchronization would be required.  If the sampling is not synchronous, oversampling tends to produce better results.  Given that you have indicated limited signal processing skill, I would recommend oversampling as it is much simpler.  For an application like this I would probably sample at 25 kHz or higher, even 100 kHz.  For the other parts of the system which have much slower data requirements, decimate or average the results to produce a lower equivalent sampling rate.

 

This sounds like a factory or lab test set up rather than field monitoring, so noise should not be a major issue.  The zero crossing techniques should work well under those conditions.  If the rise time of the square wave is longer than the sample period, you may need to interpolate the edge location when you catch a sample during the edge transition.

 

Lynn

Message 7 of 10
(7,956 Views)

Lynn,

 

I am using usb 6008 limited by just 10kS/s. We have another usb 6281 that we are using for signals from the induction machine that is coupled to our synchronous machine. We require very accurate measurements from induction machine and already max out the sampling rate of 6281 and cant add more chnnels. As a last resort, we might need to buy another daq but I thought I would see if there is anything I can do with labview to get a little better approximation.

 

I looked at the labview's example on phase locked loop and modified it for my task.

Again, I get a very good estimation as long as the frequency stays at exactly 50Hz. As soon as the frequency changes, the phase changes and with difference of +/-360 degrees. Could you or someone else please have a look at my program and see if I have done what I was supposed to do. From the control panel, sine wave's and pulse wave's frequencies have to be set first (50 and 25Hz).

 

Also, in the attached VI, I am generating a signal (using simulate signal) from the parameters of an acquired signal. Would labview allow me to do this when I take input signal from daq ?

I cant sleep until I figure out this PLL mystery.

 

Many Many Thanks,

 

Awais

0 Kudos
Message 8 of 10
(7,924 Views)

I do not have the modulation toolkit so I do not have the PLL Vis and cannot run your program.

 

A PLL implemented as analog electronic circuits can track frequency changes (up to some maximum rate), but the PLL VIs obviously are working with a block of samples rather than continuous inputs.  This could be a source the shifts: If the simulated signal is not phase continuous from iteration to iteration, the PLL would likely have large shifts as a result.

 

Sorry I cannot be of more help

 

Lynn

Message 9 of 10
(7,895 Views)

 


@f. Schubert wrote:

 

My approach would be to use the concept of a Locki-In amplifier. You need to phaseshift your ref-signal by 90°. Then multiply your measurement signal with the ref signal and the phase shifted ref signal. The obtained values for x/y coordinates of a complex number. Calculate the theta of the complex number (with the LV prim). Feed this in a low pass filter.

 

Felix


 

Felix,

 

Tried your method. Works well when both signals are sine waves but if one of the signals is a pulse/square wave like in my case, this method fails (VI is attached) adn please correct me if I am wrong. For two sine waves however, you can get the phase difference from power spectrum mag/phase as well which is quick and simpler than this approach.

 

 

 


@johnsold wrote:

 

 

I do not have the modulation toolkit so I do not have the PLL Vis and cannot run your program.

 

A PLL implemented as analog electronic circuits can track frequency changes (up to some maximum rate), but the PLL VIs obviously are working with a block of samples rather than continuous inputs.  This could be a source the shifts: If the simulated signal is not phase continuous from iteration to iteration, the PLL would likely have large shifts as a result.

 

Sorry I cannot be of more help

 

Lynn

 


 

This does make sense. 

Anyways, I suppose faster sampling is the only solution.

Thanks for your help and comments.

 

Awais

0 Kudos
Message 10 of 10
(7,864 Views)