-
Notifications
You must be signed in to change notification settings - Fork 0
/
correlations.py
68 lines (46 loc) · 1.67 KB
/
correlations.py
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
52
53
54
55
56
57
58
59
60
61
62
63
#
# PERIODIC CORRELATIONS VIA FFT
#
# Written by Sam Blake.
# TODO: use FFTW via pyFFTW
import math
import numpy as np
from scipy.fftpack import fftn, ifftn
import pyfftw
# See https://hgomersall.github.io/pyFFTW/pyfftw/interfaces/scipy_fftpack.html#module-pyfftw.interfaces.scipy_fftpack
def crosscorrelate_fftw(a,b):
"As with crosscorrelate_fftpack, but much much faster due to fftw."
# TODO: threads should not be manually set here.
# TODO: use real ffts.
a_fft = pyfftw.interfaces.scipy_fftpack.fftn(a, threads = 1, planner_effort = 'FFTW_ESTIMATE')
b_fft = np.conj(pyfftw.interfaces.scipy_fftpack.fftn(b, threads = 1, planner_effort = 'FFTW_ESTIMATE'))
ab_fft = np.multiply(a_fft, b_fft)
ab = pyfftw.interfaces.scipy_fftpack.ifftn(ab_fft, threads = 1, planner_effort = 'FFTW_ESTIMATE')
#if np.allclose(np.imag(ab), 0.0, 1.0e-5):
# return np.real(ab)
#else:
# return ab
return ab
def crosscorrelate_fftpack(a,b):
"Computes the periodic cross-correlation of the N-dimensional arrays /a/ and /b/ using the \
FFT and IFFT. This is the slow version which uses scipy.ffpack"
a_fft = fftn(a)
b_fft = np.conj(fftn(b))
ab_fft = np.multiply(a_fft, b_fft)
ab = ifftn(ab_fft)
if np.allclose(np.imag(ab), 0.0, 1.0e-5):
return np.real(ab)
else:
return ab
def autocorrelate_fftw(a):
"Computes the periodic autocorrelation from the cross-correlation."
auto = crosscorrelate_fftw(a,a)
return auto
def autocorrelate_fftpack(a):
"Computes the periodic autocorrelation from the cross-correlation."
return crosscorrelate_fftpack(a,a)
def max_off_peak(a):
"Computes the absolute maximum off-peak correlation."
na = np.abs(a)
na.flat[0] = 0.0
return np.max(na)