def filter_data(psignal, fmin, fmax): """ This function is used to filter with a bandpass filter between fmin/fmax """ if not isinstance(psignal, seism_psignal): print("[ERROR]: found error filtering psignal.") return False delta_t = psignal.dt psignal.accel = s_filter(psignal.accel, delta_t, type='bandpass', family='butter', fmin=fmin, fmax=fmax, N=4, rp=0.1, rs=100) psignal.velo = s_filter(psignal.velo, delta_t, type='bandpass', family='butter', fmin=fmin, fmax=fmax, N=4, rp=0.1, rs=100) psignal.displ = s_filter(psignal.displ, delta_t, type='bandpass', family='butter', fmin=fmin, fmax=fmax, N=4, rp=0.1, rs=100) psignal.data = np.c_[psignal.displ, psignal.velo, psignal.accel] return psignal
def filter_data(psignal, fmin, fmax): """ This function is used to filter with a bandpass filter between fmin/fmax """ if not isinstance(psignal, seism_psignal): print("[ERROR]: found error filtering psignal.") return False delta_t = psignal.dt psignal.accel = s_filter(psignal.accel, delta_t, type='bandpass', family='butter', fmin=fmin, fmax=fmax, N=4, rp=0.1, rs=100) psignal.velo = s_filter(psignal.velo, delta_t, type='bandpass', family='butter', fmin=fmin, fmax=fmax, N=4, rp=0.1, rs=100) psignal.displ = s_filter(psignal.displ, delta_t, type='bandpass', family='butter', fmin=fmin, fmax=fmax, N=4, rp=0.1, rs=100) psignal.data = np.c_[psignal.displ, psignal.velo, psignal.accel] return psignal
def process_signal_dt(signal, dt, fmax): """ Processes signal with common dt and fmax. """ # call low_pass filter at fmax signal.accel = s_filter(signal.accel, signal.dt, type='lowpass', family='butter', fmax=fmax, N=4, rp=0.1, rs=100) signal.velo = s_filter(signal.velo, signal.dt, type='lowpass', family='butter', fmax=fmax, N=4, rp=0.1, rs=100) signal.displ = s_filter(signal.displ, signal.dt, type='lowpass', family='butter', fmax=fmax, N=4, rp=0.1, rs=100) # interpolate signal.accel = interp(signal.accel, signal.samples, signal.dt, dt) signal.velo = interp(signal.velo, signal.samples, signal.dt, dt) signal.displ = interp(signal.displ, signal.samples, signal.dt, dt) signal.samples = signal.accel.size signal.dt = dt return signal
def process_signal_dt(signal, dt, fmax): """ Processes signal with common dt and fmax. """ # call low_pass filter at fmax signal.accel = s_filter(signal.accel, signal.dt, type='lowpass', family='butter', fmax=fmax, N=4, rp=0.1, rs=100) signal.velo = s_filter(signal.velo, signal.dt, type='lowpass', family='butter', fmax=fmax, N=4, rp=0.1, rs=100) signal.displ = s_filter(signal.displ, signal.dt, type='lowpass', family='butter', fmax=fmax, N=4, rp=0.1, rs=100) # interpolate signal.accel = interp(signal.accel, signal.samples, signal.dt, dt) signal.velo = interp(signal.velo, signal.samples, signal.dt, dt) signal.displ = interp(signal.displ, signal.samples, signal.dt, dt) signal.samples = signal.accel.size signal.dt = dt return signal
def process(signal): """ The function takes a signal, use its's current data to get acceleration, velocity, and displacement. Then return a psignal. """ if not isinstance(signal, seism_signal): print("[ERROR]: instance error; process signal objects only.") return # Correction of base lines was commented out here to avoid problems with synchronization # correct_baseline(signal) acc = np.array([], float) vel = np.array([], float) dis = np.array([], float) dt = signal.dt if signal.type == 'a': acc = signal.data acc = s_filter(acc, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) vel = integrate(acc, dt) vel = s_filter(vel, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) dis = integrate(vel, dt) dis = s_filter(dis, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) elif signal.type == 'v': vel = signal.data vel = s_filter(vel, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) acc = derivative(vel, dt) dis = integrate(vel, dt) dis = s_filter(dis, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) elif signal.type == 'd': dis = signal.data dis = s_filter(dis, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) vel = derivative(dis, dt) acc = derivative(vel, dt) else: pass psignal = seism_psignal(signal.samples, signal.dt, np.c_[dis, vel, acc], 'c', acc, vel, dis) return psignal
def simple_plot(parameter, filenames, stations, output_file, plot_title=None): """ plotting velocity for data and FAS only acceleration for Response """ all_styles = [ 'k', 'r', 'b', 'm', 'g', 'c', 'y', 'brown', 'gold', 'blueviolet', 'grey', 'pink' ] orientation = ['N/S', 'E/W', 'Up/Down'] # Check number of input timeseries if len(stations) > len(all_styles): print("[ERROR]: Too many timeseries to plot!") sys.exit(-1) delta_ts = [station[0].dt for station in stations] files = [os.path.basename(filename) for filename in filenames] xtmin = parameter[0] xtmax = parameter[1] xfmin = parameter[2] xfmax = parameter[3] filter_flag = parameter[4] fmin = parameter[5] fmax = parameter[6] tmin = parameter[7] tmax = parameter[8] cut_flag = parameter[9] min_is = [int(xtmin / delta_t) for delta_t in delta_ts] max_is = [int(xtmax / delta_t) for delta_t in delta_ts] period = get_period(tmin, tmax) f, axarr = plt.subplots(nrows=3, ncols=3, figsize=(14, 9)) for i in range(0, 3): title = orientation[i] signals = [station[i] for station in stations] samples = [signal.samples for signal in signals] vels = [signal.velo for signal in signals] accs = [signal.accel for signal in signals] for sample, max_i, delta_t in zip(samples, max_is, delta_ts): if sample - 1 < max_i: print("[ERROR]: t_max has to be under %f" % ((sample - 1) * delta_t)) sys.exit(1) # filtering data if filter_flag: vels = [ s_filter(vel, delta_t, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) for vel, delta_t in zip(vels, delta_ts) ] accs = [ s_filter(acc, delta_t, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) for acc, delta_t in zip(accs, delta_ts) ] # cutting signal by bounds c_vels = [ vel[min_i:max_i] for vel, min_i, max_i in zip(vels, min_is, max_is) ] c_accs = [ acc[min_i:max_i] for acc, min_i, max_i in zip(accs, min_is, max_is) ] times = [np.arange(xtmin, xtmax, delta_t) for delta_t in delta_ts] points = get_points(samples) if cut_flag: freqs, fas_s = zip(*[ FAS(c_vel, delta_t, points, xfmin, xfmax, 3) for c_vel, delta_t in zip(c_vels, delta_ts) ]) rsps = cal_acc_response(period, c_accs, delta_ts) else: freqs, fas_s = zip(*[ FAS(vel, delta_t, points, xfmin, xfmax, 3) for vel, delta_t in zip(vels, delta_ts) ]) rsps = cal_acc_response(period, accs, delta_ts) axarr[i][0] = plt.subplot2grid((3, 4), (i, 0), colspan=2, rowspan=1) axarr[i][0].set_title(title) axarr[i][0].grid(True) styles = all_styles[0:len(times)] for timeseries, c_vel, style in zip(times, c_vels, styles): axarr[i][0].plot(timeseries, c_vel, style) if i == 0: plt.legend(files, prop={'size': 8}) plt.xlim(xtmin, xtmax) axarr[i][1] = plt.subplot2grid((3, 4), (i, 2), rowspan=1, colspan=1) axarr[i][1].set_title('Fourier Amplitude Spectra') axarr[i][1].grid(True, which='both') axarr[i][1].set_xscale('log') axarr[i][1].set_yscale('log') for freq, fas, style in zip(freqs, fas_s, styles): axarr[i][1].plot(freq, fas, style) tmp_xfmin = 0 if xfmin < 0.5: tmp_xfmin = 0 else: tmp_xfmin = xfmin plt.xlim(tmp_xfmin, xfmax) axarr[i][2] = plt.subplot2grid((3, 4), (i, 3), rowspan=1, colspan=1) axarr[i][2].set_title("Response Spectra") axarr[i][2].set_xscale('log') axarr[i][2].grid(True) for rsp, style in zip(rsps, styles): axarr[i][2].plot(period, rsp, style) plt.xlim(tmin, tmax) # Make nice plots with tight_layout f.tight_layout() # Add overall title if provided if plot_title is not None: st = plt.suptitle(plot_title, fontsize=16) # shift subplots down: #st.set_y(0.95) f.subplots_adjust(top=0.92) # All done, save plot if output_file.lower().endswith(".png"): fmt = 'png' elif output_file.lower().endswith(".pdf"): fmt = 'pdf' else: print("ERROR: Unknown format!") sys.exit(-1) plt.savefig(output_file, format=fmt, transparent=False, dpi=300)
def plot_stations(parameter, filenames, station1, station2): """ This function plots two lists of psignals with Fourier Amplitude Spectra. station = a list of 3 psignals for three orientation. """ dtype = ['Displacement', 'Velocity', 'Acceleration'] orientation = ['N/S', 'E/W', 'Up/Down'] dt1 = station1[0].dt dt2 = station2[0].dt file1 = filenames[0] file2 = filenames[1] xtmin = parameter[0] xtmax = parameter[1] xfmin = parameter[2] xfmax = parameter[3] filter_flag = parameter[4] fmin = parameter[5] fmax = parameter[6] tmin = parameter[7] tmax = parameter[8] cut_flag = parameter[9] min_i1 = int(xtmin / dt1) max_i1 = int(xtmax / dt1) min_i2 = int(xtmin / dt2) max_i2 = int(xtmax / dt2) # calculating Response Spectra rsp1 = [] rsp2 = [] period = get_period(tmin, tmax) for i in range(0, 3): signal1 = station1[i] signal2 = station2[i] (acc_rsp1, acc_rsp2, vel_rsp1, vel_rsp2, dis_rsp1, dis_rsp2) = [], [], [], [], [], [] if cut_flag: acc1 = signal1.accel[min_i1:max_i1] acc2 = signal2.accel[min_i2:max_i2] else: acc1 = signal1.accel acc2 = signal2.accel for p in period: rsp = max_osc_response(acc1, dt1, 0.05, p, 0, 0) dis_rsp1.append(rsp[0]) vel_rsp1.append(rsp[1]) acc_rsp1.append(rsp[2]) rsp = max_osc_response(acc2, dt2, 0.05, p, 0, 0) dis_rsp2.append(rsp[0]) vel_rsp2.append(rsp[1]) acc_rsp2.append(rsp[2]) rsp1.append([dis_rsp1, vel_rsp1, acc_rsp1]) rsp2.append([dis_rsp2, vel_rsp2, acc_rsp2]) # from displacement to velocity to acceleration for i in range(0, 3): f, axarr = plt.subplots(nrows=3, ncols=3, figsize=(12, 9)) # iterative through psignals in each station for j in range(0, 3): title = dtype[i] + ' in ' + orientation[j] signal1 = station1[j] signal2 = station2[j] if ((not isinstance(signal1, seism_psignal)) or \ (not isinstance(signal2, seism_psignal))): print("[PLOT ERROR]: Invalid instance type: " "can only plot psignal objects.") return if signal1.data.shape[1] != 3 or signal2.data.shape[1] != 3: print("[PLOT ERROR]: psignal's data must be 3-column " "numpy array for displ, velo, accel.") return samples1 = signal1.samples samples2 = signal2.samples # psignal.data = [displ, velo, accel] data1 = signal1.data[:, i] data2 = signal2.data[:, i] # filtering data if filter_flag: data1 = s_filter(data1, dt1, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) data2 = s_filter(data2, dt2, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) # cutting signal by bounds c_data1 = data1[min_i1:max_i1] c_data2 = data2[min_i2:max_i2] t1 = np.arange(xtmin, xtmax, dt1) t2 = np.arange(xtmin, xtmax, dt2) points = get_points([samples1, samples2]) if not cut_flag: # if user chooses not to cut; use original data to calculate FAS and Response freq1, fas1 = FAS(data1, dt1, points, xfmin, xfmax, 3) freq2, fas2 = FAS(data2, dt2, points, xfmin, xfmax, 3) else: # use cutted signals to calculate FAS freq1, fas1 = FAS(c_data1, dt1, points, xfmin, xfmax, 3) freq2, fas2 = FAS(c_data2, dt2, points, xfmin, xfmax, 3) axarr[j][0] = plt.subplot2grid((3, 4), (j, 0), colspan=2, rowspan=1) axarr[j][0].set_title(title) axarr[j][0].plot(t1, c_data1, 'r', t2, c_data2, 'b') if j == 0: plt.legend([file1, file2]) plt.xlim(xtmin, xtmax) axarr[j][1] = plt.subplot2grid((3, 4), (j, 2), rowspan=1, colspan=1) axarr[j][1].set_title('Fourier Amplitude Spectra') axarr[j][1].plot(freq1, fas1, 'r', freq2, fas2, 'b') tmp_xfmin = 0 if xfmin < 0.5: tmp_xfmin = 0 else: tmp_xfmin = xfmin plt.xlim(tmp_xfmin, xfmax) axarr[j][2] = plt.subplot2grid((3, 4), (j, 3), rowspan=1, colspan=1) axarr[j][2].set_title("Response Spectra") axarr[j][2].set_xscale('log') axarr[j][2].plot(period, rsp1[j][i], 'r', period, rsp2[j][i], 'b') plt.xlim(tmin, tmax) f.tight_layout() plt.show()
def plot_signals(parameter, filenames, signal1, signal2): """ This function is to plot Signals with Fourier Amplitude Spectura. """ plt.close('all') file1 = filenames[0] file2 = filenames[1] xtmin = parameter[0] xtmax = parameter[1] xfmin = parameter[2] xfmax = parameter[3] filter_flag = parameter[4] fmin = parameter[5] fmax = parameter[6] tmin = parameter[7] tmax = parameter[8] cut_flag = parameter[9] title = 'Acceleration ONLY: ' samples1 = signal1.samples samples2 = signal2.samples data1 = signal1.data data2 = signal2.data dt1 = signal1.dt dt2 = signal2.dt # filtering data if filter_flag: data1 = s_filter(data1, dt1, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) data2 = s_filter(data2, dt2, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) # cutting signals by bounds min_i = int(xtmin / dt1) max_i = int(xtmax / dt1) c_data1 = data1[min_i:max_i] min_i = int(xtmin / dt2) max_i = int(xtmax / dt2) c_data2 = data2[min_i:max_i] t1 = np.arange(xtmin, xtmax, dt1) t2 = np.arange(xtmin, xtmax, dt2) points = get_points([samples1, samples2]) # setting period for Response period = get_period(tmin, tmax) if not cut_flag: # if user chooses not to cut; use origina/filted data for FAS and Response freq1, fas1 = FAS(data1, dt1, points, xfmin, xfmax, 3) freq2, fas2 = FAS(data2, dt2, points, xfmin, xfmax, 3) rsp1, rsp2 = cal_acc_response(period, [data1, data2], [dt1, dt2]) else: # else uses cutted data for FAS and Response freq1, fas1 = FAS(c_data1, dt1, points, xfmin, xfmax, 3) freq2, fas2 = FAS(c_data2, dt2, points, xfmin, xfmax, 3) rsp1, rsp2 = cal_acc_response(period, [c_data1, c_data2], [dt1, dt2]) f, axarr = plt.subplots(nrows=1, ncols=3, figsize=(12, 3)) axarr[0] = plt.subplot2grid((1, 4), (0, 0), colspan=2) axarr[0].set_title(title) axarr[0].plot(t1, c_data1, 'r', t2, c_data2, 'b') plt.legend([file1, file2]) plt.xlim(xtmin, xtmax) axarr[1] = plt.subplot2grid((1, 4), (0, 2), colspan=1) axarr[1].set_title('Fourier Amplitude Spectra') axarr[1].plot(freq1, fas1, 'r', freq2, fas2, 'b') tmp_xfmin = 0 if xfmin < 0.5: tmp_xfmin = 0 else: tmp_xfmin = xfmin plt.xlim(tmp_xfmin, xfmax) axarr[2] = plt.subplot2grid((1, 4), (0, 3)) axarr[2].set_title("Response Spectra") axarr[2].set_xscale('log') axarr[2].plot(period, rsp1, 'r', period, rsp2, 'b') plt.xlim(tmin, tmax) f.tight_layout() plt.show()
def simple_plot(parameter, filenames, stations, output_file, plot_title=None): """ plotting velocity for data and FAS only acceleration for Response """ all_styles = ['k', 'r', 'b', 'm', 'g', 'c', 'y', 'brown', 'gold', 'blueviolet', 'grey', 'pink'] orientation = ['N/S', 'E/W', 'Up/Down'] # Check number of input timeseries if len(stations) > len(all_styles): print("[ERROR]: Too many timeseries to plot!") sys.exit(-1) delta_ts = [station[0].dt for station in stations] files = [os.path.basename(filename) for filename in filenames] xtmin = parameter[0] xtmax = parameter[1] xfmin = parameter[2] xfmax = parameter[3] filter_flag = parameter[4] fmin = parameter[5] fmax = parameter[6] tmin = parameter[7] tmax = parameter[8] cut_flag = parameter[9] min_is = [int(xtmin/delta_t) for delta_t in delta_ts] max_is = [int(xtmax/delta_t) for delta_t in delta_ts] period = get_period(tmin, tmax) f, axarr = plt.subplots(nrows=3, ncols=3, figsize=(14, 9)) for i in range(0, 3): title = orientation[i] signals = [station[i] for station in stations] samples = [signal.samples for signal in signals] vels = [signal.velo for signal in signals] accs = [signal.accel for signal in signals] for sample, max_i, delta_t in zip(samples, max_is, delta_ts): if sample - 1 < max_i: print("[ERROR]: t_max has to be under %f" % ((sample - 1) * delta_t)) sys.exit(1) # filtering data if filter_flag: vels = [s_filter(vel, delta_t, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) for vel, delta_t in zip(vels, delta_ts)] accs = [s_filter(acc, delta_t, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) for acc, delta_t in zip(accs, delta_ts)] # cutting signal by bounds c_vels = [vel[min_i:max_i] for vel, min_i, max_i in zip(vels, min_is, max_is)] c_accs = [acc[min_i:max_i] for acc, min_i, max_i in zip(accs, min_is, max_is)] times = [np.arange(xtmin, xtmax, delta_t) for delta_t in delta_ts] points = get_points(samples) if cut_flag: freqs, fas_s = zip(*[FAS(c_vel, delta_t, points, xfmin, xfmax, 3) for c_vel, delta_t in zip(c_vels, delta_ts)]) rsps = cal_acc_response(period, c_accs, delta_ts) else: freqs, fas_s = zip(*[FAS(vel, delta_t, points, xfmin, xfmax, 3) for vel, delta_t in zip(vels, delta_ts)]) rsps = cal_acc_response(period, accs, delta_ts) axarr[i][0] = plt.subplot2grid((3, 4), (i, 0), colspan=2, rowspan=1) axarr[i][0].set_title(title) axarr[i][0].grid(True) styles = all_styles[0:len(times)] for timeseries, c_vel, style in zip(times, c_vels, styles): axarr[i][0].plot(timeseries, c_vel, style) if i == 0: plt.legend(files, prop={'size':8}) plt.xlim(xtmin, xtmax) axarr[i][1] = plt.subplot2grid((3, 4), (i, 2), rowspan=1, colspan=1) axarr[i][1].set_title('Fourier Amplitude Spectra') axarr[i][1].grid(True, which='both') axarr[i][1].set_xscale('log') axarr[i][1].set_yscale('log') for freq, fas, style in zip(freqs, fas_s, styles): axarr[i][1].plot(freq, fas, style) tmp_xfmin = 0 if xfmin < 0.5: tmp_xfmin = 0 else: tmp_xfmin = xfmin plt.xlim(tmp_xfmin, xfmax) axarr[i][2] = plt.subplot2grid((3, 4), (i, 3), rowspan=1, colspan=1) axarr[i][2].set_title("Response Spectra") axarr[i][2].set_xscale('log') axarr[i][2].grid(True) for rsp, style in zip(rsps, styles): axarr[i][2].plot(period, rsp, style) plt.xlim(tmin, tmax) # Make nice plots with tight_layout f.tight_layout() # Add overall title if provided if plot_title is not None: st = plt.suptitle(plot_title, fontsize=16) # shift subplots down: #st.set_y(0.95) f.subplots_adjust(top=0.92) # All done, save plot if output_file.lower().endswith(".png"): fmt = 'png' elif output_file.lower().endswith(".pdf"): fmt = 'pdf' else: print("ERROR: Unknown format!") sys.exit(-1) plt.savefig(output_file, format=fmt, transparent=False, dpi=300)
def plot_stations(parameter, filenames, station1, station2): """ This function plots two lists of psignals with Fourier Amplitude Spectra. station = a list of 3 psignals for three orientation. """ dtype = ['Displacement', 'Velocity', 'Acceleration'] orientation = ['N/S', 'E/W', 'Up/Down'] dt1 = station1[0].dt dt2 = station2[0].dt file1 = filenames[0] file2 = filenames[1] xtmin = parameter[0] xtmax = parameter[1] xfmin = parameter[2] xfmax = parameter[3] filter_flag = parameter[4] fmin = parameter[5] fmax = parameter[6] tmin = parameter[7] tmax = parameter[8] cut_flag = parameter[9] min_i1 = int(xtmin/dt1) max_i1 = int(xtmax/dt1) min_i2 = int(xtmin/dt2) max_i2 = int(xtmax/dt2) # calculating Response Spectra rsp1 = [] rsp2 = [] period = get_period(tmin, tmax) for i in range(0, 3): signal1 = station1[i] signal2 = station2[i] (acc_rsp1, acc_rsp2, vel_rsp1, vel_rsp2, dis_rsp1, dis_rsp2) = [], [], [], [], [], [] if cut_flag: acc1 = signal1.accel[min_i1:max_i1] acc2 = signal2.accel[min_i2:max_i2] else: acc1 = signal1.accel acc2 = signal2.accel for p in period: rsp = max_osc_response(acc1, dt1, 0.05, p, 0, 0) dis_rsp1.append(rsp[0]) vel_rsp1.append(rsp[1]) acc_rsp1.append(rsp[2]) rsp = max_osc_response(acc2, dt2, 0.05, p, 0, 0) dis_rsp2.append(rsp[0]) vel_rsp2.append(rsp[1]) acc_rsp2.append(rsp[2]) rsp1.append([dis_rsp1, vel_rsp1, acc_rsp1]) rsp2.append([dis_rsp2, vel_rsp2, acc_rsp2]) # from displacement to velocity to acceleration for i in range(0, 3): f, axarr = plt.subplots(nrows=3, ncols=3, figsize=(12, 9)) # iterative through psignals in each station for j in range(0, 3): title = dtype[i] + ' in ' + orientation[j] signal1 = station1[j] signal2 = station2[j] if ((not isinstance(signal1, seism_psignal)) or \ (not isinstance(signal2, seism_psignal))): print("[PLOT ERROR]: Invalid instance type: " "can only plot psignal objects.") return if signal1.data.shape[1] != 3 or signal2.data.shape[1] != 3: print("[PLOT ERROR]: psignal's data must be 3-column " "numpy array for displ, velo, accel.") return samples1 = signal1.samples samples2 = signal2.samples # psignal.data = [displ, velo, accel] data1 = signal1.data[:, i] data2 = signal2.data[:, i] # filtering data if filter_flag: data1 = s_filter(data1, dt1, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) data2 = s_filter(data2, dt2, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) # cutting signal by bounds c_data1 = data1[min_i1:max_i1] c_data2 = data2[min_i2:max_i2] t1 = np.arange(xtmin, xtmax, dt1) t2 = np.arange(xtmin, xtmax, dt2) points = get_points([samples1, samples2]) if not cut_flag: # if user chooses not to cut; use original data to calculate FAS and Response freq1, fas1 = FAS(data1, dt1, points, xfmin, xfmax, 3) freq2, fas2 = FAS(data2, dt2, points, xfmin, xfmax, 3) else: # use cutted signals to calculate FAS freq1, fas1 = FAS(c_data1, dt1, points, xfmin, xfmax, 3) freq2, fas2 = FAS(c_data2, dt2, points, xfmin, xfmax, 3) axarr[j][0] = plt.subplot2grid((3, 4), (j, 0), colspan=2, rowspan=1) axarr[j][0].set_title(title) axarr[j][0].plot(t1, c_data1, 'r', t2, c_data2, 'b') if j == 0: plt.legend([file1, file2]) plt.xlim(xtmin, xtmax) axarr[j][1] = plt.subplot2grid((3, 4), (j, 2), rowspan=1, colspan=1) axarr[j][1].set_title('Fourier Amplitude Spectra') axarr[j][1].plot(freq1, fas1, 'r', freq2, fas2, 'b') tmp_xfmin = 0 if xfmin < 0.5: tmp_xfmin = 0 else: tmp_xfmin = xfmin plt.xlim(tmp_xfmin, xfmax) axarr[j][2] = plt.subplot2grid((3, 4), (j, 3), rowspan=1, colspan=1) axarr[j][2].set_title("Response Spectra") axarr[j][2].set_xscale('log') axarr[j][2].plot(period, rsp1[j][i], 'r', period, rsp2[j][i], 'b') plt.xlim(tmin, tmax) f.tight_layout() plt.show()
def plot_signals(parameter, filenames, signal1, signal2): """ This function is to plot Signals with Fourier Amplitude Spectura. """ plt.close('all') file1 = filenames[0] file2 = filenames[1] xtmin = parameter[0] xtmax = parameter[1] xfmin = parameter[2] xfmax = parameter[3] filter_flag = parameter[4] fmin = parameter[5] fmax = parameter[6] tmin = parameter[7] tmax = parameter[8] cut_flag = parameter[9] title = 'Acceleration ONLY: ' samples1 = signal1.samples samples2 = signal2.samples data1 = signal1.data data2 = signal2.data dt1 = signal1.dt dt2 = signal2.dt # filtering data if filter_flag: data1 = s_filter(data1, dt1, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) data2 = s_filter(data2, dt2, type='bandpass', family='ellip', fmin=fmin, fmax=fmax, N=3, rp=0.1, rs=100) # cutting signals by bounds min_i = int(xtmin/dt1) max_i = int(xtmax/dt1) c_data1 = data1[min_i:max_i] min_i = int(xtmin/dt2) max_i = int(xtmax/dt2) c_data2 = data2[min_i:max_i] t1 = np.arange(xtmin, xtmax, dt1) t2 = np.arange(xtmin, xtmax, dt2) points = get_points([samples1, samples2]) # setting period for Response period = get_period(tmin, tmax) if not cut_flag: # if user chooses not to cut; use origina/filted data for FAS and Response freq1, fas1 = FAS(data1, dt1, points, xfmin, xfmax, 3) freq2, fas2 = FAS(data2, dt2, points, xfmin, xfmax, 3) rsp1, rsp2 = cal_acc_response(period, [data1, data2], [dt1, dt2]) else: # else uses cutted data for FAS and Response freq1, fas1 = FAS(c_data1, dt1, points, xfmin, xfmax, 3) freq2, fas2 = FAS(c_data2, dt2, points, xfmin, xfmax, 3) rsp1, rsp2 = cal_acc_response(period, [c_data1, c_data2], [dt1, dt2]) f, axarr = plt.subplots(nrows=1, ncols=3, figsize=(12, 3)) axarr[0] = plt.subplot2grid((1, 4), (0, 0), colspan=2) axarr[0].set_title(title) axarr[0].plot(t1, c_data1, 'r', t2, c_data2, 'b') plt.legend([file1, file2]) plt.xlim(xtmin, xtmax) axarr[1] = plt.subplot2grid((1, 4), (0, 2), colspan=1) axarr[1].set_title('Fourier Amplitude Spectra') axarr[1].plot(freq1, fas1, 'r', freq2, fas2, 'b') tmp_xfmin = 0 if xfmin < 0.5: tmp_xfmin = 0 else: tmp_xfmin = xfmin plt.xlim(tmp_xfmin, xfmax) axarr[2] = plt.subplot2grid((1, 4), (0, 3)) axarr[2].set_title("Response Spectra") axarr[2].set_xscale('log') axarr[2].plot(period, rsp1, 'r', period, rsp2, 'b') plt.xlim(tmin, tmax) f.tight_layout() plt.show()
def process(signal): """ The function takes a signal, use its's current data to get acceleration, velocity, and displacement. Then return a psignal. """ if not isinstance(signal, seism_signal): print("[ERROR]: instance error; process signal objects only.") return # Correction of base lines was commented out here to avoid problems with synchronization # correct_baseline(signal) acc = np.array([], float) vel = np.array([], float) dis = np.array([], float) dt = signal.dt if signal.type == 'a': acc = signal.data acc = s_filter(acc, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) vel = integrate(acc, dt) vel = s_filter(vel, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) dis = integrate(vel, dt) dis = s_filter(dis, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) elif signal.type == 'v': vel = signal.data vel = s_filter(vel, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) acc = derivative(vel, dt) dis = integrate(vel, dt) dis = s_filter(dis, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) elif signal.type == 'd': dis = signal.data dis = s_filter(dis, signal.dt, type='highpass', family='butter', fmin=0.05, N=5) vel = derivative(dis, dt) acc = derivative(vel, dt) else: pass psignal = seism_psignal(signal.samples, signal.dt, np.c_[dis, vel, acc], 'c', acc, vel, dis) return psignal