PhaseAmpCouplingNoise.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Jul 5 09:21:49 2019
  4. @author: rgarcia1
  5. """
  6. from PhyREC.NeoInterface import NeoSegment#, ReadMCSFile
  7. import PhyREC.PlotWaves as Rplt
  8. import quantities as pq
  9. import matplotlib.pyplot as plt
  10. import numpy as np
  11. import neo
  12. import PhyREC.SignalProcess as RPro
  13. import deepdish as dd
  14. from scipy.signal import hilbert
  15. def ReadMCSFile(McsFile,Include, OutSeg=None, SigNamePrefix=''):
  16. import McsPy.McsData as McsData
  17. Dat = McsData.RawData(McsFile)
  18. Rec = Dat.recordings[0]
  19. NSamps = Rec.duration
  20. if OutSeg is None:
  21. OutSeg = NeoSegment()
  22. for AnaStrn, AnaStr in Rec.analog_streams.iteritems():
  23. if len(AnaStr.channel_infos) == 1:
  24. continue
  25. for Chn, Chinfo in AnaStr.channel_infos.iteritems():
  26. print 'Analog Stream ', Chinfo.label, Chinfo.sampling_frequency
  27. ChName = str(SigNamePrefix + Chinfo.label)
  28. print ChName
  29. if ChName not in Include:
  30. continue
  31. Fs = Chinfo.sampling_frequency
  32. Var, Unit = AnaStr.get_channel_in_range(Chn, 0, NSamps)
  33. sig = neo.AnalogSignal(pq.Quantity(Var, Chinfo.info['Unit']),
  34. t_start=0*pq.s,
  35. sampling_rate=Fs.magnitude*pq.Hz,
  36. name=ChName)
  37. OutSeg.AddSignal(sig)
  38. return OutSeg
  39. def MeanStd(Data):
  40. Arr = np.zeros([len(Data.keys()),len(Data[Data.keys()[0]]['psd'])])
  41. for iT,TrtName in enumerate(Data.keys()):
  42. Arr[iT,:] = Data[TrtName]['psd'][:,0]
  43. return np.mean(Arr,0), np.std(Arr,0)
  44. Path = '23072019/B12784O18-T3/'
  45. InFileM = Path + '2019-07-23T16-59-52B12784O18-T3-ACDC-PostEth-LowFreqNoise-0.25V.h5' ############
  46. InFileS = Path + '2019-07-23T16-59-52B12784O18-T3-ACDC-PostEth-LowFreqNoise-0.25V_2.h5' ##########
  47. Gm = dd.io.load(Path + 'GM-B12784O18-T3')
  48. BW = 100
  49. ivgainDC = 118.8*pq.V#the gain (1e6) is already applied to the saved signal ## Check this gain
  50. ivgainAC = 1188*pq.V
  51. TWind = (140*pq.s, 2350*pq.s)
  52. #DCch = ('ME5', 'ME7', 'ME29', 'ME31', 'SE5', 'SE7', 'SE29', 'SE31')
  53. DCch = ('SE7','ME31',)#'ME1','ME2')#'SE7','SE31',)#'ME1','ME2')#'SE7','SE31',)#'SE29','SE31','ME5','ME7','ME29','ME31',)
  54. Include = DCch#('ME1','ME2','ME4','ME6')
  55. color = ['r','orange','g','b']
  56. Rec = ReadMCSFile(InFileM,
  57. Include,
  58. OutSeg=None,
  59. SigNamePrefix='M')
  60. Rec = ReadMCSFile(InFileS,
  61. Include,
  62. OutSeg=Rec,
  63. SigNamePrefix='S')
  64. #%% Plot Sig
  65. plt.close('all')
  66. Slots= []
  67. SlotsComp = []
  68. counter = -1
  69. for sig in Rec.Signals():
  70. SigProDC = [{'function': RPro.Gain, 'args': {'Gain': pq.A/(Gm[sig.name][8]*(pq.A/pq.V)*ivgainDC)}},
  71. {'function': RPro.SetZero, 'args' : {'TWind' :TWind}},
  72. # {'function': RPro.DownSampling, 'args' : {'Fact' :10}},
  73. {'function': RPro.Filter, 'args': {'Type':'bandpass',
  74. 'Order':2,
  75. 'Freqs':(0.005,0.05)}},
  76. ]
  77. if sig.name not in DCch:
  78. continue
  79. counter +=1
  80. sig.ProcessChain = SigProDC
  81. sig = sig.GetSignal(TWind)
  82. if counter==0:
  83. sig0 = sig.GetSignal(TWind)
  84. else:
  85. sig1 = sig.GetSignal(TWind)
  86. Slots.append(Rplt.WaveSlot(sig,
  87. Position=0,
  88. Alpha=0.5))
  89. SlotsComp.append(Rplt.WaveSlot(sig,
  90. Position=0,
  91. Alpha=0.5))
  92. Splots = Rplt.PlotSlots(Slots)
  93. Splots.PlotChannels(Time=TWind,
  94. Units='V')
  95. fig, (ax1,ax2,ax3) = plt.subplots(3,1)
  96. HT0 = hilbert(np.array(sig0).T[0,:])
  97. ax1.plot([],color='w', label='[2-4 Hz]')
  98. ax1.plot(sig0.times, sig0*1e6,'k',label = 'signal')
  99. ax1.plot(sig0.times,np.real(HT0)*1e6,'b',label = 'Real(HT)')
  100. ax1.plot(sig0.times,np.abs(HT0)*1e6,'r',label = 'Mag(HT)')
  101. HT1= hilbert(np.array(sig1).T[0,:])
  102. ax2.plot([],color='w', label='ISA [0.007-0.1 Hz]')
  103. ax2.plot(sig1.times,sig1*1e6,'k',label = 'signal')
  104. ax2.plot(sig1.times,np.real(HT1)*1e6,'k',label = 'Real(HT)')
  105. ax2.plot(sig1.times,np.abs(HT1)*1e6,'r',label = 'Mag(HT)')
  106. ax3.plot(sig1.times,np.angle(HT1,deg=True))
  107. ax1.set_xlim([140,2350])
  108. ax2.set_xlim([140,2350])
  109. ax3.set_xlim([140,2350])
  110. ax1.set_ylabel('Sig ($\mu$V)')
  111. ax2.set_ylabel('Sig ($\mu$V)')
  112. ax3.set_ylabel('Sig phase (degree)')
  113. ax1.legend()
  114. ax2.legend()
  115. plt.figure()
  116. plt.plot(np.angle(HT1,deg=True),np.abs(HT1),'*')
  117. plt.figure()
  118. plt.plot(np.angle(HT1,deg=True),np.real(HT1),'*')
  119. plt.figure()
  120. plt.plot(np.angle(HT1,deg=True), np.abs(HT0),'*')
  121. fig, ax = plt.subplots()
  122. phaseShift = np.angle(HT1,deg=True)-np.angle(HT0,deg=True)
  123. for iSamp, samp in enumerate(phaseShift):
  124. if samp <= -180:
  125. phaseShift[iSamp] = samp+360
  126. if samp >=180:
  127. phaseShift[iSamp] = samp-360
  128. h = plt.hist2d(phaseShift, np.log10(np.abs(HT0)), normed = True, bins=50, range= [[np.min(phaseShift), np.max(phaseShift)], [-6.0, -2.5]],vmin=0, vmax=0.04, cmap='jet')#norm = LogNorm())#
  129. plt.colorbar(h[3])