Created
January 15, 2024 13:05
-
-
Save romanbsd/6e4249fef9b5f0240f0e31d8fb3130a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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