def test_whitesignal_high_dt(Simulator, high, dt, seed, plt): t = 1. rms = 0.5 d = 500 process = WhiteSignal(t, high, rms=rms) with nengo.Network() as model: u = nengo.Node(process, size_out=d) up = nengo.Probe(u) with Simulator(model, seed=seed, dt=dt) as sim: sim.run(t) values = sim.data[up] freq, val_psd = psd(values, dt=dt) trange = sim.trange() plt.subplot(2, 1, 1) plt.title("First two D of white noise process, high=%d Hz" % high) plt.plot(trange, values[:, :2]) plt.xlim(right=trange[-1]) plt.subplot(2, 1, 2) plt.title("Power spectrum") plt.plot(freq, val_psd, drawstyle='steps') plt.xlim(0, high * 2.0) assert np.allclose(np.std(values, axis=1), rms, rtol=0.15) assert np.all(val_psd[npext.rfftfreq(len(trange), dt) > high] < rms * 0.5)
def make_step(self, size_in, size_out, dt, rng): assert size_in == 0 d = size_out n_coefficients = int(np.ceil(self.period / dt / 2.)) shape = (d, n_coefficients + 1) sigma = self.rms * np.sqrt(0.5) coefficients = 1j * rng.normal(0., sigma, size=shape) coefficients += rng.normal(0., sigma, size=shape) coefficients[:, 0] = 0. coefficients[:, -1].imag = 0. if self.high is not None: set_to_zero = npext.rfftfreq(2 * n_coefficients, d=dt) > self.high coefficients[:, set_to_zero] = 0. power_correction = np.sqrt(1. - np.sum(set_to_zero, dtype=float) / n_coefficients) if power_correction > 0.: coefficients /= power_correction coefficients *= np.sqrt(2 * n_coefficients) signal = np.fft.irfft(coefficients, axis=1) def step(t): i = int(round(t / dt)) return signal[:, i % signal.shape[1]] return step
def make_step(self, shape_in, shape_out, dt, rng): assert shape_in == (0, ) nyquist_cutoff = 0.5 / dt if self.high > nyquist_cutoff: raise ValidationError("High must not exceed the Nyquist frequency " "for the given dt (%0.3f)" % nyquist_cutoff, attr='high', obj=self) n_coefficients = int(np.ceil(self.period / dt / 2.)) shape = (n_coefficients + 1, ) + shape_out sigma = self.rms * np.sqrt(0.5) coefficients = 1j * rng.normal(0., sigma, size=shape) coefficients += rng.normal(0., sigma, size=shape) coefficients[0] = 0. coefficients[-1].imag = 0. set_to_zero = npext.rfftfreq(2 * n_coefficients, d=dt) > self.high coefficients[set_to_zero] = 0. power_correction = np.sqrt(1. - np.sum(set_to_zero, dtype=float) / n_coefficients) if power_correction > 0.: coefficients /= power_correction coefficients *= np.sqrt(2 * n_coefficients) signal = np.fft.irfft(coefficients, axis=0) def step_whitesignal(t): i = int(round(t / dt)) return signal[i % signal.shape[0]] return step_whitesignal
def make_step(self, shape_in, shape_out, dt, rng): assert shape_in == (0,) nyquist_cutoff = 0.5 / dt if self.high > nyquist_cutoff: raise ValidationError("High must not exceed the Nyquist frequency " "for the given dt (%0.3f)" % nyquist_cutoff, attr='high', obj=self) n_coefficients = int(np.ceil(self.period / dt / 2.)) shape = (n_coefficients + 1,) + shape_out sigma = self.rms * np.sqrt(0.5) coefficients = 1j * rng.normal(0., sigma, size=shape) coefficients += rng.normal(0., sigma, size=shape) coefficients[0] = 0. coefficients[-1].imag = 0. set_to_zero = npext.rfftfreq(2 * n_coefficients, d=dt) > self.high coefficients[set_to_zero] = 0. power_correction = np.sqrt( 1. - np.sum(set_to_zero, dtype=float) / n_coefficients) if power_correction > 0.: coefficients /= power_correction coefficients *= np.sqrt(2 * n_coefficients) signal = np.fft.irfft(coefficients, axis=0) def step_whitesignal(t): i = int(round(t / dt)) return signal[i % signal.shape[0]] return step_whitesignal
def test_whitesignal_high_dt(Simulator, high, dt, seed, plt): t = 1. rms = 0.5 d = 500 process = WhiteSignal(t, high, rms=rms) with nengo.Network() as model: u = nengo.Node(process, size_out=d) up = nengo.Probe(u) with Simulator(model, seed=seed, dt=dt) as sim: sim.run(t) values = sim.data[up] freq, val_psd = psd(values, dt=dt) trange = sim.trange() plt.subplot(2, 1, 1) plt.title("First two D of white noise process, high=%d Hz" % high) plt.plot(trange, values[:, :2]) plt.xlim(right=trange[-1]) plt.subplot(2, 1, 2) plt.title("Power spectrum") plt.plot(freq, val_psd, drawstyle='steps') plt.xlim(right=high * 2.0) assert np.allclose(np.std(values, axis=1), rms, rtol=0.15) assert np.all(val_psd[npext.rfftfreq(len(trange), dt) > high] < rms * 0.5)
def make_step(self, size_in, size_out, dt, rng): assert size_in == 0 d = size_out n_coefficients = int(np.ceil(self.period / dt / 2.)) shape = (n_coefficients + 1, d) sigma = self.rms * np.sqrt(0.5) coefficients = 1j * rng.normal(0., sigma, size=shape) coefficients += rng.normal(0., sigma, size=shape) coefficients[0] = 0. coefficients[-1].imag = 0. if self.high is not None: set_to_zero = npext.rfftfreq(2 * n_coefficients, d=dt) > self.high coefficients[set_to_zero] = 0. power_correction = np.sqrt( 1. - np.sum(set_to_zero, dtype=float) / n_coefficients) if power_correction > 0.: coefficients /= power_correction coefficients *= np.sqrt(2 * n_coefficients) signal = np.fft.irfft(coefficients, axis=0) def step_whitesignal(t): i = int(round(t / dt)) return signal[i % signal.shape[0]] return step_whitesignal
def test_rfftfreq_fallback(monkeypatch): np_rfftfreq = np.fft.rfftfreq monkeypatch.delattr(np.fft, "rfftfreq") importlib.reload(npext) for n in (3, 4, 8, 9): for d in (1.0, 3.4, 9.8): assert np.allclose(npext.rfftfreq(n, d=d), np_rfftfreq(n, d=d))
def make_step(self, shape_in, shape_out, dt, rng, state): assert shape_in == (0,) nyquist_cutoff = 0.5 / dt if self.high > nyquist_cutoff: raise ValidationError( "High must not exceed the Nyquist frequency " "for the given dt (%0.3f)" % nyquist_cutoff, attr="high", obj=self, ) n_coefficients = int(np.ceil(self.period / dt / 2.0)) shape = (n_coefficients + 1,) + shape_out sigma = self.rms * np.sqrt(0.5) coefficients = 1j * rng.normal(0.0, sigma, size=shape) coefficients += rng.normal(0.0, sigma, size=shape) coefficients[0] = 0.0 coefficients[-1].imag = 0.0 set_to_zero = npext.rfftfreq(2 * n_coefficients, d=dt) > self.high coefficients[set_to_zero] = 0.0 power_correction = np.sqrt( 1.0 - np.sum(set_to_zero, dtype=float) / n_coefficients ) if power_correction > 0.0: coefficients /= power_correction coefficients *= np.sqrt(2 * n_coefficients) signal = np.fft.irfft(coefficients, axis=0) if self.y0 is not None: # Starts each dimension off where it is closest to y0 def shift(x): offset = np.argmin(abs(self.y0 - x)) return np.roll(x, -offset + 1) # +1 since t starts at dt signal = np.apply_along_axis(shift, 0, signal) def step_whitesignal(t): i = int(round(t / dt)) return signal[i % signal.shape[0]] return step_whitesignal
def make_sample(self, dt, d=1, rng=np.random): n_coefficients = int(np.ceil(self.duration / dt / 2.)) shape = (d, n_coefficients + 1) sigma = self.rms * np.sqrt(0.5) coefficients = 1j * rng.normal(0., sigma, size=shape) coefficients += rng.normal(0., sigma, size=shape) coefficients[:, 0] = 0. coefficients[:, -1].imag = 0. if self.high is not None: set_to_zero = rfftfreq(2 * n_coefficients, d=dt) > self.high coefficients[:, set_to_zero] = 0. power_correction = np.sqrt(1. - np.sum(set_to_zero, dtype=float) / n_coefficients) if power_correction > 0.: coefficients /= power_correction coefficients *= np.sqrt(2 * n_coefficients) t = np.array([0]) signal = np.fft.irfft(coefficients, axis=1) return functools.partial(self.sample, t=t, signal=signal)
def make_sample(self, dt, d=1, rng=np.random): n_coefficients = int(np.ceil(self.duration / dt / 2.)) shape = (d, n_coefficients + 1) sigma = self.rms * np.sqrt(0.5) coefficients = 1j * rng.normal(0., sigma, size=shape) coefficients += rng.normal(0., sigma, size=shape) coefficients[:, 0] = 0. coefficients[:, -1].imag = 0. if self.high is not None: set_to_zero = rfftfreq(2 * n_coefficients, d=dt) > self.high coefficients[:, set_to_zero] = 0. power_correction = np.sqrt( 1. - np.sum(set_to_zero, dtype=float) / n_coefficients) if power_correction > 0.: coefficients /= power_correction coefficients *= np.sqrt(2 * n_coefficients) t = np.array([0]) signal = np.fft.irfft(coefficients, axis=1) return functools.partial(self.sample, t=t, signal=signal)
def test_whitenoise_high(high, rng, plt): rms = 0.5 d = 500 t = 1000 dt = 0.001 process = WhiteNoise(dt * t, high, rms=rms) values = nengo.processes.sample(t, process, dt=dt, d=d, rng=rng) freq, val_psd = psd(values) trange = np.arange(1, t + 1) * dt plt.subplot(2, 1, 1) plt.title("First two D of white noise process, high=%d Hz" % high) plt.plot(trange, values[:, :2]) plt.xlim(right=trange[-1]) plt.subplot(2, 1, 2) plt.title("Power spectrum") plt.plot(freq, val_psd, drawstyle='steps') plt.xlim(right=high * 2.0) assert np.allclose(np.std(values, axis=1), rms, rtol=0.15) assert np.all(val_psd[rfftfreq(t, dt) > high] < rms * 0.5)
def test_whitesignal_high(high, rng, plt): rms = 0.5 d = 500 t = 1 dt = 0.001 process = WhiteSignal(t, high, rms=rms) values = process.run(t, d=d, dt=dt, rng=rng) freq, val_psd = psd(values) trange = process.trange(t, dt=dt) plt.subplot(2, 1, 1) plt.title("First two D of white noise process, high=%d Hz" % high) plt.plot(trange, values[:, :2]) plt.xlim(right=trange[-1]) plt.subplot(2, 1, 2) plt.title("Power spectrum") plt.plot(freq, val_psd, drawstyle='steps') plt.xlim(right=high * 2.0) assert np.allclose(np.std(values, axis=1), rms, rtol=0.15) assert np.all(val_psd[npext.rfftfreq(t, dt) > high] < rms * 0.5)
def make_step(self, shape_in, shape_out, dt, rng): assert shape_in == (0,) nyquist_cutoff = 0.5 / dt if self.high > nyquist_cutoff: raise ValidationError("High must not exceed the Nyquist frequency " "for the given dt (%0.3f)" % nyquist_cutoff, attr='high', obj=self) n_coefficients = int(np.ceil(self.period / dt / 2.)) shape = (n_coefficients + 1,) + shape_out sigma = self.rms * np.sqrt(0.5) coefficients = 1j * rng.normal(0., sigma, size=shape) coefficients += rng.normal(0., sigma, size=shape) coefficients[0] = 0. coefficients[-1].imag = 0. set_to_zero = npext.rfftfreq(2 * n_coefficients, d=dt) > self.high coefficients[set_to_zero] = 0. power_correction = np.sqrt( 1. - np.sum(set_to_zero, dtype=float) / n_coefficients) if power_correction > 0.: coefficients /= power_correction coefficients *= np.sqrt(2 * n_coefficients) signal = np.fft.irfft(coefficients, axis=0) if self.y0 is not None: # Starts each dimension off where it is closest to y0 def shift(x): offset = np.argmin(abs(self.y0 - x)) return np.roll(x, -offset+1) # +1 since t starts at dt signal = np.apply_along_axis(shift, 0, signal) def step_whitesignal(t): i = int(round(t / dt)) return signal[i % signal.shape[0]] return step_whitesignal
def psd(values, dt=0.001): freq = npext.rfftfreq(values.shape[0], d=dt) power = 2. * np.std(np.abs(np.fft.rfft(values, axis=0)), axis=1) / np.sqrt( values.shape[0]) return freq, power
def psd(values, dt=0.001): freq = npext.rfftfreq(values.shape[0], d=dt) power = 2. * np.std(np.abs(np.fft.rfft( values, axis=0)), axis=1) / np.sqrt(values.shape[0]) return freq, power