|
@@ -1,12 +1,55 @@
|
1
|
1
|
import numpy as np
|
2
|
2
|
from scipy.optimize import least_squares
|
|
3
|
+from scipy.optimize import minimize
|
|
4
|
+from scipy.linalg import lstsq as sclstsq
|
3
|
5
|
|
4
|
|
-def harmonic ( sN, f0, fs, nK, t ):
|
|
6
|
+def harmonic2 ( f1, f2, sN, fs, nK, t ):
|
5
|
7
|
"""
|
6
|
|
- Performs inverse calculation of harmonics contaminating a signal.
|
|
8
|
+ Performs inverse calculation of two harmonics contaminating a signal.
|
7
|
9
|
Args:
|
|
10
|
+ f01 = base frequency of the first sinusoidal noise
|
|
11
|
+ f02 = base frequency of the second sinusoidal noise
|
8
|
12
|
sN = signal containing noise
|
|
13
|
+ fs = sampling frequency
|
|
14
|
+ nK = number of harmonics to calculate
|
|
15
|
+ t = time samples
|
|
16
|
+ """
|
|
17
|
+ print("building matrix ")
|
|
18
|
+ A = np.zeros( (len(t), 4*nK) )
|
|
19
|
+ #f1 = f1MHz * 1e-3
|
|
20
|
+ #f2 = f2MHz * 1e-3
|
|
21
|
+ for irow, tt in enumerate(t):
|
|
22
|
+ A[irow, 0:2*nK:2] = np.cos( np.arange(nK)*2*np.pi*(f1/fs)*irow )
|
|
23
|
+ A[irow, 1:2*nK:2] = np.sin( np.arange(nK)*2*np.pi*(f1/fs)*irow )
|
|
24
|
+ A[irow, 2*nK::2] = np.cos( np.arange(nK)*2*np.pi*(f2/fs)*irow )
|
|
25
|
+ A[irow, 2*nK+1::2] = np.sin( np.arange(nK)*2*np.pi*(f2/fs)*irow )
|
|
26
|
+
|
|
27
|
+ v = np.linalg.lstsq(A, sN, rcond=1e-8)
|
|
28
|
+ #v = sclstsq(A, sN) #, rcond=1e-6)
|
|
29
|
+
|
|
30
|
+ alpha = v[0][0:2*nK:2]
|
|
31
|
+ beta = v[0][1:2*nK:2]
|
|
32
|
+ amp = np.sqrt( alpha**2 + beta**2 )
|
|
33
|
+ phase = np.arctan(- beta/alpha)
|
|
34
|
+
|
|
35
|
+ alpha2 = v[0][2*nK::2]
|
|
36
|
+ beta2 = v[0][2*nK+1::2]
|
|
37
|
+ amp2 = np.sqrt( alpha2**2 + beta2**2 )
|
|
38
|
+ phase2 = np.arctan(- beta2/alpha2)
|
|
39
|
+
|
|
40
|
+ h = np.zeros(len(t))
|
|
41
|
+ for ik in range(nK):
|
|
42
|
+ h += np.sqrt(alpha[ik]**2 + beta[ik]**2) * np.cos( 2.*np.pi*ik * (f1/fs) * np.arange(0, len(t), 1 ) + phase[ik] ) \
|
|
43
|
+ + np.sqrt(alpha2[ik]**2 + beta2[ik]**2) * np.cos( 2.*np.pi*ik * (f2/fs) * np.arange(0, len(t), 1 ) + phase2[ik] )
|
|
44
|
+
|
|
45
|
+ return sN-h
|
|
46
|
+
|
|
47
|
+def harmonic ( f0, sN, fs, nK, t ):
|
|
48
|
+ """
|
|
49
|
+ Performs inverse calculation of harmonics contaminating a signal.
|
|
50
|
+ Args:
|
9
|
51
|
f0 = base frequency of the sinusoidal noise
|
|
52
|
+ sN = signal containing noise
|
10
|
53
|
fs = sampling frequency
|
11
|
54
|
nK = number of harmonics to calculate
|
12
|
55
|
t = time samples
|
|
@@ -16,10 +59,6 @@ def harmonic ( sN, f0, fs, nK, t ):
|
16
|
59
|
for irow, tt in enumerate(t):
|
17
|
60
|
A[irow, 0::2] = np.cos( np.arange(nK)*2*np.pi*(f0/fs)*irow )
|
18
|
61
|
A[irow, 1::2] = np.sin( np.arange(nK)*2*np.pi*(f0/fs)*irow )
|
19
|
|
- # brutal
|
20
|
|
- #for k, ik in enumerate( np.arange(0, 2*nK, 2) ):
|
21
|
|
- # A[irow, ik ] = np.cos( k*2*np.pi*(f0/fs)*irow )
|
22
|
|
- # A[irow, ik+1] = np.sin( k*2*np.pi*(f0/fs)*irow )
|
23
|
62
|
|
24
|
63
|
v = np.linalg.lstsq(A, sN, rcond=None) #, rcond=1e-8)
|
25
|
64
|
|
|
@@ -29,9 +68,11 @@ def harmonic ( sN, f0, fs, nK, t ):
|
29
|
68
|
amp = np.sqrt( alpha**2 + beta**2 )
|
30
|
69
|
phase = np.arctan(- beta/alpha)
|
31
|
70
|
|
|
71
|
+ #print("amp:", amp, " phase", phase)
|
|
72
|
+
|
32
|
73
|
h = np.zeros(len(t))
|
33
|
74
|
for ik in range(nK):
|
34
|
|
- h += np.sqrt(alpha[ik]**2 + beta[ik]**2) * np.cos( 2.*np.pi*ik * (f0/fs) * np.arange(0, len(t), 1 ) + phase[ik] )
|
|
75
|
+ h += np.sqrt(alpha[ik]**2 + beta[ik]**2) * np.cos( 2.*np.pi*ik * (f0/fs) * np.arange(0, len(t), 1 ) + phase[ik] )
|
35
|
76
|
|
36
|
77
|
#plt.matshow(A, aspect='auto')
|
37
|
78
|
#plt.colorbar()
|
|
@@ -44,36 +85,99 @@ def harmonic ( sN, f0, fs, nK, t ):
|
44
|
85
|
#plt.figure()
|
45
|
86
|
#plt.plot(h)
|
46
|
87
|
#plt.title("modelled noise")
|
|
88
|
+ return sN-h
|
|
89
|
+
|
|
90
|
+def jacobian( f0, sN, fs, nK, t):
|
|
91
|
+ print("building Jacobian matrix ")
|
|
92
|
+ A = np.zeros( (len(t), 2*nK) )
|
|
93
|
+ for irow, tt in enumerate(t):
|
|
94
|
+ #A[irow, 0::2] = np.cos( np.arange(nK)*2*np.pi*(f0/fs)*irow )
|
|
95
|
+ #A[irow, 1::2] = np.sin( np.arange(nK)*2*np.pi*(f0/fs)*irow )
|
|
96
|
+ # brutal
|
|
97
|
+ for k, ik in enumerate( np.arange(0, 2*nK, 2) ):
|
|
98
|
+ #A[irow, ik ] = np.cos( k*2*np.pi*(f0/fs)*irow )
|
|
99
|
+ #A[irow, ik+1] = np.sin( k*2*np.pi*(f0/fs)*irow )
|
|
100
|
+ A[irow, ik ] = - (2.*np.pi*k*irow * sin((2.*np.pi*irow*f0)/fs)) / fs
|
|
101
|
+ A[irow, ik+1] = (2.*np.pi*k*irow * cos((2.*np.pi*irow*f0)/fs)) / fs
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+def harmonicNorm ( f0, sN, fs, nK, t ):
|
|
105
|
+ return np.linalg.norm( harmonic(f0, sN, fs, nK, t))
|
47
|
106
|
|
48
|
|
- return h
|
|
107
|
+def harmonic2Norm ( f0, sN, fs, nK, t ):
|
|
108
|
+ return np.linalg.norm(harmonic2(f0[0], f0[1], sN, fs, nK, t))
|
|
109
|
+
|
|
110
|
+def minHarmonic(f0, sN, fs, nK, t):
|
|
111
|
+ f02 = guessf0(sN, fs)
|
|
112
|
+ print("minHarmonic", f0, fs, nK, " guess=", f02)
|
|
113
|
+ res = minimize( harmonicNorm, np.array((f0)), args=(sN, fs, nK, t)) #, method='Nelder-Mead' )# jac=None, hess=None, bounds=None )
|
|
114
|
+ print(res)
|
|
115
|
+ return harmonic(res.x[0], sN, fs, nK, t)
|
|
116
|
+
|
|
117
|
+def minHarmonic2(f1, f2, sN, fs, nK, t):
|
|
118
|
+ #f02 = guessf0(sN, fs)
|
|
119
|
+ #print("minHarmonic2", f0, fs, nK, " guess=", f02)
|
|
120
|
+ #methods with bounds, L-BFGS-B, TNC, SLSQP
|
|
121
|
+ res = minimize( harmonic2Norm, np.array((f1,f2)), args=(sN, fs, nK, t)) #, bounds=((f1-1.,f1+1.0),(f2-1.0,f2+1.0)), method='SLSQP' )
|
|
122
|
+ print(res)
|
|
123
|
+ return harmonic2(res.x[0], res.x[1], sN, fs, nK, t)
|
|
124
|
+
|
|
125
|
+def guessf0( sN, fs ):
|
|
126
|
+ S = np.fft.fft(sN)
|
|
127
|
+ w = np.fft.fftfreq( len(sN), 1/fs )
|
|
128
|
+ imax = np.argmax( np.abs(S) )
|
|
129
|
+ #plt.plot( w, np.abs(S) )
|
|
130
|
+ #plt.show()
|
|
131
|
+ #print(w)
|
|
132
|
+ #print ( w[imax], w[imax+1] )
|
|
133
|
+ return abs(w[imax])
|
49
|
134
|
|
50
|
135
|
if __name__ == "__main__":
|
|
136
|
+
|
51
|
137
|
import matplotlib.pyplot as plt
|
52
|
138
|
|
53
|
139
|
f0 = 60 # Hz
|
54
|
|
- delta = np.random.rand()
|
55
|
|
- fs = 50000 #1e4
|
|
140
|
+ f1 = 60 # Hz
|
|
141
|
+ delta = 0 #np.random.rand()
|
|
142
|
+ delta2 = 0 #np.random.rand()
|
|
143
|
+ print("delta", delta)
|
|
144
|
+ fs = 10000 # GMR
|
56
|
145
|
t = np.arange(0, 1, 1/fs)
|
57
|
|
- phi = .234
|
58
|
|
- A = 1.0
|
59
|
|
- nK = 20
|
60
|
|
- sN = A * np.sin( (delta+f0)*2*np.pi*t + phi ) + np.random.normal(0,.1,len(t))
|
61
|
|
- sNc = A * np.sin( (delta+f0)*2*np.pi*t + phi )
|
62
|
|
- h = harmonic(sN, f0, fs, nK, t)
|
|
146
|
+ phi = 0 #np.random.rand()
|
|
147
|
+ phi2 = 0 # np.random.rand()
|
|
148
|
+ A = 1.0
|
|
149
|
+ A2 = 0.0
|
|
150
|
+ nK = 10
|
|
151
|
+ T2 = .200
|
|
152
|
+ sN = A * np.sin( ( 1*(delta +f0))*2*np.pi*t + phi ) + \
|
|
153
|
+ A2* np.sin( ( 1*(delta2 +f1))*2*np.pi*t + phi2 ) + \
|
|
154
|
+ np.random.normal(0,.1,len(t)) + \
|
|
155
|
+ + np.exp( -t/T2 )
|
|
156
|
+
|
|
157
|
+ sNc = A * np.sin( (1*(delta +f0))*2*np.pi*t + phi ) + \
|
|
158
|
+ A2* np.sin( (1*(delta2+f1))*2*np.pi*t + phi2 ) + \
|
|
159
|
+ + np.exp( -t/T2 )
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+ guessf0(sN, fs)
|
|
163
|
+
|
|
164
|
+ #h = harmonic( f0, sN, fs, nK, t)
|
|
165
|
+ #h = minHarmonic2( f0, f1, sN, fs, nK, t)
|
|
166
|
+ h = harmonic2( f0, f1, sN, fs, nK, t)
|
63
|
167
|
|
64
|
168
|
plt.figure()
|
65
|
169
|
plt.plot(t, sN, label="sN")
|
66
|
|
- plt.plot(t, sN-h, label="sN-h")
|
|
170
|
+ #plt.plot(t, sN-h, label="sN-h")
|
67
|
171
|
plt.plot(t, h, label='h')
|
68
|
|
- plt.title("true noise")
|
|
172
|
+ plt.title("harmonic")
|
69
|
173
|
plt.legend()
|
70
|
174
|
|
71
|
175
|
plt.figure()
|
72
|
176
|
plt.plot(t, sN-sNc, label='true noise')
|
73
|
|
- plt.plot(t, sN-h, label='harmonic removal')
|
|
177
|
+ plt.plot(t, h, label='harmonic removal')
|
|
178
|
+ plt.plot(t, np.exp(-t/T2), label="nmr")
|
74
|
179
|
plt.legend()
|
75
|
180
|
plt.title("true noise")
|
76
|
181
|
|
77
|
182
|
plt.show()
|
78
|
183
|
|
79
|
|
- print("hello")
|