Auto Correlation
Auto correlation, sometimes called serial correlation in the case of discrete time, is the correlation of a signal with a delayed copy of itself as a function of delay. Informally, it is the similarity between observations of a random variable as a function of the time interval between them. Auto correlation analysis is a mathematical tool for finding repeating patterns, such as the presence of a periodic signal obscured by noise, or identifying the missing fundamental frequency in a signal implicated by its harmonic frequencies. It is often used in signal processing to analyze functions or sets of values, such as signals in the time domain.
Cross Correlation
In signal processing, cross-correlation is a measure of the similarity of two series as a function of the displacement of one relative to the other. This is also known as the sliding dot product or the sliding inner product. It is commonly used to search a long signal for a known shorter feature. It has applications in pattern recognition, single particle analysis, electron tomography, averaging, cryptanalysis, and neuro physiology. Cross correlation is similar in nature to the convolution of two functions.In an auto correlation, which is the cross-correlation of a signal with itself, there will always be a zero-delay peak and its size will be the energy of the signal.
Python Script – Auto Correlation
#Auto and Cross correlation python code
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#This is cross correlation part
#in1=[1, 2, 3, 4, 5, 6,7,8,9]
#in2=[11,12,13,14,15,16,17,18,19]
#This is for auto correlation part
in1=[1, 2, 3, 4, 5, 6,7,8,9]
in2=[0,0,0,0,1, 2, 3, 4, 5, 6,7,8,9]
print(in1)
print(in2)
if len(in1) > len(in2):
pad = len(in1)-len(in2)
in2=np.append([in2],[np.zeros(pad)])
elif len(in1) < len(in2):
pad = len(in2)-len(in1)
in1=np.append([in1],[np.zeros(pad)])
out_len = len(in1)
out = np.zeros(out_len)
corr = np.corrcoef(in1, in2)
print(corr)
tmp= in2
tmp = np.transpose(tmp)
i = 0
while i < out_len:
out[i] = in1[i] * tmp[i]
i += 1
indexMaxValue = max(out)
print(indexMaxValue)
peak_index = pd.Series(out).idxmax()+1
print("Number of zeros in delayed vector =", len(in2)- peak_index)
print("Index of Max or peak value from start i.e. position 0 =", pd.Series(out).idxmax())
plt.plot(out)
plt.show()
Input Vector – Auto Correlation
in1=[1, 2, 3, 4, 5, 6,7,8,9]
in2=[0,0,0,0,1, 2, 3, 4, 5, 6,7,8,9]
Input Vector – Cross Correlation
in1=[1, 2, 3, 4, 5, 6,7,8,9]
in2=[11,12,13,14,15,16,17,18,19]
Output – Auto Correlation
Output – Cross Correlation