Skip to contents

Computes the phase difference and maximum normalized correlation between two input signals after phase-aligning the second signal (b) to the first signal (a).

Usage

phase_diff(a, b)

Arguments

a

A numeric vector or time series representing the first signal.

b

A numeric vector or time series representing the second signal.

Value

A numeric vector of length two:

  • The first element represents the phase difference (in radians) required to maximize alignment between the two signals.

  • The second element represents the maximum normalized correlation achieved after phase alignment.

Details

[Experimental]

This function performs the following steps:

  1. Computes the Fourier Transform of both input signals using fftab.

  2. Calculates the cross-spectrum of the signals.

  3. Converts the cross-spectrum to polar form and computes the weighted average phase difference.

  4. Adjusts the phase of the second signal (b) using .shift_phase to maximize alignment with the first signal (a).

  5. Computes the normalized correlation between the phase-aligned signals.

The correlation is normalized using the variances of both signals and will generally be higher than the correlation between the original signals due to the optimal phase alignment.

Examples

phase_diff(
  sin(seq(0, 2 * pi, length.out = 128)),
  cos(seq(0, 2 * pi, length.out = 128))
)
#>  phase_diff correlation 
#>   1.5707963   0.9999504