Time response data class by murrayrm · Pull Request #649 · python-control/python-control
This PR adds a TimeResponseData class, following the discussion in #645, which provides a more object-oriented approach to the representation of time response data. All time responses functions now return an instance of TimeResponseData containing the results of simulations. The class implements an __iter__ method that allows backward compatibility with current call signatures.
Old approach (still works):
t, y = ct.step_response(siso_sys)
plt.plot(t, y)
t, y = ct.step_response(mimo_sys)
plt.plot(t, y[0, 1], label="input 1 to output 0")
plt.plot(t, y[1, 1], label="input 1 to output 1")
New approach:
response = ct.step_response(siso_sys)
plt.plot(response.time, response.outputs)
response = ct.step_response(mimo_sys)
plt.plot(response.time, response.outputs[0, 1], label="input 1 to output 0")
plt.plot(response.time, response.outputs[1, 1], label="input 1 to output 1")
The attributes time, outputs, states and inputs are actually class properties that carry out "squeeze processing" allowing use of 1D arrays for SISO systems. Time response data can also be accessed via the attributes t, y, x, and u, which are always in "MIMO" format (indexed by output, input (trace), and time:
response = ct.step_response(siso_sys)
plt.plot(response.t, response.y[0, 0])
Other notes:
-
I adopted the terminology of a "trace" to handle the time response data returned by
step_responseandimpulse_response, where a MIMO system generates a full matrix of input/output responses. The output, state, and input responses for those functions are represented by a 3D array indexed by output/state/input, trace, and time. -
The terminology for
time,outputs,statesandinputswas chosen to be consistent with theOptimalControlResultclass. This is implemented via a set of properties that perform the squeeze (and transpose) processing to provide compatibility with our existing functionality. -
The
InputOutputDataclass is currently only used to store the output of simulations, but it is set up to serve as a pure data representation class (similar toFrequencyResponseData) and could later be useful for things like input to system identification routines. -
No changes to existing unit tests were needed => should be fully backwards compatible.
At a future date, we may want to consider adding some additional functionality:
- Plot methods that plot input/output simulations in a uniform way
- Methods to convert data into
DataFrameformat for use of pandas plotting capabilities
I'm still not completely sure if this is a useful PR, but I was motivated by the way a similar approach taken in JuliaControl and thought I would give it a try. Suggestions and comments welcome (will leave in draft form for a bit to allow for discussion).