Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. import numpy as np, matplotlib.pyplot as plt, PhyPraKit as ppk
  2. from scipy import interpolate, signal
  3.  
  4.  
  5. fname = "signal.csv"
  6.  
  7. hlines, data = ppk.readCSV(fname, nlhead=1)
  8. print (hlines)
  9. print (" --> Anzahl Spalten", data.shape[1])
  10. print (" --> Datenzeilen", data.shape[1])
  11.  
  12.  
  13. t = data[0]
  14. a = data[1]
  15.  
  16. glatt = int(input("Glättungsparameter:"))
  17. a_smooth = ppk.meanFilter(a, glatt)
  18.  
  19. #bilde Differenz
  20. differenz = a - a_smooth
  21.  
  22. # make plots
  23. fig=plt.figure(1, figsize=(7.5, 7.5))
  24. fig.suptitle('Signal mit Glättung und Differenz')
  25. fig.subplots_adjust(left=0.14, bottom=0.1, right=0.97, top=0.93,
  26. wspace=None, hspace=.75)#
  27. ax1=fig.add_subplot(3, 1, 1)
  28. ax1.plot(t, a)
  29. ax1.set_title('Rohdaten')
  30. ax1.set_xlabel('Zeit (ms)', size='large')
  31. ax1.set_ylabel('Amplitude (mV)', size='large')
  32. ax1.grid()
  33.  
  34. ax2=fig.add_subplot(3, 1, 2)
  35. ax2.plot(t, a_smooth)
  36. ax1.set_title('Geglättetes Signal')
  37. ax2.set_xlabel('Zeit (ms)', size='large')
  38. ax2.set_ylabel(u'geglättete Amplitude (mV)', size='large')
  39. ax2.grid()
  40.  
  41. ax3=fig.add_subplot(3, 1, 3)
  42. ax3.plot(t, differenz)
  43. ax3.set_title('Differenz')
  44. ax3.set_xlabel('Zeit (ms)', size='large')
  45. ax3.set_ylabel(u'Differenz der Amplitude (mV)', size='large')
  46. ax3.grid()
  47.  
  48. plt.show()
  49.  
  50. #
  51. ac_a = ppk.autocorrelate(a_smooth)
  52. ac_t = t
  53.  
  54. # find maxima and minima using convolution peak finder
  55. width= 5
  56. pidxac = ppk.convolutionPeakfinder(ac_a, width, th=0.66) #ursprünglich 0.66 Anzahl kreuze (am ende überdeckt)
  57. didxac = ppk.convolutionPeakfinder(-ac_a, width, th=0.66) #ursprünglich 0.66
  58. if len(pidxac) > 1:
  59. print(" --> %i auto-correlation peaks found"%(len(pidxac)))
  60. pidxac[0]=0 # first peak is at 0 by construction
  61. ac_tp, ac_ap= np.array(ac_t[pidxac]), np.array(ac_a[pidxac])
  62. ac_td, ac_ad= np.array(ac_t[didxac]), np.array(ac_a[didxac])
  63. else:
  64. print("*!!* not enough correlation peaks found")
  65.  
  66. # make plots
  67. # 1. signal
  68. fig = plt.figure(1, figsize=(7.5, 9.))
  69. fig.suptitle('Periodendauer/ Verteilung Zeitabstände')
  70. fig.subplots_adjust(left=0.1, bottom=0.1, right=0.98, top=0.98,
  71. wspace=0.3, hspace=0.5)
  72.  
  73. ax1 = fig.add_subplot(3, 1, 1)
  74. ax1.plot(t, a_smooth)
  75. ax1.set_title('geglättetes Signal')
  76. ax1.set_xlabel('Zeit (ms)', size='large')
  77. ax1.set_ylabel('$amplitude$ (a.u.)', size='large')
  78. ax1.grid()
  79.  
  80. # 2. auto-correlation
  81. ax2=fig.add_subplot(3,1,2)
  82. ax2.plot(ac_tp, ac_ap, 'bx', alpha=0.9, label='peaks')
  83. ax2.plot(ac_td, ac_ad, 'rx', alpha=0.9, label='dips')
  84. ax2.plot(ac_t, ac_a)
  85. ax2.set_title('Korrelation')
  86. ax2.set_xlabel('Zeit(ms) ', size='large')
  87. ax2.set_ylabel('$autocorrelation$', size='large')
  88. ax2.legend(loc='best', numpoints=1, prop={'size':10})
  89. # ax2.set_yscale('log')
  90. ax2.grid()
  91.  
  92. # 3. analysis of auto-correlation function
  93. ax3 = fig.add_subplot(3, 1, 3)
  94. ac_dtp = ac_tp[1:] - ac_tp[:-1]
  95. ac_dtd = ac_td[1:] - ac_td[:-1]
  96. bins=np.linspace(min(min(ac_dtp),min(ac_dtd)), max(max(ac_dtp), max(ac_dtd)), 100)
  97. bc, be, _ = ax3.hist([ac_dtp, ac_dtd], bins, stacked = True,
  98. color=['b','r'], label=['peaks','dips'], alpha=0.5)
  99. ax3.set_title('')
  100. ax3.set_xlabel('Zeitdifferenz von Maxima und Minima$ (ms)', size='large')
  101. ax3.set_ylabel('Frequenz', size='large')
  102. ax3.legend(loc='best', numpoints=1, prop={'size':10})
  103. ax3.grid()
  104.  
  105. plt.show()
  106.  
  107. # analyis of histogram
  108. m_dtp, s_dtp, sm_dtp = ppk.histstat(bc[0], be, pr=False)
  109. m_dtd, s_dtd, sm_dtd = ppk.histstat(bc[1], be, pr=False)
  110.  
  111. print("Periodendauer entspricht:")
  112. print(" --> Zeitdifferenz Auto-Correlation: (%.5g +/- %.2g) ms"%(m_dtp, sm_dtp))
  113. print(" --> (%.5g +/- %.2g) ms"%(m_dtd, sm_dtd))
  114.  
  115. #Fourierdarstellung
  116. print("** Fourier Spectrum")
  117. freq, amp = ppk.FourierSpectrum(t, a_smooth, fmax=20)
  118. # freq, amp = ppk.Fourier_fft(t, a) # use fast algorithm
  119. frequency = freq[np.where(amp==max(amp))]
  120. print(" --> Frequenz mit max. Amplitude: ", frequency)
  121.  
  122. # make plots
  123. fig=plt.figure(1, figsize=(7.5, 7.5))
  124. fig.suptitle('Fourier-Transformerte')
  125. fig.subplots_adjust(left=0.14, bottom=0.1, right=0.97, top=0.93,
  126. wspace=None, hspace=.25)#
  127. ax1=fig.add_subplot(2, 1, 1)
  128. ax1.plot(t, a_smooth)
  129. ax1.set_xlabel('Zeit in ms', size='large')
  130. ax1.set_ylabel('Amplitude mV', size='large')
  131. ax1.grid()
  132.  
  133. ax2=fig.add_subplot(2,1,2)
  134. ax2.plot(freq, amp, 'b')
  135. ax2.set_xlabel('$Frequenz$ $f$ (kHz)', size='large')
  136. ax2.set_ylabel('$Amplitude$', size='large')
  137. ax2.set_yscale('log')
  138. ax2.grid()
  139.  
  140. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement