def createRickerPeak(self, sizey, sizex): ricker_x = ricker(self.size_patch[1], self.output_sigma_factor * sizey / self.padding) ricker_y = ricker(self.size_patch[0], self.output_sigma_factor * sizex / self.padding) ry = np.ones([1, len(ricker_y)]) * ricker_y rx = np.ones([1, len(ricker_x)]) * ricker_x ricker_peak = np.transpose(ry) * rx return fftd(ricker_peak)
def createRickerMask(self): ricker_x = ricker(self.size_patch[1], self.sigma * self.size_patch[1] / self.padding) ricker_y = ricker(self.size_patch[0], self.sigma * self.size_patch[0] / self.padding) ry = np.ones([1, len(ricker_y)]) * ricker_y rx = np.ones([1, len(ricker_x)]) * ricker_x self.targetMask = np.transpose(ry) * rx self.targetMask = self.targetMask.astype(np.float32)
def translated_ricker_wavelet(times, scale, t): wavelet = sig.ricker(len(times), scale) dt = times[0] - times[1] zero_index = times.size / 2 center_index = np.where(np.abs(times - t) < 1.0e-9)[0][0] shift = center_index - zero_index return np.roll(wavelet, shift)
def convolve_with_wavelet(self): from scipy.signal import ricker, convolve, hilbert self.gwin = ricker(500, 21) self.seis = convolve(self.seis, self.gwin, 'same') return self
def test_draw_wavelet_ricker(self): width = .5 points = int(10*width) w = sig.ricker(points, width) # plt.plot(np.linspace(0., 1., points) * width, w, 'r-') plt.plot(w, 'r-') plt.show()
def create_seismogram(rays, observe, dt, tracelen): seismogram = Seismogram() times = [dt * i for i in range(tracelen)] for j, rec in enumerate(observe.receivers): offset = rec.x # rays_ = [r for r in np.nditer(rays) if abs(r.x_finish - offset) <= 0.2] rays_ = [ rr for r in rays.values() for rr in r if abs(rr.x_finish - offset) <= 0.001 ] trace_i = np.zeros(len(times)) for ray in rays_: # ampl_curve = [r for r in reflections if float(r.boundary_z) == float(ray.reflection_z)][0] # r_coeff = ampl_curve.get_amplitude_by_offset(offset) r_coeff = ray.calculate_dynamic_factor() reflection_index = int(ray.time / dt) if reflection_index < len(trace_i): trace_i[reflection_index] = r_coeff.real signal = ricker(50, 7) signal /= max(abs(signal)) trace_values = np.convolve(trace_i, signal)[0:len(times)].real seismogram.add_trace(Trace(trace_values, dt, offset)) return seismogram
def create_seismic_trace(self, reflection=None, data_all=None): """Returns an array that contains the seismic trace based on the input data. **Arguments**: reflection: array, list of reflection coefficient values **Outputs**: data: array of seismic trace values """ if data_all == None and reflection != None: points = len(reflection) width = points / 100 else: density = [] velocity = [] for i in range(len(data_all)): for j in range(len(data_all[i])): density.append(data_all[i][j][4][1][0]) velocity.append(data_all[i][j][4][3][0]) impedance = np.array(velocity) * np.array(density) reflection = [0] for i in range(1, len(impedance)): reflection.append((impedance[i] - impedance[i - 1]) / (impedance[i] + impedance[i - 1])) points = len(reflection) width = points / 100 wavelet = signal.ricker(points, width) data = signal.convolve(reflection, wavelet, mode="same") # return data
def f(): window_type = display['window_type'] window_size = int(display['size']) p = float(display['param']) window = np.zeros(window_size) if window_type == 'square': window = np.ones(window_size) elif window_type == 'exponential': window = np.exp(np.arange(window_size) * p) elif window_type == 'hanning': window = np.hanning(window_size) elif window_type == 'blackman': window = np.blackman(window_size) elif window_type == 'ricker': window = ricker(window_size, p) elif window_type == 'gaussian': window = gaussian(window_size, p) elif window_type == 'barthann': window = barthann(window_size) elif window_type == 'flattop': window = flattop(window_size) elif window_type == 'cosine': window = cosine(window_size) elif window_type == 'triangle': window = triang(window_size) return window
def compute_whole_sgy_file(): seismic_sgy = "" with segyio.open(seismic_sgy, 'r') as f: trace = f.trace[500] widths = numpy.arange(1, 11, 0.1) Wf = [] for w in widths: w = ricker(50, w) F = fft.fftshift(fft.fftfreq(256)) W = fft.fftshift(fft.fft(numpy.real(w), 256)) Wf.append(numpy.amax(numpy.abs(W))) p_trace = numpy.zeros(250) p_trace[0:len(trace)] = trace # cwt_tf = cwt(trace, ricker, numpy.arange(1, 11, 0.1)) volume = segyio.tools.cube(seismic_sgy) cwt_cube = numpy.zeros((*volume.shape, len(Wf))) shape = cwt_cube.shape for i in range(0, shape[0]): for x in range(0, shape[1]): trace = numpy.squeeze(volume[i, x, :]) c = cwt(trace, ricker, widths) cwt_cube[i, x, :, :] = numpy.fliplr(c).T return cwt_cube, volume
def draw_wavelet_ricker(): width = 100. points = int(10*width) w = sig.ricker(points, width) plt.plot(w, 'k-', ) plt.gca().yaxis.set_ticks_position('none') plt.show()
def run(self, timeserieData, longitude=None, latitude=None): lista1 = [] points = len(timeserieData) width = 40 ricker = signal.ricker(points, width) return ricker
def hat(y,a,b): peaky = [] vec2 = signal.ricker(20, 4) for j in range(a,b): yii = y[j:j+20] yiii = yii * vec2 peaky.append(sum(yiii)) return peaky
def calculator1d(a): nnan = ~np.isnan(a) a = a[nnan] if self.wavelet == 'ricker': wavelet = signal.ricker(min(10 * self.width, len(a)), self.width) else: raise NotImplementedError() return signal.convolve(a, wavelet, mode='same')
def test_draw_strengths(self): ecg = self.ecg_mouse() y = ecg.getLowFreq() scale = 0.0015 * ecg.getDataFrequency() wt = WP.get_WT(y, scale) wt = clc.normalize(wt, 'median_abs') mm = WP.get_mm_array(wt) mm = WP.filter_mm_array(mm, wt) WP.draw_MM_strengths(mm, y, wt, wavelet=100*sig.ricker(scale*10, scale), diffs=True)
def sample(): x = np.arange(100) points = 100 a = 4 vec2 = signal.ricker(points,a) print vec2 print vec2.shape plt.plot(x,vec2,'r') return
def get_scalogram(data, **kwargs): if 'wavelet' in kwargs: wavelet = kwargs['wavelet'] else: wavelet = signal.ricker() if 'levels' in kwargs: levels = kwargs['levels'] else: levels = np.arange(1, 11) return signal.cwt(data, wavelet, levels)
def wavelet_ricker_series(): n_lines = 15 n_points = 100 max_offset = 800 hue = np.random.choice(['blue', 'purple', 'red']) luminosity = None rand_color = randomcolor.RandomColor() background_color = np.array( rand_color.generate( hue=hue, luminosity=luminosity, format_='Array_rgb')) / 256. plt.rcParams["figure.facecolor"] = background_color[0] fig, axes = plt.subplots(n_lines, sharex=True, frameon=True) offset = 1 rand_color = randomcolor.RandomColor() value = 1.0 for ax in axes: for _ in range(5): x_offset = np.random.uniform(0, max_offset) a = np.random.uniform(1, 5) n_points /= 2. color_rgb = np.array( rand_color.generate(hue=hue, luminosity=luminosity, format_='Array_rgb')) / 256. color_rgb = color_rgb[0] color_hsv = color.rgb2hsv(color_rgb) color_hsv[2] *= 0.4 + 0.6 * x_offset / max_offset color_rgb = color.hsv2rgb(color_hsv) ax.axis('off') n_points = 500 sample_frac = 0.1 mode = np.random.uniform(0, n_points) x = np.random.triangular(0, mode, n_points, size=int(n_points * sample_frac)) x = [int(i) for i in x] y = signal.ricker(int(n_points), 25) y = y[x] ax.scatter(x_offset + np.array(x), 90 * y, color=color_rgb, s=10.0, alpha=0.99) # ax.set_aspect(2) offset += 3 # ax.set_xlim(0, 500) fig.set_figheight(5) # fig.set_figwidth(4) plt.show()
def synth(**kwargs): """return ricker wavelet synthetic data""" # defaults if 'pol' not in kwargs: kwargs['pol'] = 0. if 'delta' not in kwargs: kwargs['delta'] = 1. if 'split' not in kwargs: kwargs['split'] = [] if 'noise' not in kwargs: kwargs['noise'] = 0.001 if 'nsamps' not in kwargs: kwargs['nsamps'] = 1001 if 'width' not in kwargs: kwargs['width'] = 32 if 'noisewidth' not in kwargs: kwargs['noisewidth'] = kwargs['width'] / 4 # initiate data with clean ricker wavelet nsamps = int(kwargs['nsamps']) x = signal.ricker(nsamps, kwargs['width']) y = np.zeros(nsamps) # rotate to polarisation # negative because we are doing the active rotation of data, whereas # core.rotate does the passive transormation of the co-ordinate system x, y = core.rotate(x, y, -kwargs['pol']) if isinstance(kwargs['split'], tuple): fast, lag = kwargs['split'] # add any splitting -- lag samples must be even slag = core.time2samps(lag, kwargs['delta'], mode='even') x, y = core.split(x, y, fast, slag) elif isinstance(split, list): for parms in kwargs['split']: fast, lag = parms # add any splitting -- lag samples must be even slag = core.time2samps(lag, kwargs['delta'], mode='even') x, y = core.split(x, y, fast, slag) # add noise - do this last to avoid splitting the noise x = x + core.noise(x.size, kwargs['noise'], int(kwargs['noisewidth'])) y = y + core.noise(x.size, kwargs['noise'], int(kwargs['noisewidth'])) z = core.noise(x.size, kwargs['noise'], int(kwargs['noisewidth'])) if 'ray' in kwargs: if not isinstance(kwargs['ray'], tuple): raise Exception('ray must be a tuple (azi,inc)') if len(kwargs['ray']) != 2: raise Exception('ray must be length 2 (azi,inc)') az, inc = math.radians(kwargs['ray'][0]), math.radians( kwargs['ray'][1]) sinc, cinc = math.sin(inc), math.cos(inc) saz, caz = math.sin(az), math.cos(az) rot = np.array([[-cinc * caz, saz, sinc * caz], [-cinc * saz, -caz, sinc * saz], [sinc, 0, cinc]]) xyz = np.dot(rot, np.vstack((x, y, z))) x, y, z = xyz[0], xyz[1], xyz[2] return x, y, z
def test_fft_wavelet_ricker(self): width = 1. points = int(10*width) w = sig.ricker(points, width) fft, f = get_fft(w, 1000) max_fq = np.argmax(fft) logging.info('Peak frequency = %f', f[max_fq]) __, p = plt.subplots(2) p[0].plot(w, 'r-') p[1].plot(f, fft, 'g-') plt.show()
def _sync_impulse_transform(self, data: np.ndarray) -> np.ndarray: len_sync = 80 alpha = np.random.uniform(1.0, 7.0) sync_impulse = ricker(len_sync, alpha) trace_with_sync = np.zeros(self.height_model - len_sync + 1) trace_with_sync[np.random.randint(0, np.ceil(self.height_model * 0.5))] = \ np.random.uniform(-0.6, -0.3) if np.random.random() > 0.5 else np.random.uniform(0.3, 0.6) trace_with_sync = (np.convolve(trace_with_sync, sync_impulse))[:, np.newaxis] return data + trace_with_sync
def generate_trace(reflection_event_times=(20, 1400, 2400), reflection_event_amplitude=(6.0, -12.0, 8.0), nmo_velocity=(1.6, 2.0, 2.4), wavelet_lenght=50, wavelet_width=5, **kwargs): """Generates a seismic trace with Ricker impulse using reflectivity parameters and trace's headers. Parameters ---------- reflection_event_time : 1d np.ndarray, defaults to (20, 1400, 2400) Zero-offset times of reflection events measured in ms. reflection_event_amplitude : 1d np.ndarray, defaults to (6.0, -12.0, 8.0) Amplitudes of reflection events. nmo_velocity : 1d np.ndarray, defaults to (1.6, 2.0, 2.4) NMO velocities for the reflection events, m/ms. wavelet_lenght : int, defaults to 50 Overall lenght of the vector with Ricker wavelet. Equivalent of `points` parameter of `scipy.signal.ricker`. wavelet_width : int, defaults to 5 Width parameter of the wavelet itself. Equivalent of `a` parameter of `scipy.signal.ricker`. kwargs : dict Dict with trace header values. This function uses TRACE_SAMPLE_COUNT, TRACE_SAMPLE_INTERVAL and offset. Returns ------- trace : 1d-ndarray Generated seismic trace. """ n_samples = kwargs.get('TRACE_SAMPLE_COUNT') sample_rate = kwargs.get('TRACE_SAMPLE_INTERVAL') offset = kwargs.get('offset') sample_rate = sample_rate / 1000 # cast sample rate (dt) from microsec to millisec times = np.array(reflection_event_times) / sample_rate # cast to samples reflections = np.array(reflection_event_amplitude) velocities = np.array(nmo_velocity) equal_lengths = (len(times) == len(reflections) == len(velocities)) if not equal_lengths: raise ValueError("'reflection_event_times', 'reflection_event_amplitude' and 'nmo_velocity' should have equal lengths") # Inversed normal moveout shifted_times = ((times**2 + offset**2 / (velocities * sample_rate)**2)**0.5).astype(int) ref_series = np.zeros(max(n_samples, max(shifted_times)) + 1) # Tweak reflection event amplitudes to make Survey and Gathers statistics differ reflections = np.random.normal(reflections, np.abs(reflections) / 5) ref_series[shifted_times] = reflections ref_series[min(shifted_times):] += np.random.normal(1, 0.5, size=len(ref_series)-min(shifted_times)) # Generate "seismic" signal by convolving reflectivity series with the "impulse" wavelet trace = np.convolve(ref_series, signal.ricker(wavelet_lenght, wavelet_width), mode='same')[:n_samples] trace = trace.astype(np.float32) return trace
def make_random_signal(nsamp): """Make a single trace with random reflectivity A random reflectivity trace is convolved with a zero phase ricker wavelet Args: nsamp: the number of samples in the output trace Returns: A 1D array with the signal """ ref = np.random.rand(nsamp)*2-1 wav = sig.ricker(80,5) filtered = np.convolve(ref, wav,'same') return filtered
def ricker_center_freq(dt): # Get the frequency spectrum of the Ricker wavelet # with width 1 in units of samples wavelet = sig.ricker(1e3, 1) # arbitrary num. of points, not too big or small frequency_spectrum = np.fft.fft(wavelet)[:int(round(wavelet.size / 2))] spectrum_magnitude = np.sqrt( np.real(frequency_spectrum)**2 + np.imag(frequency_spectrum)**2) frequencies = np.fft.fftfreq(frequency_spectrum.size*2, d=1)\ [:int(round(wavelet.size/2))] # Empirically, fc/fp = 1.186; where fp is the peak FFT frequency and # fc is the desired center frequency. # - Divide by the sample width to get units of Hz return 1.186 * frequencies[np.argmax(spectrum_magnitude)] / dt
def clip_to_linear(data, threshold=1.0, kernel_width=0.1, ends_clipped=0.05): ''' Takes the second derivative of the data with a ricker wavelet. Data is clipped to the linear portion (2nd derivative ~ 0) Parameters ---------- data : numpy.ndarray x and y data. threshold : float, optional Acceptable value of the second derivative to be called linear. kernel_width : float, optional Kernel width set to this percentage of the data length ends_clipped : float, optional Percentage of data to clip off at the ends. End points have residual effects from the convolution. Returns ------- data_clipped : numpy.ndarray Linear portion of the data set returned. ''' from scipy.signal import ricker y = data[1, :] x = data[0, :] num_pts = len(y) kernel = ricker(num_pts, num_pts * kernel_width) sec_deriv = np.convolve(y, kernel, mode="same") # Ends go back to being ~ linear, so clip them off if ends_clipped > 0.0: clipped_pts = int(num_pts * ends_clipped) sec_deriv = sec_deriv[clipped_pts: num_pts - clipped_pts] y = y[clipped_pts: num_pts - clipped_pts] x = x[clipped_pts: num_pts - clipped_pts] linear_pts = np.abs(sec_deriv) < threshold data_clipped = np.empty((2, len(y[linear_pts]))) data_clipped[:, :] = x[linear_pts], y[linear_pts] return data_clipped
def _mwi(self): """ 功能:利用ricker小波(Mexican hat)对滤波后的信号进行滑动波积分(mwi),并 保存滑动波积分后信号的平方 hat的宽度等于qrs波的宽度 在滑动积分后的信号中找到所有的局部波峰点 """ b = signal.ricker(self.qrs_width, 4) self.sig_i = signal.filtfilt(b, [1], self.sig_f, axis=0)**2 # 保存mwi增益,因为采用的是零相漂滤波器(前后双向滤波器)所以增益*2 # 并保存从原始信号到滑动积分信号的全部增益 self.mwi_gain = get_filter_gain( b, [1], np.mean([self.fc_low, self.fc_high]), self.fs) * 2 self.transform_gain = self.filter_gain * self.mwi_gain self.peak_inds_i = find_local_peaks(self.sig_i, radius=self.qrs_radius) self.n_peaks_i = len(self.peak_inds_i)
def make_delayed_signal_pair(nsamp, delay): """Make a pair of identical traces with specified delay A random reflectivity trace is convolved with a zero phase ricker wavelet and the created trace and a delayed version are returned Args: nsamp: the number of samples in the output trace delay: the number of samples to delay the second trace Returns: Two 1D arrays with the undelayed and delayed signal """ ref = np.random.rand(nsamp+abs(delay))*2-1 wav = sig.ricker(80,5) filtered = np.convolve(ref, wav,'same') if delay < 0 : return filtered[0:nsamp], filtered[-delay:nsamp-delay] else: return filtered[delay:nsamp+delay], filtered[0:nsamp]
def peak_finder_pro(measurement): E0 = measurement.energy_cal[0] slope = measurement.energy_cal[1] energy_axis = measurement.channel energy_axis = energy_axis.astype(float) energy_axis[:] = [E0 + slope * x for x in range(len(measurement.channel))] """energy_spectra is the spectra that will be loaded and analyzed""" fwhm_list = [] for i in energy_axis: fwhm = 0.05 * energy_axis[i] ** 0.5 fwhm_list = fwhm_list.append(fwhm) counts = measurement.data peaks_found = [] start = 0 end = start + 50 for energy in energy_axis: E_start = energy_axis[start] E_end = energy_axis[end] energy_range = range(E_start, E_end) count_total = 0 for i in energy_range: count_total = count_total + counts[energy_range[i]] avg_range = count_total/len(energy_range) avg_ends = (counts[start] + counts[end]) / 2 threshold = 1.1 * avg_ends if avg_range > threshold: energy_average = start + 25 avg_fwhm = fwhm_list[energy_average] width_parameter = avg_fwhm * 3 wavelet = signal.ricker(width_parameter, avg_fwhm) counts_range = range(counts[E_start], counts[E_end]) wave_transform = signal.cwt(counts_range, wavelet, width_parameter) peak_finder = signal.find_peaks_cwt(wave_transform, counts_range) peaks_found.append(peak_finder) next_range = peak_finder + 0.5 * avg_fwhm start = next_range else: start += 1 return peaks_found
def _mwi(self): """ Apply moving wave integration with a ricker (Mexican hat) wavelet onto the filtered signal, and save the square of the integrated signal. The width of the hat is equal to the qrs width Also find all local peaks in the mwi signal. """ b = signal.ricker(self.qrs_width, 4) self.sig_i = signal.filtfilt(b, [1], self.sig_f, axis=0) ** 2 # Save the mwi gain (x2 due to double filtering) and the total gain # from raw to mwi self.mwi_gain = get_filter_gain(b, [1], np.mean([self.fc_low, self.fc_high]), self.fs) * 2 self.transform_gain = self.filter_gain * self.mwi_gain self.peak_inds_i = find_local_peaks(self.sig_i, radius=self.qrs_radius) self.n_peaks_i = len(self.peak_inds_i)
def createCubedGaussianWhiteNoiseConvolvedWithRickerWavelet( num_signal, num_sample, num_point_ricker_wavelet, width_parameter_ricker_wavelet): gaussian_white_noise = np.random.randn(num_signal, num_sample) cubed_noise_by_signal_sample = np.power(gaussian_white_noise, 3) cubed_noise_by_signal_sample[:, num_sample * 0 // 10:num_sample * 1 // 10] = 0 #cubed_noise_by_signal_sample *= np.random.poisson(100./num_sample, (num_signal, num_sample)) #cubed_noise_by_signal_sample = np.random.poisson(1, (num_signal, num_sample)) ricker_wavelet = signal.ricker(num_point_ricker_wavelet, width_parameter_ricker_wavelet) output_sample_length = num_sample output_by_signal_sample = np.zeros((num_signal, output_sample_length)) for idx_signal in range(num_signal): output_by_signal_sample[idx_signal, :] = np.convolve( cubed_noise_by_signal_sample[idx_signal, :], ricker_wavelet, 'same') #output_by_signal_sample[idx_signal,17] = 1e-15 return output_by_signal_sample
def make_synthetic(self, ricker_width=5, ricker_points=50): """ Generate and store 2d or 3d synthetic seismic. Synthetic seismic generation relies on generated velocity and density models. Hence, can be run only after `generate_velocities`, `generate_velocity_model` and `generate_density_model` methods. Parameters ---------- ricker_width : float Width of the ricker-wave - `a`-parameter of `scipy.signal.ricker`. ricker_points : int Number of points in the ricker-wave - `points`-parameter of `scipy.signal.ricker`. """ wavelet = ricker(ricker_points, ricker_width) # Colvolve reflection coefficients with wavelet to obtain synthetic # NOTE: convolution is performed only along depth-axis, hence additional axes in kernel kernel = wavelet.reshape((1, ) * (self.velocity_model.ndim - 1) + (-1, )) self.synthetic = convolve(self.reflectivity_coefficients, kernel, mode='same') return self
def synth(**kwargs): """return ricker wavelet synthetic data""" # defaults if 'pol' not in kwargs: kwargs['pol'] = 0. if 'delta' not in kwargs: kwargs['delta'] = 1. if 'split' not in kwargs: kwargs['split'] = [] if 'noise' not in kwargs: kwargs['noise'] = 0.001 if 'nsamps' not in kwargs: kwargs['nsamps'] = 1001 if 'width' not in kwargs: kwargs['width'] = 32 if 'noisewidth' not in kwargs: kwargs['noisewidth'] = kwargs['width']/4 # initiate data with clean ricker wavelet nsamps = int(kwargs['nsamps']) x = signal.ricker(nsamps, kwargs['width']) y = np.zeros(nsamps) # rotate to polarisation # negative because we are doing the active rotation of data, whereas # core.rotate does the passive transormation of the co-ordinate system x,y = rotate(x, y, -kwargs['pol']) if isinstance(kwargs['split'], tuple): fast, lag = kwargs['split'] # add any splitting -- lag samples must be even slag = time2samps(lag, kwargs['delta'], mode='even') x,y = split(x, y, fast, slag) elif isinstance(kwargs['split'], list): for parms in kwargs['split']: fast, lag = parms # add any splitting -- lag samples must be even slag = time2samps(lag, kwargs['delta'], mode='even') x,y = split(x, y, fast, slag) # add noise - do this last to avoid splitting the noise x = x + noise(x.size, kwargs['noise'], int(kwargs['noisewidth'])) y = y + noise(x.size, kwargs['noise'], int(kwargs['noisewidth'])) return x,y
def createCubedGaussianWhiteNoiseConvolvedWithRickerWavelet( num_signal, num_sample, num_point_ricker_wavelet, width_parameter_ricker_wavelet): gaussian_white_noise = np.random.randn(num_signal,num_sample) cubed_noise_by_signal_sample = np.power(gaussian_white_noise, 3) cubed_noise_by_signal_sample[:, num_sample * 0 // 10: num_sample*1//10] = 0 #cubed_noise_by_signal_sample *= np.random.poisson(100./num_sample, (num_signal, num_sample)) #cubed_noise_by_signal_sample = np.random.poisson(1, (num_signal, num_sample)) ricker_wavelet = signal.ricker(num_point_ricker_wavelet, width_parameter_ricker_wavelet) output_sample_length = num_sample output_by_signal_sample = np.zeros((num_signal,output_sample_length)) for idx_signal in range(num_signal): output_by_signal_sample[idx_signal,:] = np.convolve( cubed_noise_by_signal_sample[idx_signal,:], ricker_wavelet, 'same') #output_by_signal_sample[idx_signal,17] = 1e-15 return output_by_signal_sample
def _mwi(self): """ Apply moving wave integration (mwi) with a ricker (Mexican hat) wavelet onto the filtered signal, and save the square of the integrated signal. The width of the hat is equal to the qrs width After integration, find all local peaks in the mwi signal. """ wavelet_filter = signal.ricker(self.qrs_width, 4) self.sig_i = signal.filtfilt(wavelet_filter, [1], self.sig_f, axis=0) ** 2 # Save the mwi gain (x2 due to double filtering) and the total # gain from raw to mwi self.mwi_gain = get_filter_gain(wavelet_filter, [1], np.mean([self.fc_low, self.fc_high]), self.fs) * 2 self.transform_gain = self.filter_gain * self.mwi_gain self.peak_inds_i = find_local_peaks(self.sig_i, radius=self.qrs_radius) self.n_peaks_i = len(self.peak_inds_i)
def get_wavelet_decomposition(self, widths=range(1, 14, 3), window=50, **_): """ Cached wavelet transform calculation followed by dimensionaluty reduction via PCA. Parameters ---------- widths : list of numbers Widths of wavelets to calculate decomposition for. window : int Width of amplitudes slice to calculate wavelet transform on. """ amplitudes = self.load_attribute('amplitudes', window=window) result_shape = *amplitudes.shape[:2], len(widths) result = np.empty(result_shape, dtype=np.float32) for idx, width in enumerate(widths): wavelet = ricker(window, width).reshape(1, 1, -1) result[:, :, idx] = convolve(amplitudes, wavelet, mode='constant')[:, :, window // 2] return result
def gen_spike_templates(numSpks, muWidth=90, sigmaWidth=10, muFac=0.1, sigmaFac=0.02, muVert=200, sigmaVert=40): """ numSpks: # of Spikes, muWidth: mean width of spike, sigmaWidth: stdev of spike width, muFac: mean width fator, sigmaFac: stdev of spike factor, muVert: mean vertical scaling factor, sigmaVert: stdev of vertical scaling factor """ from scipy import signal waveforms = [] for i in range(numSpks): width = sigmaWidth * np.random.randn() + muWidth factor = (np.abs(sigmaFac * np.random.randn() + muFac)) * width polarity = np.random.choice([1, -1], 1)[0] vertical = sigmaVert * np.random.randn() + muVert # Add a linear equations on top | super impose multiple rickers waveforms.append(polarity * signal.ricker(width, factor) * vertical) return np.asarray(waveforms, dtype=object)
def cpu_version(self, num_samps, a): return signal.ricker(num_samps, a)
def showSplash(a,dir_path,widfac,highfac,fontfac): ''' Creates the splash screen shown when starting GPRPy GUI for common-offset profiles. ''' try: filename=os.path.join(dir_path,'exampledata','SnS','ComOffs','XLINE00.DT1') snakeGPR = gp.gprpyProfile(filename) maxpoint=100; x=snakeGPR.twtt[0:maxpoint] y=snakeGPR.data[0:maxpoint,10] except: rick = signal.ricker(150, 4.0) x = np.linspace(0,85,100) y = rick[50:150]*25000 # Snake body lw=7#5 a.plot(x,y,'k',linewidth=lw*widfac,solid_capstyle='round') # Snake head Path = mpath.Path xshift=0 headval=2500*widfac/highfac path_data = [ (Path.MOVETO, [xshift,headval]), (Path.CURVE3, [-20+xshift,0]), (Path.LINETO, [xshift,-headval]), (Path.CURVE3, [10+xshift,0]), (Path.LINETO, [xshift,headval]), (Path.CLOSEPOLY, [xshift,headval])] codes, verts = zip(*path_data) path = mpath.Path(verts, codes) patch = mpatches.PathPatch(path) patch.set_facecolor('black') a.add_patch(patch) # Eyes eye1 = mpatches.Ellipse([-2,1000], 3, 1000) eye2 = mpatches.Ellipse([-2,-1000], 3, 1000) eye1.set_facecolor('white') eye2.set_facecolor('white') a.add_patch(eye1) a.add_patch(eye2) # Tongue x, y = np.array([[-10, -13, -15], [0.0, 0.0, 600]]) line1 = mlines.Line2D(x, y, lw=2*widfac, color='black') x, y = np.array([[-10, -13, -15], [0.0, 0.0, -600]]) line2 = mlines.Line2D(x, y, lw=2*widfac, color='black') a.add_line(line1) a.add_line(line2) # Axis setup a.set_xlim([-25,90]) a.set_ylim([-28000,12000]) a.axis('off') # Text font = {'family': 'DejaVu Sans', 'color': 'black', 'weight': 'bold', 'style': 'italic', 'size': 60*fontfac #'size': 45.6 } # a.text(35,-10000,'GPRPy',fontdict=font) a.text(50,-10000,'GPRPy',fontdict=font) fontver = {'family': 'DejaVu Sans', 'color': 'black', 'style': 'italic', 'size': 13.5*fontfac #'size': 45.6 } a.text(50,-12000,'Version 1.0.9',fontdict=fontver) # add UA logo filename1=os.path.join(dir_path,'toolbox','splashdat', 'A_Square_Logo_4c.png') ua = im.imread(filename1) #yanchor = -24500 #yheight = 10000*0.9 yanchor = -24000 yheight = 10000*0.8 xanchor = -20 figsize = a.figure.get_size_inches() figratio = figsize[0]/figsize[1] ratio = a.get_data_ratio()*figratio xwidth = yheight/ratio a.imshow(ua, aspect='auto', extent=(xanchor, xanchor+xwidth, yanchor, yanchor+yheight), interpolation='spline36') # # add UA words # filename1=os.path.join(dir_path,'toolbox','splashdat', # 'UA-StackedNameplate.png') # ua = im.imread(filename1) # #yanchor = -24500 # #yheight = 10000*0.9 # yanchor = -24000 # yheight = 10000*0.4 # xanchor = -10 # figsize = a.figure.get_size_inches() # figratio = figsize[0]/figsize[1] # ratio = a.get_data_ratio()*figratio # xwidth = 5*yheight/ratio # a.imshow(ua, aspect='auto', extent=(xanchor, xanchor+xwidth, # yanchor, yanchor+yheight), # interpolation='spline36') # Add NSF logo filename2=os.path.join(dir_path,'toolbox','splashdat', 'NSF_4-Color_bitmap_Logo.png') nsf = im.imread(filename2) yanchor = -25000 yheight = 10000 xanchor = -5 figsize = a.figure.get_size_inches() figratio = figsize[0]/figsize[1] ratio = a.get_data_ratio()*figratio xwidth = yheight/ratio a.imshow(nsf, aspect='auto', extent=(xanchor, xanchor+xwidth, yanchor, yanchor+yheight), interpolation='spline36') font2 = {'family': 'DejaVu Sans', 'color': 'black', 'size': 13.5*fontfac} #'size': 10.26} a.text(-5,-27000,'EAR-1550732',fontdict=font2) # Add name/email font3 = {'family': 'DejaVu Sans', 'color': 'gray', 'size': 13.5*fontfac} #'size' : 10.26} a.text(70,-22000,'Alain Plattner',fontdict=font3) a.text(59,-24000,'*****@*****.**',fontdict=font3)
def _learn_init_params(self, n_calib_beats=8): """ Find a number of consecutive beats and use them to initialize: - recent qrs amplitude - recent noise amplitude - recent rr interval - qrs detection threshold The learning works as follows: - Find all local maxima (largest sample within `qrs_radius` samples) of the filtered signal. - Inspect the local maxima until `n_calib_beats` beats are found: - Calculate the cross-correlation between a ricker wavelet of length `qrs_width`, and the filtered signal segment centered around the local maximum. - If the cross-correlation exceeds 0.6, classify it as a beat. - Use the beats to initialize the previously described parameters. - If the system fails to find enough beats, the default parameters will be used instead. See the docstring of `XQRS._set_default_init_params` for detauls. Parameters ---------- n_calib_beats : int, optional Number of calibration beats to detect for learning """ if self.verbose: print('Learning initial signal parameters...') last_qrs_ind = -self.rr_max qrs_inds = [] qrs_amps = [] noise_amps = [] ricker_wavelet = signal.ricker(self.qrs_radius * 2, 4).reshape(-1,1) # Find the local peaks of the signal. peak_inds_f = find_local_peaks(self.sig_f, self.qrs_radius) peak_inds_f_r = np.where(peak_inds_f > self.qrs_width)[0] peak_inds_f_l = np.where(peak_inds_f <= self.sig_len - self.qrs_width)[0] # Skip if no peaks in range if (not peak_inds_f.size or not peak_inds_f_r.size or not peak_inds_f_l.size): if self.verbose: print('Failed to find %d beats during learning.' % n_calib_beats) self._set_default_init_params() return # Go through the peaks and find qrs peaks and noise peaks. # only inspect peaks with at least qrs_radius around either side for peak_num in range(peak_inds_f_r[0], peak_inds_f_l[0]): i = peak_inds_f[peak_num] # Calculate cross-correlation between the filtered signal # segment and a ricker wavelet # Question: should the signal be squared? Case for inverse qrs # complexes sig_segment = normalize((self.sig_f[i - self.qrs_radius: i + self.qrs_radius]).reshape(-1, 1), axis=0) xcorr = np.correlate(sig_segment[:, 0], ricker_wavelet[:,0]) # Classify as qrs if xcorr is large enough if xcorr > 0.6 and i-last_qrs_ind > self.rr_min: last_qrs_ind = i qrs_inds.append(i) qrs_amps.append(self.sig_i[i]) else: noise_amps.append(self.sig_i[i]) if len(qrs_inds) == n_calib_beats: break # Found enough calibration beats to initialize parameters if len(qrs_inds) == n_calib_beats: if self.verbose: print('Found %d beats during learning.' % n_calib_beats + ' Initializing using learned parameters') # QRS amplitude is most important. qrs_amp = np.mean(qrs_amps) # Set noise amplitude if found if noise_amps: noise_amp = np.mean(noise_amps) else: # Set default of 1/10 of qrs amplitude noise_amp = qrs_amp / 10 # Get rr intervals of consecutive beats, if any. rr_intervals = np.diff(qrs_inds) rr_intervals = rr_intervals[rr_intervals < self.rr_max] if rr_intervals.any(): rr_recent = np.mean(rr_intervals) else: rr_recent = self.rr_init # If an early qrs was detected, set last_qrs_ind so that it can be # picked up. last_qrs_ind = min(0, qrs_inds[0] - self.rr_min - 1) self._set_init_params(qrs_amp_recent=qrs_amp, noise_amp_recent=noise_amp, rr_recent=rr_recent, last_qrs_ind=last_qrs_ind) # Failed to find enough calibration beats. Use default values. else: if self.verbose: print('Failed to find %d beats during learning.' % n_calib_beats) self._set_default_init_params()
# just for fun: The spike at 0 is caused by # a nonzero mean of the random phase signal Q_nonzero_mean = Q + 1. Q_nonzero_mean[0] = 0.0 plt.plot(np.fft.irfft(Q_nonzero_mean)) plt.plot(np.fft.irfft(Q)) plt.xlabel("Time sample nr.") plt.legend(["Random phase signal with nonzero mean", "Random phase signal with zero mean"]) plt.show() # here is a simple time domain signal "Green fct" # to use for convolution s = np.zeros(2001) s[1500: 1600] = ricker(100, a=20.) plt.title("Input \"Greens function\"") plt.plot(s) plt.show() # if we perform convolution in freq. domain: S = np.fft.rfft(s) plt.plot(np.fft.irfft(Q * S, n=2001)) # and convolution of the same signals, # but with an algorithm that does it right, # starting from time domain signals: q = np.fft.irfft(Q, n=2001) plt.plot(fftconvolve(s, q, mode="full"), '--') # then we see that the signals are identical from the start
twti = np.cumsum(ds/vmodel, axis=0)*2 # calculate irregular sampled twt times twt = np.arange(0, np.max(twti)+0.10, dt) # new twt times re-sampled to 4 ms dnx = int(nx/disc) twtvmodel = np.zeros((len(twt), dnx)) # velocity model in time and discretized for i in xrange(0, nx, disc): # set the time re-sample velocity model twtvmodel[:, int(i/disc)] = np.interp(twt, twti[:, i], vmodel[:, i]) zimp = np.ones(twtvmodel.shape)*1000.*twtvmodel # create impedance z = rho*v rc = (zimp[1:]-zimp[:-1])/(zimp[1:]+zimp[:-1]) # calculate reflection coefs fig = mpl.figure(figsize=(11, 7)) # plottings mpl.subplots_adjust(right=0.98, left=0.11, hspace=0.4, top=0.93, bottom=0.13) mpl.subplot2grid((3, 4), (0, 0), colspan=4) # plot rc model mpl.title("Zero-offset section reflection coefficients", fontsize=13, family='sans-serif', weight='bold') rcs = utils.matrix2stream(rc.transpose(), header={'delta': dt}) mpl.seismic_wiggle(rcs, normalize=True, scale=1.5, ranges=[0, nx]) # rc model wavelet = signal.ricker(255, 1/(2*f*dt)) # create wavelet samples = signal.filtfilt(wavelet, np.ones(len(wavelet)), rc, axis=0, padlen=len(twt)-2) # convolve rc*wavelet mpl.ylabel('twt (s)') mpl.subplot2grid((3, 4), (1, 0), colspan=4) # plot zero-offset traces traces = utils.matrix2stream(samples.transpose(), header={'delta': dt}) mpl.seismic_image(traces, cmap=mpl.pyplot.cm.jet, aspect='auto', ranges=[0, nx]) mpl.seismic_wiggle(traces, ranges=[0, nx], normalize=True) mpl.ylabel('twt (s)') mpl.title("Zero-offset section amplitude", fontsize=13, family='sans-serif', weight='bold') ax = mpl.subplot2grid((3, 4), (2, 0), colspan=4) # plot vmodel mpl.imshow(vmodel, extent=[0, nx, nz*ds, 0], cmap=mpl.pyplot.cm.bwr, aspect='auto', origin='upper') ax.autoscale(False) mpl.ylabel('depth (m)')
def _learn_init_params(self, n_calib_beats=8): """ Find a number of consecutive beats and use them to initialize: - recent qrs amplitude - recent noise amplitude - recent rr interval - qrs detection threshold The learning works as follows: - Find all local maxima (largest sample within `qrs_radius` samples) of the filtered signal. - Inspect the local maxima until `n_calib_beats` beats are found: - Calculate the cross-correlation between a ricker wavelet of length `qrs_width`, and the filtered signal segment centered around the local maximum. - If the cross-correlation exceeds 0.6, classify it as a beat. - Use the beats to initialize the previously described parameters. - If the system fails to find enough beats, the default parameters will be used instead. See the docstring of `XQRS._set_default_init_params` for detauls. Parameters ---------- n_calib_beats : int, optional Number of calibration beats to detect for learning """ if self.verbose: print('Learning initial signal parameters...') last_qrs_ind = -self.rr_max qrs_inds = [] qrs_amps = [] noise_amps = [] ricker_wavelet = signal.ricker(self.qrs_radius * 2, 4).reshape(-1,1) # Find the local peaks of the signal. peak_inds_f = find_local_peaks(self.sig_f, self.qrs_radius) # Peak numbers at least qrs_width away from signal boundaries peak_nums_r = np.where(peak_inds_f > self.qrs_width)[0] peak_nums_l = np.where(peak_inds_f <= self.sig_len - self.qrs_width)[0] # Skip if no peaks in range if (not peak_inds_f.size or not peak_nums_r.size or not peak_nums_l.size): if self.verbose: print('Failed to find %d beats during learning.' % n_calib_beats) self._set_default_init_params() return # Go through the peaks and find qrs peaks and noise peaks. # only inspect peaks with at least qrs_radius around either side for peak_num in range(peak_nums_r[0], peak_nums_l[-1]): i = peak_inds_f[peak_num] # Calculate cross-correlation between the filtered signal # segment and a ricker wavelet # Question: should the signal be squared? Case for inverse qrs # complexes sig_segment = normalize((self.sig_f[i - self.qrs_radius: i + self.qrs_radius]).reshape(-1, 1), axis=0) xcorr = np.correlate(sig_segment[:, 0], ricker_wavelet[:,0]) # Classify as qrs if xcorr is large enough if xcorr > 0.6 and i-last_qrs_ind > self.rr_min: last_qrs_ind = i qrs_inds.append(i) qrs_amps.append(self.sig_i[i]) else: noise_amps.append(self.sig_i[i]) if len(qrs_inds) == n_calib_beats: break # Found enough calibration beats to initialize parameters if len(qrs_inds) == n_calib_beats: if self.verbose: print('Found %d beats during learning.' % n_calib_beats + ' Initializing using learned parameters') # QRS amplitude is most important. qrs_amp = np.mean(qrs_amps) # Set noise amplitude if found if noise_amps: noise_amp = np.mean(noise_amps) else: # Set default of 1/10 of qrs amplitude noise_amp = qrs_amp / 10 # Get rr intervals of consecutive beats, if any. rr_intervals = np.diff(qrs_inds) rr_intervals = rr_intervals[rr_intervals < self.rr_max] if rr_intervals.any(): rr_recent = np.mean(rr_intervals) else: rr_recent = self.rr_init # If an early qrs was detected, set last_qrs_ind so that it can be # picked up. last_qrs_ind = min(0, qrs_inds[0] - self.rr_min - 1) self._set_init_params(qrs_amp_recent=qrs_amp, noise_amp_recent=noise_amp, rr_recent=rr_recent, last_qrs_ind=last_qrs_ind) self.learned_init_params = True # Failed to find enough calibration beats. Use default values. else: if self.verbose: print('Failed to find %d beats during learning.' % n_calib_beats) self._set_default_init_params()
max_index_i = i max_index_j = j print "max_index_j (position, x-axis): ", max_index_j, " position: ",t[max_index_j] print "max_index_i (scale, y-axis): ", max_index_i, " scale: ",widths[max_index_i] # Plot the signal # Also plot the wavelet with the overall best match with the signal a = widths[max_index_i] tt_half = t[max_index_j]/2.0 ttmax = 1.5*t[max_index_j] ttmin = 0.5*t[max_index_j] tstep = (ttmax-ttmin)/(20.0*a) twavelet = np.arange(ttmin,ttmax,tstep) Ft = signal.ricker(len(twavelet),a) f1 = plt.figure() ax1 = f1.add_subplot(111) ax1.plot(t,sig,'ro-') ax1.plot(twavelet,Ft,'bo--') ax1.set_xlabel("Time") ax1.set_ylabel("Intensity") f1.show() # Plot C(a,b) # setup scaling on colormap # # It's important to get the extent correct because that will # determine whether the scaling is correct (ie, aligned with the center of
from scipy import signal import matplotlib.pyplot as plt points = 100 a = 4.0 vec2 = signal.ricker(points, a) print(len(vec2)) # 100 plt.plot(vec2) plt.show()
for j in range(0, n): c[i] += x[i - j] * y[j] return c # + # Cell 2 - creating signals npts = 4096 # number of samples (initial: 4096) dt = 0.025 # sample rate (initial: 0.025) t = np.linspace(0, npts * dt, npts) # time axis for plotting box = np.zeros(npts) # boxcar function for i in range(400, 600): box[i] = 1. rk = signal.ricker( npts, 20.0) # Rieker wavelet, 2nd number = width of wavelet (intial: 20.0) cosine = np.cos(t / 2) # cosine wavelet print('number of samples of input signals:', npts) # plot plt.rcParams['figure.figsize'] = 15, 3 xmax = 104 # max. value for x-axis to zoom into (initial: 104) plt.subplot(131) plt.plot(t, box, 'b') plt.ylim(0, 1.2) plt.xlim(0, xmax) plt.xlabel('time [s]') plt.title('Boxcar') plt.ylabel('amplitude') plt.subplot(132)
def mexh(pts, scale): """Compute a 2d Mexican Hat wavelet 'kernel'""" mexican_hat_1d = ricker(pts, scale) mexican_hat_2d = np.outer(mexican_hat_1d, mexican_hat_1d) return mexican_hat_2d