本文整理汇总了Python中scipy.linalg.solve_toeplitz方法的典型用法代码示例。如果您正苦于以下问题:Python linalg.solve_toeplitz方法的具体用法?Python linalg.solve_toeplitz怎么用?Python linalg.solve_toeplitz使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.linalg
的用法示例。
在下文中一共展示了linalg.solve_toeplitz方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_solve_equivalence
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def test_solve_equivalence():
# For toeplitz matrices, solve_toeplitz() should be equivalent to solve().
random = np.random.RandomState(1234)
for n in (1, 2, 3, 10):
c = random.randn(n)
if random.rand() < 0.5:
c = c + 1j * random.randn(n)
r = random.randn(n)
if random.rand() < 0.5:
r = r + 1j * random.randn(n)
y = random.randn(n)
if random.rand() < 0.5:
y = y + 1j * random.randn(n)
# Check equivalence when both the column and row are provided.
actual = solve_toeplitz((c,r), y)
desired = solve(toeplitz(c, r=r), y)
assert_allclose(actual, desired)
# Check equivalence when the column is provided but not the row.
actual = solve_toeplitz(c, b=y)
desired = solve(toeplitz(c), y)
assert_allclose(actual, desired)
示例2: test_unstable
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def test_unstable():
# this is a "Gaussian Toeplitz matrix", as mentioned in Example 2 of
# I. Gohbert, T. Kailath and V. Olshevsky "Fast Gaussian Elimination with
# Partial Pivoting for Matrices with Displacement Structure"
# Mathematics of Computation, 64, 212 (1995), pp 1557-1576
# which can be unstable for levinson recursion.
# other fast toeplitz solvers such as GKO or Burg should be better.
random = np.random.RandomState(1234)
n = 100
c = 0.9 ** (np.arange(n)**2)
y = random.randn(n)
solution1 = solve_toeplitz(c, b=y)
solution2 = solve(toeplitz(c), y)
assert_allclose(solution1, solution2)
示例3: test_multiple_rhs
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def test_multiple_rhs():
random = np.random.RandomState(1234)
c = random.randn(4)
r = random.randn(4)
for offset in [0, 1j]:
for yshape in ((4,), (4, 3), (4, 3, 2)):
y = random.randn(*yshape) + offset
actual = solve_toeplitz((c,r), b=y)
desired = solve(toeplitz(c, r=r), y)
assert_equal(actual.shape, yshape)
assert_equal(desired.shape, yshape)
assert_allclose(actual, desired)
示例4: test_native_list_arguments
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def test_native_list_arguments():
c = [1,2,4,7]
r = [1,3,9,12]
y = [5,1,4,2]
actual = solve_toeplitz((c,r), y)
desired = solve(toeplitz(c, r=r), y)
assert_allclose(actual, desired)
示例5: test_zero_diag_error
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def test_zero_diag_error():
# The Levinson-Durbin implementation fails when the diagonal is zero.
random = np.random.RandomState(1234)
n = 4
c = random.randn(n)
r = random.randn(n)
y = random.randn(n)
c[0] = 0
assert_raises(np.linalg.LinAlgError,
solve_toeplitz, (c, r), b=y)
示例6: test_wikipedia_counterexample
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def test_wikipedia_counterexample():
# The Levinson-Durbin implementation also fails in other cases.
# This example is from the talk page of the wikipedia article.
random = np.random.RandomState(1234)
c = [2, 2, 1]
y = random.randn(3)
assert_raises(np.linalg.LinAlgError, solve_toeplitz, c, b=y)
示例7: mfbe2lsf
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def mfbe2lsf(mfbe, lsf_order):
NFFT = 512
M = get_filterbank(n_filters=mfbe.shape[1], NFFT=NFFT, normalize=False, htk=True)
M_inv = pinv(M)
p = lsf_order
lsf = np.zeros(( len(mfbe), lsf_order), dtype=np.float64)
spec = np.zeros((len(mfbe), NFFT/2+1), dtype=np.float64)
for i, mfbe_vec in enumerate(mfbe):
# invert mel filterbank
spec_vec = np.dot(M_inv, np.power(10, mfbe_vec))
# floor reconstructed spectrum
spec_vec = np.maximum(spec_vec, 1e-9)
# squared magnitude 2-sided spectrum
twoside = np.r_[spec_vec, np.flipud(spec_vec[1:-1])]
twoside = np.square(twoside)
r = np.fft.ifft(twoside)
r = r.real
# reference from talkbox
# a,_,_ = TB.levinson(r, order=p)
# levinson-durbin
a = LA.solve_toeplitz(r[0:p],r[1:p+1])
a = np.r_[1.0, -1.0*a]
lsf[i,:] = poly2lsf(a)
# reconstructed all-pole spectrum
w, H = freqz(b=1.0, a=a, worN=NFFT, whole=True)
spec[i,:] = np.abs(H[:(NFFT/2+1)])
return lsf, spec
示例8: spec2lsf
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def spec2lsf(spec, lsf_order=30):
NFFT = 2*(spec.shape[0]-1)
n_frames = spec.shape[1]
p = lsf_order
lsf = np.zeros(( n_frames, lsf_order), dtype=np.float64)
spec_rec = np.zeros(spec.shape)
for i, spec_vec in enumerate(spec.T):
# floor reconstructed spectrum
spec_vec = np.maximum(spec_vec, 1e-9)
# squared magnitude 2-sided spectrum
twoside = np.r_[spec_vec, np.flipud(spec_vec[1:-1])]
twoside = np.square(twoside)
r = np.fft.ifft(twoside)
r = r.real
# levinson-durbin
a = LA.solve_toeplitz(r[0:p],r[1:p+1])
a = np.r_[1.0, -1.0*a]
lsf[i,:] = poly2lsf(a)
# reconstructed all-pole spectrum
w, H = freqz(b=1.0, a=a, worN=NFFT, whole=True)
spec_rec[:,i] = np.abs(H[:(NFFT/2+1)])
return lsf, spec_rec
示例9: LS_Filter_Toeplitz
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def LS_Filter_Toeplitz(refChannel, srvChannel, filterLen, peek=10,
return_filter=False):
'''Block east squares adaptive filter. Computes filter coefficients using
scipy's solve_toeplitz function. This assumes the autocorrelation matrix of
refChannel is Hermitian and Toeplitz (i.e. wide the reference signal is
wide sense stationary). Faster than the direct matrix inversion method but
inaccurate if the assumptions are violated.
Parameters:
refChannel: Array containing the reference channel signal
srvChannel: Array containing the surveillance channel signal
filterLen: Length of the least squares filter (in samples)
peek: Number of noncausal filter taps. Set to zero for a
causal filter. If nonzero, clutter estimates can depend
on future values of the reference signal (this helps
sometimes)
return_filter: Boolean indicating whether to return the filter taps
Returns:
srvChannelFiltered: Surveillance channel signal with clutter removed
filterTaps: (optional) least squares filter taps
'''
if refChannel.shape != srvChannel.shape:
raise ValueError('Input vectors must have the same length')
# shift reference channel because for some reason the filtering works
# better when you allow the clutter filter to be noncausal
refChannelShift = np.roll(refChannel, -1*peek)
# compute the first column of the autocorelation matrix of ref
autocorrRef = xcorr(refChannelShift, refChannelShift, 0,
filterLen + peek - 1)
# compute the cross-correlation of ref and srv
xcorrSrvRef = xcorr(srvChannel, refChannelShift, 0,
filterLen + peek - 1)
# solve the Toeplitz least squares problem
filterTaps = solve_toeplitz(autocorrRef, xcorrSrvRef)
# compute clutter signal and remove from surveillance Channel
clutter = np.convolve(refChannelShift, filterTaps, mode = 'full')
clutter = clutter[0:srvChannel.shape[0]]
srvChannelFiltered = srvChannel - clutter
if return_filter:
return srvChannelFiltered, filterTaps
else:
return srvChannelFiltered
示例10: solve_unit_norm_dual
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def solve_unit_norm_dual(lhs, rhs, lambd0, factr=1e7, debug=False,
lhs_is_toeplitz=False):
if np.all(rhs == 0):
return np.zeros(lhs.shape[0]), 0.
n_atoms = lambd0.shape[0]
n_times_atom = lhs.shape[0] // n_atoms
# precompute SVD
# U, s, V = linalg.svd(lhs)
if lhs_is_toeplitz:
# first column of the toeplitz matrix lhs
lhs_c = lhs[0, :]
# lhs will not stay toeplitz if we add different lambd on the diagonal
assert n_atoms == 1
def x_star(lambd):
lambd += 1e-14 # avoid numerical issues
# lhs_inv = np.dot(V.T / (s + np.repeat(lambd, n_times_atom)), U.T)
# return np.dot(lhs_inv, rhs)
lhs_c_copy = lhs_c.copy()
lhs_c_copy[0] += lambd
return linalg.solve_toeplitz(lhs_c_copy, rhs)
else:
def x_star(lambd):
lambd += 1e-14 # avoid numerical issues
# lhs_inv = np.dot(V.T / (s + np.repeat(lambd, n_times_atom)), U.T)
# return np.dot(lhs_inv, rhs)
return linalg.solve(lhs + np.diag(np.repeat(lambd, n_times_atom)),
rhs)
def dual(lambd):
x_hats = x_star(lambd)
norms = linalg.norm(x_hats.reshape(-1, n_times_atom), axis=1)
return (x_hats.T.dot(lhs).dot(x_hats) - 2 * rhs.T.dot(x_hats) + np.dot(
lambd, norms ** 2 - 1.))
def grad_dual(lambd):
x_hats = x_star(lambd).reshape(-1, n_times_atom)
return linalg.norm(x_hats, axis=1) ** 2 - 1.
def func(lambd):
return -dual(lambd)
def grad(lambd):
return -grad_dual(lambd)
bounds = [(0., None) for idx in range(0, n_atoms)]
if debug:
assert optimize.check_grad(func, grad, lambd0) < 1e-5
lambd_hats, _, _ = optimize.fmin_l_bfgs_b(func, x0=lambd0, fprime=grad,
bounds=bounds, factr=factr)
x_hat = x_star(lambd_hats)
return x_hat, lambd_hats
示例11: pade
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import solve_toeplitz [as 别名]
def pade(time,dipole):
damp_const = 120.0
dipole = np.asarray(dipole) - dipole[0]
stepsize = time[1] - time[0]
#print dipole
damp = np.exp(-(stepsize*np.arange(len(dipole)))/float(damp_const))
dipole *= damp
M = len(dipole)
N = int(np.floor(M / 2))
#print "N = ", N
num_pts = 20000
if N > num_pts:
N = num_pts
#print "Trimmed points to: ", N
# G and d are (N-1) x (N-1)
# d[k] = -dipole[N+k] for k in range(1,N)
d = -dipole[N+1:2*N]
try:
from scipy.linalg import toeplitz, solve_toeplitz
except ImportError:
print("You'll need SciPy version >= 0.17.0")
try:
# Instead, form G = (c,r) as toeplitz
#c = dipole[N:2*N-1]
#r = np.hstack((dipole[1],dipole[N-1:1:-1]))
b = solve_toeplitz((dipole[N:2*N-1],\
np.hstack((dipole[1],dipole[N-1:1:-1]))),d,check_finite=False)
except np.linalg.linalg.LinAlgError:
# OLD CODE: sometimes more stable
# G[k,m] = dipole[N - m + k] for m,k in range(1,N)
G = dipole[N + np.arange(1,N)[:,None] - np.arange(1,N)]
b = np.linalg.solve(G,d)
# Now make b Nx1 where b0 = 1
b = np.hstack((1,b))
# b[m]*dipole[k-m] for k in range(0,N), for m in range(k)
a = np.dot(np.tril(toeplitz(dipole[0:N])),b)
p = np.poly1d(a)
q = np.poly1d(b)
# If you want energies greater than 2*27.2114 eV, you'll need to change
# the default frequency range to something greater.
#frequency = np.arange(0.00,2.0,0.00005)
frequency = np.arange(0.3,0.75,0.0002)
W = np.exp(-1j*frequency*stepsize)
fw = p(W)/q(W)
return fw, frequency