def plot_tf(self, windowed=False, bode=True):
     if bode:
         X_mag = 20*np.log10(abs(self.tf))
     else:
         X_mag = abs(self.tf)
                 
     fig = plt.figure()
     
     if windowed:
         if bode:
             W_mag = 20*np.log10(abs(self.windowed_tf))
         else:
             W_mag = abs(self.windowed_tf)
         
         ax1 = fig.add_subplot(211)
         ax2 = fig.add_subplot(212)
         ax2.plot(self.windowed_freqs, W_mag, label='Windowed FFT')
         ax2.legend()
         
         p = df.plotdf(fig, ax2)
         p.plot()
     else:
         ax1 = fig.add_subplot(111)
         
     ax1.plot(self.freqs, X_mag, label='FFT')
     ax1.legend()
     
     p = df.plotdf(fig, ax1)
     p.plot()
     
     plt.show()
 def get_noise(self, signal):
     """
     Allows the user to select the noise portion of signal and 
     returns it for further analysis
     """
     # Get the noise portion
     fig = plt.figure()
     ax = fig.add_subplot(111)
     ax.plot(signal, '.')
     ax.set_title('Select noise range and close window')
     
     p = df.plotdf(fig, ax, max_points=1E6)
     p.plot()
     plt.show()
     
     xlims = p.x_bounds
     temp_noise = signal[xlims[0]:xlims[-1]]
     l = len(temp_noise)
     
     if l < self.L_sig:
         # Make the length of the noise equal to the length of the
         # data by making copies of the noise section
         ratio = int(float(self.L_sig) / l) + 1
         
         noise = np.zeros(ratio*l)        
         
         for i in range(ratio):
             noise[i*l:(i+1)*l] = temp_noise
             
     else:
         noise = temp_noise            
         
     return noise[0:self.L_sig]
 def plot_fft(self, bode=True, show=True):
     
     freqs = self.freqs
     X = self.transform
     
     if bode:
         X_mag = 20*np.log10(abs(X))
     else:
         X_mag = abs(X)
 
     fig = plt.figure()
     ax = fig.add_subplot(111)
     ax.plot(freqs, X_mag, label='{} FFT'.format(self.name))
     ax.set_ylabel('|FFT|')
     ax.set_xlabel('Frequency (Hz)')
     ax.legend()
     
     p = df.plotdf(fig,ax)
     p.plot()
     
     fig = plt.figure()
     ax = fig.add_subplot(211)
     ax.plot(self.time, self.signal, label='{}'.format(self.name))
     ax.set_ylabel('(A)')
     ax.set_xlabel('Time (s)')
     ax.legend()
     p = df.plotdf(fig,ax)
     p.plot()
     
     ax = fig.add_subplot(212)
     ax.plot(freqs, X_mag, label='{} FFT'.format(self.name))
     ax.set_ylabel('|FFT|')
     ax.set_xlabel('Frequency (Hz)')
     ax.legend()
     
     p = df.plotdf(fig,ax)
     p.plot()
     
     if show:        
         plt.show()
Beispiel #4
0
plt.figure()
plt.title('PSD in dB')
plt.plot(I_fft.freqs[0:L], 10*np.log10(abs(I_fft.transform[0:L])**2),
         label='Input')
plt.plot(Ir_fft.freqs[0:L], 10*np.log10(abs(Ir_fft.transform[0:L])**2),
         label='Output')
plt.plot(I_fft.noise_freqs[0:L], 10*np.log10(abs(I_fft.noise_fft[0:L])**2),
         '.', label='Noise')
# plt.plot(I_fft.freqs, 10*np.log10(abs(I_fft.transform)**2) -
#          10*np.log10(abs(I_fft.noise_fft)**2), '.',
#          label='Input - Noise')
# plt.plot([I_fft.freqs[0], I_fft.freqs[-1]], [-3,-3], 'r')
plt.legend()

p = df.plotdf(plt.gcf(), plt.gca(), max_points=1E5)
p.plot()
plt.show()

sys.exit(1)

# I_fft.print_details()
# Ir_fft.print_details()
#
# I_fft.plot_fft(show=False)
# Ir_fft.plot_fft(show=False)

