|
@@ -4,7 +4,6 @@ from scipy.optimize import minimize
|
4
|
4
|
from scipy.linalg import lstsq as sclstsq
|
5
|
5
|
import scipy.linalg as lin
|
6
|
6
|
|
7
|
|
-#def harmonicEuler ( f0, sN, fs, nK, t ):
|
8
|
7
|
def harmonicEuler ( sN, fs, t, f0, k1, kN, ks ):
|
9
|
8
|
"""
|
10
|
9
|
Performs inverse calculation of harmonics contaminating a signal.
|
|
@@ -16,10 +15,9 @@ def harmonicEuler ( sN, fs, t, f0, k1, kN, ks ):
|
16
|
15
|
nK = number of harmonics to calculate
|
17
|
16
|
|
18
|
17
|
"""
|
19
|
|
- #A = np.exp(1j* np.tile( np.arange(1,nK+1),(len(t), 1)) * 2*np.pi* (f0/fs) * np.tile( np.arange(1, len(t)+1, 1),(nK,1)).T )
|
20
|
18
|
KK = np.arange(k1, kN+1, 1/ks )
|
21
|
19
|
nK = len(KK)
|
22
|
|
- A = np.exp(1j* np.tile(KK,(len(t), 1)) * 2*np.pi* (f0/fs) * np.tile( np.arange(1, len(t)+1, 1),(nK,1)).T )
|
|
20
|
+ A = np.exp(1j* np.tile(KK,(len(t), 1)) * 2*np.pi* (f0/fs) * np.tile(np.arange(1, len(t)+1, 1),(nK,1)).T)
|
23
|
21
|
|
24
|
22
|
v = np.linalg.lstsq(A, sN, rcond=None)
|
25
|
23
|
alpha = np.real(v[0])
|
|
@@ -29,17 +27,12 @@ def harmonicEuler ( sN, fs, t, f0, k1, kN, ks ):
|
29
|
27
|
phase = np.angle(v[0])
|
30
|
28
|
|
31
|
29
|
h = np.zeros(len(t))
|
32
|
|
- #for ik in range(nK):
|
33
|
|
- # h += 2*amp[ik] * np.cos( 2.*np.pi*(ik+1) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase[ik] )
|
34
|
30
|
for ik, k in enumerate(KK):
|
35
|
31
|
h += 2*amp[ik] * np.cos( 2.*np.pi*(k) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase[ik] )
|
36
|
32
|
|
37
|
33
|
return sN-h
|
38
|
|
-
|
39
|
|
- res = sN-h # residual
|
40
|
34
|
|
41
|
35
|
def harmonicNorm (f0, sN, fs, t, k1, kN, ks):
|
42
|
|
- #print ("norm diff")
|
43
|
36
|
#return np.linalg.norm( harmonicEuler(sN, fs, t, f0, k1, kN, ks))
|
44
|
37
|
ii = sN < (3.* np.std(sN))
|
45
|
38
|
return np.linalg.norm( harmonicEuler(sN, fs, t, f0, k1, kN, ks)[ii] )
|
|
@@ -50,7 +43,6 @@ def minHarmonic(sN, fs, t, f0, k1, kN, ks):
|
50
|
43
|
print(res)
|
51
|
44
|
return harmonicEuler(sN, fs, t, res.x[0], k1, kN, ks)#[0]
|
52
|
45
|
|
53
|
|
-#def harmonicEuler2 ( f0, f1, sN, fs, nK, t ):
|
54
|
46
|
def harmonicEuler2 ( sN, fs, t, f0, f0k1, f0kN, f0ks, f1, f1k1, f1kN, f1ks ):
|
55
|
47
|
"""
|
56
|
48
|
Performs inverse calculation of harmonics contaminating a signal.
|
|
@@ -62,62 +54,41 @@ def harmonicEuler2 ( sN, fs, t, f0, f0k1, f0kN, f0ks, f1, f1k1, f1kN, f1ks ):
|
62
|
54
|
f0k1 = First harmonic to calulate for f0
|
63
|
55
|
f0kN = Last base harmonic to calulate for f0
|
64
|
56
|
f0ks = subharmonics to calculate
|
|
57
|
+ f1 = second base frequency of the sinusoidal noise
|
|
58
|
+ f1k1 = First harmonic to calulate for f1
|
|
59
|
+ f1kN = Last base harmonic to calulate for f1
|
|
60
|
+ f1ks = subharmonics to calculate at f1 base frequency
|
65
|
61
|
"""
|
66
|
|
-
|
67
|
|
- #A1 = np.exp(1j* np.tile( np.arange(1,nK+1),(len(t), 1)) * 2*np.pi* (f0/fs) * np.tile(np.arange(1, len(t)+1, 1),(nK,1)).T )
|
68
|
|
- #A2 = np.exp(1j* np.tile( np.arange(1,nK+1),(len(t), 1)) * 2*np.pi* (f1/fs) * np.tile(np.arange(1, len(t)+1, 1),(nK,1)).T )
|
69
|
|
- #A = np.concatenate( (A1, A2), axis=1 )
|
70
|
62
|
KK0 = np.arange(f0k1, f0kN+1, 1/f0ks)
|
71
|
63
|
nK0 = len(KK0)
|
72
|
|
- A0 = np.exp(1j* np.tile(KK0,(len(t), 1)) * 2*np.pi* (f0/fs) * np.tile( np.arange(1, len(t)+1, 1),(nK0,1)).T)
|
|
64
|
+ A0 = np.exp(1j* np.tile(KK0,(len(t), 1)) * 2*np.pi* (f0/fs) * np.tile( np.arange(1, len(t)+1, 1), (nK0,1)).T)
|
73
|
65
|
|
74
|
66
|
KK1 = np.arange(f1k1, f1kN+1, 1/f1ks)
|
75
|
67
|
nK1 = len(KK1)
|
76
|
68
|
A1 = np.exp(1j* np.tile(KK1,(len(t), 1)) * 2*np.pi* (f1/fs) * np.tile( np.arange(1, len(t)+1, 1),(nK1,1)).T)
|
77
|
|
-
|
78
|
69
|
|
79
|
70
|
A = np.concatenate((A0, A1), axis=1)
|
80
|
|
- #A = A0
|
81
|
71
|
|
82
|
72
|
v = np.linalg.lstsq(A, sN, rcond=None) # rcond=None) #, rcond=1e-8)
|
|
73
|
+
|
83
|
74
|
amp0 = np.abs(v[0][0:nK0])
|
84
|
75
|
phase0 = np.angle(v[0][0:nK0])
|
85
|
76
|
amp1 = np.abs(v[0][nK0::])
|
86
|
77
|
phase1 = np.angle(v[0][nK0::])
|
87
|
|
-
|
88
|
78
|
|
89
|
79
|
h = np.zeros(len(t))
|
90
|
|
- for ik in range(nK0):
|
91
|
|
- h += 2*amp0[ik] * np.cos( 2.*np.pi*(ik+1) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase0[ik] )
|
92
|
|
- for ik in range(nK1):
|
93
|
|
- h += 2*amp1[ik] * np.cos( 2.*np.pi*(ik+1) * (f1/fs) * np.arange(1, len(t)+1, 1 ) + phase1[ik] ) # + \
|
|
80
|
+ for ik, k in enumerate(KK0):
|
|
81
|
+ h += 2*amp0[ik] * np.cos( 2.*np.pi*(k) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase0[ik] )
|
|
82
|
+ for ik, k in enumerate(KK1):
|
|
83
|
+ h += 2*amp1[ik] * np.cos( 2.*np.pi*(k) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase1[ik] )
|
94
|
84
|
|
95
|
85
|
return sN-h
|
96
|
86
|
|
97
|
87
|
def harmonic2Norm (f0, sN, fs, t, f0k1, f0kN, f0ks, f1k1, f1kN, f1ks):
|
98
|
|
- #def harmonic2Norm ( f0, sN, fs, nK, t ):
|
99
|
88
|
#return np.linalg.norm(harmonicEuler2(f0[0], f0[1], sN, fs, nK, t))
|
100
|
89
|
ii = sN < (3.* np.std(sN))
|
101
|
90
|
return np.linalg.norm( harmonicEuler2(sN, fs, t, f0[0], f0k1, f0kN, f0ks, f0[1], f1k1, f1kN, f1ks)[ii] )
|
102
|
91
|
|
103
|
|
-#def minHarmonic(f0, sN, fs, nK, t):
|
104
|
|
-# f02 = guessf0(sN, fs)
|
105
|
|
-# print("minHarmonic", f0, fs, nK, " guess=", f02)
|
106
|
|
-# # CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr
|
107
|
|
-# res = minimize(harmonicNorm, np.array((f0)), args=(sN, fs, nK, t), jac='2-point', method='BFGS') #, jac=jacEuler) #, hess=None, bounds=None )
|
108
|
|
-# print(res)
|
109
|
|
-# return harmonicEuler(res.x[0], sN, fs, nK, t)#[0]
|
110
|
|
-
|
111
|
|
-
|
112
|
|
-
|
113
|
|
-#def minHarmonic2OLD(f1, f2, sN, fs, nK, t):
|
114
|
|
- #f02 = guessf0(sN, fs)
|
115
|
|
- #print("minHarmonic2", f0, fs, nK, " guess=", f02)
|
116
|
|
- #methods with bounds, L-BFGS-B, TNC, SLSQP
|
117
|
|
-# res = minimize( harmonic2Norm, np.array((f1,f2)), args=(sN, fs, nK, t), jac='2-point', method='BFGS') #, bounds=((f1-1.,f1+1.0),(f2-1.0,f2+1.0)), method='TNC' )
|
118
|
|
-# print(res)
|
119
|
|
-# return harmonicEuler2(res.x[0], res.x[1], sN, fs, nK, t)
|
120
|
|
-
|
121
|
92
|
def minHarmonic2(sN, fs, t, f0, f0k1, f0kN, f0ks, f1, f1k1, f1kN, f1ks):
|
122
|
93
|
# CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr
|
123
|
94
|
res = minimize(harmonic2Norm, np.array((f0, f1)), args=(sN, fs, t, f0k1, f0kN, f0ks, f1k1,f1kN, f1ks), jac='2-point', method='BFGS') # hess=None, bounds=None )
|