Surface NMR processing and inversion GUI
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

harmonic.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import numpy as np
  2. from scipy.optimize import least_squares
  3. from scipy.optimize import minimize
  4. from scipy.linalg import lstsq as sclstsq
  5. import scipy.linalg as lin
  6. #def harmonicEuler ( f0, sN, fs, nK, t ):
  7. def harmonicEuler ( sN, fs, t, f0, k1, kN, ks ):
  8. """
  9. Performs inverse calculation of harmonics contaminating a signal.
  10. Args:
  11. sN = signal containing noise
  12. fs = sampling frequency
  13. t = time samples
  14. f0 = base frequency of the sinusoidal noise
  15. nK = number of harmonics to calculate
  16. """
  17. #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 )
  18. KK = np.arange(k1, kN+1, 1/ks )
  19. nK = len(KK)
  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 )
  21. v = np.linalg.lstsq(A, sN, rcond=None)
  22. alpha = np.real(v[0])
  23. beta = np.imag(v[0])
  24. amp = np.abs(v[0])
  25. phase = np.angle(v[0])
  26. h = np.zeros(len(t))
  27. #for ik in range(nK):
  28. # h += 2*amp[ik] * np.cos( 2.*np.pi*(ik+1) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase[ik] )
  29. for ik, k in enumerate(KK):
  30. h += 2*amp[ik] * np.cos( 2.*np.pi*(k) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase[ik] )
  31. return sN-h
  32. res = sN-h # residual
  33. def harmonicNorm (f0, sN, fs, t, k1, kN, ks):
  34. #print ("norm diff")
  35. #return np.linalg.norm( harmonicEuler(sN, fs, t, f0, k1, kN, ks))
  36. ii = sN < (3.* np.std(sN))
  37. return np.linalg.norm( harmonicEuler(sN, fs, t, f0, k1, kN, ks)[ii] )
  38. def minHarmonic(sN, fs, t, f0, k1, kN, ks):
  39. # CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr
  40. res = minimize(harmonicNorm, np.array((f0)), args=(sN, fs, t, k1, kN, ks), jac='2-point', method='BFGS') # hess=None, bounds=None )
  41. print(res)
  42. return harmonicEuler(sN, fs, t, res.x[0], k1, kN, ks)#[0]
  43. def harmonicEuler2 ( f0, f1, sN, fs, nK, t ):
  44. """
  45. Performs inverse calculation of harmonics contaminating a signal.
  46. Args:
  47. f0 = base frequency of the sinusoidal noise
  48. sN = signal containing noise
  49. fs = sampling frequency
  50. nK = number of harmonics to calculate
  51. t = time samples
  52. """
  53. 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 )
  54. 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 )
  55. A = np.concatenate( (A1, A2), axis=1 )
  56. v = np.linalg.lstsq(A, sN, rcond=None) # rcond=None) #, rcond=1e-8)
  57. amp = np.abs(v[0][0:nK])
  58. phase = np.angle(v[0][0:nK])
  59. amp1 = np.abs(v[0][nK:2*nK])
  60. phase1 = np.angle(v[0][nK:2*nK])
  61. h = np.zeros(len(t))
  62. for ik in range(nK):
  63. h += 2*amp[ik] * np.cos( 2.*np.pi*(ik+1) * (f0/fs) * np.arange(1, len(t)+1, 1 ) + phase[ik] ) + \
  64. 2*amp1[ik] * np.cos( 2.*np.pi*(ik+1) * (f1/fs) * np.arange(1, len(t)+1, 1 ) + phase1[ik] )
  65. return sN-h
  66. def harmonic2Norm ( f0, sN, fs, nK, t ):
  67. return np.linalg.norm(harmonicEuler2(f0[0], f0[1], sN, fs, nK, t))
  68. #def minHarmonic(f0, sN, fs, nK, t):
  69. # f02 = guessf0(sN, fs)
  70. # print("minHarmonic", f0, fs, nK, " guess=", f02)
  71. # # CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr
  72. # res = minimize(harmonicNorm, np.array((f0)), args=(sN, fs, nK, t), jac='2-point', method='BFGS') #, jac=jacEuler) #, hess=None, bounds=None )
  73. # print(res)
  74. # return harmonicEuler(res.x[0], sN, fs, nK, t)#[0]
  75. def minHarmonic2(f1, f2, sN, fs, nK, t):
  76. #f02 = guessf0(sN, fs)
  77. #print("minHarmonic2", f0, fs, nK, " guess=", f02)
  78. #methods with bounds, L-BFGS-B, TNC, SLSQP
  79. 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' )
  80. print(res)
  81. return harmonicEuler2(res.x[0], res.x[1], sN, fs, nK, t)
  82. def guessf0( sN, fs ):
  83. S = np.fft.fft(sN)
  84. w = np.fft.fftfreq( len(sN), 1/fs )
  85. imax = np.argmax( np.abs(S) )
  86. #plt.plot( w, np.abs(S) )
  87. #plt.show()
  88. #print(w)
  89. #print ( w[imax], w[imax+1] )
  90. return abs(w[imax])
  91. if __name__ == "__main__":
  92. import matplotlib.pyplot as plt
  93. f0 = 60 # Hz
  94. f1 = 60 # Hz
  95. delta = np.random.rand() - .5
  96. delta2 = np.random.rand() - .5
  97. print("delta", delta)
  98. print("delta2", delta2)
  99. fs = 10000 # GMR
  100. t = np.arange(0, 1, 1/fs)
  101. phi = 2.*np.pi*np.random.rand() - np.pi
  102. phi2 = 2.*np.pi*np.random.rand() - np.pi
  103. print("phi", phi, phi2)
  104. A = 1.0
  105. A2 = 0.0
  106. A3 = 1.0
  107. nK = 10
  108. T2 = .200
  109. sN = A *np.sin( ( 1*(delta +f0))*2*np.pi*t + phi ) + \
  110. A2*np.sin( ( 1*(delta2 +f1))*2*np.pi*t + phi2 ) + \
  111. np.random.normal(0,.1,len(t)) + \
  112. + A3*np.exp( -t/T2 )
  113. sNc = A *np.sin( (1*(delta +f0))*2*np.pi*t + phi ) + \
  114. A2*np.sin( (1*(delta2+f1))*2*np.pi*t + phi2 ) + \
  115. + A3*np.exp( -t/T2 )
  116. guessf0(sN, fs)
  117. # single freq
  118. #h = harmonicEuler( f0, sN, fs, nK, t)
  119. h = minHarmonic( f0, sN, fs, nK, t)
  120. # two freqs
  121. #h = minHarmonic2( f0+1e-2, f1-1e-2, sN, fs, nK, t)
  122. #h = harmonicEuler2( f0, f1, sN, fs, nK, t)
  123. plt.figure()
  124. plt.plot(t, sN, label="sN")
  125. #plt.plot(t, sN-h, label="sN-h")
  126. plt.plot(t, h, label='h')
  127. plt.title("harmonic")
  128. plt.legend()
  129. plt.figure()
  130. plt.plot(t, sN-sNc, label='true noise')
  131. plt.plot(t, h, label='harmonic removal')
  132. plt.plot(t, np.exp(-t/T2), label="nmr")
  133. plt.legend()
  134. plt.title("true noise")
  135. plt.show()