# PHASEDIFF

## Purpose:

Computes the phase difference between two sinusoids.

## Syntax:

PHASEDIFF(s1,
s2, freq)

(p,
f, mag1, mag2) = PHASEDIFF(s1, s2, freq)

s1 |
- |
A
series, the first input sinusoid. |

s2 |
- |
A
series, the second input sinusoid |

freq |
- |
Optional.
A real, the known frequency of the input sinusoids. Defaults to
-1, estimate the frequency. |

## Returns:

A real, the phase difference in radians.

(p, f, mag1, mag2) = PHASEDIFF(s1,
s2, f) returns the phase difference, frequency and magnitudes of
the two sinusoids.

## Example:

W1: gcos(1000, 1/1000, 4)

W2: gcos(1000, 1/1000, 4, -pi/3)

W3: {phasediff(w1, w2)}

W3 == -1.047198 or -π/3.

## Example:

W1: gcos(1000, 1/1000, 4) + gnorm(1000, 1/1000)/10

W2: gcos(1000, 1/1000, 4, -pi/3) + gnorm(1000, 1/1000)/10

W3: {phasediff(w1, w2)}

W3 == -1.044738 or approximately -π/3.

## Example:

W1: 10 * gcos(1000, 1/1000, 4)

W2: 3 * gcos(1000, 1/1000, 4, -pi)

W3: (p, f, m1, m2) = phasediff(w1, w2); {p, f, m1,
m2}

W3 == {-3.141593, 4, 10, 3} the phase difference, frequency and magnitudes
of the input sinusoids in W1 and W2.

## Remarks:

PHASEDIFF uses SINFIT3 to compute
the least squares fit of a sinusoid to a known frequency as per the IEEE
1241 Standard.

If the input frequency is unspecified, the frequency is estimated by
locating the dominant frequency of the FFT
and using analytical rectangular window interpolation to determine the
precise frequency. The FFT interpolation handles estimated frequencies
that do not lie on a unique FFT bin.

The phase difference is always negative and expressed in radians.

## See Also:

ADDPHASE

ANGLE

FFT

PHASE

SINFIT3

## References:

IEEE Std 1241-2010 Annex B.1 "An
algorithm for three-parameter (known frequency) least-squares fit to sine-wave
data"