tf = fourier.FFT_RS_TransferFunction(I_fft, Ir_fft)

# tf.frequency_window(5.0E3)
tf.plot_tf(bode=False)
Beispiel #5
0
    def calc_dip(self,rs='all', meass='all', window=1.0):
        os.chdir('/home/jaime/Documents/ResearchTopics/Publications/Current Reflections/Data Sets/')

        for rss in self.root.iter('return_stroke'):

            r_s = int(rss.find('number').text)

            if rs == -1:
                pass
            elif 'all' in rs.lower():
                pass
            elif r_s == int(rs):
                pass
            else:
                continue

            fileSave = "./DataFiles/%s_data_%s_rs%d.p" % (self.eventNamef,
                                                          self.eventDate, r_s)
            data = pickle.load(open(fileSave, "rb"))

            event = '%s RS%d' % (self.eventName, r_s)

            keys = data.keys()

            if meass in keys:

                # ii = self.moving_average(data[meass].data, window)
                ii = data[meass].data
                time = data[meass].dataTime*1.0E-6

                fig, ax = plt.subplots(1,1)
                ax.plot(time, ii)
                plt.title('Calculate Dip')

                p = df.plotdf(fig, ax)
                p.plot()
                plt.show()

                y = p.ax.get_lines()[0].get_ydata()
                #~ y -= np.average(y[:20])

                time = p.ax.get_lines()[0].get_xdata()

                t_max = np.argmax(y)
                t_min = np.argmin(y)

                amp = np.max(y) - np.min(y)

                time *= 1E9

                plt.plot(time,y, '-x')
                plt.plot(time[t_max], y[t_max], 'ro')
                plt.plot(time[t_min], y[t_min], 'ro')
                plt.title('Dip')

                ylims = plt.gca().get_ylim()
                plt.plot([time[t_max], time[t_max]], [ylims[0], ylims[1]], 'r')
                plt.plot([time[t_min], time[t_min]], [ylims[0], ylims[1]], 'r')

                xlims = plt.gca().get_xlim()
                plt.plot([xlims[0],xlims[1]], [y[t_max],y[t_max]], 'r')
                plt.plot([xlims[0],xlims[1]], [y[t_min],y[t_min]], 'r')
                plt.text((xlims[-1] - xlims[0])/2 + xlims[0],(ylims[-1] - ylims[0])/2 + ylims[0], 'Peak = %0.2f kA' % (amp*1.0E-3))

                plt.show()

                print('RS # %d => %s: %0.2f kA' % (r_s, meass, amp*1E-3))
