def __get_streaked_spectra(self, streakspeed): if self.is_low_res == True: return None else: from cupy.fft import fft def fs_in_au(t): return 41.3414 * t # from fs to a.u. # def eV_in_au(e): return 0.271106*np.sqrt(e) # from eV to a.u. # in V/m; shape of Vectorpotential determines: 232000 V/m = 1 meV/fs max streakspeed E0 = 232000 * streakspeed ff1 = cp.flip( fft(self.__temp * cp.exp(-1j * fs_in_au(self.__tAxis) * (1 / (2) * (self.p0 * E0 * p_times_A_vals_up + 1 * E0**2 * A_square_vals_up))))) ff2 = cp.flip( fft(self.__temp * cp.exp(-1j * fs_in_au(self.__tAxis) * (1 / (2) * (self.p0 * E0 * p_times_A_vals_down + 1 * E0**2 * A_square_vals_down))))) spectrum1 = cp.square(cp.abs(ff1)) spectrum2 = cp.square(cp.abs(ff2)) # ff1=ff1/(cp.sum(cp.square(cp.abs(ff1)))) # ff1=ff2/(cp.sum(cp.square(cp.abs(ff2)))) return spectrum1, spectrum2
def test_fft_error_on_wrong_plan(self, dtype): # This test ensures the context manager plan is picked up from cupyx.scipy.fftpack import get_fft_plan from cupy.fft import fft a = testing.shaped_random(self.shape, cupy, dtype) bad_shape = tuple(5 * i for i in self.shape) b = testing.shaped_random(bad_shape, cupy, dtype) plan_wrong = get_fft_plan(b) assert isinstance(plan_wrong, cupy.cuda.cufft.Plan1d) with pytest.raises(ValueError) as ex, plan_wrong: fft(a, n=self.n, norm=self.norm) # targeting a particular error assert 'Target array size does not match the plan.' in str(ex.value)
def ideal_lp(samples, left_freq, right_freq, fs, need_fft=True): if hasattr(samples, 'device'): import cupy as np from cupy.fft import fftfreq else: import numpy as np from scipy.fft import fftfreq if need_fft: if hasattr(samples, 'device'): from cupy.fft import fft, ifft else: from scipy.fft import fft, ifft samples = fft(samples) freq_vector = fftfreq(len(np.atleast_2d(samples)[0]), 1 / fs) mask1 = freq_vector <= left_freq mask2 = freq_vector > right_freq for row in samples: row[mask1] = 0 row[mask2] = 0 if need_fft: samples = ifft(samples, axis=-1) return samples
result_folder.mkdir(parents=True, exist_ok=True) tq = tqdm(list(base_path.glob('p*_c*_s*')), 'Videos: ') for folder in tq: base_dir = split(folder)[1] if base_dir not in videos: continue tq.write(f'Reading {base_dir}') video_folder = result_folder / base_dir video_folder.mkdir(exist_ok=True) file = folder / "Color.avi" proc = LibrasVideoSegmentation(file.__str__()) variation = proc.calc_variations('EQUALIZED_SUM') np.save(video_folder / 'variation', variation) variation = cp.asarray(variation) hz = 1 / proc.fps yfft_eq_sum = fft.fft(variation) xfft_eq_sum = fft.fftfreq(variation.size, hz)[:variation.size // 2] sum_energy_eq_sum = cp.cumsum( 2.0 / variation.size * cp.abs(yfft_eq_sum[0:variation.size // 2])) eq_sum_limiar_pc_energy = sum_energy_eq_sum.max() * ENERGY_LIMIT frequency = float( xfft_eq_sum[sum_energy_eq_sum <= eq_sum_limiar_pc_energy].max()) filter_coef = firwin(NUM_TAPS, frequency, fs=2 / hz) result = lfilter(filter_coef, 1, cp.asnumpy(variation)) np.save(video_folder / 'filtered', result) result = cp.asarray(result) subs = cp.diff(result) subs2 = cp.diff(subs) critical_points = (cp.sign(subs[1:]) != cp.sign( subs[:-1])).astype(bool) maxes = critical_points & (subs2 < 0).astype(bool)