# LabVIEW

cancel
Showing results for
Did you mean:

## Phase difference of two sine curves with different frequency

hello everyone,
for an experiment I use an accelerometer which records the accelerations in three spatial directions. From two of these axes I get a sinusoidal signal. I want to determine the phase offset of the two signals. The signals have different frequencies, which are a multiple of each other. For example: Signal 1 = 20Hz; Signal 2 = 40Hz.
I have created the VI according to a similar instruction from the forum. The circuit works for the same frequencies for signal 1 and signal 2, but not for different frequencies.
For any help I would be very grateful Message 1 of 15
(225 Views)

## Re: Phase difference of two sine curves with different frequency

Hi Max,

@maxmustermann132 wrote:

The circuit works for the same frequencies for signal 1 and signal 2, but not for different frequencies.

Usually the phase difference is calculated for signals of the same frequency!

Which "circuit" are you talking about? Is there any electronics PCB involved?

When you really need the phase difference of your two signals you need to calculate the phase of each sample (will be possible for sinusoidal signals). Then you can subtract one array of phase values from the other.

(I guess this will be much easier to solve without using any ExpressVIs or DDT wires!)

Best regards,
GerdW  using LV2011SP1 + LV2017 (+LV2020 sometimes) on Win10+cRIO
Message 2 of 15
(202 Views)

## Re: Phase difference of two sine curves with different frequency

Here's a simple Thought Experiment to show that "your question is wrong".  Suppose you have two sine curves with different frequencies, say 1 Hz and 1.01 Hz.  Operationally define Phase as the difference (in time) between the Nth peak of each wave.  The first peak of the first wave of the 1 Hz sine will be at 0.25", while the peak of the first wave of the second will be at 0.2525", a difference of 0.0025".  The next "phase difference" will be twice this, 0.0050", the third "phase difference" will be 0.0075", etc.

Bob Schor

Message 3 of 15
(186 Views)

## Re: Phase difference of two sine curves with different frequency

This is what I do when I want to find the phase difference of signals with different frequencies provided that my frequencies are all harmonics of each other.

Assume your 20 Hz signal is the following

Cos[2 pi f t + theta] where f is the frequency, t is the time, and theta is the phase.

To get the phase of the 40 Hz signal relative to the 20 Hz signal do the following

Cos[2*(2 pi f t + theta)] so 2*theta is the phase of your "pseudo"-40Hz signal.

Use that 2*theta as your reference phase to compute 40Hz phase difference.

mcduff

EDIT: Multiply your reference by itself, remove mean, and measure phase

Message 4 of 15
(181 Views)

## Re: Phase difference of two sine curves with different frequency

Hello mcduff,

thank you for your reply. It is exactly what I am looking for. In my case, the frequencies are always integer multiples (harmonics) of each other.
I am not sure if I understood your solution correctly. I have now multiplied the signal 1 (20Hz) by itself. What do you meant by removing mean? Do I still have to derive my sinus signal for your solution into a cos function?
A new image is attached, with this adjustment I get a stable phase shift (the phase shift does not jump now like before), but it does not match the real value. Do you have an idea where my error lies?

vi is also attached.

Thanks in advance! Message 5 of 15
(152 Views)

## Re: Phase difference of two sine curves with different frequency

Suggestions:

1. Get rid of the compression to measure phase. Not sure how it works, it may be introducing aliasing if not done correctly.
2. Play with some example so you understand what is going on. (See below and attachment)
3. Get rid of dynamic data, see simplified program attached.

Look at some math identities;  whenever you multiply sines and cosines you get sum and difference frequencies. So if we multiply a sine by itself the sum would be f+f, the difference f-f, so a DC signal also. You are using a FFT to measure phase so not an issue here.

Understand how your functions are working; see the example I sent; there is a -90(270) phase shift when squaring the signal. (See below)  mcduff

Message 6 of 15
(141 Views)

## Re: Phase difference of two sine curves with different frequency

How big is the phase difference and over what range can it vary. Compared to a 20Hz signal, a 40Hz signal will have twice as many periods and you cannot tell the difference once it is shifted by one period. (If you had a quadrature signal of both waves, the higher frequency would go around twice in the complex plane. So while the 20Hz signal would do a 360, the 40Hz signal would do a 720 (in snowboarder terms) over one cycle of the lower frequency).

So only if you expect a relatively small phase shift, you could compare the upslope zero crossing of the 20Hz signal with the nearest upslope zero crossing of the 40Hz signal and map it into a 20Hz phase difference. LabVIEW Champion. It all comes together in GCentral What does "Engineering Redefined" mean??
Message 7 of 15
(131 Views)

## Re: Phase difference of two sine curves with different frequency

@maxmustermann132 wrote:

but it does not match the real value.

You need to think about your Phase Reference and how you define it. As long as you are consistent and keep track of things then it should work out. BUT for simplicity I suggest using a COSINE as your reference.

For example assume you use a SIN wave as your reference. You square the signal to get the 2nd harmonic. The signal looks like this(I subtracted the DC part) Sin(blue), Sin^2(Orange)

Like the example I showed earlier there is a phase shift between the two. Why, because

Sin^2(2 pi f t) = 1/2(1 - Cos(2*(2 pi f t) ) )

Notice how the sin went to cos and a negative amplitude, this means a -90(270) degree shift; just like in the example I posted earlier.

Now see what happens when you use a COS wave as your reference. You square the signal to get the 2nd harmonic. The signal looks like this(I subtracted the DC part) Cos(blue), Cos^2(orange)

Notice they start in phase! Why, because

Cos^2(2 pi f t) = 1/2(1 + Cos(2*(2 pi f t) ) )

Suggestion:

Make your own 10 Hz reference; generate a COS wave. Use that to measure the phase difference for your 10 Hz signal. Square the reference and use that to measure the 20 Hz signal.

Otherwise you need to know how much sin wave is in your reference and keep track of that when you square the signal.

mcduff

Message 8 of 15
(113 Views)

## Re: Phase difference of two sine curves with different frequency

Hello Mcduff,
Thank you for your detailed help, you have really helped us a lot. We now have a suitable circuit for the second harmonic. Now we try to determine the phase shift from the first to the third harmonic oscillation with the following formula: we have tested the circuit on your example and it gives us correct results. If we now insert the circuit into our plan, we do not get a suitable result. Do you see the problem or do we have a mistake in thinking?  Message 9 of 15
(74 Views)

## Re: Phase difference of two sine curves with different frequency

As I said earlier, the simplest method is to choose your own reference.

Here I would choose Cos(2 pi f t) with a 0 degree/radian phase shift; that is, I would make my OWN SYNTHETIC reference. For higher harmonics, just multiply (2 pi f t) by whatever integer you want. Don't worry about squaring, cubing, etc, it is not necessary.

If you want to use a reference from one of your current signals then you need to do a bit more work. The multiplication(squaring, etc) will get you harmonics of the current frequency, but you NEED to make TWO references, I & Q (X & Y). These references are 90 out of phase of each other. You need to multiply with you signal then Low Pass Filter, get the amplitudes, then take the ArcTan. It is too much to reply here. (This is how a lock-in amplifier works, sorry I don't know any other way to get the phase.)

SRS has an excellent tutorial about how a lock-in and this method works. It can be implemented in LabVIEW; however, for simplicity I would still go with a synthetic Cos as my reference.

Tutorial attached; the first two pages should explain everything.

mcduff

Message 10 of 15
(65 Views)