@@ -471,6 +471,45 @@ def test_time_vector(self):
471
471
squeeze = False )
472
472
self .assertTrue (isinstance (context .exception , ValueError ))
473
473
474
+ def test_discrete_time_steps (self ):
475
+ """Make sure rounding errors in sample time are handled properly"""
476
+ # See https://github.com/python-control/python-control/issues/332)
477
+ #
478
+ # These tests play around with the input time vector to make sure that
479
+ # small rounding errors don't generate spurious errors.
480
+
481
+ # Discrete time system to use for simulation
482
+ # self.siso_dtf2 = TransferFunction([1], [1, 1, 0.25], 0.2)
483
+
484
+ # Set up a time range and simulate
485
+ T = np .arange (0 , 100 , 0.2 )
486
+ tout1 , yout1 = step_response (self .siso_dtf2 , T )
487
+
488
+ # Simulate every other time step
489
+ T = np .arange (0 , 100 , 0.4 )
490
+ tout2 , yout2 = step_response (self .siso_dtf2 , T )
491
+ np .testing .assert_array_almost_equal (tout1 [::2 ], tout2 )
492
+ np .testing .assert_array_almost_equal (yout1 [::2 ], yout2 )
493
+
494
+ # Add a small error into some of the time steps
495
+ T = np .arange (0 , 100 , 0.2 )
496
+ T [1 :- 2 :2 ] -= 1e-12 # tweak second value and a few others
497
+ tout3 , yout3 = step_response (self .siso_dtf2 , T )
498
+ np .testing .assert_array_almost_equal (tout1 , tout3 )
499
+ np .testing .assert_array_almost_equal (yout1 , yout3 )
500
+
501
+ # Add a small error into some of the time steps (w/ skipping)
502
+ T = np .arange (0 , 100 , 0.4 )
503
+ T [1 :- 2 :2 ] -= 1e-12 # tweak second value and a few others
504
+ tout4 , yout4 = step_response (self .siso_dtf2 , T )
505
+ np .testing .assert_array_almost_equal (tout2 , tout4 )
506
+ np .testing .assert_array_almost_equal (yout2 , yout4 )
507
+
508
+ # Make sure larger errors *do* generate an error
509
+ T = np .arange (0 , 100 , 0.2 )
510
+ T [1 :- 2 :2 ] -= 1e-3 # change second value and a few others
511
+ self .assertRaises (ValueError , step_response , self .siso_dtf2 , T )
512
+
474
513
def test_time_series_data_convention (self ):
475
514
"""Make sure time series data matches documentation conventions"""
476
515
# SISO continuous time
0 commit comments