def test_signal_sampling_nonint4(self): FT = np.zeros((4, 4, 4)) FT[3, 3, 3] = 1 def sampled(xcord, ycord, tcord): return np.cos(2 * np.pi / 4 * (xcord + ycord + tcord)) + np.cos( np.pi / 4 * ycord ) xcord_range = np.arange(0, 4, 1) ycord_range = np.arange(0, 4, 1) tcord_range = np.arange(0, 4, 1) TD_samples = np.zeros((4, 4, 4)) for x in xcord_range: for y in ycord_range: for t in tcord_range: TD_samples[x, y, t] = sampled(x, y, t) opt = {"time_domain_samples": TD_samples} signal = MultiDimPeriodicSignal(opt) time_sig = signal.get_time_signal([1, 2]) print("BLA") print(time_sig.sample(0.8)) print(signal.sample([1, 2, 0.8])) assert ( signal.sample([2, 3, 1]) <= signal.sample([2, 3, 0.5]) <= signal.sample([2, 3, 0]) )
def test_time_signal_object_creation(self): TD = np.random.randint(0, 20, (4, 5, 5)) / 10.0 opt = {"time_domain_samples": TD} signal = MultiDimPeriodicSignal(opt) time_signal = signal.get_time_signal([2, 3]) # import pudb # pudb.set_trace() estimated_value = time_signal.sample(0.1) target_value = signal.sample([2, 3, 0.1]) assert normed_difference(estimated_value, target_value) < 1e-6
def test_signal_sampling_int1_2(self): opt = {"time_domain_samples": np.random.random((3, 3, 3))} signal = MultiDimPeriodicSignal(opt) xcord, tcord = np.random.randint(0, 3), np.random.randint(0, 3) assert ( normed_difference( signal.sample([xcord, 0, tcord]), opt["time_domain_samples"][xcord, 0, tcord], ) < 1e-6 )
def test_signal_sampling_nonint3(self): FT = np.zeros((3, 3, 3)) FT[1, 1, 1] = 1 def sampled(xcord, ycord, tcord): return (1 / 3 ** 3) * np.cos(2 * np.pi / 3 * (xcord + ycord + tcord)) opt = {"freq_domain_samples": FT} signal = MultiDimPeriodicSignal(opt) assert normed_difference(signal.sample([1, 2, 1]), sampled(1, 2, 1)) < 1e-6 assert normed_difference(signal.sample([1, 1, 0.1]), sampled(1, 1, 0.1)) < 1e-6 assert normed_difference(signal.sample([0, 2.1, 1]), sampled(0, 2.1, 1)) < 1e-6 assert ( normed_difference(signal.sample([1.5, 2.1, 1]), sampled(1.5, 2.1, 1)) < 1e-6 ) time_sig = signal.get_time_signal([1.5, 2.1]) assert ( normed_difference(np.real(time_sig.sample(1)), sampled(1.5, 2.1, 1)) < 1e-6 )
def test_signal_sampling_nonint1_2(self): opt = {"time_domain_samples": np.random.random((2, 4, 1))} signal = MultiDimPeriodicSignal(opt) xcord, ycord = np.random.randint(0, 2), np.random.randint(0, 2) assert ( normed_difference( signal.sample([xcord, ycord, 0.33]), opt["time_domain_samples"][xcord, ycord, 0], ) < 1e-6 )
def test_sampling(self): discontinuities = [1, 2] values = [1] signal = Signal.piecewiseConstantSignal(discontinuities, values) samples = signal.sample([0, 0.1, 1.2, 1.5, 1.7, 2.3, 3.5]) assert samples[0] == 0 assert samples[1] == 0 assert samples[2] == 1 assert samples[3] == 1 assert samples[4] == 1 assert samples[5] == 0 assert samples[6] == 0
def test_signal_generation_2(self): period = 1 signal = Signal.periodicBandlimitedSignal(period, 3, [1, -1, 3 - 1j]) t = np.arange(0, 1, 1e-3) samples = signal.sample(t) target = ( 1 - 2 * np.cos(2 * np.pi * t) + 2 * 3 * np.cos(4 * np.pi * t) + 2 * np.sin(4 * np.pi * t) ) assert np.linalg.norm(samples - target) < 1e-6
def test_sampling2(self): discontinuities = [1, 2, 5, 6] values = [1, 4, -2] signal = Signal.piecewiseConstantSignal(discontinuities, values) samples = signal.sample([0, 0.1, 1.2, 1.5, 1.7, 2.3, 3.5, 5.6, 6.5]) assert samples[0] == 0 assert samples[1] == 0 assert samples[2] == 1 assert samples[3] == 1 assert samples[4] == 1 assert samples[5] == 4 assert samples[6] == 4 assert samples[7] == -2 assert samples[8] == 0
def test_signal_sampling(self): opt = {"time_domain_samples": np.random.random((7, 5, 7))} signal = MultiDimPeriodicSignal(opt) coordinates = [0, 0, 0] assert ( normed_difference( signal.sample(coordinates), opt["time_domain_samples"][ coordinates[0], coordinates[1], coordinates[2] ], ) < 1e-6 ) coordinates = [2, 3, 1] assert ( normed_difference( signal.sample(coordinates), opt["time_domain_samples"][ coordinates[0], coordinates[1], coordinates[2] ], ) < 1e-6 )
def test_sampling(self): omega = np.pi discontinuities = [1, 2, 5, 6] values = [1, 4, -2] t = np.arange(0, 10, 0.1) signal = Signal.piecewiseConstantSignal(discontinuities, values) samples = signal.sample(t) filtered_signal = signal.low_pass_filter(omega) samples_filtered = filtered_signal.sample(t) sampled_sinc = Helpers.sinc(t - 5, omega) samples_discrete_filtered = scipy.signal.convolve(samples, sampled_sinc) * ( t[1] - t[0] ) offset = int(len(sampled_sinc) / 2) assert ( np.abs(samples_filtered[40] - samples_discrete_filtered[offset + 40]) < 1e-2 )
def test_signal_integration(self): opt = {"time_domain_samples": np.random.random((6, 5, 6))} TD = np.random.randint(0, 20, (5, 3, 6)) / 10 # TD = np.ones((4,4,4)) # TD[1,1,1] = 0 opt = {"time_domain_samples": TD} signal = MultiDimPeriodicSignal(opt) x, y, target_time = [2.8, 1.2, 4.5] precise_integral = signal.get_precise_integral([x, y, target_time]) BLsig_precise_integral = signal.get_time_signal([x, y]).get_precise_integral( 0, target_time ) delta_t = 1e-3 estimated_integral = 0 for t in np.arange(0, target_time, delta_t): estimated_integral += delta_t * signal.sample([x, y, t]) print("integral = " + str(precise_integral)) print(estimated_integral) assert normed_difference(precise_integral, estimated_integral) < 1e-3 assert normed_difference(precise_integral, BLsig_precise_integral) < 1e-3
def test_signal_sampling(self): period = 3 signal = Signal.periodicBandlimitedSignal(period, 2, [1, 2]) time = np.arange(0, 6, 0.5) samples = signal.sample(time) assert len(samples) == len(time)