Beispiel #6
0
    def calc_dip_time(self, rs='all', meass='all', window=1.0):
        os.chdir('/home/jaime/Documents/ResearchTopics/Publications/Current Reflections/Data Sets/')

        for rss in self.root.iter('return_stroke'):

            r_s = int(rss.find('number').text)

            if rs == -1:
                pass
            elif 'all' in rs.lower():
                pass
            elif r_s == int(rs):
                pass
            else:
                continue

            fileSave = "./DataFiles/%s_data_%s_rs%d.p" % (self.eventNamef,
                                                          self.eventDate,
                                                          r_s)
            data = pickle.load(open(fileSave, "rb"))

            event = '%s RS%d' % (self.eventName, r_s)

            keys = data.keys()

            if meass in keys:

                ii = data[meass].data
                # ii = self.moving_average(ii, window)
                time = data[meass].dataTime*1.0E-6

                fig, ax = plt.subplots(1, 1)
                ax.plot(time, ii)
                plt.title('Get Noise Only')

                p = df.plotdf(fig, ax)
                p.plot()
                plt.show()

                y = p.ax.get_lines()[0].get_ydata()

                mean = np.mean(y)
                std = np.std(y)
                three_sigma = mean + 2*std

                fig, ax = plt.subplots(1,1)
                ax.plot(time, ii)
                plt.title('Get Rise Only')

                p = df.plotdf(fig, ax)
                p.plot()
                plt.show()

                y = p.ax.get_lines()[0].get_ydata()
                y -= mean

                t = p.ax.get_lines()[0].get_xdata()

                peak = np.max(y)
                amp = peak - three_sigma

                ind_ten = np.argmax(np.abs(1.0/(0.1*amp + three_sigma - y)))
                ind_ninety = np.argmax(np.abs(1.0/(0.9*amp + three_sigma - y)))

                ind_start = np.argmax(np.abs(1.0/(three_sigma - y)))
                t_start = t[ind_start]
                t_max = t[np.argmax(y)]

                t_ten = t[ind_ten]
                t_ninety = t[ind_ninety]

                fig, ax = plt.subplots(1,1)
                ax.plot(time, ii)
                plt.title('Get Dip Only')

                p = df.plotdf(fig, ax)
                p.plot()
                plt.show()

                y = p.ax.get_lines()[0].get_ydata()
                y -= mean

                t = p.ax.get_lines()[0].get_xdata()

                amp = np.max(y) - np.min(y)
                dip_peak = amp

                ind_end = 0
                t_end = t[ind_end]
                t_min = t[np.argmin(y)]

                ind1 = np.where(time == t_start)
                ind2 = np.where(time == t_end)
                ind3 = np.where(time == t_max)
                ind4 = np.where(time == t_min)

                risetime = t_ninety - t_ten

                delay = t_end - t_start
                delay1 = t_min - t_max

                plt.plot(time*1E6, ii - mean)
                ylims = plt.gca().get_ylim()
                plt.plot([time[ind1]*1E6, time[ind1]*1E6],
                         [ylims[0], ylims[1]],'r')
                plt.plot([time[ind2]*1E6, time[ind2]*1E6],
                         [ylims[0], ylims[1]], 'r')
                plt.plot([time[ind3]*1E6, time[ind3]*1E6],
                         [ylims[0], ylims[1]], 'g')
                plt.plot([time[ind4]*1E6, time[ind4]*1E6],
                         [ylims[0], ylims[1]], 'g')

                xlims = plt.gca().get_xlim()
                plt.plot([xlims[0],xlims[1]], [0,0],
                         '--k', linewidth=1.0)
                plt.plot([xlims[0],xlims[1]],
                         [three_sigma - mean, three_sigma - mean],
                         '--g', linewidth=1.0)

                plt.text(time[ind1]*1E6,0, '%0.2f (us)' % (delay*1E6), color='red')
                plt.text(time[ind4]*1E6,0, '%0.2f (us)' % (delay1*1E6), color='green')
                plt.xlim([0,30])


                print('RS # %d => Peak: %0.2f (kA)' % (r_s, peak*1E-3))
                print('RS # %d => Risetime: %0.2f (ns)' %
                      (r_s, risetime*1.0E9))
                print('RS # %d => Dip Peak: %0.2f (kA)' %
                      (r_s, dip_peak*1E-3))
                print('RS # %d => Dip Delay start to start: %0.2f (us)' %
                      (r_s, delay*1E6))
                print('RS # %d => Dip Delay peak to dip: %0.2f (us)' %
                      (r_s, delay1*1E6))
                plt.show()
