def plot_clust(h5_main, labels, mean_resp, x_pts, data_avg=None, tidx_off=0): try: _labels = np.array(labels.value).T[0] except: _labels = np.array(labels) labels_unique = np.unique(_labels) parms_dict = hdf_utils.get_params(h5_main) clust_tfp = [] # color defaults are blue, orange, green, red, purple... colors = [ '#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf' ] fig, ax = plt.subplots(nrows=1, figsize=(12, 6)) ax.set_xlabel('Distance to Nearest Boundary (um)') ax.set_ylabel('tfp (us)') for i in labels_unique: labels_tfp = [] if not data_avg.any(): for sig in h5_main[_labels == labels_unique[i], :]: pix = pixel.Pixel(sig, parms_dict) pix.inst_freq = sig pix.fit_freq_product() labels_tfp.append(pix.tfp) labels_tfp = np.array(labels_tfp) else: labels_tfp = data_avg[_labels == labels_unique[i]] ax.plot(x_pts[_labels == labels_unique[i]] * 1e6, labels_tfp * 1e6, c=colors[i], linestyle='None', marker='.') parms_dict['trigger'] += tidx_off / parms_dict['sampling_rate'] pix = pixel.Pixel(mean_resp[i], parms_dict) pix.inst_freq = mean_resp[i] pix.fit_freq_product() clust_tfp.append(pix.tfp) parms_dict['trigger'] -= tidx_off / parms_dict['sampling_rate'] ax.plot(np.mean(x_pts[_labels == labels_unique[i]] * 1e6), pix.tfp * 1e6, marker='o', markerfacecolor=colors[i], markersize=8, markeredgecolor='k') print('tfp of clusters: ', clust_tfp) return ax, fig
def trefm_workup(self, dec): p = pixel.Pixel(self.xv[:, 0].reshape(-1, 1)[::dec], self.workup_params) p.remove_dc() p.average() p.remove_dc() # p.check_drive_freq() p.apply_window() p.fir_filter() p.hilbert_transform() p.calculate_phase(correct_slope=True) p.calculate_inst_freq() self.p = p # Extract a copy of the bandpass filter used by the FFtrEFM workup # Code from from ffta.pixel.fir_filter nyq_rate = 0.5 * p.sampling_rate bw_half = p.filter_bandwidth / 2 freq_low = (p.drive_freq - bw_half) / nyq_rate freq_high = (p.drive_freq + bw_half) / nyq_rate band = [freq_low, freq_high] self.trefm_fir = signal.firwin(p.n_taps, band, pass_zero=False, window='parzen') self.phase = self.p.phase self.freq = self.p.inst_freq self.tout = self.t[::dec] + (self.workup_params['n_taps'] - 1) / 2 * self.sim_params['dt'] * dec
def analyze(self): """ Analyzes the line with the given method. Returns ------- tfp : (n_pixels,) array_like Time from trigger to first-peak, in seconds. shift : (n_pixels,) array_like Frequency shift from trigger to first-peak, in Hz. inst_freq : (n_points, n_pixels) array_like Instantenous frequencies of the line. """ # Split the signal array into pixels. pixel_signals = np.split(self.signal_array, self.n_pixels, axis=1) # Iterate over pixels and return tFP and shift arrays. for i, pixel_signal in enumerate(pixel_signals): p = pixel.Pixel(pixel_signal, self.params) (self.tfp[i], self.shift[i], self.inst_freq[:, i]) = p.analyze() return (self.tfp, self.shift, self.inst_freq)
def analyze(self): """ Analyzes the line with the given method. :returns: tuple (tfp, shift, inst_freq) WHERE array_like tfp is time from trigger to first-peak, in seconds in format (n_pixels,) array_like shift is frequency shift from trigger to first-peak, in Hz in format (n_pixels,) array_like inst_freq is instantaneous frequencies of the line in format (n_pixels,) """ # Split the signal array into pixels. pixel_signals = np.split(self.signal_array, self.n_pixels, axis=1) # Iterate over pixels and return tFP and shift arrays. for i, pixel_signal in enumerate(pixel_signals): p = pixel.Pixel(pixel_signal, self.params) (self.tfp[i], self.shift[i], self.inst_freq[:, i]) = p.analyze() return (self.tfp, self.shift, self.inst_freq)
def test_filter(hdf_file, freq_filts, parameters={}, pixelnum=[0, 0], noise_tolerance=5e-7, show_plots=True, check_filter=True): """ Applies FFT Filter to the file at a specific line and displays the result :param hdf_file: hdf_file to work on, e.g. hdf.file['/FF-raw'] if that's a Dataset if ndarray, uses passed or default parameters Use ndarray.flatten() to ensure correct dimensions :type hdf_file: h5Py file or Nx1 NumPy array (preferred is NumPy array) :param freq_filts: Contains the filters to apply to the test signal :type freq_filts: list of FrequencyFilter class objects :param parameters: Contains parameters in FF-raw file for constructing filters. Automatic if a Dataset/File Must contain num_pts and samp_rate to be functional :type parameters: dict, optional :param pixelnum: For extracting a specific pixel to do FFT Filtering on :type pixelnum: int, optional :param noise_tolerance: Amount of noise below which signal is set to 0 :type noise_tolerance: float 0 to 1 :param show_plots: Turns on FFT plots from Pycroscopy :type show_plots : bool, optional :returns: tuple (filt_line, freq_filts, fig_filt, axes_filt) WHERE numpy.ndarray filt_line is filtered signal of hdf_file list freq_filts is the filter parameters to be passed to SignalFilter matplotlib controls fig_filt, axes_filt - Only functional if show_plots is on """ reshape = False ftype = str(type(hdf_file)) if ('h5py' in ftype) or ('Dataset' in ftype): # hdf file parameters = get_utils.get_params(hdf_file) hdf_file = get_utils.get_pixel(hdf_file, [pixelnum[0], pixelnum[1]], array_form=True, transpose=False) hdf_file = hdf_file.flatten() if len(hdf_file.shape) == 2: reshape = True hdf_file = hdf_file.flatten() sh = hdf_file.shape # Test filter on a single line: filt_line, fig_filt, axes_filt = px.processing.gmode_utils.test_filter( hdf_file, frequency_filters=freq_filts, noise_threshold=noise_tolerance, show_plots=show_plots) # If need to reshape if reshape: filt_line = np.reshape(filt_line, sh) # Test filter out in Pixel if check_filter: plt.figure() plt.plot(hdf_file, 'b') plt.plot(filt_line, 'k') h5_px_filt = pixel.Pixel(filt_line, parameters) h5_px_filt.clear_filter_flags() h5_px_filt.analyze() h5_px_filt.plot(newplot=True) h5_px_raw = pixel.Pixel(hdf_file, parameters) h5_px_raw.analyze() h5_px_raw.plot(newplot=True) # h5_px_raw_unfilt = pixel.Pixel(hdf_file, parameters) # h5_px_raw_unfilt.clear_filter_flags() # h5_px_raw_unfilt.analyze() # h5_px_raw_unfilt.plot(newplot=False,c1='y', c2='c') return filt_line, freq_filts, fig_filt, axes_filt
from ffta import simulation, pixel from ffta.utils import load from matplotlib import pyplot as plt path = 'sim_parameters.cfg' can_params, force_params, sim_params = load.simulation_configuration(path) c = simulation.Cantilever(can_params, force_params, sim_params) c.simulate(trigger_phase=0) parameters = { 'bandpass_filter': 1.0, 'drive_freq': 277261, 'filter_bandwidth': 10000.0, 'n_taps': 499, 'roi': 0.0003, 'sampling_rate': 1e7, 'total_time': 0.0008192, 'trigger': 0.0004096, 'window': 'blackman', 'wavelet_analysis': 0 } p = pixel.Pixel(c.Z, parameters) p.analyze() plt.figure() plt.plot(p.inst_freq[p.tidx:(p.tidx + 2000)])