pykoop.random_input

random_input(t_range, t_step, low, high, cutoff, order=2, rng=None, output='function')

Generate a smooth random input.

Generates uniform random data between specified bounds, lowpass filters the data, then optionally linearly interpolates to return a function of time.

Uses a Butterworth filter of specified order.

Parameters:
  • t_range ((2,) tuple) – Start and end times in a tuple (s).

  • t_step (float) – Time step at which to generate random data (s).

  • low (float or (n, 1) np.ndarray) – Lower bound for uniform random distribution.

  • high (float or (n, 1) np.ndarray) – Upper bound for uniform random distribution.

  • cutoff (float) – Cutoff frequency for Butterworth lowpass filter (Hz).

  • order (int) – Order of Butterworth lowpass filter.

  • rng (Generator) – Random number generator, numpy.random.default_rng(seed).

  • output (str) – Output format to use. Value ‘array’ causes the function to return an array of smoothed data. Value ‘function’ causes the function to return a function generated by linearly interpolating that same array.

Returns:

If output is ‘function’, returns a function representing linearly-interpolated lowpass-filtered uniformly-random data. If output is ‘array’, returns an array containing lowpass-filtered uniformly-random data. Units are same as low and high.

Return type:

function or np.ndarray

Examples

Simulate a mass-spring-damper with random input

>>> t_range = (0, 1)
>>> t_step = 1e-3
>>> x0 = np.array([0, 0])
>>> u_max = np.array([1])
>>> u = pykoop.random_input(t_range, t_step, -u_max, u_max, cutoff=0.01)
>>> t, x = msd.simulate(t_range, t_step, x0, u)