|
@@ -16,7 +16,6 @@ def harmonicEuler ( sN, fs, t, f0, k1, kN, ks ):
|
16
|
16
|
nK = number of harmonics to calculate
|
17
|
17
|
|
18
|
18
|
"""
|
19
|
|
-
|
20
|
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 )
|
21
|
20
|
KK = np.arange(k1, kN+1, 1/ks )
|
22
|
21
|
nK = len(KK)
|
|
@@ -51,36 +50,50 @@ def minHarmonic(sN, fs, t, f0, k1, kN, ks):
|
51
|
50
|
print(res)
|
52
|
51
|
return harmonicEuler(sN, fs, t, res.x[0], k1, kN, ks)#[0]
|
53
|
52
|
|
54
|
|
-def harmonicEuler2 ( f0, f1, sN, fs, nK, t ):
|
|
53
|
+#def harmonicEuler2 ( f0, f1, sN, fs, nK, t ):
|
|
54
|
+def harmonicEuler2 ( sN, fs, t, f0, f0k1, f0kN, f0ks, f1, f1k1, f1kN, f1ks ):
|
55
|
55
|
"""
|
56
|
56
|
Performs inverse calculation of harmonics contaminating a signal.
|
57
|
57
|
Args:
|
58
|
|
- f0 = base frequency of the sinusoidal noise
|
59
|
58
|
sN = signal containing noise
|
60
|
59
|
fs = sampling frequency
|
61
|
|
- nK = number of harmonics to calculate
|
62
|
60
|
t = time samples
|
|
61
|
+ f0 = first base frequency of the sinusoidal noise
|
|
62
|
+ f0k1 = First harmonic to calulate for f0
|
|
63
|
+ f0kN = Last base harmonic to calulate for f0
|
|
64
|
+ f0ks = subharmonics to calculate
|
63
|
65
|
"""
|
64
|
|
- 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 )
|
65
|
|
- 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 )
|
66
|
|
- A = np.concatenate( (A1, A2), axis=1 )
|
67
|
|
-
|
|
66
|
+ #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 )
|
|
67
|
+ #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 )
|
|
68
|
+ #A = np.concatenate( (A1, A2), axis=1 )
|
|
69
|
+ KK0 = np.arange(f0k1, f0kN+1, 1/f0ks )
|
|
70
|
+ nK0 = len(KK0)
|
|
71
|
+ 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 )
|
|
72
|
+ KK1 = np.arange(f1k1, f1kN+1, 1/f1ks )
|
|
73
|
+ nK1 = len(KK1)
|
|
74
|
+ 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 )
|
|
75
|
+ A = np.concatenate( (A0, A1), axis=1 )
|
68
|
76
|
|
69
|
77
|
v = np.linalg.lstsq(A, sN, rcond=None) # rcond=None) #, rcond=1e-8)
|
70
|
|
- amp = np.abs(v[0][0:nK])
|
71
|
|
- phase = np.angle(v[0][0:nK])
|
72
|
|
- amp1 = np.abs(v[0][nK:2*nK])
|
73
|
|
- phase1 = np.angle(v[0][nK:2*nK])
|
|
78
|
+ amp = np.abs(v[0][0:nK0])
|
|
79
|
+ phase = np.angle(v[0][0:nK0])
|
|
80
|
+ amp1 = np.abs(v[0][nK0::])
|
|
81
|
+ phase1 = np.angle(v[0][nK0::])
|
74
|
82
|
|
75
|
83
|
h = np.zeros(len(t))
|
76
|
|
- for ik in range(nK):
|
77
|
|
- h += 2*amp[ik] * np.cos( 2.*np.pi*(ik+1) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase[ik] ) + \
|
78
|
|
- 2*amp1[ik] * np.cos( 2.*np.pi*(ik+1) * (f1/fs) * np.arange(1, len(t)+1, 1 ) + phase1[ik] )
|
|
84
|
+ for ik in range(nK0):
|
|
85
|
+ h += 2*amp[ik] * np.cos( 2.*np.pi*(ik+1) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase[ik] )
|
|
86
|
+ for ik in range(nK1):
|
|
87
|
+ h += 2*amp1[ik] * np.cos( 2.*np.pi*(ik+1) * (f1/fs) * np.arange(1, len(t)+1, 1 ) + phase1[ik] ) # + \
|
|
88
|
+ # 2*amp1[ik] * np.cos( 2.*np.pi*(ik+1) * (f1/fs) * np.arange(1, len(t)+1, 1 ) + phase1[ik] )
|
79
|
89
|
|
80
|
90
|
return sN-h
|
81
|
91
|
|
82
|
|
-def harmonic2Norm ( f0, sN, fs, nK, t ):
|
83
|
|
- return np.linalg.norm(harmonicEuler2(f0[0], f0[1], sN, fs, nK, t))
|
|
92
|
+def harmonic2Norm (f0, sN, fs, t, f0k1, f0kN, f0ks, f1k1, f1kN, f1ks):
|
|
93
|
+ #def harmonic2Norm ( f0, sN, fs, nK, t ):
|
|
94
|
+ #return np.linalg.norm(harmonicEuler2(f0[0], f0[1], sN, fs, nK, t))
|
|
95
|
+ ii = sN < (3.* np.std(sN))
|
|
96
|
+ return np.linalg.norm( harmonicEuler2(sN, fs, t, f0[0], f0k1, f0kN, f0ks, f0[1], f1k1, f1kN, f1ks)[ii] )
|
84
|
97
|
|
85
|
98
|
#def minHarmonic(f0, sN, fs, nK, t):
|
86
|
99
|
# f02 = guessf0(sN, fs)
|
|
@@ -92,13 +105,19 @@ def harmonic2Norm ( f0, sN, fs, nK, t ):
|
92
|
105
|
|
93
|
106
|
|
94
|
107
|
|
95
|
|
-def minHarmonic2(f1, f2, sN, fs, nK, t):
|
|
108
|
+#def minHarmonic2OLD(f1, f2, sN, fs, nK, t):
|
96
|
109
|
#f02 = guessf0(sN, fs)
|
97
|
110
|
#print("minHarmonic2", f0, fs, nK, " guess=", f02)
|
98
|
111
|
#methods with bounds, L-BFGS-B, TNC, SLSQP
|
99
|
|
- 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' )
|
|
112
|
+# 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' )
|
|
113
|
+# print(res)
|
|
114
|
+# return harmonicEuler2(res.x[0], res.x[1], sN, fs, nK, t)
|
|
115
|
+
|
|
116
|
+def minHarmonic2(sN, fs, t, f0, f0k1, f0kN, f0ks, f1, f1k1, f1kN, f1ks):
|
|
117
|
+ # CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr
|
|
118
|
+ 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 )
|
100
|
119
|
print(res)
|
101
|
|
- return harmonicEuler2(res.x[0], res.x[1], sN, fs, nK, t)
|
|
120
|
+ return harmonicEuler2(sN, fs, t, res.x[0], f0k1, f0kN, f0ks, res.x[1], f1kN, f1kN, f1ks)#[0]
|
102
|
121
|
|
103
|
122
|
def guessf0( sN, fs ):
|
104
|
123
|
S = np.fft.fft(sN)
|