Lesson 14 of 15
Cross-Correlation
Cross-Correlation
Cross-correlation measures the similarity between two signals as a function of time lag. It is used in radar, sonar, communications, and audio fingerprinting.
Definition
The normalized cross-correlation at lag for periodic signals of length :
The modulo wraps around the signal, treating it as periodic (circular cross-correlation).
Autocorrelation
The autocorrelation of a signal with itself:
At lag , autocorrelation equals signal power. It is symmetric: .
Applications
- Time delay estimation: find the lag where is maximized to locate a signal source
- Pitch detection: autocorrelation peaks at lags corresponding to the period
- Pattern matching: cross-correlate a template with a signal to find occurrences
Example
, (same signal):
- Lag 0:
- Lag 1:
- Lag 2:
Your Task
Implement:
xcorr(x, y, lag)— normalized circular cross-correlation at lagautocorrelation(x, lag)— callsxcorr(x, x, lag)
def xcorr(x, y, lag):
N = len(x)
return sum(x[n] * y[(n + lag) % N] for n in range(N)) / N
def autocorrelation(x, lag):
return xcorr(x, x, lag)Python runtime loading...
Loading...
Click "Run" to execute your code.