# Detecting phase between square wave and sine wave signals

I have a circuit that has a digital square wave input (generated by PLD, 1.8Vp) and a sine wave output (0.5 - 3.5 Vp). Both signals have 100kHz frequency, however the phase is different.

What is a good way to detect the phase difference between these two signals? Phase detectors I've seen so far are for either all digital or all analog signals? Is there one for the mixed signals circuit like the one I have?

Update

Knowing the phase difference with 1 degree is sufficient for my application. The frequencies are always locked relative to each other and never change. The square wave drives the analog electronics and analogs produce the sine wave which has AM modulated signal in it. The amplitude of the signal is, however, very low compared to the amplitude of the carrier. Due to the production variability the analogs (include some hand-winded inductors) have high unit to unit variability of the phase, and I am trying come up with an auto-tuning method for the DSP that processes the output sine wave.

• How accurately do you need to know the phase difference? And do you need to do this as a one-time experiment or as part of the ongoing function of your circuit? Are the two parts actually frequency-locked together (or derive their frequency from a common reference) or are they just both nominally 100 kHz? Aug 2, 2012 at 16:42

Phase detection is the easiest for digital signals; it's basically an XOR gate. I would convert the sine to a square wave. Feed a comparator with the sine on one input and the averaged sine (LPF) on the other, so that the comparator gives a 50 % duty cycle square wave. Then use a digital phase detector.

• I've considered using a comparator to make a sine wave out of a square wave, but the problem is that the amplitude of the sine wave may vary within 0.5 to 3.5 V range. This is a production variability and there is nothing I can do about that fact. Though, zero cross detection may be a solution in that case... May be worth considering. Aug 2, 2012 at 14:52
• @udushu - The averaging LPF solves that. It will take care of varying amplitude and DC offset. Aug 2, 2012 at 14:54
• @stevenvh, I don't think the LPF is enough to guarantee that the input signal amplitude doesn't affect the phase measurement. Comparators have a property called dispersion that means that the propagation delay changes as the input amplitude changes. It looks like a comparator that can meet OP's needs for phase error (about 40 ns) without further compensation should also have low enough dispersion; but it will be an important thing to check when picking a comparator for this use. Aug 3, 2012 at 4:52
• @ThePhoton - You've got a point, but OP hadn't mentioned his 1° accuracy yet when I answered. OTOH there are comparators with much less than 100 ps dispersion, which is a few orders of magnitude better than the 1°. Aug 3, 2012 at 5:01
• @ThePhoton - It seems that zero-cross comparator is a solution. Thanks all for the discussion. Aug 7, 2012 at 14:32

Since you say you have a DSP processing the sine wave, you can use a complex Fourier transform to measure the phase (you need only evaluate the DFT at the known frequency).

This is actually closely related to what Curd suggested about mixing - a single point DFT is a type of mixer followed by integrators or low-pass filters. The difference is that by doing it in the complex domain (or using an IQ mixer in the analog one) you can determine the angle of the complex output. Using only the real components or only a single mixer, you cannot tell lead from lag and amplitude sensitivity would be more of a challenge.

• Wish that I could. That would have solved a lot of problems I am currently having. Unfortunately, the DSP I have to use has nowhere near the horsepower needed to do real-time DFTs on a 100kHz signal. Aug 7, 2012 at 14:29
• You only have to calculate one frequency bin - not the normal #bins=#samples people associate with a fourier transform. Also, how often do you have to measure the phase? If infrequently, the real question is if your system can sample and store at that rate, not if it can process at it. Aug 7, 2012 at 15:30
• The DSP is currently configured to sample at 20kHz and only the demodulated signal is being sampled, not the 100kHz carrier. I don't think that the humble dsPIC33 I am using will be able to sample a 100kHz signal at all. Aug 7, 2012 at 20:03
• @udushu a quick search suggests the dsPIC may be able to do about a megasample/second. But if the analog bandwidth is sufficient and you know the frequency, you can also intentionally undersample and exploit the aliasing. Aug 8, 2012 at 18:58

Assuming that the amplitudes of both input signals are constant (if not they could be made constant by a AGC circuit) you can use a mixer (multiplicator) as phase detector:

If the signals are in phase the output will be positive.
If the signals are 180° out of phase the output will be negative.
For other phase differences the output will be somewhere between those values.

E.g. phase detection is mentioned in the datasheet as one of the applications of analog mutliplier IC AD633 .