Beispiel #7
0
    def calc_rise(self, rs='all', meass='all'):
        os.chdir('/home/jaime/Documents/ResearchTopics/Publications/Current Reflections/Data Sets/')
        for rss in self.root.iter('return_stroke'):

            r_s = int(rss.find('number').text)

            if rs == -1:
                pass
            elif 'all' in rs.lower():
                pass
            elif r_s == int(rs):
                pass
            else:
                continue

            fileSave = "./DataFiles/%s_data_%s_rs%d.p" % (self.eventNamef,
                                                             self.eventDate,
                                                             r_s)
            data = pickle.load(open(fileSave, "rb"))

            event = '%s RS%d' % (self.eventName, r_s)

            keys = data.keys()

            if meass in keys:

                ii = data[meass].data
                time = data[meass].dataTime*1.0E-6

                fig, ax = plt.subplots(1,1)
                ax.plot(time, ii)
                plt.title('Get Noise Only')

                p = df.plotdf(fig, ax)
                p.plot()
                plt.show()

                y = p.ax.get_lines()[0].get_ydata()

                mean = np.mean(y)
                std = np.std(y)
                three_sigma = 3*std

                fig, ax = plt.subplots(1,1)
                ax.plot(time, ii - mean)
                plt.title('Select rise only')

                p = df.plotdf(fig, ax)
                p.plot()
                plt.show()

                y = p.ax.get_lines()[0].get_ydata()
                # y -= mean

                t = p.ax.get_lines()[0].get_xdata()

                peak = np.max(y)
                amp = peak - three_sigma

                t_start = np.argmax(np.abs(1.0/(0.1*amp + three_sigma - y)))
                t_end = np.argmax(np.abs(1.0/(0.9*amp + three_sigma - y)))

                rise_time = (t[t_end] - t[t_start])*1.0E9

                tt_start = np.argmax(np.abs(1.0/(three_sigma - y)))

                ind1 = np.where(time == t[tt_start])
                ind2 = np.where(time == t[t_start])
                ind3 = np.where(time == t[t_end])

                time *= 1.0E9

                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.plot(time, ii - mean, '-x')
                ax.plot(time[ind2],ii[ind2], 'ro')
                ax.plot(time[ind3], ii[ind3], 'ro')
                ax.set_title('Peak and Risetime')

                ylims = plt.gca().get_ylim()
                ax.plot([time[ind1], time[ind1]], [ylims[0], ylims[1]],
                         '--r', linewidth=1.0)
                ax.plot([time[ind2], time[ind2]], [ylims[0], ylims[1]],
                         '--r', linewidth=1.0)
                ax.plot([time[ind3], time[ind3]], [ylims[0], ylims[1]],
                         '--r', linewidth=1.0)

                xlims = plt.gca().get_xlim()
                ax.plot([xlims[0],xlims[1]], [ii[ind2],ii[ind2]],
                         '--r', linewidth=1.0)
                ax.plot([xlims[0],xlims[1]], [ii[ind3],ii[ind3]],
                         '--r', linewidth=1.0)

                ax.plot([xlims[0],xlims[1]], [0,0],
                         '--k', linewidth=1.0)
                ax.plot([xlims[0],xlims[1]], [three_sigma,three_sigma],
                         '--g', linewidth=1.0)

                plt.text((xlims[-1] - xlims[0])/2 + xlims[0],(ylims[-1] - ylims[0])/2 + ylims[0]+1000, 'Peak = %0.2f kA' % ((peak)*1.0E-3))
                plt.text((xlims[-1] - xlims[0])/2 + xlims[0],(ylims[-1] - ylims[0])/2 + ylims[0], 'Risetime = %0.2f ns' % rise_time)

                ax.set_xlim([0,30E-6*1E9])

                print('RS # %d => %s: %0.2f A %0.2f ns' % (r_s, meass, peak,
                                                            rise_time))

                p = df.plotdf(fig, ax)
                p.plot()

                plt.show()
    def calc_dE_pulse(self, rs='all', meass='all'):

        for rss in self.root.iter('return_stroke'):

            r_s = int(rss.find('number').text)

            if rs == -1:
                pass
            elif 'all' in rs.lower():
                pass
            elif r_s == int(rs):
                pass
            else:
                continue

            fileSave = "./DataFiles/%s_data_%s_rs%d.p" % (self.eventNamef,
                                                          self.eventDate,
                                                          r_s)
            data = pickle.load(open(fileSave, "rb"))

            event = '%s RS%d' % (self.eventName, r_s)

            keys = data.keys()

            if meass in keys:

                ii = data[meass]['data']
                time = data[meass]['time']*1.0E-6

                fig, ax = plt.subplots(1,1)
                ax.plot(time, ii)
                plt.title('Select RS peak and bipolar pulse')

                p = df.plotdf(fig, ax)
                p.plot()
                plt.show()

                y = p.ax.get_lines()[0].get_ydata()
                # y -= mean

                t = p.ax.get_lines()[0].get_xdata()

                peak = np.argmax(y)
                trough = np.argmin(y)

                delay = (t[trough] - t[peak])*1.0E9

                ind2 = np.where(time == t[peak])
                ind3 = np.where(time == t[trough])

                time *= 1.0E9

                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.plot(time, ii, '-x')
                ax.plot(time[ind2],ii[ind2], 'ro')
                ax.plot(time[ind3], ii[ind3], 'ro')
                ax.set_title('dE/dt pulse delay')

                ylims = plt.gca().get_ylim()
                ax.plot([time[ind2], time[ind2]], [ylims[0], ylims[1]],
                         '--r', linewidth=1.0)
                ax.plot([time[ind3], time[ind3]], [ylims[0], ylims[1]],
                         '--r', linewidth=1.0)

                xlims = plt.gca().get_xlim()
                ax.plot([xlims[0],xlims[1]], [ii[ind2],ii[ind2]],
                         '--r', linewidth=1.0)
                ax.plot([xlims[0],xlims[1]], [ii[ind3],ii[ind3]],
                         '--r', linewidth=1.0)

                plt.text((xlims[-1] - xlims[0])/2 + xlims[0],(ylims[-1] - ylims[0])/2 + ylims[0], 'Risetime = %0.2f ns' % delay)

                ax.set_xlim([0,30E-6*1E9])

                print('RS # %d => %s: %0.2f ns' % (r_s, meass, delay))

                p = df.plotdf(fig, ax)
                p.plot()

                plt.show()
 def noise_analysis(self, bode=True, plot=True):
     """
     Finds the FFT of the noise signal and displays it together with
     the signal of interest to compare the frequency contents.
     
     Because it is noise analysis, it is more appropriate to find the
     power spectral density (PSD). PSD = abs(Xfft)**2.
     
     This is not the one sided PSD. The one-sided PSD is 2*PSD. The
     plots here show the dual-sided PSD up to the maximum resolvable
     frequency (L_fft/2)
     """
     
     if not(isinstance(self.noise, np.ndarray)):
         self.noise = self.get_noise(self.signal)
     else:
         self.noise = self.get_noise(self.noise)
     
     # Get FFT of noise
     hann = np.hanning(len(self.noise))
     X_s = fft(self.noise*hann, self.L_fft)/float(self.L_fft)
     freqs = self.freqs
     
     self.noise_fft = X_s
     self.noise_freqs = freqs
     
     if plot:
         # Plot
         fig = plt.figure()
         ax = fig.add_subplot(111)
         ax.plot(self.time*1E3,self.noise, label='Noise')
         ax.plot(self.time*1E3, self.signal, label='{}'.format(self.name))
         ax.set_ylabel('(A)')
         ax.set_xlabel('Time (ms)')
         ax.legend()
         
         p = df.plotdf(fig,ax)
         p.plot()
         
         if bode:
             psd_noise = 10*np.log10(abs(X_s)**2)
             psd_signal = 10*np.log10(abs(self.transform)**2)
             
         else:
             psd_noise = abs(X_s)**2
             psd_signal = abs(self.transform)**2
         
         fig = plt.figure()
         ax = fig.add_subplot(111)
         ax.plot(freqs[0:self.L_fft/2+1]*1E-3, psd_noise[0:self.L_fft/2+1],
                 '.', label='Noise PSD')
         ax.plot(freqs[0:self.L_fft/2+1]*1E-3, psd_signal[0:self.L_fft/2+1],
                 label='{} PSD'.format(self.name))
         ax.set_ylabel('PSD')
         ax.set_xlabel('Frequency (kHz)')
         # ax.set_yscale('log')
         ax.legend()
         
         p = df.plotdf(fig,ax)
         p.plot()
         
         plt.show()
import matplotlib.pyplot as plt

import iclrt_tools.plotting.dfplots as df

### Regular dfplots
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, axisbg='#FFFFCC')

x = np.arange(0.0, 5.0, 0.01)
y = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))

ax.plot(x, y, '-')
ax.set_ylim(-2,2)
ax.set_title('Press left mouse button and drag to test')

p = df.plotdf(fig, ax)
p.plot()

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, axisbg='#FFFFCC')

x = np.arange(0.0, 5.0, 0.01)
y = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))

line = ax.plot(x, y, '-')
ax.set_ylim(-2,2)
ax.set_title('Press left mouse button and drag to test')

p1 = df.plotdf(fig, ax)
p1.plot()