# StateSpace¶

class sharpy.linear.src.libss.StateSpace(A, B, C, D, dt=None)[source]

Wrap state-space models allocation into a single class and support both full and sparse matrices. The class emulates

scipy.signal.ltisys.StateSpaceContinuous scipy.signal.ltisys.StateSpaceDiscrete

but supports sparse matrices and other functionalities.

Methods: - get_mats: return matrices as tuple - check_types: check matrices types are supported - freqresp: calculate frequency response over range. - addGain: project inputs/outputs - scale: allows scaling a system

Projects input u or output y the state-space system through the gain matrix K. The input ‘where’ determines whether inputs or outputs are projected as:

• where=’in’: inputs are projected such that:

u_new -> u=K*u_new -> SS -> y => u_new -> SSnew -> y

• where=’out’: outputs are projected such that:

u -> SS -> y -> y_new=K*y => u -> SSnew -> ynew

Parameters
• K (np.array or Gain) – gain matrix or Gain object

• where (str) – in or out

Warning

This is not a wrapper of the addGain method in this module, as the state-space matrices are directly overwritten.

disc2cont()[source]

Transform a discrete time system to a continuous time system using a bilinear (Tustin) transformation.

Wrapper of disc2cont()

eigvals()[source]
Returns

Eigenvalues of the system

Return type

np.ndarray

freqresp(wv)[source]

Calculate frequency response over frequencies wv

Note: this wraps frequency response function.

classmethod from_scipy(scipy_ss)[source]

Transforms a scipy.signal.lti or dlti into a StateSpace class

Parameters

scipy_ss (scipy.signal.ltisys.StateSpaceContinous or scipy.signal.ltisys.StateSpaceDiscrete) – Scipy State Space object.

Returns

SHARPy state space object

Return type

StateSpace

property inputs

Number of inputs $$m$$ to the system.

Loads a state-space object from an h5 file, including variable information

Parameters

h5_file_name (str) – Path to file

Returns

Return type

StateSpace

max_eig()[source]

Returns most unstable eigenvalue

property outputs

Number of outputs $$p$$ of the system.

project(wt, v)[source]

Given 2 transformation matrices, (WT, V) of shapes (Nk, self.states) and (self.states, Nk) respectively, this routine projects the state space model states according to:

$Anew = WT A V \ Bnew = WT B \ Cnew = C V \ Dnew = D \$

The projected model has the same number of inputs/outputs as the original one, but Nk states.

Parameters
• wt (Gain or np.ndarray) – Left projection matrix

• v (Gain or np.ndarray) – Righty projection matrix

remove_inputs(*input_remove_list)[source]

Removes inputs through their variable names.

Needs that the StateSpace attribute input_variables is defined.

Parameters

input_remove_list (list(str)) – List of inputs to remove

remove_outputs(*output_remove_list)[source]

Removes outputs through their variable names.

Needs that the StateSpace attribute output_variables is defined.

Parameters

output_remove_list (list(str)) – List of outputs to remove

retain_inout_channels(retain_channels, where)[source]

Retain selected input or output channels only.

Parameters
• retain_channels (list) – List of channels to retain

• where (str) – in or out for input/output channels

save(path)[source]

Save state-space object to h5 file

scale(input_scal=1.0, output_scal=1.0, state_scal=1.0)[source]

Given a state-space system, scales the equations such that the original state, input and output, (x, u and y), are substituted by

The entries input_scal/output_scal/state_scal can be:
• floats: in this case all input/output are scaled by the same value

• lists/arrays of length Nin/Nout: in this case each dof will be scaled

by a different factor

If the original system has form:

xnew=A*x+B*u y=C*x+D*u

the transformation is such that:

property states

Number of states $$n$$ of the system.

transfer_function_evaluation(s)[source]

Returns the transfer function of the system evaluated at $$s\in\mathbb{C}$$.

Parameters

s (complex) – Point in the complex plane at which to evaluate the transfer function.

Returns

Transfer function evaluated at $$s$$.

Return type

np.ndarray

truncate(N)[source]

Retains only the first N states.