-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
81 lines (60 loc) · 1.97 KB
/
utils.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Utility functions to aid the frequency domain FRI method of estimating
a stream of decaying exponentials.
Authors: Benjamin Bejar, Gavin Mischler
"""
import numpy as np
from numpy.fft import fftshift
from scipy.linalg import toeplitz, svd
def F(alpha,Zk,wk,K,cadzow=False):
"""
Cost function for finding the optimal decay factor.
"""
# estimated Fourier coefficients
Sk = fftshift(Zk) * (alpha + 1j*wk)
if cadzow:
N = len(Sk)
Sk = Cadzow(Sk, K=K, N=N)
# find annihilating filter
s = svd( toeplitz(Sk[K:],Sk[np.arange(K,-1,-1)]) )[1]
# return smallest singular value
return s[-1]
def Cadzow(Xk, K, N, tol_ratio=10000, max_iter=10):
"""
Implement Cadzow denoising
Parameters
----------
Xk : signal to denoise
K : number of most significant members to take
N : number of samples in the signal
tol_ratio : min ratio of (K+1)th singular value / Kth singular value
to stop iterations
max_iter : maximum number of iterations to run
Returns
-------
X : denoised signal
"""
X = Xk.copy()
ratio = 0
iters = 0
while (ratio < tol_ratio and iters < max_iter):
iters += 1
# perform svd
#print(toeplitz(X[K:],X[np.arange(K,-1,-1)]).shape)
U, s, Vh = svd(toeplitz(X[K:],X[np.arange(K,-1,-1)]))
# update ratio of singular values for cutoff
ratio = s[K-1] / s[K]
# build S' : first K diagonals of S
s_ = s[:K]
sz1 = U.shape[1]
sz2 = Vh.shape[0]
S_ = np.zeros(shape=(sz1, sz2))
for elem,(i,j) in enumerate(zip(np.arange(K),np.arange(K))):
S_[i,j] = s_[elem]
# least squares approx. for A
A_ = U @ S_ @ Vh
# denoised Xk is the average of the diagonals
for idx, off in enumerate(np.arange(K,K-N,-1)):
temp = np.mean(np.diagonal(A_,offset=off))
X[idx] = temp
return X