-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossCorrelation.py
More file actions
52 lines (38 loc) · 1.39 KB
/
crossCorrelation.py
File metadata and controls
52 lines (38 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 14 2016
Autor: DiegoDZ
Compute the cross correlation between all columns of two input matrix, A and B,
using the Fast Fourier Transform.
run: python crossCorrelation.py input_A input_B > output
"""
import numpy as np
import sys
def crossCorrelation(arg1,arg2):
#Load files
A = np.loadtxt(str(arg1))
B = np.loadtxt(str(arg2))
#Define params
nSteps = len(A)
nNodes = len(A[0])
#Cross-correlation works as follows:
#1. Take FFT of each column of both input matrix
#2. Multiply one resulting transform by the complex conjugate of the other
#3. Calculate the inverse transform of the product
a = np.zeros((nSteps, nNodes),dtype=complex)
b = np.zeros((nSteps, nNodes),dtype=complex)
for i in range(0,nNodes,1):
a[:,i] = np.fft.fft(A[:,i] - np.mean(A[:,i]))
b[:,i] = np.conjugate(np.fft.fft(B[:,i]- np.mean(B[:,i])))
#Multiply all columns of matrix a by all columns of matrix b.
C = (a[...,None]*b[:,None]).reshape(a.shape[0],-1)
#Calculate the inverse
CAB = np.zeros((nSteps, nNodes*nNodes))
for j in range (0, nNodes*nNodes, 1):
CAB[:,j] = np.fft.ifft(C[:,j])
CAB /= nSteps
return CAB[0 : nSteps/2, :]
CAB = crossCorrelation(sys.argv[1],sys.argv[2])
#Print the result in the correct format
print '\n'.join(' '.join(str(cell) for cell in row) for row in CAB)
#EOF