# Analog: Calculating phase difference

#### SalceyForest

Joined Nov 6, 2015
11
To introduce myself I'm an electronic technician. To those who don't know, the difference between technicians and engineers is that a technician just doesn't have the math.

My problem is this: I have to measure the phase difference between two approximate (but always in sync) sine waves 10Hz - 200kHz. They're approximate because they're produced by pickups off rotating machinery. Could anyone point me in the direction of what area to study to resolve this problem, which is currently done by using comparators to produce a PWM signal relating to phase. I assume there are DSP chips which eat this sort of thing for breakfast but without knowing the principles I don't really know enough to choose one. The phase produced will be part of a control system, so will need to be read (or produced if fast enough) by some sort of PIC.

#### Papabravo

Joined Feb 24, 2006
17,001
There are at leas two ways to look at a pair of harmonically related sine waves. They are peaks and zero crossings. So the phase difference can be measured as a time delay between the 10Hz. zero crossing (peak) and the 200kHz zero crossing (peak). In terms of circuitry zero crossings are generally easier to detect. This time delay can be either positive or negative. Once you have the time delay you can compute the phase angle in radians.

#### SalceyForest

Joined Nov 6, 2015
11
It's odd, but you know how things suddenly make sense after you stupidly air them to the public? Forgetting the 10Hz, I was worried about the ability of hardware to measure a voltage at a suitably high frequency and then calculate a result without dropouts. But that isn't necessary is it? All I need to do is dedicate a couple of high-speed A-D's (20MHz+?) and trigger interrupts off the MSB change. Thanks!

#### crutschow

Joined Mar 14, 2008
27,930
The required AD speed is determined by the phase resolution you need.
Note that you need to look at the magnitude of the complete digital word, not just the MSBs to determine the peak point of the signal.
What is the problem with using the comparators that are now being used?

#### Papabravo

Joined Feb 24, 2006
17,001
The required AD speed is determined by the phase resolution you need.
Note that you need to look at the magnitude of the complete digital word, not just the MSBs to determine the peak point of the signal.
What is the problem with using the comparators that are now being used?
Depending on the coding of the analog word the MSB transition from 1 (negative) to 0 (positive) defines the zero crossing, not the peak.

#### crutschow

Joined Mar 14, 2008
27,930
Depending on the coding of the analog word the MSB transition from 1 (negative) to 0 (positive) defines the zero crossing, not the peak.
Would it then not be the sign bit and not the MSB?

#### Papabravo

Joined Feb 24, 2006
17,001
Would it then not be the sign bit and not the MSB?
I don't think so since most converters use twos complement notation as opposed to sign magnitude format. Assuming a 4 bit word:

-1 is 1111 in twos complement, and 1001 in sign magnitude

#### BobaMosfet

Joined Jul 1, 2009
1,883
To introduce myself I'm an electronic technician. To those who don't know, the difference between technicians and engineers is that a technician just doesn't have the math.

My problem is this: I have to measure the phase difference between two approximate (but always in sync) sine waves 10Hz - 200kHz. They're approximate because they're produced by pickups off rotating machinery. Could anyone point me in the direction of what area to study to resolve this problem, which is currently done by using comparators to produce a PWM signal relating to phase. I assume there are DSP chips which eat this sort of thing for breakfast but without knowing the principles I don't really know enough to choose one. The phase produced will be part of a control system, so will need to be read (or produced if fast enough) by some sort of PIC.

The correct solution is an OpAmp. The difference between an engineer and a technician is that the former has math. The difference between a designer and an engineer is that the former has understanding.

#### Papabravo

Joined Feb 24, 2006
17,001
The correct solution is an OpAmp. The difference between an engineer and a technician is that the former has math. The difference between a designer and an engineer is that the former has understanding.
Wrong. The correct distinction is that an engineer considers many possible solutions and picks the one that meets the requirements. Since the requirements are somewhat fuzzy at this point it is hard to make an evaluation. For the moment we are just spitballing here trying to define what done means in fundamental terms.

#### SalceyForest

