Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created January 15, 2024 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanbsd/6e4249fef9b5f0240f0e31d8fb3130a4 to your computer and use it in GitHub Desktop.
Save romanbsd/6e4249fef9b5f0240f0e31d8fb3130a4 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
def cross_correlation(x, y):
# Calculate cross-correlation using convolution
ccf_values = np.convolve(x, y[::-1], mode='full') / np.sum(y**2)
# Adjust indices to represent lags
lags = np.arange(-(len(y) - 1), len(x))
return lags, ccf_values
# Generate two sample signals
np.random.seed(42)
signal_x = np.random.randn(100)
signal_y = np.random.randn(50)
# Calculate cross-correlation
lags, ccf_values = cross_correlation(signal_x, signal_y)
# Plot cross-correlation values
plt.stem(lags, ccf_values)
plt.title('Cross-Correlation Function (CCF)')
plt.xlabel('Lag')
plt.ylabel('CCF Value')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment