def read_data(self): self.ttime,self.y,self.num=read_two_columns_from_dialog('Select Input File',self.master) self.ns=self.num dur=self.ttime[self.num-1]-self.ttime[0] self.dt=dur/float(self.num) self.sr=1./self.dt self.sr,self.dt=sample_rate_check(self.ttime,self.y,self.num,self.sr,self.dt) plt.ion() plt.clf() plt.figure(1) plt.plot(self.ttime, self.y, linewidth=1.0,color='b') # disregard error plt.grid(True) plt.xlabel('Time (sec)') plt.ylabel(self.y_string.get()) plt.title('Time History') plt.draw() print ("\n Input Signal ") print ("\n samples = %d " % self.num) sr,self.dt,ave,sd,rms,skewness,kurtosis,dur=signal_stats(self.ttime, self.y,self.num) self.button_calculate.config(state = 'normal')
def read_data(self): self.a, self.b, self.num = read_two_columns_from_dialog( 'Select Input File', self.master) sr, dt, mean, sd, rms, skew, kurtosis, dur = signal_stats( self.a, self.b, self.num) print("\n") print(" sr=%8.4g samples/sec" % sr) print(" mean=%8.4g" % mean) print(" std dev=%8.4g" % sd) print(" rms=%8.4g" % rms) print("\n %d points \n" % self.num) plt.close(1) plt.figure(1) plt.plot(self.a, self.b, linewidth=1.0, color='b') # disregard error plt.grid(True) plt.xlabel(self.x_string.get()) plt.ylabel(self.y_string.get()) plt.title(self.t_string.get()) plt.draw() self.button_extract.config(height=2, width=15, state='normal')
def read_data(self): self.a,self.b,self.num=read_two_columns_from_dialog('Select Input File',self.master) sr,dt,mean,sd,rms,skew,kurtosis,dur=signal_stats(self.a,self.b,self.num) print("\n") print(" sr=%8.4g samples/sec" %sr) print(" mean=%8.4g" %mean) print(" std dev=%8.4g" %sd) print(" rms=%8.4g" %rms) print("\n %d points \n" %self.num)
def calculate_invfft(cls, self): print(" Calculating inverse FFT ") YI = ifft(self.Y) print(" YIR") YIR = YI.real print(" psd_th") self.psd_th = YIR[0:self.np] self.np = len(self.psd_th) self.TT = linspace(0, (self.np - 1) * self.dt, self.np) print(" ") print("num_fft=%d " % self.num_fft) print("np=%d " % self.np) stddev = std(self.psd_th) self.psd_th *= (self.spec_grms / stddev) #### check psd ############################################################ a = self.TT b = self.psd_th self.num = len(a) sr, dt, mean, sd, rms, skew, kurtosis, dur = signal_stats( a, b, self.num) self.sr, self.dt = sample_rate_check(a, b, self.num, sr, dt)
def Butterworth_filter_main(self): self.y=self.y_original self.a=np.zeros((4,4),'f') self.b=np.zeros((4,4),'f') self.alpha=np.zeros(2*self.l,'f') self.s=(1+1j)*np.zeros(20,'f') self.om=0 ntype= int(self.Lb_ws.curselection()[0]) self.iband=ntype+1 nref= int(self.Lb_pc.curselection()[0]) self.iphase=nref+1 print ('iband=%d' %self.iband) print ('iphase=%d' %self.iphase) iflag=0 if(self.iband == 1): title_label='Lowpass Filtered Time History' print (title_label) self.f=float(self.f1r.get()) if(self.f>=0.5*self.sr): strer='cutoff frequency must be < Nyquist frequency' tkMessageBox.showerror('Error',strer) iflag=1 if(self.iband == 2): title_label='Highpass Filtered Time History' print (title_label) self.f=float(self.f1r.get()) if(self.f>=0.5*self.sr): strer='cutoff frequency must be < Nyquist frequency' tkMessageBox.showerror('Error',strer) iflag=1 if(self.iband == 3): title_label='Bandpass Filtered Time History' print(" This bandpass filter is implemented as a ") print(" highpass filter and lowpass filter in series. ") self.fh=float(self.f1r.get()) self.fl=float(self.f2r.get()) if(self.fh>0.5*self.sr): strer='error: lower frequency must be < Nyquist frequency' tkMessageBox.showerror('Error',strer) iflag=1 if(self.fl>0.5*self.sr): strer='error: upper frequency must be < Nyquist frequency' tkMessageBox.showerror('Error',strer) self.f=self.fh iflag=1 ################################################################################ if(iflag==0): self.freq=self.f if(self.iband !=3): self.coefficients(self) if(self.iband == 1 or self.iband ==2): self.applymethod(self) if(self.iband == 3): self.f=self.fh self.freq=self.f print("\n Step 1") self.iband=2 self.coefficients(self) self.applymethod(self) self.f=self.fl self.freq=self.f print("\n Step 2") self.iband=1 self.coefficients(self) self.applymethod(self) print (" ") print ("Filtered signal statistics") sr,dt,ave,sd,rms,skewness,kurtosis,dur=signal_stats(self.ttime, self.y,self.num) self.button_sav.config(state = 'normal') plt.close(2) plt.figure(2) nn=len(self.ttime) n= int(self.Lb_ws.curselection()[0]) if(n==0): out1='Lowpass Filtered Time History fc='+str(self.freq)+' Hz' if(n==1): out1='Highpass Filtered Time History fc='+str(self.freq)+' Hz' if(n==2): out1='Bandpass Filtered Time History fc='+str(self.fh)+' to '+str(self.fl)+' Hz' tt=self.ttime y=self.y iflag=0 ijk=0 while(iflag==0): ijk+=1 if(ijk==5): break nn=len(tt) print(nn) try: iflag=1 plt.plot(tt, y, linewidth=1.0,color='b') # disregard error plt.grid(True) plt.xlabel('Time (sec)') plt.ylabel(self.y_string.get()) if(nn>=1000000 and ijk==1): ymin, ymax = plt.ylim() ymin=2*ymin ymax=2*ymax plt.ylim( ymin, ymax ) else: ymin=10*ymin ymax=10*ymax plt.ylim( ymin, ymax ) print(ymax) plt.title(out1) plt.draw() except: iflag=0
def calculation(self): n = int(self.Lb1.curselection()[0]) if not self.ampr.get(): tkMessageBox.showwarning('Warning', 'Enter std dev', parent=self.button_calculate) return if not self.srr.get(): tkMessageBox.showwarning('Warning', 'Enter sample rate', parent=self.button_calculate) return if not self.durr.get(): tkMessageBox.showwarning('Warning', 'Enter duration', parent=self.button_calculate) return sigma = float(self.ampr.get()) sr = float(self.srr.get()) dur = float(self.durr.get()) dt = 1. / sr self.np = int(dur / dt) if (self.np == 0): tkMessageBox.showwarning('Warning', 'Enter number of point = 0') print('\n self.np=%d dur=%8.4g dt=%8.4g \n' % (self.np, dur, dt)) print(" error ") return mu = 0 for i in range(0, int(self.np)): self.a.append(random.gauss(mu, sigma)) self.TT.append(i * dt) if (n == 0): self.lpf = float(self.lpfr.get()) if (self.lpf > 0.3 * sr): self.lpf = 0.3 * sr self.a = vb_white_noise.Butterworth_filter(self.TT, self.a, self.np, self.lpf, dt) print(" ") print("Signal statistics") sd = std(self.a) self.a *= sigma / sd sr, dt, ave, sd, rms, skewness, kurtosis, dur = signal_stats( self.TT, self.a, self.np) # print(len(self.TT)) # print(len(self.a)) plt.ion() plt.clf() plt.figure(1) plt.plot(self.TT, self.a, linewidth=1.0, color='b') # disregard error plt.grid(True) plt.xlabel('Time (sec)') plt.ylabel('Amp') plt.title('White Noise Time History') plt.draw() plt.figure(2) hist, bins = histogram(self.a, bins=21, density=False) width = 0.7 * (bins[1] - bins[0]) center = (bins[:-1] + bins[1:]) / 2 plt.bar(center, hist, align='center', width=width) plt.ylabel('Counts') plt.xlabel('Amplitude') plt.title('Histogram') plt.draw() self.button_ex.config(state='normal')
def decimate(self): ndec = 2 + int(self.Lb3.curselection()[0]) nf = 1 + int(self.Lb4.curselection()[0]) self.num = len(self.a) ############################################################################### t = self.a y = self.b if (nf == 1): self.fc = float(self.lpfr.get()) if (self.fc >= 0.5 * self.sr): strer = 'cutoff frequency must be < Nyquist frequency' tkMessageBox.showerror('Error', strer) return f = self.fc fh = f fl = f iphase = 1 # refilter for phase correction dt = self.dt l = 6 # order iband = 1 # lowpass y = BUTTERWORTH(l, f, fh, fl, dt, iband, iphase, y).Butterworth_filter_main() print(" ") print("Filtered signal statistics") sr, dt, ave, sd, rms, skewness, kurtosis, dur = signal_stats( t, y, self.num) ############################################################################### self.td = t[0:self.num:ndec] self.yd = y[0:self.num:ndec] self.button_save.config(state='normal') plt.ion() plt.clf() plt.figure(1) plt.plot(self.a, self.b, linewidth=1.0, color='b') # disregard error plt.grid(True) plt.xlabel(self.xstr) plt.ylabel(self.ystr) plt.title(self.tstr) plt.draw() plt.figure(2) plt.plot(self.td, self.yd, linewidth=1.0, color='b') # disregard error plt.grid(True) plt.xlabel(self.xstr) plt.ylabel(self.ystr) plt.title("Decimated: " + self.tstr) plt.draw() self.button_extract.config(height=2, width=15, state='normal')
def calculation(self): n = int(self.Lb1.curselection()[0]) sigma = float(self.ampr.get()) sr = float(self.srr.get()) dur = float(self.durr.get()) dt = 1. / sr self.np = int(dur / dt) mu = 0 for i in range(0, int(2 * self.np)): self.a.append(random.gauss(mu, sigma)) self.z = fft(self.a) num = len(self.z) self.z /= float(num) epi = 8 * pi tpi = 2 * pi df = 1. / (num * dt) nh = int(num / 2.) H = zeros(nh, complex128) for i in range(0, nh): s = (1j) * (i - 1) * tpi * df H[i] = 3 / sqrt(s + epi) # print(H.dtype) nf = nh frf = zeros(num, complex128) frf_amp = zeros(num, complex128) A = zeros(num, complex128) frf[0:nf - 1] = H[0:nf - 1] aa = H bb = flipud(aa) for i in range(0, nf): frf[i + nf] = bb[i].conjugate() nf = 2 * nf print(frf.dtype) frf_amp = flipud(frf) # print(frf_amp.dtype) # print(self.z.dtype) # print(A.dtype) for i in range(0, num): A[i] = frf_amp[i] * self.z[i] pink = ifft(A).real self.a = pink[1:self.np - 1] length_a = len(self.a) for i in range(0, length_a): self.TT.append(i * dt) if (n == 0): self.lpf = float(self.lpfr.get()) if (self.lpf > 0.3 * sr): self.lpf = 0.3 * sr self.a = vb_pink_noise.Butterworth_filter(self.TT, self.a, length_a, self.lpf, dt) print(" ") print("Signal statistics") sd = std(self.a) self.a *= sigma / sd sr, dt, ave, sd, rms, skewness, kurtosis, dur = signal_stats( self.TT, self.a, length_a) plt.ion() plt.clf() plt.figure(1) plt.plot(self.TT, self.a, linewidth=1.0, color='b') # disregard error plt.grid(True) plt.xlabel('Time (sec)') plt.ylabel('Amp') plt.title('Pink Noise Time History') plt.draw() plt.figure(2) hist, bins = histogram(self.a, bins=21, density=False) width = 0.7 * (bins[1] - bins[0]) center = (bins[:-1] + bins[1:]) / 2 plt.bar(center, hist, align='center', width=width) plt.ylabel('Counts') plt.xlabel('Amplitude') plt.title('Histogram') plt.draw() self.button_ex.config(state='normal')
def calculate(self): self.fc=float(self.fcr.get()) OM=tan(pi*self.fc*self.dt/self.scale) OM2=OM**2 den=1+3*OM+3*OM2 b0=3*OM2/den b1=2*b0 b2=b0 a1=2*(-1+3*OM2)/den a2=(1-3*OM+3*OM2)/den print ("\n Apply filter ") bc=[b0,b1,b2] ac=[1,a1,a2] self.yf=lfilter(bc, ac, self.y, axis=-1, zi=None) plt.close(2) plt.figure(2) plt.plot(self.ttime, self.yf, linewidth=1.0,color='b') # disregard error plt.grid(True) plt.xlabel('Time (sec)') plt.ylabel(self.y_string.get()) out1='Lowpass Filtered Time History fc='+str(self.fc)+' Hz' plt.title(out1) ### fmin=self.fc/100; if(fmin>1.): fmin=1 fmax=5*self.fc nf=1 nf=int(np.ceil(48.*np.log(fmax/fmin)/np.log(2.))) print ("\n nf = %d " % nf) f=np.zeros(nf,'f') H=np.zeros(nf,'complex') H_mag=np.zeros(nf,'f') H_phase=np.zeros(nf,'f') f[0]=fmin for i in range(1,int(nf)): f[i]=f[i-1]*2**(1./48.) for i in range(0,int(nf)): s=(1J)*(self.scale*f[i]/self.fc) H[i]=3./(s**2+3*s+3) H_phase[i]=(180./pi)*atan2(H[i].imag,H[i].real) H_mag[i]=abs(H[i]) plt.close(3) plt.figure(3) plt.plot(f, H_mag) title_string= ' Transfer Magnitude fc='+str(self.fc)+' Hz' plt.title(title_string) plt.xlabel('Frequency (Hz) ') plt.ylabel('Magnitude') plt.grid(True, which="both") plt.xscale('linear') plt.yscale('linear') plt.yticks([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]) plt.draw() plt.close(4) plt.figure(4) plt.plot(f, H_phase) title_string= ' Transfer Phase fc='+str(self.fc)+' Hz' plt.title(title_string) plt.xlabel('Frequency (Hz) ') plt.ylabel('Phase(deg)') plt.grid(True, which="both") plt.xscale('linear') plt.draw() print ("\n Output Signal ") sr,self.dt,ave,sd,rms,skewness,kurtosis,dur=signal_stats(self.ttime, self.yf,self.num) self.button_sav.config(state = 'normal')
def read_data(self): self.a, self.b, self.num = read_two_columns_from_dialog( 'Select Input File', self.master) dur = self.a[self.num - 1] - self.a[0] self.dt = dur / float(self.num) self.sr = 1. / self.dt sr, dt, mean, sd, rms, skew, kurtosis, dur = signal_stats( self.a, self.b, self.num) bb = self.b - mean v = differentiate_function(bb, self.num, self.dt) rf = (np.std(v) / np.std(self.b)) / (2 * np.pi) amax = max(self.b) amin = min(self.b) aamin = abs(amin) mmax = amax if (aamin > mmax): mmax = aamin crest = mmax / sd ############################################################################### y = np.zeros(self.num, 'f') self.v = np.zeros(self.num, 'f') y = self.b k = 0 self.v[0] = y[0] k = 1 for i in range(1, (self.num - 1)): slope1 = (y[i] - y[i - 1]) slope2 = (y[i + 1] - y[i]) if ((slope1 * slope2) <= 0): self.v[k] = y[i] k += 1 last_v = k self.v[k] = y[self.num - 1] self.v = abs(self.v[0:last_v]) # for i in range (0,last_v): # print ("%8.4g" %self.v[i]) ############################################################################### q_sr = " sample rate = %8.4g sps \n" % sr q_dt = " time step = %8.4g sec \n\n" % dt q_dur = " duration = %8.4g sec \n" % dur q_num = " num = %9.6g \n\n" % self.num q_mean = " mean = %8.4g \n" % mean q_sd = " std dev = %8.4g \n" % sd q_rms = " rms = %8.4g \n\n" % rms q_max = " max = %8.4g \n" % amax q_min = " min = %8.4g \n" % amin q_crest = "crest factor = %8.4g \n\n" % crest q_skew = " skewness = %8.4g \n" % skew q_kurt = " kurtosis = %8.4g \n\n" % kurtosis q_rf = " Rice Characteristic Frequency \n = %8.4g Hz " % rf self.textWidget.delete(1.0, tk.END) self.textWidget.insert('1.0', q_sr) self.textWidget.insert('end', q_dt) self.textWidget.insert('end', q_dur) self.textWidget.insert('end', q_num) self.textWidget.insert('end', q_max) self.textWidget.insert('end', q_min) self.textWidget.insert('end', q_crest) self.textWidget.insert('end', q_mean) self.textWidget.insert('end', q_sd) self.textWidget.insert('end', q_rms) self.textWidget.insert('end', q_skew) self.textWidget.insert('end', q_kurt) self.textWidget.insert('end', q_rf) self.sr, self.dt = sample_rate_check(self.a, self.b, self.num, self.sr, self.dt) self.button_replot_histogram.config(state='normal') plt.ion() plt.clf() plt.close(1) plt.figure(1) plt.plot(self.a, self.b, linewidth=1.0, color='b') # disregard error plt.grid(True) plt.xlabel('Time (sec)') plt.ylabel(self.y_string.get()) plt.title('Input Time History') plt.draw() print("\n samples = %d " % self.num) self.plot_histogram(self)