I have two squarewaves of the same frequency but with different dutycycle. How can I calculate the phase difference between the two? I know that for sinewaves one can use the correlation coefficient, but I am not sure how to do it with squarewaves, especially if the duty cycle is different. I was thinking maybe it is possible by calculating the area under the product of the two waves, which is basically the correlation coefficient, but how is the phase difference related to that?
-
$\begingroup$ There is no such thing. Both square waves have a harmonic spectrum and each spectral line will have a different phase (and amplitude) difference. There is no single number you could meaningfully define as "the phase difference". $\endgroup$– HilmarAug 11 at 12:11
-
$\begingroup$ @Hilmar someone can identify the phase of the fundamental as being the reference phase for each square wave. $\endgroup$– robert bristow-johnsonAug 11 at 12:50
2 Answers
If I were to define the reference phase of a square wave with different duty cycles (PWM), it would be like this. First I would define this PWM square wave as an even-symmetry function:
$$ x(t) \triangleq \begin{cases} 1 - K_\mathrm{d} \qquad & |t|<\frac{K_\mathrm{d}P}{2} \\ \\ -K_\mathrm{d} \qquad & \frac{K_\mathrm{d}P}{2}<|t|<\frac{P}{2} \\ \\ x\big(t-\left\lfloor\frac{t}{P}+\frac12\right\rfloor P\big) \qquad & |t|>\frac{P}{2} \\ \end{cases} $$
$x(t+P)=x(t) \quad \forall t$ is periodic with fundamental frequency $\frac1P$ and $0<K_\mathrm{d}<1$ is the duty cycle. Because this is an even function (or $x(-t)=x(t)$), then only the cosine terms remain in the Fourier series.
Then phase aligning two of these square waves, with the same period $P$ but possibly different $K_\mathrm{d}$ can be well defined with cross-correlating the two and looking for the maximum lag. After thinking about this, there could be flat spots in the result of cross-correlation, so pick the middle of the flat peak as the "maximum" lag. Then that lag is normalized by $P$ and you have a relative phase.
This can be done with two flip flops and an xor gate (followed by an RC low pass filter). An XOR gate can be used as a multiplier of hard-limited sinusoids (square waves), which is ideal for producing a linear voltage versus phase response (phase detector), that is unique from 0 to 180 degrees.
Here specifically phase is used as an alternative to the time delay between the two waveforms, converting time delay to phase using:
$$\theta = 2\pi f \tau$$
Where $f = 1/T$ as the frequency of the square wave, and $T$ as the period.
Each flip flip is used as a frequency divider, thus ensuring a 50% duty cycle as needed for the accurate phase measurement. Since we are dividing by two, the phase between the resulting 50% duty cycle square waves at half the original frequency will also be divided by two, so the result will be a voltage that is directly proportional to the phase divided by two.
In all digital implementations, Time Interval Counters can be used when a higher frequency reference clock is available. The rising edge of the first signal starts the counter, and the rising edge of the second signal stops the counter, providing a count proportional to the time delay (which is related to the phase as provided above).
-
$\begingroup$ So it's the phase difference of the half-frequency waveforms? $\endgroup$ Aug 13 at 18:09
-
$\begingroup$ @robertbristow-johnson Right, the time difference is the same but the period is twice as long so the computed phase is half. This is consistent with when we go the other way, frequency multiplication, from $2\cos(2\pi f t + \phi)^2 = 1 + \cos(2\pi 2f t + 2\phi)$: in a frequency doubler, we double the frequency but also double the phase. $\endgroup$ Aug 13 at 21:23