Joined Nov 6, 2015
11
The required AD speed is determined by the phase resolution you need.
Note that you need to look at the magnitude of the complete digital word, not just the MSBs to determine the peak point of the signal.
What is the problem with using the comparators that are now being used?
The comparators are prone to spiking the power lines when they switch, being ultra-high speed notwithstanding decoupling also they and their associated interfacing (the input weakens as the wheel slows) are probably just unnecessary, error-inducing components. The original idea was just to buffer the inputs and ram them straight into a DSP which could do the hard work without influencing the signal in any way and then just output the speed, voltage and phase information to the PIC, probably via SPI.

#### crutschow

Joined Mar 14, 2008
27,930
The correct solution is an OpAmp. The difference between an engineer and a technician is that the former has math. The difference between a designer and an engineer is that the former has understanding.
So which category are you? An op amp isn't a solution without a circuit.
How would you use the op amp to measure phase difference?

#### Papabravo

Joined Feb 24, 2006
17,001
The comparators are prone to spiking the power lines when they switch, being ultra-high speed notwithstanding decoupling ...
Not on a correctly designed PC board they don't.

#### WBahn

Joined Mar 31, 2012
26,398
Would it then not be the sign bit and not the MSB?
Not really. Assuming the ADC output is in two's complement (probably a reasonable assumption), while we often refer to it the msb as the "sign bit" because it, alone, does give us that information, it also carries information about the magnitude of the value as well and, as a consequence, is not "just" a sign bit. The value represented by an N-bit two's complement value is

$$V \; = \; b_{N-1} 2^{-(N-1)} \; + \; \sum_{k=0}^{N-2} b_k 2^k$$

where b_k is bit k such that b_0 is the lsb.

This can be written a bit more compactly using predicate notation (think C's conditional operator) as

$$V \; = \; \sum_{k=0}^{N-1} b_k 2^{\( (k < N-1)? \; k \; : \; -k$$}
\)

#### BR-549

Joined Sep 22, 2013
4,938
Isn't a 10 Hz signal and a 200 kHz signal in sync, in phase?

If you want to monitor mechanical play, don't compare the signals with each other. Compare both inputs with an independent reference. Or two independent references locked in phase.

For sensor play, use AGC.

#### WBahn

Joined Mar 31, 2012
26,398
Isn't a 10 Hz signal and a 200 kHz signal in sync, in phase?
Not necessarily -- it depends on the precise definition being used for the term "in sync". Consider the three voltage waveforms of a three-phase power supply. Are they "in sync"? Whether the answer is yes or no depends on that precise definition. In the TS's context, it almost certainly means just that the two waveforms have a fixed relationship to each other.

#### SalceyForest

Joined Nov 6, 2015
11
Not really. Assuming the ADC output is in two's complement (probably a reasonable assumption), while we often refer to it the msb as the "sign bit" because it, alone, does give us that information, it also carries information about the magnitude of the value as well and, as a consequence, is not "just" a sign bit. The value represented by an N-bit two's complement value is

$$V \; = \; b_{N-1} 2^{-(N-1)} \; + \; \sum_{k=0}^{N-2} b_k 2^k$$

where b_k is bit k such that b_0 is the lsb.

This can be written a bit more compactly using predicate notation (think C's conditional operator) as

$$V \; = \; \sum_{k=0}^{N-1} b_k 2^{\( (k < N-1)? \; k \; : \; -k$$}
\)
Sadly, that is EXACTLY what I meant by not having the math, although the tex reduction (V \; = \; \sum_{k=0}^{N-1} b_k 2^{$$(k < N-1)? \; k \; : \; -k$$}) etc. is much more comprehensible.

Yes, the two waveforms are in a fixed relationship derived by a rotating shaft at varying speed.

#### WBahn

Joined Mar 31, 2012
26,398
Sadly, that is EXACTLY what I meant by not having the math, although the tex reduction (V \; = \; \sum_{k=0}^{N-1} b_k 2^{$$(k < N-1)? \; k \; : \; -k$$}) etc. is much more comprehensible.

Yes, the two waveforms are in a fixed relationship derived by a rotating shaft at varying speed.
Are the two waveforms always at the same frequency? If not, what is the relationship between the two frequencies? Some of the responses you've gotten have interpreted your initial post as saying that one waveform is at 10 Hz and the other is at 200 kHz -- is that what you meant?

What do these waveforms look like? Are they sinusoidal?