本文整理汇总了Python中numpy.correlate方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.correlate方法的具体用法?Python numpy.correlate怎么用?Python numpy.correlate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.correlate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _autocorr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def _autocorr(data):
"""
Calculates the auto correlation of a given array.
Parameters
----------
data : array-like
The array to calculate the autocorrelation of
Returns
-------
u : ndarray
The array of autocorrelations
"""
u = np.correlate(data, data, mode='full')
# Take upper half of correlation matrix
return u[u.size // 2:]
示例2: _presample_varcov
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def _presample_varcov(self, params):
"""
Returns the inverse of the presample variance-covariance.
Notes
-----
See Hamilton p. 125
"""
k = self.k_trend
p = self.k_ar
p1 = p+1
# get inv(Vp) Hamilton 5.3.7
params0 = np.r_[-1, params[k:]]
Vpinv = np.zeros((p, p), dtype=params.dtype)
for i in range(1, p1):
Vpinv[i-1, i-1:] = np.correlate(params0, params0[:i],)[:-1]
Vpinv[i-1, i-1:] -= np.correlate(params0[-i:], params0,)[:-1]
Vpinv = Vpinv + Vpinv.T - np.diag(Vpinv.diagonal())
return Vpinv
示例3: ar_generator
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def ar_generator(N=512, sigma=1.):
# this generates a signal u(n) = a1*u(n-1) + a2*u(n-2) + ... + v(n)
# where v(n) is a stationary stochastic process with zero mean
# and variance = sigma
# this sequence is shown to be estimated well by an order 8 AR system
taps = np.array([2.7607, -3.8106, 2.6535, -0.9238])
v = np.random.normal(size=N, scale=sigma**0.5)
u = np.zeros(N)
P = len(taps)
for l in range(P):
u[l] = v[l] + np.dot(u[:l][::-1], taps[:l])
for l in range(P,N):
u[l] = v[l] + np.dot(u[l-P:l][::-1], taps)
return u, v, taps
#JP: small differences to using np.correlate, because assumes mean(s)=0
# denominator is N, not N-k, biased estimator
# misnomer: (biased) autocovariance not autocorrelation
#from nitime.utils
示例4: extractMseq
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def extractMseq(cover, stego, secret_length, m, tau=1):
u"""Extract secret informations by spread spectrum using m-sequence.
@param cover : cover data (2 dimensional np.ndarray)
@param stego : stego data (2 dimension np.ndarray)
@param secret_length : length of secret information
@param m : M-Sequence
@param tau : embed shift interval
@return secret : extracted secret information
"""
cover = _image2vrctor(cover)
stego = _image2vrctor(stego)
m_length = len(m)
data = stego - cover
data = data[:m_length:tau]
secret_data = correlate(m, data, cycle=CYCLE)
center = ((m_length-1)*2+1)//2
secret_data = secret_data[center:center+secret_length]
secret_data = list(map(_checkData, secret_data))
return secret_data
示例5: test_rank0
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def test_rank0(self, dt):
a = np.array(np.random.randn()).astype(dt)
a += 1j * np.array(np.random.randn()).astype(dt)
b = np.array(np.random.randn()).astype(dt)
b += 1j * np.array(np.random.randn()).astype(dt)
y_r = (correlate(a.real, b.real)
+ correlate(a.imag, b.imag)).astype(dt)
y_r += 1j * (-correlate(a.real, b.imag) + correlate(a.imag, b.real))
y = correlate(a, b, 'full')
assert_array_almost_equal(y, y_r, decimal=self.decimal(dt) - 1)
assert_equal(y.dtype, dt)
assert_equal(correlate([1], [2j]), correlate(1, 2j))
assert_equal(correlate([2j], [3j]), correlate(2j, 3j))
assert_equal(correlate([3j], [4]), correlate(3j, 4))
示例6: test_consistency_correlate_funcs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def test_consistency_correlate_funcs(self):
# Compare np.correlate, signal.correlate, signal.correlate2d
a = np.arange(5)
b = np.array([3.2, 1.4, 3])
for mode in ['full', 'valid', 'same']:
assert_almost_equal(np.correlate(a, b, mode=mode),
signal.correlate(a, b, mode=mode))
assert_almost_equal(np.squeeze(signal.correlate2d([a], [b],
mode=mode)),
signal.correlate(a, b, mode=mode))
# See gh-5897
if mode == 'valid':
assert_almost_equal(np.correlate(b, a, mode=mode),
signal.correlate(b, a, mode=mode))
assert_almost_equal(np.squeeze(signal.correlate2d([b], [a],
mode=mode)),
signal.correlate(b, a, mode=mode))
示例7: test_float
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def test_float(self):
self._setup(float)
z = np.correlate(self.x, self.y, 'full')
assert_array_almost_equal(z, self.z1)
z = np.correlate(self.x, self.y[:-1], 'full')
assert_array_almost_equal(z, self.z1_4)
z = np.correlate(self.y, self.x, 'full')
assert_array_almost_equal(z, self.z2)
z = np.correlate(self.x[::-1], self.y, 'full')
assert_array_almost_equal(z, self.z1r)
z = np.correlate(self.y, self.x[::-1], 'full')
assert_array_almost_equal(z, self.z2r)
z = np.correlate(self.xs, self.y, 'full')
assert_array_almost_equal(z, self.zs)
示例8: test_object
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def test_object(self):
self._setup(Decimal)
z = np.correlate(self.x, self.y, 'full')
assert_array_almost_equal(z, self.z1)
z = np.correlate(self.y, self.x, 'full')
assert_array_almost_equal(z, self.z2)
示例9: test_no_overwrite
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def test_no_overwrite(self):
d = np.ones(100)
k = np.ones(3)
np.correlate(d, k)
assert_array_equal(d, np.ones(100))
assert_array_equal(k, np.ones(3))
示例10: test_complex
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def test_complex(self):
x = np.array([1, 2, 3, 4+1j], dtype=complex)
y = np.array([-1, -2j, 3+1j], dtype=complex)
r_z = np.array([3-1j, 6, 8+1j, 11+5j, -5+8j, -4-1j], dtype=complex)
r_z = r_z[::-1].conjugate()
z = np.correlate(y, x, mode='full')
assert_array_almost_equal(z, r_z)
示例11: xcorr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def xcorr(x, y, maxlags=None):
"""Cross-correlation between two 1D signals of the same length."""
ns = len(x)
if len(y) != ns:
raise ValueError("x and y should have the same length.")
maxlags = maxlags or ns - 1
return np.correlate(x, y, mode='full')[ns - 1 - maxlags:ns + maxlags]
示例12: test_float
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def test_float(self):
self._setup(np.float)
z = np.correlate(self.x, self.y, 'full')
assert_array_almost_equal(z, self.z1)
z = np.correlate(self.x, self.y[:-1], 'full')
assert_array_almost_equal(z, self.z1_4)
z = np.correlate(self.y, self.x, 'full')
assert_array_almost_equal(z, self.z2)
z = np.correlate(self.x[::-1], self.y, 'full')
assert_array_almost_equal(z, self.z1r)
z = np.correlate(self.y, self.x[::-1], 'full')
assert_array_almost_equal(z, self.z2r)
z = np.correlate(self.xs, self.y, 'full')
assert_array_almost_equal(z, self.zs)
示例13: test_complex
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def test_complex(self):
x = np.array([1, 2, 3, 4+1j], dtype=np.complex)
y = np.array([-1, -2j, 3+1j], dtype=np.complex)
r_z = np.array([3-1j, 6, 8+1j, 11+5j, -5+8j, -4-1j], dtype=np.complex)
r_z = r_z[::-1].conjugate()
z = np.correlate(y, x, mode='full')
assert_array_almost_equal(z, r_z)
示例14: ccf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def ccf(x, y, unbiased=True):
'''cross-correlation function for 1d
Parameters
----------
x, y : arrays
time series data
unbiased : boolean
if True, then denominators for autocovariance is n-k, otherwise n
Returns
-------
ccf : array
cross-correlation function of x and y
Notes
-----
This is based np.correlate which does full convolution. For very long time
series it is recommended to use fft convolution instead.
If unbiased is true, the denominator for the autocovariance is adjusted
but the autocorrelation is not an unbiased estimtor.
'''
cvf = ccovf(x, y, unbiased=unbiased, demean=True)
return cvf / (np.std(x) * np.std(y))
示例15: norm_corr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import correlate [as 别名]
def norm_corr(x,y,mode = 'valid'):
"""Returns the correlation between two ndarrays, by calling np.correlate in
'same' mode and normalizing the result by the std of the arrays and by
their lengths. This results in a correlation = 1 for an auto-correlation"""
return ( np.correlate(x,y,mode) /
(np.std(x)*np.std(y)*(x.shape[-1])) )
# from matplotlib axes.py
# note: self is axis