
    (ph#                       S r SSKrSSKJr  SSKJr  SSKJr  SSK	J
r
JrJrJrJrJrJr  SSKJrJrJrJrJrJrJr  SSKrSS	KJrJrJrJrJrJ r J!r!J"r"J#r#  SSK$r$/ S
Qr% " S S5      r& " S S\&5      r' " S S\&5      r( " S S\&5      r) " S S\)\'5      r* " S S\)\(5      r+ " S S\&5      r, " S S\,\'5      r- " S S\,\(5      r. " S S\&5      r/ " S S \/\'5      r0 " S! S"\/\(5      r1S8S# jr2S$ r3S9S% jr4S9S& jr5S:S' jr6S;S( jr7 " S) S*5      r8S+ r9S, r:S- r;S. r<S/ r=S0 r>S1 r?S<S2 jr@S=S3 jrAS9S4 jrBS9S5 jrCS>S6 jrDS:S7 jrEg)?z]
ltisys -- a collection of classes and functions for modeling linear
time invariant systems.
    N)qr)linalg)make_interp_spline   )tf2zpkzpk2tf	normalizefreqsfreqz	freqs_zpk	freqz_zpk)tf2ssabcd_normalizess2tfzpk2ssss2zpkcont2discrete_atleast_2d_or_none)	real
atleast_1dsqueezeasarrayzerosdot	transposeoneslinspace)ltidltiTransferFunctionZerosPolesGain
StateSpacelsimimpulsestepbodefreqrespplace_polesdlsimdstepdimpulse	dfreqrespdbodec                      ^  \ rS rSrU 4S jrU 4S jr\S 5       r\S 5       r\S 5       r	\S 5       r
S rS	 rS
 rSrU =r$ )LinearTimeInvariant.   c                 J   > U [         L a  [        S5      e[        TU ]  U 5      $ )z2Create a new object, don't allow direct instances.z\The LinearTimeInvariant class is not meant to be used directly, use `lti` or `dlti` instead.)r/   NotImplementedErrorsuper__new__clssystemkwargs	__class__s      G/var/www/html/venv/lib/python3.13/site-packages/scipy/signal/_ltisys.pyr4   LinearTimeInvariant.__new__/   s/    %%% '; < < ws##    c                 L   > [         TU ]  5         SU l        SU l        SU l        gO
Initialize the `lti` baseclass.

The heavy lifting is done by the subclasses.
N)r3   __init__inputsoutputs_dt)selfr9   s    r:   r@   LinearTimeInvariant.__init__7   s%     	r<   c                     U R                   $ )zAReturn the sampling time of the system, `None` for `lti` systems.rC   rD   s    r:   dtLinearTimeInvariant.dtC        xxr<   c                 <    U R                   c  0 $ SU R                   0$ )NrI   )rI   rH   s    r:   _dt_dictLinearTimeInvariant._dt_dictH   s    77?I$''?"r<   c                 6    U R                  5       R                  $ )zZeros of the system.)to_zpkr   rH   s    r:   r   LinearTimeInvariant.zerosO        {{}"""r<   c                 6    U R                  5       R                  $ )zPoles of the system.)rP   polesrH   s    r:   rT   LinearTimeInvariant.polesT   rR   r<   c                 P    [        U [        5      (       a  U $ U R                  5       $ )zConvert to `StateSpace` system, without copying.

Returns
-------
sys: StateSpace
    The `StateSpace` system. If the class is already an instance of
    `StateSpace` then this instance is returned.
)
isinstancer"   to_ssrH   s    r:   _as_ssLinearTimeInvariant._as_ssY   s"     dJ''K::<r<   c                 P    [        U [        5      (       a  U $ U R                  5       $ )zConvert to `ZerosPolesGain` system, without copying.

Returns
-------
sys: ZerosPolesGain
    The `ZerosPolesGain` system. If the class is already an instance of
    `ZerosPolesGain` then this instance is returned.
)rW   r!   rP   rH   s    r:   _as_zpkLinearTimeInvariant._as_zpkg   s"     dN++K;;= r<   c                 P    [        U [        5      (       a  U $ U R                  5       $ )zConvert to `TransferFunction` system, without copying.

Returns
-------
sys: ZerosPolesGain
    The `TransferFunction` system. If the class is already an instance of
    `TransferFunction` then this instance is returned.
)rW   r    to_tfrH   s    r:   _as_tfLinearTimeInvariant._as_tfu   s#     d,--K::<r<   )rC   rA   rB   )__name__
__module____qualname____firstlineno__r4   r@   propertyrI   rM   r   rT   rY   r\   r`   __static_attributes____classcell__r9   s   @r:   r/   r/   .   sq    $
   # # # # # # !   r<   r/   c                   t   ^  \ rS rSrSrU 4S jrU 4S jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSrU =r$ )r      a@  
Continuous-time linear time invariant system base class.

Parameters
----------
*system : arguments
    The `lti` class can be instantiated with either 2, 3 or 4 arguments.
    The following gives the number of arguments and the corresponding
    continuous-time subclass that is created:

        * 2: `TransferFunction`:  (numerator, denominator)
        * 3: `ZerosPolesGain`: (zeros, poles, gain)
        * 4: `StateSpace`:  (A, B, C, D)

    Each argument can be an array or a sequence.

See Also
--------
ZerosPolesGain, StateSpace, TransferFunction, dlti

Notes
-----
`lti` instances do not exist directly. Instead, `lti` creates an instance
of one of its subclasses: `StateSpace`, `TransferFunction` or
`ZerosPolesGain`.

If (numerator, denominator) is passed in for ``*system``, coefficients for
both the numerator and denominator should be specified in descending
exponent order (e.g., ``s^2 + 3s + 5`` would be represented as ``[1, 3,
5]``).

Changing the value of properties that are not directly part of the current
system representation (such as the `zeros` of a `StateSpace` system) is
very inefficient and may lead to numerical inaccuracies. It is better to
convert to the specific system representation first. For example, call
``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.

Examples
--------
>>> from scipy import signal

>>> signal.lti(1, 2, 3, 4)
StateSpaceContinuous(
array([[1]]),
array([[2]]),
array([[3]]),
array([[4]]),
dt: None
)

Construct the transfer function
:math:`H(s) = \frac{5(s - 1)(s - 2)}{(s - 3)(s - 4)}`:

>>> signal.lti([1, 2], [3, 4], 5)
ZerosPolesGainContinuous(
array([1, 2]),
array([3, 4]),
5,
dt: None
)

Construct the transfer function :math:`H(s) = \frac{3s + 4}{1s + 2}`:

>>> signal.lti([3, 4], [1, 2])
TransferFunctionContinuous(
array([3., 4.]),
array([1., 2.]),
dt: None
)

c                 &  > U [         L ay  [        U5      nUS:X  a  [        R                  " [        /UQ76 $ US:X  a  [        R                  " [        /UQ76 $ US:X  a  [
        R                  " [
        /UQ76 $ [        S5      e[        TU ]  U 5      $ )/Create an instance of the appropriate subclass.         zF`system` needs to be an instance of `lti` or have 2, 3 or 4 arguments.)r   lenTransferFunctionContinuousr4   ZerosPolesGainContinuousStateSpaceContinuous
ValueErrorr3   )r6   r7   Nr9   s      r:   r4   lti.__new__   s    #:FAAv199.9179 9a/77,7/57 7a+334H =5;= = ! "@ A A ws##r<   c                     > [         TU ]  " U6   gr>   )r3   r@   )rD   r7   r9   s     r:   r@   lti.__init__   s     	&!r<   c                     [        XX#S9$ )zU
Return the impulse response of a continuous-time system.
See `impulse` for details.
X0Trv   )r$   rD   r|   r}   rv   s       r:   r$   lti.impulse   s    
 ta--r<   c                     [        XX#S9$ )zO
Return the step response of a continuous-time system.
See `step` for details.
r{   )r%   r~   s       r:   r%   lti.step   s    
 D1**r<   c                     [        XX#S9$ )zW
Return the response of a continuous-time system to input `U`.
See `lsim` for details.
)r|   )r#   )rD   Ur}   r|   s       r:   output
lti.output   s    
 DQ&&r<   c                     [        XUS9$ )a  
Calculate Bode magnitude and phase data of a continuous-time system.

Returns a 3-tuple containing arrays of frequencies [rad/s], magnitude
[dB] and phase [deg]. See `bode` for details.

Examples
--------
>>> from scipy import signal
>>> import matplotlib.pyplot as plt

>>> sys = signal.TransferFunction([1], [1, 1])
>>> w, mag, phase = sys.bode()

>>> plt.figure()
>>> plt.semilogx(w, mag)    # Bode magnitude plot
>>> plt.figure()
>>> plt.semilogx(w, phase)  # Bode phase plot
>>> plt.show()

wn)r&   rD   r   r   s      r:   r&   lti.bode   s    , D##r<   c                     [        XUS9$ )z
Calculate the frequency response of a continuous-time system.

Returns a 2-tuple containing arrays of frequencies [rad/s] and
complex magnitude.
See `freqresp` for details.
r   )r'   r   s      r:   r'   lti.freqresp  s     Q''r<   c                     [        S5      e)zReturn a discretized version of the current system.

Parameters: See `cont2discrete` for details.

Returns
-------
sys: instance of `dlti`
z5to_discrete is not implemented for this system class.)r2   rD   rI   methodalphas       r:   to_discretelti.to_discrete  s     " #2 3 	3r<    NNNNNd   N'  zohN)rb   rc   rd   re   __doc__r4   r@   r$   r%   r   r&   r'   r   rg   rh   ri   s   @r:   r   r      s7    FN$&".+'$0(
3 
3r<   r   c                      ^  \ rS rSrSrU 4S jrU 4S jr\S 5       r\R                  S 5       rSS jr
SS jrSS	 jrSS
 jrSS jrSrU =r$ )r   i+  a  
Discrete-time linear time invariant system base class.

Parameters
----------
*system: arguments
    The `dlti` class can be instantiated with either 2, 3 or 4 arguments.
    The following gives the number of arguments and the corresponding
    discrete-time subclass that is created:

        * 2: `TransferFunction`:  (numerator, denominator)
        * 3: `ZerosPolesGain`: (zeros, poles, gain)
        * 4: `StateSpace`:  (A, B, C, D)

    Each argument can be an array or a sequence.
dt: float, optional
    Sampling time [s] of the discrete-time systems. Defaults to ``True``
    (unspecified sampling time). Must be specified as a keyword argument,
    for example, ``dt=0.1``.

See Also
--------
ZerosPolesGain, StateSpace, TransferFunction, lti

Notes
-----
`dlti` instances do not exist directly. Instead, `dlti` creates an instance
of one of its subclasses: `StateSpace`, `TransferFunction` or
`ZerosPolesGain`.

Changing the value of properties that are not directly part of the current
system representation (such as the `zeros` of a `StateSpace` system) is
very inefficient and may lead to numerical inaccuracies.  It is better to
convert to the specific system representation first. For example, call
``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.

If (numerator, denominator) is passed in for ``*system``, coefficients for
both the numerator and denominator should be specified in descending
exponent order (e.g., ``z^2 + 3z + 5`` would be represented as ``[1, 3,
5]``).

.. versionadded:: 0.18.0

Examples
--------
>>> from scipy import signal

>>> signal.dlti(1, 2, 3, 4)
StateSpaceDiscrete(
array([[1]]),
array([[2]]),
array([[3]]),
array([[4]]),
dt: True
)

>>> signal.dlti(1, 2, 3, 4, dt=0.1)
StateSpaceDiscrete(
array([[1]]),
array([[2]]),
array([[3]]),
array([[4]]),
dt: 0.1
)

Construct the transfer function
:math:`H(z) = \frac{5(z - 1)(z - 2)}{(z - 3)(z - 4)}` with a sampling time
of 0.1 seconds:

>>> signal.dlti([1, 2], [3, 4], 5, dt=0.1)
ZerosPolesGainDiscrete(
array([1, 2]),
array([3, 4]),
5,
dt: 0.1
)

Construct the transfer function :math:`H(z) = \frac{3z + 4}{1z + 2}` with
a sampling time of 0.1 seconds:

>>> signal.dlti([3, 4], [1, 2], dt=0.1)
TransferFunctionDiscrete(
array([3., 4.]),
array([1., 2.]),
dt: 0.1
)

c                 8  > U [         L a  [        U5      nUS:X  a  [        R                  " [        /UQ70 UD6$ US:X  a  [        R                  " [        /UQ70 UD6$ US:X  a  [
        R                  " [
        /UQ70 UD6$ [        S5      e[        TU ]  U 5      $ )rm   rn   ro   rp   zG`system` needs to be an instance of `dlti` or have 2, 3 or 4 arguments.)r   rq   TransferFunctionDiscreter4   ZerosPolesGainDiscreteStateSpaceDiscreteru   r3   )r6   r7   r8   rv   r9   s       r:   r4   dlti.__new__  s    $;FAAv/77,A/5A9?A Aa-556L I7=IAGI Ia)112D <v <4:< < ! "@ A A ws##r<   c                 V   > UR                  SS5      n[        TU ]  " U0 UD6  X0l        g)r?   rI   TN)popr3   r@   rI   )rD   r7   r8   rI   r9   s       r:   r@   dlti.__init__  s,     ZZd#&+F+r<   c                     U R                   $ )z'Return the sampling time of the system.rG   rH   s    r:   rI   dlti.dt  rK   r<   c                     Xl         g r   rG   )rD   rI   s     r:   rI   r     s    r<   c                     [        XX#S9$ )z]
Return the impulse response of the discrete-time `dlti` system.
See `dimpulse` for details.
x0tr   )r+   rD   r   r   r   s       r:   r$   dlti.impulse  s    
 q..r<   c                     [        XX#S9$ )zW
Return the step response of the discrete-time `dlti` system.
See `dstep` for details.
r   )r*   r   s       r:   r%   	dlti.step  s    
 TA++r<   c                     [        XX#S9$ )zX
Return the response of the discrete-time system to input `u`.
See `dlsim` for details.
)r   )r)   )rD   ur   r   s       r:   r   dlti.output  s    
 Ta''r<   c                     [        XUS9$ )a{  
Calculate Bode magnitude and phase data of a discrete-time system.

Returns a 3-tuple containing arrays of frequencies [rad/s], magnitude
[dB] and phase [deg]. See `dbode` for details.

Examples
--------
>>> from scipy import signal
>>> import matplotlib.pyplot as plt

Construct the transfer function :math:`H(z) = \frac{1}{z^2 + 2z + 3}`
with sampling time 0.5s:

>>> sys = signal.TransferFunction([1], [1, 2, 3], dt=0.5)

Equivalent: signal.dbode(sys)

>>> w, mag, phase = sys.bode()

>>> plt.figure()
>>> plt.semilogx(w, mag)    # Bode magnitude plot
>>> plt.figure()
>>> plt.semilogx(w, phase)  # Bode phase plot
>>> plt.show()

r   )r-   r   s      r:   r&   	dlti.bode  s    8 T!$$r<   c                     [        XX#S9$ )z
Calculate the frequency response of a discrete-time system.

Returns a 2-tuple containing arrays of frequencies [rad/s] and
complex magnitude.
See `dfreqresp` for details.

)r   r   whole)r,   )rD   r   r   r   s       r:   r'   dlti.freqresp  s     a55r<   )rC   rI   r   r   r   Nr   F)rb   rc   rd   re   r   r4   r@   rf   rI   setterr$   r%   r   r&   r'   rg   rh   ri   s   @r:   r   r   +  s^    Wp$&	   YY /,(%<	6 	6r<   r   c                      ^  \ rS rSrSrU 4S jrU 4S jrS r\S 5       r	\	R                  S 5       r	\S 5       r\R                  S	 5       rS
 rS rS rS r\S 5       r\S 5       rSrU =r$ )r    i  aV	  Linear Time Invariant system class in transfer function form.

Represents the system as the continuous-time transfer function
:math:`H(s)=\sum_{i=0}^N b[N-i] s^i / \sum_{j=0}^M a[M-j] s^j` or the
discrete-time transfer function
:math:`H(z)=\sum_{i=0}^N b[N-i] z^i / \sum_{j=0}^M a[M-j] z^j`, where
:math:`b` are elements of the numerator `num`, :math:`a` are elements of
the denominator `den`, and ``N == len(b) - 1``, ``M == len(a) - 1``.
`TransferFunction` systems inherit additional
functionality from the `lti`, respectively the `dlti` classes, depending on
which system representation is used.

Parameters
----------
*system: arguments
    The `TransferFunction` class can be instantiated with 1 or 2
    arguments. The following gives the number of input arguments and their
    interpretation:

        * 1: `lti` or `dlti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 2: array_like: (numerator, denominator)
dt: float, optional
    Sampling time [s] of the discrete-time systems. Defaults to `None`
    (continuous-time). Must be specified as a keyword argument, for
    example, ``dt=0.1``.

See Also
--------
ZerosPolesGain, StateSpace, lti, dlti
tf2ss, tf2zpk, tf2sos

Notes
-----
Changing the value of properties that are not part of the
`TransferFunction` system representation (such as the `A`, `B`, `C`, `D`
state-space matrices) is very inefficient and may lead to numerical
inaccuracies.  It is better to convert to the specific system
representation first. For example, call ``sys = sys.to_ss()`` before
accessing/changing the A, B, C, D system matrices.

If (numerator, denominator) is passed in for ``*system``, coefficients
for both the numerator and denominator should be specified in descending
exponent order (e.g. ``s^2 + 3s + 5`` or ``z^2 + 3z + 5`` would be
represented as ``[1, 3, 5]``)

Examples
--------
Construct the transfer function
:math:`H(s) = \frac{s^2 + 3s + 3}{s^2 + 2s + 1}`:

>>> from scipy import signal

>>> num = [1, 3, 3]
>>> den = [1, 2, 1]

>>> signal.TransferFunction(num, den)
TransferFunctionContinuous(
array([1., 3., 3.]),
array([1., 2., 1.]),
dt: None
)

Construct the transfer function
:math:`H(z) = \frac{z^2 + 3z + 3}{z^2 + 2z + 1}` with a sampling time of
0.1 seconds:

>>> signal.TransferFunction(num, den, dt=0.1)
TransferFunctionDiscrete(
array([1., 3., 3.]),
array([1., 2., 1.]),
dt: 0.1
)

c                 D  > [        U5      S:X  a+  [        US   [        5      (       a  US   R                  5       $ U [        L aN  UR                  S5      c  [        R                  " [        /UQ70 UD6$ [        R                  " [        /UQ70 UD6$ [        TU ]  U 5      $ )z8Handle object conversion if input is an instance of lti.r   r   rI   )
rq   rW   r/   r_   r    getrr   r4   r   r3   r5   s      r:   r4   TransferFunction.__new__6  s    v;!
6!96I J J!9??$$ ""zz$'199.  
 077,   ws##r<   c                    > [        US   [        5      (       a  g[        TU ]  " S0 UD6  SU l        SU l        [        U6 u  U l        U l        g)z&Initialize the state space LTI system.r   Nr   )	rW   r/   r3   r@   _num_denr	   numdenrD   r7   r8   r9   s      r:   r@   TransferFunction.__init__K  sM     fQi!455 	"6"		&/$(r<   c           	          U R                   R                   S[        U R                  5       S[        U R                  5       S[        U R
                  5       S3$ )z7Return representation of the system's transfer function(
,
,
dt: 
))r9   rb   reprr   r   rI   rH   s    r:   __repr__TransferFunction.__repr__Y  sR     ~~&&'sDHH~cDHH~ =/&	
r<   c                     U R                   $ )z+Numerator of the `TransferFunction` system.)r   rH   s    r:   r   TransferFunction.numb       yyr<   c                     [        U5      U l        [        U R                  R                  5      S:  a$  U R                  R                  u  U l        U l        g SU l        SU l        g Nr   )r   r   rq   r   shaperB   rA   )rD   r   s     r:   r   r   g  sH    sO	 txx~~"(,%DL$+DLDKr<   c                     U R                   $ )z-Denominator of the `TransferFunction` system.)r   rH   s    r:   r   TransferFunction.denr  r   r<   c                 $    [        U5      U l        g r   )r   r   )rD   r   s     r:   r   r   w  s    sO	r<   c                 H    UR                   U l         UR                  U l        g)z
Copy the parameters of another `TransferFunction` object

Parameters
----------
system : `TransferFunction`
    The `StateSpace` system that is to be copied

N)r   r   rD   r7   s     r:   _copyTransferFunction._copy{  s     ::::r<   c                 .    [         R                  " U 5      $ )z
Return a copy of the current `TransferFunction` system.

Returns
-------
sys : instance of `TransferFunction`
    The current system (copy)

copydeepcopyrH   s    r:   r_   TransferFunction.to_tf       }}T""r<   c                 h    [        [        U R                  U R                  5      0 U R                  D6$ )z
Convert system representation to `ZerosPolesGain`.

Returns
-------
sys : instance of `ZerosPolesGain`
    Zeros, poles, gain representation of the current system

)r!   r   r   r   rM   rH   s    r:   rP   TransferFunction.to_zpk  s.     vdhh9 / $/ 	/r<   c                 h    [        [        U R                  U R                  5      0 U R                  D6$ z
Convert system representation to `StateSpace`.

Returns
-------
sys : instance of `StateSpace`
    State space model of the current system

)r"   r   r   r   rM   rH   s    r:   rX   TransferFunction.to_ss  s.     54884 + MM+ 	+r<   c                    [        U 5      [        U5      -
  nUS:  a/  [        R                  " [        R                  " U5      U45      nX4$ US:  a-  [        R                  " [        R                  " U* 5      U 45      n X4$ )a%  Change a transfer function from the variable `z` to `z**-1`.

Parameters
----------
num, den: 1d array_like
    Sequences representing the coefficients of the numerator and
    denominator polynomials, in order of descending degree of 'z'.
    That is, ``5z**2 + 3z + 2`` is presented as ``[5, 3, 2]``.

Returns
-------
num, den: 1d array_like
    Sequences representing the coefficients of the numerator and
    denominator polynomials, in order of ascending degree of 'z**-1'.
    That is, ``5 + 3 z**-1 + 2 z**-2`` is presented as ``[5, 3, 2]``.
r   rq   nphstackr   r   r   diffs      r:   
_z_to_zinvTransferFunction._z_to_zinv  sn    $ 3x#c("!8))RXXd^S12C x AX))RXXte_c23Cxr<   c                    [        U 5      [        U5      -
  nUS:  a/  [        R                  " U[        R                  " U5      45      nX4$ US:  a-  [        R                  " U [        R                  " U* 5      45      n X4$ )a%  Change a transfer function from the variable `z` to `z**-1`.

Parameters
----------
num, den: 1d array_like
    Sequences representing the coefficients of the numerator and
    denominator polynomials, in order of ascending degree of 'z**-1'.
    That is, ``5 + 3 z**-1 + 2 z**-2`` is presented as ``[5, 3, 2]``.

Returns
-------
num, den: 1d array_like
    Sequences representing the coefficients of the numerator and
    denominator polynomials, in order of descending degree of 'z'.
    That is, ``5z**2 + 3z + 2`` is presented as ``[5, 3, 2]``.
r   r   r   s      r:   
_zinv_to_zTransferFunction._zinv_to_z  sn    $ 3x#c("!8))S"((4.12C x AX))S"((D5/23Cxr<   )r   r   r   rA   r   rB   )rb   rc   rd   re   r   r4   r@   r   rf   r   r   r   r   r_   rP   rX   staticmethodr   r   rg   rh   ri   s   @r:   r    r      s    JV$*0
   	ZZ    	ZZ$ $
#/+  0  r<   r    c                   "    \ rS rSrSrSS jrSrg)rr   i  a  
Continuous-time Linear Time Invariant system in transfer function form.

Represents the system as the transfer function
:math:`H(s)=\sum_{i=0}^N b[N-i] s^i / \sum_{j=0}^M a[M-j] s^j`, where
:math:`b` are elements of the numerator `num`, :math:`a` are elements of
the denominator `den`, and ``N == len(b) - 1``, ``M == len(a) - 1``.
Continuous-time `TransferFunction` systems inherit additional
functionality from the `lti` class.

Parameters
----------
*system: arguments
    The `TransferFunction` class can be instantiated with 1 or 2
    arguments. The following gives the number of input arguments and their
    interpretation:

        * 1: `lti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 2: array_like: (numerator, denominator)

See Also
--------
ZerosPolesGain, StateSpace, lti
tf2ss, tf2zpk, tf2sos

Notes
-----
Changing the value of properties that are not part of the
`TransferFunction` system representation (such as the `A`, `B`, `C`, `D`
state-space matrices) is very inefficient and may lead to numerical
inaccuracies.  It is better to convert to the specific system
representation first. For example, call ``sys = sys.to_ss()`` before
accessing/changing the A, B, C, D system matrices.

If (numerator, denominator) is passed in for ``*system``, coefficients
for both the numerator and denominator should be specified in descending
exponent order (e.g. ``s^2 + 3s + 5`` would be represented as
``[1, 3, 5]``)

Examples
--------
Construct the transfer function
:math:`H(s) = \frac{s^2 + 3s + 3}{s^2 + 2s + 1}`:

>>> from scipy import signal

>>> num = [1, 3, 3]
>>> den = [1, 2, 1]

>>> signal.TransferFunction(num, den)
TransferFunctionContinuous(
array([ 1.,  3.,  3.]),
array([ 1.,  2.,  1.]),
dt: None
)

Nc           	      ^    [        [        U R                  U R                  4UUUS9SS SU06$ )z
Returns the discretized `TransferFunction` system.

Parameters: See `cont2discrete` for details.

Returns
-------
sys: instance of `dlti` and `StateSpace`
r   r   NrI   )r    r   r   r   r   s       r:   r   &TransferFunctionContinuous.to_discrete  sH      $((/C/16<5:"< =@R"A ' $&	' 	'r<   r   r   rb   rc   rd   re   r   r   rg   r   r<   r:   rr   rr     s    9v'r<   rr   c                       \ rS rSrSrSrg)r   i.  a(  
Discrete-time Linear Time Invariant system in transfer function form.

Represents the system as the transfer function
:math:`H(z)=\sum_{i=0}^N b[N-i] z^i / \sum_{j=0}^M a[M-j] z^j`, where
:math:`b` are elements of the numerator `num`, :math:`a` are elements of
the denominator `den`, and ``N == len(b) - 1``, ``M == len(a) - 1``.
Discrete-time `TransferFunction` systems inherit additional functionality
from the `dlti` class.

Parameters
----------
*system: arguments
    The `TransferFunction` class can be instantiated with 1 or 2
    arguments. The following gives the number of input arguments and their
    interpretation:

        * 1: `dlti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 2: array_like: (numerator, denominator)
dt: float, optional
    Sampling time [s] of the discrete-time systems. Defaults to `True`
    (unspecified sampling time). Must be specified as a keyword argument,
    for example, ``dt=0.1``.

See Also
--------
ZerosPolesGain, StateSpace, dlti
tf2ss, tf2zpk, tf2sos

Notes
-----
Changing the value of properties that are not part of the
`TransferFunction` system representation (such as the `A`, `B`, `C`, `D`
state-space matrices) is very inefficient and may lead to numerical
inaccuracies.

If (numerator, denominator) is passed in for ``*system``, coefficients
for both the numerator and denominator should be specified in descending
exponent order (e.g., ``z^2 + 3z + 5`` would be represented as
``[1, 3, 5]``).

Examples
--------
Construct the transfer function
:math:`H(z) = \frac{z^2 + 3z + 3}{z^2 + 2z + 1}` with a sampling time of
0.5 seconds:

>>> from scipy import signal

>>> num = [1, 3, 3]
>>> den = [1, 2, 1]

>>> signal.TransferFunction(num, den, dt=0.5)
TransferFunctionDiscrete(
array([ 1.,  3.,  3.]),
array([ 1.,  2.,  1.]),
dt: 0.5
)

r   Nrb   rc   rd   re   r   rg   r   r<   r:   r   r   .  s    <z 	r<   r   c                      ^  \ rS rSrSrU 4S jrU 4S jrS r\S 5       r	\	R                  S 5       r	\S 5       r\R                  S	 5       r\S
 5       r\R                  S 5       rS rS rS rS rSrU =r$ )r!   io  a  
Linear Time Invariant system class in zeros, poles, gain form.

Represents the system as the continuous- or discrete-time transfer function
:math:`H(s)=k \prod_i (s - z[i]) / \prod_j (s - p[j])`, where :math:`k` is
the `gain`, :math:`z` are the `zeros` and :math:`p` are the `poles`.
`ZerosPolesGain` systems inherit additional functionality from the `lti`,
respectively the `dlti` classes, depending on which system representation
is used.

Parameters
----------
*system : arguments
    The `ZerosPolesGain` class can be instantiated with 1 or 3
    arguments. The following gives the number of input arguments and their
    interpretation:

        * 1: `lti` or `dlti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 3: array_like: (zeros, poles, gain)
dt: float, optional
    Sampling time [s] of the discrete-time systems. Defaults to `None`
    (continuous-time). Must be specified as a keyword argument, for
    example, ``dt=0.1``.


See Also
--------
TransferFunction, StateSpace, lti, dlti
zpk2ss, zpk2tf, zpk2sos

Notes
-----
Changing the value of properties that are not part of the
`ZerosPolesGain` system representation (such as the `A`, `B`, `C`, `D`
state-space matrices) is very inefficient and may lead to numerical
inaccuracies.  It is better to convert to the specific system
representation first. For example, call ``sys = sys.to_ss()`` before
accessing/changing the A, B, C, D system matrices.

Examples
--------
Construct the transfer function
:math:`H(s) = \frac{5(s - 1)(s - 2)}{(s - 3)(s - 4)}`:

>>> from scipy import signal

>>> signal.ZerosPolesGain([1, 2], [3, 4], 5)
ZerosPolesGainContinuous(
array([1, 2]),
array([3, 4]),
5,
dt: None
)

Construct the transfer function
:math:`H(z) = \frac{5(z - 1)(z - 2)}{(z - 3)(z - 4)}` with a sampling time
of 0.1 seconds:

>>> signal.ZerosPolesGain([1, 2], [3, 4], 5, dt=0.1)
ZerosPolesGainDiscrete(
array([1, 2]),
array([3, 4]),
5,
dt: 0.1
)

c                 D  > [        U5      S:X  a+  [        US   [        5      (       a  US   R                  5       $ U [        L aN  UR                  S5      c  [        R                  " [        /UQ70 UD6$ [        R                  " [        /UQ70 UD6$ [        TU ]  U 5      $ )z9Handle object conversion if input is an instance of `lti`r   r   rI   )
rq   rW   r/   rP   r!   r   rs   r4   r   r3   r5   s      r:   r4   ZerosPolesGain.__new__  s    v;!
6!96I J J!9##%% . zz$'/77,  
 .55*   ws##r<   c                    > [        US   [        5      (       a  g[        TU ]  " S0 UD6  SU l        SU l        SU l        Uu  U l        U l        U l	        g)z)Initialize the zeros, poles, gain system.r   Nr   )
rW   r/   r3   r@   _zeros_poles_gainr   rT   gainr   s      r:   r@   ZerosPolesGain.__init__  sQ     fQi!455"6"
,2)
DJ	r<   c                     U R                   R                   S[        U R                  5       S[        U R                  5       S[        U R
                  5       S[        U R                  5       S3
$ )z5Return representation of the `ZerosPolesGain` system.r   r   r   r   )r9   rb   r   r   rT   r  rI   rH   s    r:   r   ZerosPolesGain.__repr__  sd     ~~&&'sDJJ DJJ DII  =/	&	
r<   c                     U R                   $ )z%Zeros of the `ZerosPolesGain` system.)r   rH   s    r:   r   ZerosPolesGain.zeros       {{r<   c                     [        U5      U l        [        U R                  R                  5      S:  a$  U R                  R                  u  U l        U l        g SU l        SU l        g r   )r   r   rq   r   r   rB   rA   )rD   r   s     r:   r   r    sM     ' tzz 1$(,

(8(8%DL$+DLDKr<   c                     U R                   $ )z%Poles of the `ZerosPolesGain` system.)r   rH   s    r:   rT   ZerosPolesGain.poles  r  r<   c                 $    [        U5      U l        g r   )r   r   )rD   rT   s     r:   rT   r    s     'r<   c                     U R                   $ )z$Gain of the `ZerosPolesGain` system.r  rH   s    r:   r  ZerosPolesGain.gain  s     zzr<   c                     Xl         g r   r  )rD   r  s     r:   r  r     s    
r<   c                 j    UR                   U l         UR                  U l        UR                  U l        g)z
Copy the parameters of another `ZerosPolesGain` system.

Parameters
----------
system : instance of `ZerosPolesGain`
    The zeros, poles gain system that is to be copied

N)rT   r   r  r   s     r:   r   ZerosPolesGain._copy  s%     \\
\\
KK	r<   c                 ~    [        [        U R                  U R                  U R                  5      0 U R
                  D6$ )z
Convert system representation to `TransferFunction`.

Returns
-------
sys : instance of `TransferFunction`
    Transfer function of the current system

)r    r   r   rT   r  rM   rH   s    r:   r_   ZerosPolesGain.to_tf  s4      

DJJ		!J 1"&--1 	1r<   c                 .    [         R                  " U 5      $ )z
Return a copy of the current 'ZerosPolesGain' system.

Returns
-------
sys : instance of `ZerosPolesGain`
    The current system (copy)

r   rH   s    r:   rP   ZerosPolesGain.to_zpk  r   r<   c                 ~    [        [        U R                  U R                  U R                  5      0 U R
                  D6$ r   )r"   r   r   rT   r  rM   rH   s    r:   rX   ZerosPolesGain.to_ss+  s4     6$**djj$))D + MM+ 	+r<   )r  r   r   r  rA   rB   rT   r   )rb   rc   rd   re   r   r4   r@   r   rf   r   r   rT   r  r   r_   rP   rX   rg   rh   ri   s   @r:   r!   r!   o  s    CH$,3
   \\    \\( (   
[[  1
#+ +r<   r!   c                   "    \ rS rSrSrSS jrSrg)rs   i9  a  
Continuous-time Linear Time Invariant system in zeros, poles, gain form.

Represents the system as the continuous time transfer function
:math:`H(s)=k \prod_i (s - z[i]) / \prod_j (s - p[j])`, where :math:`k` is
the `gain`, :math:`z` are the `zeros` and :math:`p` are the `poles`.
Continuous-time `ZerosPolesGain` systems inherit additional functionality
from the `lti` class.

Parameters
----------
*system : arguments
    The `ZerosPolesGain` class can be instantiated with 1 or 3
    arguments. The following gives the number of input arguments and their
    interpretation:

        * 1: `lti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 3: array_like: (zeros, poles, gain)

See Also
--------
TransferFunction, StateSpace, lti
zpk2ss, zpk2tf, zpk2sos

Notes
-----
Changing the value of properties that are not part of the
`ZerosPolesGain` system representation (such as the `A`, `B`, `C`, `D`
state-space matrices) is very inefficient and may lead to numerical
inaccuracies.  It is better to convert to the specific system
representation first. For example, call ``sys = sys.to_ss()`` before
accessing/changing the A, B, C, D system matrices.

Examples
--------
Construct the transfer function
:math:`H(s)=\frac{5(s - 1)(s - 2)}{(s - 3)(s - 4)}`:

>>> from scipy import signal

>>> signal.ZerosPolesGain([1, 2], [3, 4], 5)
ZerosPolesGainContinuous(
array([1, 2]),
array([3, 4]),
5,
dt: None
)

Nc           	      t    [        [        U R                  U R                  U R                  4UUUS9SS SU06$ )z
Returns the discretized `ZerosPolesGain` system.

Parameters: See `cont2discrete` for details.

Returns
-------
sys: instance of `dlti` and `ZerosPolesGain`
r   Nr   rI   )r!   r   r   rT   r  r   s       r:   r   $ZerosPolesGainContinuous.to_discretem  sM     DJJ

DII>"(!&( ),-
  	r<   r   r   r   r   r<   r:   rs   rs   9  s    1fr<   rs   c                       \ rS rSrSrSrg)r   i  aT  
Discrete-time Linear Time Invariant system in zeros, poles, gain form.

Represents the system as the discrete-time transfer function
:math:`H(z)=k \prod_i (z - q[i]) / \prod_j (z - p[j])`, where :math:`k` is
the `gain`, :math:`q` are the `zeros` and :math:`p` are the `poles`.
Discrete-time `ZerosPolesGain` systems inherit additional functionality
from the `dlti` class.

Parameters
----------
*system : arguments
    The `ZerosPolesGain` class can be instantiated with 1 or 3
    arguments. The following gives the number of input arguments and their
    interpretation:

        * 1: `dlti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 3: array_like: (zeros, poles, gain)
dt: float, optional
    Sampling time [s] of the discrete-time systems. Defaults to `True`
    (unspecified sampling time). Must be specified as a keyword argument,
    for example, ``dt=0.1``.

See Also
--------
TransferFunction, StateSpace, dlti
zpk2ss, zpk2tf, zpk2sos

Notes
-----
Changing the value of properties that are not part of the
`ZerosPolesGain` system representation (such as the `A`, `B`, `C`, `D`
state-space matrices) is very inefficient and may lead to numerical
inaccuracies.  It is better to convert to the specific system
representation first. For example, call ``sys = sys.to_ss()`` before
accessing/changing the A, B, C, D system matrices.

Examples
--------
Construct the transfer function
:math:`H(s) = \frac{5(s - 1)(s - 2)}{(s - 3)(s - 4)}`:

>>> from scipy import signal

>>> signal.ZerosPolesGain([1, 2], [3, 4], 5)
ZerosPolesGainContinuous(
array([1, 2]),
array([3, 4]),
5,
dt: None
)

Construct the transfer function
:math:`H(z) = \frac{5(z - 1)(z - 2)}{(z - 3)(z - 4)}` with a sampling time
of 0.1 seconds:

>>> signal.ZerosPolesGain([1, 2], [3, 4], 5, dt=0.1)
ZerosPolesGainDiscrete(
array([1, 2]),
array([3, 4]),
5,
dt: 0.1
)

r   Nr   r   r<   r:   r   r     s    AD 	r<   r   c                   d  ^  \ rS rSrSrSrSrU 4S jrU 4S jrS r	S r
S	 rS
 rS rS rS rS rS rS r\S 5       r\R*                  S 5       r\S 5       r\R*                  S 5       r\S 5       r\R*                  S 5       r\S 5       r\R*                  S 5       rS rS rS rS rSrU =r$ )r"   i  aR  
Linear Time Invariant system in state-space form.

Represents the system as the continuous-time, first order differential
equation :math:`\dot{x} = A x + B u` or the discrete-time difference
equation :math:`x[k+1] = A x[k] + B u[k]`. `StateSpace` systems
inherit additional functionality from the `lti`, respectively the `dlti`
classes, depending on which system representation is used.

Parameters
----------
*system: arguments
    The `StateSpace` class can be instantiated with 1 or 4 arguments.
    The following gives the number of input arguments and their
    interpretation:

        * 1: `lti` or `dlti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 4: array_like: (A, B, C, D)
dt: float, optional
    Sampling time [s] of the discrete-time systems. Defaults to `None`
    (continuous-time). Must be specified as a keyword argument, for
    example, ``dt=0.1``.

See Also
--------
TransferFunction, ZerosPolesGain, lti, dlti
ss2zpk, ss2tf, zpk2sos

Notes
-----
Changing the value of properties that are not part of the
`StateSpace` system representation (such as `zeros` or `poles`) is very
inefficient and may lead to numerical inaccuracies.  It is better to
convert to the specific system representation first. For example, call
``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.

Examples
--------
>>> from scipy import signal
>>> import numpy as np
>>> a = np.array([[0, 1], [0, 0]])
>>> b = np.array([[0], [1]])
>>> c = np.array([[1, 0]])
>>> d = np.array([[0]])

>>> sys = signal.StateSpace(a, b, c, d)
>>> print(sys)
StateSpaceContinuous(
array([[0, 1],
       [0, 0]]),
array([[0],
       [1]]),
array([[1, 0]]),
array([[0]]),
dt: None
)

>>> sys.to_discrete(0.1)
StateSpaceDiscrete(
array([[1. , 0.1],
       [0. , 1. ]]),
array([[0.005],
       [0.1  ]]),
array([[1, 0]]),
array([[0]]),
dt: 0.1
)

>>> a = np.array([[1, 0.1], [0, 1]])
>>> b = np.array([[0.005], [0.1]])

>>> signal.StateSpace(a, b, c, d, dt=0.1)
StateSpaceDiscrete(
array([[1. , 0.1],
       [0. , 1. ]]),
array([[0.005],
       [0.1  ]]),
array([[1, 0]]),
array([[0]]),
dt: 0.1
)

g      Y@Nc                 D  > [        U5      S:X  a+  [        US   [        5      (       a  US   R                  5       $ U [        L aN  UR                  S5      c  [        R                  " [        /UQ70 UD6$ [        R                  " [        /UQ70 UD6$ [        TU ]  U 5      $ )z4Create new StateSpace object and settle inheritance.r   r   rI   )
rq   rW   r/   rX   r"   r   rt   r4   r   r3   r5   s      r:   r4   StateSpace.__new__  s     v;!
6!96I J J!9??$$ *zz$'+334H G5;G?EG G *112D E39E=CE E ws##r<   c                    > [        US   [        5      (       a  g[        TU ]  " S0 UD6  SU l        SU l        SU l        SU l        [        U6 u  U l	        U l
        U l        U l        g)z+Initialize the state space lti/dlti system.r   Nr   )rW   r/   r3   r@   _A_B_C_Dr   ABCDr   s      r:   r@   StateSpace.__init__1  sc     fQi!455 	"6")7)@&r<   c                    U R                   R                   S[        U R                  5       S[        U R                  5       S[        U R
                  5       S[        U R                  5       S[        U R                  5       S3$ )z1Return representation of the `StateSpace` system.r   r   r   r   )r9   rb   r   r%  r&  r'  r(  rI   rH   s    r:   r   StateSpace.__repr__A  sn     ~~&&'sDFF|nCDFF|nCDFF|nCDFF|n =/&	
r<   c           	      ~    [        U[        [        R                  [        [
        [        R                  [        45      $ r   )rW   r"   r   ndarrayfloatcomplexnumberintrD   others     r:   _check_binop_otherStateSpace._check_binop_otherL  s+    %*bjj%"$))S"2 3 	3r<   c           
         U R                  U5      (       d  [        $ [        U[        5      (       Ga  [	        U5      [	        U 5      La  [        $ U R
                  UR
                  :w  a  [        S5      eU R                  R                  S   nUR                  R                  S   n[        R                  " [        R                  " U R                  [        R                  " U R                  UR                  5      45      [        R                  " [        X245      UR                  45      45      n[        R                  " [        R                  " U R                  UR                   5      UR                  45      n[        R                  " U R                  [        R                  " U R                   UR                  5      45      n[        R                  " U R                   UR                   5      nOZU R                  n[        R                  " U R                  U5      nU R                  n[        R                  " U R                   U5      n[        R"                  " UR$                  UR$                  UR$                  UR$                  5      n[        [        R&                  " XHS9[        R&                  " XXS9[        R&                  " XhS9[        R&                  " XxS940 U R(                  D6$ )a  
Post-multiply another system or a scalar

Handles multiplication of systems in the sense of a frequency domain
multiplication. That means, given two systems E1(s) and E2(s), their
multiplication, H(s) = E1(s) * E2(s), means that applying H(s) to U(s)
is equivalent to first applying E2(s), and then E1(s).

Notes
-----
For SISO systems the order of system application does not matter.
However, for MIMO systems, where the two systems are matrices, the
order above ensures standard Matrix multiplication rules apply.
z,Cannot multiply systems with different `dt`.r   dtype)r4  NotImplementedrW   r"   typerI   	TypeErrorr%  r   r   vstackr   r   r&  r'  r   r(  result_typer8  r   rM   )	rD   r3  n1n2abcdcommon_dtypes	            r:   __mul__StateSpace.__mul__P  s    &&u--!!eZ((E{$t*,%%ww%((" NOOaBq!B 		299dffbffTVVUWW.E%FG99eRHouww%?@B CA		266$&&%''2EGG<=A		466266$&&%''#:;<Atvvuww'A Atvvu%AAtvvu%A~~aggqwwI"**Q;**Q;**Q;**Q;+ !MM	+ 	+r<   c           
      X   U R                  U5      (       a  [        U[        5      (       a  [        $ U R                  nU R
                  n[        R                  " XR                  5      n[        R                  " XR                  5      n[        R                  " UR                  UR                  UR                  UR                  5      n[        [        R                  " X&S9[        R                  " X6S9[        R                  " XFS9[        R                  " XVS940 U R                  D6$ )z4Pre-multiply a scalar or matrix (but not StateSpace)r7  )r4  rW   r"   r9  r%  r&  r   r   r'  r(  r=  r8  r   rM   rD   r3  r@  rA  rB  rC  rD  s          r:   __rmul__StateSpace.__rmul__  s    &&u--E:1N1N!! FFFFFF5&&!FF5&&!~~aggqwwI"**Q;**Q;**Q;**Q;+ !MM	+ 	+r<   c                     [        U R                  U R                  U R                  * U R                  * 40 U R
                  D6$ )z8Negate the system (equivalent to pre-multiplying by -1).)r"   r%  r&  r'  r(  rM   rH   s    r:   __neg__StateSpace.__neg__  s0    $&&$&&466'DFF7LdmmLLr<   c           
         U R                  U5      (       d  [        $ [        U[        5      (       a  [	        U5      [	        U 5      La#  [        S[	        U 5       S[	        U5       35      eU R                  UR                  :w  a  [        S5      e[        R                  " U R                  UR                  5      n[        R                  " U R                  UR                  45      n[        R                  " U R                  UR                  45      nU R                  UR                  -   nO[        R                   " U5      nU R                  R"                  UR"                  :X  a4  U R                  nU R                  nU R                  nU R                  U-   nO0[%        SU R                  R"                   SUR"                   S35      e[        R&                  " UR(                  UR(                  UR(                  UR(                  5      n[        [        R*                  " X&S9[        R*                  " X6S9[        R*                  " XFS9[        R*                  " XVS940 U R,                  D6$ )z=
Adds two systems in the sense of frequency domain addition.
zCannot add z and z'Cannot add systems with different `dt`.z1Cannot add systems with incompatible dimensions ()r7  )r4  r9  rW   r"   r:  r;  rI   r   
block_diagr%  r   r<  r&  r   r'  r(  
atleast_2dr   ru   r=  r8  r   rM   rH  s          r:   __add__StateSpace.__add__  s    &&u--!!eZ((E{$t*,+d4j\tE{m LMMww%((" IJJ !!$&&%''2A		466577+,A		466577+,A AMM%(Evv||u{{*FFFFFFFFUN  "004~U5;;-q"R S S ~~aggqwwI"**Q;**Q;**Q;**Q;+ !MM	+ 	+r<   c                 ^    U R                  U5      (       d  [        $ U R                  U* 5      $ r   r4  r9  rR  r2  s     r:   __sub__StateSpace.__sub__  s*    &&u--!!||UF##r<   c                 \    U R                  U5      (       d  [        $ U R                  U5      $ r   rU  r2  s     r:   __radd__StateSpace.__radd__  s(    &&u--!!||E""r<   c                 ^    U R                  U5      (       d  [        $ U * R                  U5      $ r   rU  r2  s     r:   __rsub__StateSpace.__rsub__  s*    &&u--!!u%%r<   c                     U R                  U5      (       a  [        U[        5      (       a  [        $ [        U[        R
                  5      (       a  UR                  S:  a  [        S5      eU R                  SU-  5      $ )z
Divide by a scalar
r   z3Cannot divide StateSpace by non-scalar numpy arraysr   )	r4  rW   r"   r9  r   r-  ndimru   rE  r2  s     r:   __truediv__StateSpace.__truediv__  sa    
 &&u--E:1N1N!!eRZZ((UZZ!^RSS||AeG$$r<   c                     U R                   $ )z(State matrix of the `StateSpace` system.)r!  rH   s    r:   r%  StateSpace.A       wwr<   c                 $    [        U5      U l        g r   )r   r!  )rD   r%  s     r:   r%  rc        %a(r<   c                     U R                   $ )z(Input matrix of the `StateSpace` system.)r"  rH   s    r:   r&  StateSpace.B  rd  r<   c                 `    [        U5      U l        U R                  R                  S   U l        g )Nr   )r   r"  r&  r   rA   )rD   r&  s     r:   r&  rh    s"    %a(ffll2&r<   c                     U R                   $ )z)Output matrix of the `StateSpace` system.)r#  rH   s    r:   r'  StateSpace.C  rd  r<   c                 `    [        U5      U l        U R                  R                  S   U l        g )Nr   )r   r#  r'  r   rB   )rD   r'  s     r:   r'  rk    s!    %a(vv||Ar<   c                     U R                   $ )z.Feedthrough matrix of the `StateSpace` system.)r$  rH   s    r:   r(  StateSpace.D  rd  r<   c                 $    [        U5      U l        g r   )r   r$  )rD   r(  s     r:   r(  rn    rf  r<   c                     UR                   U l         UR                  U l        UR                  U l        UR                  U l        g)z
Copy the parameters of another `StateSpace` system.

Parameters
----------
system : instance of `StateSpace`
    The state-space system that is to be copied

N)r%  r&  r'  r(  r   s     r:   r   StateSpace._copy  s0     r<   c                     [        [        U R                  U R                  U R                  U R
                  40 UD60 U R                  D6$ )z
Convert system representation to `TransferFunction`.

Parameters
----------
kwargs : dict, optional
    Additional keywords passed to `ss2zpk`

Returns
-------
sys : instance of `TransferFunction`
    Transfer function of the current system

)r    r   r!  r"  r#  r$  rM   rD   r8   s     r:   r_   StateSpace.to_tf(  sG      tww$'' "1)/"1 C48MMC 	Cr<   c                     [        [        U R                  U R                  U R                  U R
                  40 UD60 U R                  D6$ )z
Convert system representation to `ZerosPolesGain`.

Parameters
----------
kwargs : dict, optional
    Additional keywords passed to `ss2zpk`

Returns
-------
sys : instance of `ZerosPolesGain`
    Zeros, poles, gain representation of the current system

)r!   r   r!  r"  r#  r$  rM   rs  s     r:   rP   StateSpace.to_zpk:  sG     vdggtww  0(. 0 B37==B 	Br<   c                 .    [         R                  " U 5      $ )z
Return a copy of the current `StateSpace` system.

Returns
-------
sys : instance of `StateSpace`
    The current system (copy)

r   rH   s    r:   rX   StateSpace.to_ssL  r   r<   )
r%  r&  r'  r(  r!  r"  r#  r$  rA   rB   )rb   rc   rd   re   r   __array_priority____array_ufunc__r4   r@   r   r4  rE  rI  rL  rR  rV  rY  r\  r`  rf   r%  r   r&  r'  r(  r   r_   rP   rX   rg   rh   ri   s   @r:   r"   r"     s"   Sl O$$A 	
3:+x+$M/+b$#&%   XX) )   XX' '   XX' '   XX) )C$B$
# 
#r<   r"   c                   "    \ rS rSrSrSS jrSrg)rt   iY  ax  
Continuous-time Linear Time Invariant system in state-space form.

Represents the system as the continuous-time, first order differential
equation :math:`\dot{x} = A x + B u`.
Continuous-time `StateSpace` systems inherit additional functionality
from the `lti` class.

Parameters
----------
*system: arguments
    The `StateSpace` class can be instantiated with 1 or 3 arguments.
    The following gives the number of input arguments and their
    interpretation:

        * 1: `lti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 4: array_like: (A, B, C, D)

See Also
--------
TransferFunction, ZerosPolesGain, lti
ss2zpk, ss2tf, zpk2sos

Notes
-----
Changing the value of properties that are not part of the
`StateSpace` system representation (such as `zeros` or `poles`) is very
inefficient and may lead to numerical inaccuracies.  It is better to
convert to the specific system representation first. For example, call
``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.

Examples
--------
>>> import numpy as np
>>> from scipy import signal

>>> a = np.array([[0, 1], [0, 0]])
>>> b = np.array([[0], [1]])
>>> c = np.array([[1, 0]])
>>> d = np.array([[0]])

>>> sys = signal.StateSpace(a, b, c, d)
>>> print(sys)
StateSpaceContinuous(
array([[0, 1],
       [0, 0]]),
array([[0],
       [1]]),
array([[1, 0]]),
array([[0]]),
dt: None
)

Nc           	          [        [        U R                  U R                  U R                  U R
                  4UUUS9SS SU06$ )z
Returns the discretized `StateSpace` system.

Parameters: See `cont2discrete` for details.

Returns
-------
sys: instance of `dlti` and `StateSpace`
r   Nr   rI   )r"   r   r%  r&  r'  r(  r   s       r:   r    StateSpaceContinuous.to_discrete  sR     =$&&$&&$&&$&&)I)+06/46 7:r; !  	! 	!r<   r   r   r   r   r<   r:   rt   rt   Y  s    6p!r<   rt   c                       \ rS rSrSrSrg)r   i  a-  
Discrete-time Linear Time Invariant system in state-space form.

Represents the system as the discrete-time difference equation
:math:`x[k+1] = A x[k] + B u[k]`.
`StateSpace` systems inherit additional functionality from the `dlti`
class.

Parameters
----------
*system: arguments
    The `StateSpace` class can be instantiated with 1 or 3 arguments.
    The following gives the number of input arguments and their
    interpretation:

        * 1: `dlti` system: (`StateSpace`, `TransferFunction` or
          `ZerosPolesGain`)
        * 4: array_like: (A, B, C, D)
dt: float, optional
    Sampling time [s] of the discrete-time systems. Defaults to `True`
    (unspecified sampling time). Must be specified as a keyword argument,
    for example, ``dt=0.1``.

See Also
--------
TransferFunction, ZerosPolesGain, dlti
ss2zpk, ss2tf, zpk2sos

Notes
-----
Changing the value of properties that are not part of the
`StateSpace` system representation (such as `zeros` or `poles`) is very
inefficient and may lead to numerical inaccuracies.  It is better to
convert to the specific system representation first. For example, call
``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.

Examples
--------
>>> import numpy as np
>>> from scipy import signal

>>> a = np.array([[1, 0.1], [0, 1]])
>>> b = np.array([[0.005], [0.1]])
>>> c = np.array([[1, 0]])
>>> d = np.array([[0]])

>>> signal.StateSpace(a, b, c, d, dt=0.1)
StateSpaceDiscrete(
array([[ 1. ,  0.1],
       [ 0. ,  1. ]]),
array([[ 0.005],
       [ 0.1  ]]),
array([[1, 0]]),
array([[0]]),
dt: 0.1
)

r   Nr   r   r<   r:   r   r     s    9t 	r<   r   c           
      
   [        U [        5      (       a  U R                  5       nO6[        U [        5      (       a  [	        S5      e[        U 6 R                  5       n[        U5      n[        UR                  5      S:w  a  [        S5      e[        [        R                  UR                  UR                  UR                  UR                  45      u  pgpUR                  S   n
UR                  S   nUR                   nUc  [#        XR                  R$                  5      n[        R&                  " X4UR                  R$                  5      nUS   S:X  a  X=S'   OGUS   S:  a3  [)        U[*        R,                  " [/        U5      US   -  5      5      US'   O[        S5      eUSL =(       dD    [        U[0        [2        45      =(       a    US:H  =(       d    [        R4                  " U5      (       + nUS:X  aE  [7        XR8                  -  5      nU(       d  U[7        XR8                  -  5      -  nX/[7        U5      4$ US   US   -
  n[        R:                  " [        R<                  " U5      U5      (       d  [        S5      eU(       ah  [*        R,                  " UR8                  U-  5      n[?        SU5       H  nUUS-
     U-  UU'   M     [7        XR8                  -  5      nX/[7        U5      4$ [        U5      nUR@                  S:X  a  USS2[        RB                  4   nUR                  S   U:w  a  [        S	5      eUR                  S   U:w  a  [        S
5      eU(       d  [        RD                  " [        RF                  " UU-  UU-  /5      [        R"                  " XU-   45      /5      n[*        R,                  " UR8                  5      nUSU
2SU
24   nUU
S2SU
24   n[?        SU5       H  nUUS-
     U-  UUS-
     U-  -   UU'   M     GO&[        RD                  " [        RF                  " UU-  UU-  [        R"                  " X45      /5      [        RF                  " [        R"                  " XU-   45      [        RH                  " U5      /5      [        R"                  " XSU-  -   45      /5      n[*        R,                  " UR8                  5      nUSU
2SU
24   nUX-   S2SU
24   nUXU-   2SU
24   U-
  n[?        SU5       H&  nUUS-
     U-  UUS-
     U-  -   UU   U-  -   UU'   M(     [7        XR8                  -  5      [7        XR8                  -  5      -   nX/[7        U5      4$ )a$  
Simulate output of a continuous-time linear system.

Parameters
----------
system : an instance of the LTI class or a tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

    * 1: (instance of `lti`)
    * 2: (num, den)
    * 3: (zeros, poles, gain)
    * 4: (A, B, C, D)

U : array_like
    An input array describing the input at each time `T`
    (interpolation is assumed between given times).  If there are
    multiple inputs, then each column of the rank-2 array
    represents an input.  If U = 0 or None, a zero input is used.
T : array_like
    The time steps at which the input is defined and at which the
    output is desired.  Must be nonnegative, increasing, and equally spaced.
X0 : array_like, optional
    The initial conditions on the state vector (zero by default).
interp : bool, optional
    Whether to use linear (True, the default) or zero-order-hold (False)
    interpolation for the input array.

Returns
-------
T : 1D ndarray
    Time values for the output.
yout : 1D ndarray
    System response.
xout : ndarray
    Time evolution of the state vector.

Notes
-----
If (num, den) is passed in for ``system``, coefficients for both the
numerator and denominator should be specified in descending exponent
order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

Examples
--------
We'll use `lsim` to simulate an analog Bessel filter applied to
a signal.

>>> import numpy as np
>>> from scipy.signal import bessel, lsim
>>> import matplotlib.pyplot as plt

Create a low-pass Bessel filter with a cutoff of 12 Hz.

>>> b, a = bessel(N=5, Wn=2*np.pi*12, btype='lowpass', analog=True)

Generate data to which the filter is applied.

>>> t = np.linspace(0, 1.25, 500, endpoint=False)

The input signal is the sum of three sinusoidal curves, with
frequencies 4 Hz, 40 Hz, and 80 Hz.  The filter should mostly
eliminate the 40 Hz and 80 Hz components, leaving just the 4 Hz signal.

>>> u = (np.cos(2*np.pi*4*t) + 0.6*np.sin(2*np.pi*40*t) +
...      0.5*np.cos(2*np.pi*80*t))

Simulate the filter with `lsim`.

>>> tout, yout, xout = lsim((b, a), U=u, T=t)

Plot the result.

>>> plt.plot(t, u, 'r', alpha=0.5, linewidth=1, label='input')
>>> plt.plot(tout, yout, 'k', linewidth=1.5, label='output')
>>> plt.legend(loc='best', shadow=True, framealpha=1)
>>> plt.grid(alpha=0.3)
>>> plt.xlabel('t')
>>> plt.show()

In a second example, we simulate a double integrator ``y'' = u``, with
a constant input ``u = 1``.  We'll use the state space representation
of the integrator.

>>> from scipy.signal import lti
>>> A = np.array([[0.0, 1.0], [0.0, 0.0]])
>>> B = np.array([[0.0], [1.0]])
>>> C = np.array([[1.0, 0.0]])
>>> D = 0.0
>>> system = lti(A, B, C, D)

`t` and `u` define the time and input signal for the system to
be simulated.

>>> t = np.linspace(0, 5, num=50)
>>> u = np.ones_like(t)

Compute the simulation, and then plot `y`.  As expected, the plot shows
the curve ``y = 0.5*t**2``.

>>> tout, y, x = lsim(system, u, t)
>>> plt.plot(t, y)
>>> plt.grid(alpha=0.3)
>>> plt.xlabel('t')
>>> plt.show()

z3lsim can only be used with continuous-time systems.r   zT must be a rank-1 array.r   Nz Initial time must be nonnegative        z"Time steps are not equally spaced.z5U must have the same number of rows as elements in T.z(System does not define that many inputs.rn   )%rW   r   rY   r   AttributeErrorr   rq   r   ru   mapr   r   r%  r&  r'  r(  sizer   r8  emptyr   r   expmr   r1  r.  anyr   r}   allcloser   ranger_  newaxisr<  r   identity)r7   r   r}   r|   interpsysr%  r&  r'  r(  n_statesn_inputsn_stepsxoutno_inputyoutrI   expAT_dtiMexpMTAdBdBd1Bd0s                            r:   r#   r#     s   X &#mmo	FD	!	! ( ) 	) 6l!!#1A
177|q455RZZ#%%suu!=>JA!wwqzHwwqzHffG	z8UU[[)88W'5DtqyQ	
1b&++ilQqT&9:;Q;<<T	 AU|,8bFF1I  !|tccz"GAG$$D%%	
1!B;;rwwqz2&&=>> ;;qssRx(q'"A1Q3i(*DG #tccz"%% 	1Avv{amwwqzW - . 	. 	wwqzXCDD IIryy!b&!b&!12xxh+> ?@B C ACC 9H9ixi'(89ixi'(q'"A1Q3i"nq1v{2DG # IIryy!b&!b&"$((H+?"@"B Cyy"((H6I+J"K"$++h"7"9 :xxa(l+B CD	F G
 ACC 9H9ixi'(H%&		12H00)8);<sBq'"A1Q3i"nq1v|3adSj@DG # 4##:SS!11DGDM!!r<   c                     [         R                  " U 5      n[        [        [	        U5      5      5      nUS:X  a  SnSU-  n[        SSU-  U5      nU$ )a  Compute a reasonable set of time samples for the response time.

This function is used by `impulse` and `step`  to compute the response time
when the `T` argument to the function is None.

Parameters
----------
A : array_like
    The system matrix, which is square.
n : int
    The number of time samples to generate.

Returns
-------
t : ndarray
    The 1-D array of length `n` of time samples at which the response
    is to be computed.
r        ?   )r   eigvalsminabsr   r   )r%  r   valsrtcr   s         r:   _default_response_timesr    sP    , >>!DCT
OACx	qBa"fa AHr<   c                    [        U [        5      (       a  U R                  5       nO6[        U [        5      (       a  [	        S5      e[        U 6 R                  5       nUc  [        UR                  5      nO[        UR                  U-   5      nUc  SnUc  [        UR                  U5      nO[        U5      n[        USX%SS9u  pgnX'4$ )a  Impulse response of continuous-time system.

Parameters
----------
system : an instance of the LTI class or a tuple of array_like
    describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

        * 1 (instance of `lti`)
        * 2 (num, den)
        * 3 (zeros, poles, gain)
        * 4 (A, B, C, D)

X0 : array_like, optional
    Initial state-vector.  Defaults to zero.
T : array_like, optional
    Time points.  Computed if not given.
N : int, optional
    The number of time points to compute (if `T` is not given).

Returns
-------
T : ndarray
    A 1-D array of time points.
yout : ndarray
    A 1-D array containing the impulse response of the system (except for
    singularities at zero).

Notes
-----
If (num, den) is passed in for ``system``, coefficients for both the
numerator and denominator should be specified in descending exponent
order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

Examples
--------
Compute the impulse response of a second order system with a repeated
root: ``x''(t) + 2*x'(t) + x(t) = u(t)``

>>> from scipy import signal
>>> system = ([1.0], [1.0, 2.0, 1.0])
>>> t, y = signal.impulse(system)
>>> import matplotlib.pyplot as plt
>>> plt.plot(t, y)

z6impulse can only be used with continuous-time systems.r   r  F)r  )rW   r   rY   r   r  r   r&  r  r%  r   r#   )r7   r|   r}   rv   r  X_hs           r:   r$   r$     s    ` &#mmo	FD	!	! ( ) 	) 6l!!#	zCEENCEEBJyy#CEE1-AJ3A/GA!4Kr<   c                    [        U [        5      (       a  U R                  5       nO6[        U [        5      (       a  [	        S5      e[        U 6 R                  5       nUc  SnUc  [        UR                  U5      nO[        U5      n[        UR                  UR                  R                  5      n[        XEX!SS9nUS   US   4$ )a  Step response of continuous-time system.

Parameters
----------
system : an instance of the LTI class or a tuple of array_like
    describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

        * 1 (instance of `lti`)
        * 2 (num, den)
        * 3 (zeros, poles, gain)
        * 4 (A, B, C, D)

X0 : array_like, optional
    Initial state-vector (default is zero).
T : array_like, optional
    Time points (computed if not given).
N : int, optional
    Number of time points to compute if `T` is not given.

Returns
-------
T : 1D ndarray
    Output time points.
yout : 1D ndarray
    Step response of system.


Notes
-----
If (num, den) is passed in for ``system``, coefficients for both the
numerator and denominator should be specified in descending exponent
order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

Examples
--------
>>> from scipy import signal
>>> import matplotlib.pyplot as plt
>>> lti = signal.lti([1.0], [1.0, 1.0])
>>> t, y = signal.step(lti)
>>> plt.plot(t, y)
>>> plt.xlabel('Time [s]')
>>> plt.ylabel('Amplitude')
>>> plt.title('Step response for 1. Order Lowpass')
>>> plt.grid()

z3step can only be used with continuous-time systems.r   F)r|   r  r   r   )rW   r   rY   r   r  r  r%  r   r   r   r8  r#   )r7   r|   r}   rv   r  r   r  s          r:   r%   r%     s    b &#mmo	FD	!	! ( ) 	) 6l!!#yy#CEE1-AJQWWceekk"A/D7DGr<   c                    [        XUS9u  pS[        R                  " [        U5      5      -  n[        R                  " [        R
                  " UR                  UR                  5      5      S-  [        R                  -  nXU4$ )a  
Calculate Bode magnitude and phase data of a continuous-time system.

Parameters
----------
system : an instance of the LTI class or a tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

        * 1 (instance of `lti`)
        * 2 (num, den)
        * 3 (zeros, poles, gain)
        * 4 (A, B, C, D)

w : array_like, optional
    Array of frequencies (in rad/s). Magnitude and phase data is calculated
    for every value in this array. If not given a reasonable set will be
    calculated.
n : int, optional
    Number of frequency points to compute if `w` is not given. The `n`
    frequencies are logarithmically spaced in an interval chosen to
    include the influence of the poles and zeros of the system.

Returns
-------
w : 1D ndarray
    Frequency array [rad/s]
mag : 1D ndarray
    Magnitude array [dB]
phase : 1D ndarray
    Phase array [deg]

Notes
-----
If (num, den) is passed in for ``system``, coefficients for both the
numerator and denominator should be specified in descending exponent
order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

.. versionadded:: 0.11.0

Examples
--------
>>> from scipy import signal
>>> import matplotlib.pyplot as plt

>>> sys = signal.TransferFunction([1], [1, 1])
>>> w, mag, phase = signal.bode(sys)

>>> plt.figure()
>>> plt.semilogx(w, mag)    # Bode magnitude plot
>>> plt.figure()
>>> plt.semilogx(w, phase)  # Bode phase plot
>>> plt.show()

r         4@g     f@)	r'   r   log10r  unwraparctan2imagr   pi)r7   r   r   ymagphases         r:   r&   r&   `  sc    p F1%DA
#a&!
!CIIbjj01E9BEEAE5=r<   c                 t   [        U [        5      (       a/  [        U [        [        45      (       a  U nOGU R	                  5       nO6[        U [
        5      (       a  [        S5      e[        U 6 R	                  5       nUR                  S:w  d  UR                  S:w  a  [        S5      eUb  UnOUn[        U[        5      (       a2  [        UR                  R                  5       UR                  US9u  pX4$ [        U[        5      (       a,  [        UR                  UR                   UR"                  US9u  pUW4$ )a  Calculate the frequency response of a continuous-time system.

Parameters
----------
system : an instance of the `lti` class or a tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

        * 1 (instance of `lti`)
        * 2 (num, den)
        * 3 (zeros, poles, gain)
        * 4 (A, B, C, D)

w : array_like, optional
    Array of frequencies (in rad/s). Magnitude and phase data is
    calculated for every value in this array. If not given, a reasonable
    set will be calculated.
n : int, optional
    Number of frequency points to compute if `w` is not given. The `n`
    frequencies are logarithmically spaced in an interval chosen to
    include the influence of the poles and zeros of the system.

Returns
-------
w : 1D ndarray
    Frequency array [rad/s]
H : 1D ndarray
    Array of complex magnitude values

Notes
-----
If (num, den) is passed in for ``system``, coefficients for both the
numerator and denominator should be specified in descending exponent
order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

Examples
--------
Generating the Nyquist plot of a transfer function

>>> from scipy import signal
>>> import matplotlib.pyplot as plt

Construct the transfer function :math:`H(s) = \frac{5}{(s-1)^3}`:

>>> s1 = signal.ZerosPolesGain([], [1, 1, 1], [5])

>>> w, H = signal.freqresp(s1)

>>> plt.figure()
>>> plt.plot(H.real, H.imag, "b")
>>> plt.plot(H.real, -H.imag, "r")
>>> plt.show()
z7freqresp can only be used with continuous-time systems.r   z@freqresp() requires a SISO (single input, single output) system.)worN)rW   r   r    r!   r\   r   r  rA   rB   ru   r
   r   ravelr   r   r   rT   r  )r7   r   r   r  r  r  s         r:   r'   r'     s   l &#f/@AAC.."C	FD	!	! ( ) 	) 6l""$
zzQ#++* + , 	, 	}#'(( SWW]]_cggD9
 4K 
C	(	(CIIsxxdCa4Kr<   c                       \ rS rSrS rSrg)Bunchi  c                 :    U R                   R                  U5        g r   )__dict__update)rD   kwdss     r:   r@   Bunch.__init__  s    T"r<   r   N)rb   rc   rd   re   r@   rg   r   r<   r:   r  r    s    #r<   r  c                    [         R                  " U5      nUR                  S:  a  [        S5      e[	        U5      nU R                  S:  a  [        S5      eUR                  S:  a  [        S5      eU R
                  S   U R
                  S   :w  a  [        S5      e[        U5      U R
                  S   :  a&  [        SU R
                  S   [        U5      4-  5      e[        U5      U R
                  S   :  a&  [        S	[        U5      U R
                  S   4-  5      e[         R                  R                  U5      nU H  n[        Xr:H  5      U:  d  M  [        S
5      e   [        nUS;  a  [        S5      eUS:X  a5  [        n[        [         R                  " U5      5      (       d  [        S5      eUS:  a  [        S5      eUS:  a  [        S5      eX4$ )z
Check the poles come in complex conjugate pairs
Check shapes of A, B and poles are compatible.
Check the method chosen is compatible with provided poles
Return update method to use and ordered poles

r   zPoles must be a 1D array like.rn   zA must be a 2D array/matrix.zB must be a 2D array/matrixr   zA must be squarez2maximum number of poles is %d but you asked for %dz/number of poles is %d but you should provide %dzFat least one of the requested pole is repeated more than rank(B) times)KNV0YTz0The method keyword must be one of 'YT' or 'KNV0'r  z'Complex poles are not supported by KNV0z#maxiter must be at least equal to 1zrtol can not be greater than 1)r   r   r_  ru   _order_complex_polesr   rq   r   matrix_ranksum_YT_loop
_KNV0_loopallisreal)	r%  r&  rT   r   rtolmaxiterr  pupdate_loops	            r:   _valid_inputsr    s    JJuEzzA~9:: 'Evvz788vvz677wwqzQWWQZ+,,
5zAGGAJM''!*c%j12 3 	3
5zAGGAJJe*aggaj12 3 	3
		a Aqz?Q 7 8 8 
 K]"KLL 299U#$$FGG{>?? ax9::r<   c                    [         R                  " U [         R                  " U 5         5      n/ n[         R                  " U [         R                  " U 5      S:     5       HF  n[         R                  " U5      U ;   d  M  UR                  U[         R                  " U5      45        MH     [         R                  " X45      nU R                  S   [        U5      :w  a  [        S5      eU$ )z
Check we have complex conjugates pairs and reorder P according to YT, ie
real_poles, complex_i, conjugate complex_i, ....
The lexicographic sort on the complex poles is added to help the user to
compare sets of poles.
r   z-Complex poles must come with their conjugates)
r   sortr  r  conjextendr   r   rq   ru   )rT   ordered_polesim_polesr  s       r:   r  r  /	  s     GGE"))E"234MHWWU2775>A-./771:OOQ
O, 0 II}78M{{1~]++HIIr<   c                 X   [         R                  " X#SS9n[        USS9u  pg[         R                  " X   X   R                  5      n[         R                  " XSS2S4   5      n	[         R
                  " U	S5      (       d+  U	[         R                  R                  U	5      -  n
XSS2U4'   gg)z
Algorithm "KNV0" Kautsky et Al. Robust pole
assignment in linear state feedback, Int journal of Control
1985, vol 41 p 1129->1155
https://la.epfl.ch/files/content/sites/la/files/
    users/105941/public/KautskyNicholsDooren

r   axisfullmodeNr   r   )r   deletes_qrr   r}   r  r   norm)r&  ker_poletransfer_matrixjrT   transfer_matrix_not_jQR
mat_ker_pjyjxjs              r:   _KNV0r  C	  s     IIoqA %F3DAX[]]3J	
aeH	%B ;;r1		r"" "1 r<   c           
      z   USS2S[         R                  4   nUSS2S[         R                  4   n[         R                  " [         R                  " X   R                  [         R                  " XVR                  5      [         R                  " XeR                  5      -
  5      X   5      n[         R                  R                  U5      u  pn
UR                  SS2SS2[         R                  4   u  pU
SS2SS2[         R                  4   u  p[         R                  " USS2U[         R                  4   USS2U[         R                  4   45      n[         R                  " U	S   U	S   5      (       dK  [         R                  " X   U5      n[         R                  " X   U5      n[         R                  " UU45      nO[         R                  " [         R                  " X   [         R                  " X   R                  5      45      [         R                  " [         R                  " X   R                  5      X   45      45      n[         R                  " [         R                  " X45      [         R                  " X45      45      n[         R                  " UU5      n[         R                  " [         R                  " UUR                  5      U5      n[         R                  " US5      (       d  [         R                  " S5      U-  [         R                  R                  U5      -  nUSUSS2U4   R                  S   2S4   USS2U4'   UUSS2U4   R                  S   S2S4   USS2U4'   gUSUSS2U4   R                  S   2S4   USS2U4'   UUSS2U4   R                  S   S2S4   USS2U4'   g)zE
Applies algorithm from YT section 6.1 page 19 related to real pairs
Nr   rn   r   r   )r   r  r   r}   r   svdr<  r  r   r   r   sqrtr  )r  r  r  r  r  r   vmumsmvmmu1mu2nu1nu2&transfer_matrix_j_mo_transfer_matrix_jker_pole_imo_mu1ker_pole_i_nu1ker_pole_mu_nuker_pole_ijmu_nu_matrixtransfer_matrix_ijs                         r:   _YT_realr  r	  s8   
 	
!R
A	!R
A 	rvvhkmmRVVAss^
q##& !	&A q!JBBttBQB2::%&HC"1"a#$HC .0YYAq"**,-Aq"**,-8/ .0* ;;r!ube$$66(+s3S1$4n#EFii "		8;+-88HK4E4E+F+H !I "		288HK4E4E+F4<K+A !B!" # yyYYz"BIIsj$9: \:~~7G7G HCE;;)1-- ggaj);; iinn-?@A 2+_QT"((++Q.!1 !3AqD!''*+Q.!1 !/+_QT"((++Q.!1 !/AqD!''*+Q.!1r<   c                     [         R                  " S5      USS2S[         R                  4   -  n[         R                  " S5      USS2S[         R                  4   -  nUSU-  -   nX   n[         R                  " [         R                  " [         R                  " UR
                  5      [         R                  " U[         R                  " U5      R
                  5      [         R                  " [         R                  " U5      UR
                  5      -
  5      U5      n	[         R                  R                  U	5      u  p[         R                  " [         R                  " U
5      5      nUSS2US   [         R                  4   nUSS2US   [         R                  4   nUSS2U[         R                  4   SUSS2U[         R                  4   -  -   n[         R                  " [         R                  " XS      5      [         R                  " XS      5      5      (       d  [         R                  " X5      nO.[         R                  " X45      n[         R                  " UU5      n[         R                  " [         R                  " U[         R                  " UR
                  5      5      U5      n[         R                  " US5      (       dk  U[         R                  R                  U5      -  n[         R                  " USS2S4   5      USS2U4'   [         R                  " USS2S4   5      USS2U4'   g[         R                  " USS2S4   5      USS2U4'   [         R                  " USS2S4   5      USS2U4'   g)zH
Applies algorithm from YT section 6.2 page 20 related to complex pairs
rn   Nr  r                 ?r   )r   r  r  r   r  r}   r   eigargsortr  r  r   r  r   r  )r  r  r  r  r  uruir   r  r  e_vale_vec	e_val_idxr  r  r  ker_pole_mumu1_mu2_matrixtransfer_matrix_i_js                      r:   _YT_complexr  	  s   
 
AaRZZ'(	(B	AaRZZ'(	(B
RU
A +K
rvvbggkmm,bffQ
.Evvbggaj!##&/' ()4	6A 99==#LE

266%=)I
9R="**,
-C
9R="**,
-C 	1bjj()
?1a+,,	- + ;;rvvebM23 ffUR=%9:< <ff[.C:.ff[.9&&RWW[]]5K!LDF ;;*A..2IINN./ 0 "(;AqD(A B1 "(;AqD(A B1 !#AqD(9 :1 "AqD(9 :1r<   c                    U[         R                  " U5         R                  S   nUS-  nUS:  a  U/S//nO/ / /n[         R                  " US-   [	        U5      S-   S5      n	[         R                  " SXvS-  -   5      n
US   R                  SU
-  5        US   R                  SU
-  S-   5        US   R                  U	5        US   R                  U	S-   5        [         R                  " SUS-   5      n
US   R                  SU
-  S-
  5        US   R                  SU
-  5        US:X  aF  [         R                  " US   5      (       a(  US   R                  S5        US   R                  S5        US   R                  U	5        US   R                  U	S-   5        [         R                  " SXvS-  -   5      nU HC  n[        SUS-   5       H-  nUS   R                  U5        US   R                  X-   5        M/     ME     US:X  aF  [         R                  " US   5      (       a(  US   R                  S5        US   R                  S5        US   R                  U	5        US   R                  U	S-   5        [         R                  " SXvS-  -   5      nU HT  n[        US-   US-   5       H;  nX-   nX:  a  X-   U-
  nUS   R                  U5        US   R                  U5        M=     MV     US:X  aF  [         R                  " US   5      (       a(  US   R                  S5        US   R                  S5        US   R                  U	5        US   R                  U	S-   5        [        SUS-   5       H-  nUS   R                  U5        US   R                  X-   5        M/     US:X  aF  [         R                  " US   5      (       a(  US   R                  S5        US   R                  S5        US   R                  U	5        US   R                  U	S-   5        [         R                  " U5      R                  S-
  nSnSnUU:  Ga%  U(       Gd  [         R                  " [         R                  R                  U5      5      nU GH  u  pX:X  a@  US:X  d   S5       e[         R                  " X-   5      (       d   S5       e[        X0XU5        MK  [         R                  " XU4SS9n[        USS	9u  nn[         R                  " X-   5      (       a@  [         R                  " X,   5      (       d   S
[!        U5      -   5       e[#        U UXU5        M  [         R                  " X-   5      ) (       d   S
[!        U5      -   5       e[%        U UXU5        GM     [         R&                  " [         R(                  " [         R*                  " S5      5      [         R                  " [         R                  R                  U5      5      45      n[         R                  " UU-
  U-  5      nUU:  a0  U[         R(                  " [         R*                  " S5      5      :  a  SnUS-  nUU:  a
  U(       d  GM  UWU4$ )z
Algorithm "YT" Tits, Yang. Globally Convergent
Algorithms for Robust Pole Assignment by State Feedback
https://hdl.handle.net/1903/5598
The poles P have to be sorted accordingly to section 6.2 page 20

r   rn   r   Fzi!=0 for KNV call in YTzcalling KNV on a complex poler  r  r  z"mixing real and complex in YT_realT)r   r  r   arangerq   r  appendr  arrayr}   r  r   detr  r  r  strr  r  maxr  spacing)r  r  rT   r&  r  r  nb_realhnbupdate_orderr_compr_pr_jr  r  idx_1stopnb_trydet_transfer_matrixbtransfer_matrix_not_i_jr  r  det_transfer_matrixcur_rtols                          r:   r  r  	  s    BIIe$%++A.G
Q,C { 	A3'2wYYwqy#e*Q,2F
))AsQ;
'CO1S5!O1S57#O6"O6!8$
))As1u
CO1S57#O1S5!
axBIIeAh''Qq!Qq!O6"O6!8$
))AsQ;
'Cq#a%AO""1%O""13' ! 
 axBIIeAh''Qq!Qq!O6"O6!8$
))AsQ;
'Cs1ugai(ACEGO""1%O""5) )  axBIIeAh''Qq!Qq!O6"O6!8$1c!e_Qq!Qqu%  axBIIeAh''Qq!Qq!O6"O6!8$88L)++A-LDF
7
4!vvbiimmO&DE DAvAv888vyy**K,KK*a?u=*,))OV9:+<' 3&A199UX&&99UX.. 2 1%'*5z12 2.Xq/a@IIeh// 2 2%'*5z22 2/!_C- !0 !ffbggbjjm&<"$&&)G"H&J K66 !" ! d?2RWWRZZ]5KKD!G 7
44H 6!!r<   c                    SnSnXt:  Ga<  U(       Gd4  [         R                  " [         R                  R                  U5      5      n[	        UR
                  S   5       H  n	[        X0XU5        M     [         R                  " [         R                  " [         R                  " S5      5      [         R                  " [         R                  R                  U5      5      45      n
[         R                  " X-
  U
-  5      nX:  a0  U
[         R                  " [         R                  " S5      5      :  a  SnUS-  nXt:  a
  U(       d  GM4  UWU4$ )zA
Loop over all poles one by one and apply KNV method 0 algorithm
Fr   r   T)
r   r  r   r  r  r   r  r  r  r  )r  r  rT   r&  r  r  r  r  r  r  r  r  s               r:   r  r  a
  s     DF

4!vvbiimmO&DEqwwqz"A!59 # !ffbggbjjm&<"$&&)G"H&J K66.E*+ ,?2RWWRZZ]5KKD! 
44 6!!r<   c           
      x   [        XX#XE5      u  pbSnSn[        USS9u  p[        R                  R	                  U5      nU	SS2SU24   nU	SS2US24   nU
SU2SS24   n
UR
                  S   U:X  GaP  [        R                  " U R
                  5      nSnXR
                  S   :  a  X/   n[        R                  " U5      XU4'   [        R                  " U5      ) (       a`  [        R                  " U5      * XUS-   4'   [        R                  " U5      XS-   US-   4'   [        R                  " U5      XS-   U4'   US-  nUS-  nXR
                  S   :  a  M  [        R                  R                  XU -
  SS9S   n[        R                  " U R
                  S   5      n[        R                  n[        R                  nGO6/ nSn[        UR
                  S   5       GH}  nU(       a  SnM  [        R                  " UR                  XU   [        R                  " UR
                  S   5      -  -
  5      R                  n[        USS9u  nnUSS2UR
                  S   S24   n[        R                   " USS	9SS2[        R"                  4   nU[        R                  R%                  U5      -  n[        R                  " UU   5      ) (       aV  [        R&                  " [        R                  " U5      [        R                  " U5      /5      nUR)                  UU/5        S
nOUR+                  U5        US:X  a  UnGMe  [        R&                  " WU45      nGM     US:  a9  U" UWX!XT5      u  npxU(       d$  US:  a  SU SU S3n[,        R.                  " USS9  WR1                  [2        5      nSnXR
                  S   S-
  :  a  [        R                  " X/   5      ) (       aI  USS2U4   R5                  5       nUSS2US-   4   nUSU-  -
  USS2U4'   USU-  -   USS2US-   4'   US-  nUS-  nXR
                  S   S-
  :  a  M   [        R                  R7                  UR                  [        R                  " [        R8                  " U5      UR                  5      5      R                  n[        R                  R7                  U
[        R                  " UR                  UU -
  5      5      nU* n[        R                  " U5      n[?        5       n!UU!l         [C        [        R                  RE                  U [        R                  " UU5      -
  5      S   5      U!l#        UU!l$        UU!l%        UU!l&        UU!l'        U!$ ! [        R                  R:                   a  n [=        S5      U eSn A ff = f)a|  
Compute K such that eigenvalues (A - dot(B, K))=poles.

K is the gain matrix such as the plant described by the linear system
``AX+BU`` will have its closed-loop poles, i.e the eigenvalues ``A - B*K``,
as close as possible to those asked for in poles.

SISO, MISO and MIMO systems are supported.

Parameters
----------
A, B : ndarray
    State-space representation of linear system ``AX + BU``.
poles : array_like
    Desired real poles and/or complex conjugates poles.
    Complex poles are only supported with ``method="YT"`` (default).
method: {'YT', 'KNV0'}, optional
    Which method to choose to find the gain matrix K. One of:

        - 'YT': Yang Tits
        - 'KNV0': Kautsky, Nichols, Van Dooren update method 0

    See References and Notes for details on the algorithms.
rtol: float, optional
    After each iteration the determinant of the eigenvectors of
    ``A - B*K`` is compared to its previous value, when the relative
    error between these two values becomes lower than `rtol` the algorithm
    stops.  Default is 1e-3.
maxiter: int, optional
    Maximum number of iterations to compute the gain matrix.
    Default is 30.

Returns
-------
full_state_feedback : Bunch object
    full_state_feedback is composed of:
        gain_matrix : 1-D ndarray
            The closed loop matrix K such as the eigenvalues of ``A-BK``
            are as close as possible to the requested poles.
        computed_poles : 1-D ndarray
            The poles corresponding to ``A-BK`` sorted as first the real
            poles in increasing order, then the complex conjugates in
            lexicographic order.
        requested_poles : 1-D ndarray
            The poles the algorithm was asked to place sorted as above,
            they may differ from what was achieved.
        X : 2-D ndarray
            The transfer matrix such as ``X * diag(poles) = (A - B*K)*X``
            (see Notes)
        rtol : float
            The relative tolerance achieved on ``det(X)`` (see Notes).
            `rtol` will be NaN if it is possible to solve the system
            ``diag(poles) = (A - B*K)``, or 0 when the optimization
            algorithms can't do anything i.e when ``B.shape[1] == 1``.
        nb_iter : int
            The number of iterations performed before converging.
            `nb_iter` will be NaN if it is possible to solve the system
            ``diag(poles) = (A - B*K)``, or 0 when the optimization
            algorithms can't do anything i.e when ``B.shape[1] == 1``.

Notes
-----
The Tits and Yang (YT), [2]_ paper is an update of the original Kautsky et
al. (KNV) paper [1]_.  KNV relies on rank-1 updates to find the transfer
matrix X such that ``X * diag(poles) = (A - B*K)*X``, whereas YT uses
rank-2 updates. This yields on average more robust solutions (see [2]_
pp 21-22), furthermore the YT algorithm supports complex poles whereas KNV
does not in its original version.  Only update method 0 proposed by KNV has
been implemented here, hence the name ``'KNV0'``.

KNV extended to complex poles is used in Matlab's ``place`` function, YT is
distributed under a non-free licence by Slicot under the name ``robpole``.
It is unclear and undocumented how KNV0 has been extended to complex poles
(Tits and Yang claim on page 14 of their paper that their method can not be
used to extend KNV to complex poles), therefore only YT supports them in
this implementation.

As the solution to the problem of pole placement is not unique for MIMO
systems, both methods start with a tentative transfer matrix which is
altered in various way to increase its determinant.  Both methods have been
proven to converge to a stable solution, however depending on the way the
initial transfer matrix is chosen they will converge to different
solutions and therefore there is absolutely no guarantee that using
``'KNV0'`` will yield results similar to Matlab's or any other
implementation of these algorithms.

Using the default method ``'YT'`` should be fine in most cases; ``'KNV0'``
is only provided because it is needed by ``'YT'`` in some specific cases.
Furthermore ``'YT'`` gives on average more robust results than ``'KNV0'``
when ``abs(det(X))`` is used as a robustness indicator.

[2]_ is available as a technical report on the following URL:
https://hdl.handle.net/1903/5598

References
----------
.. [1] J. Kautsky, N.K. Nichols and P. van Dooren, "Robust pole assignment
       in linear state feedback", International Journal of Control, Vol. 41
       pp. 1129-1155, 1985.
.. [2] A.L. Tits and Y. Yang, "Globally convergent algorithms for robust
       pole assignment by state feedback", IEEE Transactions on Automatic
       Control, Vol. 41, pp. 1432-1452, 1996.

Examples
--------
A simple example demonstrating real pole placement using both KNV and YT
algorithms.  This is example number 1 from section 4 of the reference KNV
publication ([1]_):

>>> import numpy as np
>>> from scipy import signal
>>> import matplotlib.pyplot as plt

>>> A = np.array([[ 1.380,  -0.2077,  6.715, -5.676  ],
...               [-0.5814, -4.290,   0,      0.6750 ],
...               [ 1.067,   4.273,  -6.654,  5.893  ],
...               [ 0.0480,  4.273,   1.343, -2.104  ]])
>>> B = np.array([[ 0,      5.679 ],
...               [ 1.136,  1.136 ],
...               [ 0,      0,    ],
...               [-3.146,  0     ]])
>>> P = np.array([-0.2, -0.5, -5.0566, -8.6659])

Now compute K with KNV method 0, with the default YT method and with the YT
method while forcing 100 iterations of the algorithm and print some results
after each call.

>>> fsf1 = signal.place_poles(A, B, P, method='KNV0')
>>> fsf1.gain_matrix
array([[ 0.20071427, -0.96665799,  0.24066128, -0.10279785],
       [ 0.50587268,  0.57779091,  0.51795763, -0.41991442]])

>>> fsf2 = signal.place_poles(A, B, P)  # uses YT method
>>> fsf2.computed_poles
array([-8.6659, -5.0566, -0.5   , -0.2   ])

>>> fsf3 = signal.place_poles(A, B, P, rtol=-1, maxiter=100)
>>> fsf3.X
array([[ 0.52072442+0.j, -0.08409372+0.j, -0.56847937+0.j,  0.74823657+0.j],
       [-0.04977751+0.j, -0.80872954+0.j,  0.13566234+0.j, -0.29322906+0.j],
       [-0.82266932+0.j, -0.19168026+0.j, -0.56348322+0.j, -0.43815060+0.j],
       [ 0.22267347+0.j,  0.54967577+0.j, -0.58387806+0.j, -0.40271926+0.j]])

The absolute value of the determinant of X is a good indicator to check the
robustness of the results, both ``'KNV0'`` and ``'YT'`` aim at maximizing
it.  Below a comparison of the robustness of the results above:

>>> abs(np.linalg.det(fsf1.X)) < abs(np.linalg.det(fsf2.X))
True
>>> abs(np.linalg.det(fsf2.X)) < abs(np.linalg.det(fsf3.X))
True

Now a simple example for complex poles:

>>> A = np.array([[ 0,  7/3.,  0,   0   ],
...               [ 0,   0,    0,  7/9. ],
...               [ 0,   0,    0,   0   ],
...               [ 0,   0,    0,   0   ]])
>>> B = np.array([[ 0,  0 ],
...               [ 0,  0 ],
...               [ 1,  0 ],
...               [ 0,  1 ]])
>>> P = np.array([-3, -1, -2-1j, -2+1j]) / 3.
>>> fsf = signal.place_poles(A, B, P, method='YT')

We can plot the desired and computed poles in the complex plane:

>>> t = np.linspace(0, 2*np.pi, 401)
>>> plt.plot(np.cos(t), np.sin(t), 'k--')  # unit circle
>>> plt.plot(fsf.requested_poles.real, fsf.requested_poles.imag,
...          'wo', label='Desired')
>>> plt.plot(fsf.computed_poles.real, fsf.computed_poles.imag, 'bx',
...          label='Placed')
>>> plt.grid()
>>> plt.axis('image')
>>> plt.axis([-1.1, 1.1, -1.1, 1.1])
>>> plt.legend(bbox_to_anchor=(1.05, 1), loc=2, numpoints=1)

r   r  r  Nr   r   )rcondFr  TzSConvergence was not reached after maxiter iterations.
You asked for a tolerance of z	, we got .rn   )
stacklevelr  zfThe poles you've chosen can't be placed. Check the controllability matrix and try another set of poles)(r  r  r   r   r  r   r   r   r  r  lstsqeyenanr  r   r}   r  r  r  r   r  r
  warningswarnastyper/  r   solvediagLinAlgErrorru   r  gain_matrixr  r  computed_polesrequested_polesr  r  nb_iter)"r%  r&  rT   r   r  r  r  r  r.  r   zrankBu0u1
diag_polesidxr  r+  r  r  skip_conjugater  pole_space_jr  r  
ker_pole_jtransfer_matrix_jr  err_msgrelimgr  efull_state_feedbacks"                                     r:   r(   r(   {
  s)   j 'qUDJK HG
 DAII!!!$E	
1fuf9B	
1ef9B	&5&!)A 	wwqzU( XXagg&
KKN"
A#%771:JCx 		!}*,''!*
A:&+-771:
q5#a%<()+
q5#:&q1HC KKN" iiooaARo@C&&,66&&
   qwwqz"A!&66"$$(266!''!*3E*E(EFHHL 62DAq1l003445J$ !#z :1bjj= I!2!#0A!B"C		%(##$&IIrww7H/I/1ww7H/I/K %L!Z 89 "&
+Av"3"$))_>O,P"Qe #h 19&1(O27G'K#D(D1H44868*AO  g!4
 *009KKN1$$		%*%%%af-224%aQh/ +.bf*3',/3J3q5)q1HC KKN1$$	<		 1 1266"''%.:I:K:K4M NNOa ))//!RVVBDD!A#->?K ,K''+&K'&1#)=
		a"&&K001!4*
& +0'+'")) yy$$ 	< 4 5:;<	<s   B)V
 
V9(V44V9c           	      z   [        U [        5      (       a  [        S5      e[        U [        5      (       d  [        U SS SU S   06n [        U [        5      nU R                  5       n [        R                  " U5      nUR                  S:X  a   [        R                  " U5      R                  nUc  [        U5      nUS-
  U R                  -  nO3US   n[        [        R                  " X`R                  -  5      5      S-   n[        R                  " XPR                   R"                  S   45      n[        R                  " XPR$                  R"                  S   45      n[        R&                  " SXeS9n	Uc6  [        R                  " U R                   R"                  S   45      USSS24'   O[        R(                  " U5      USSS24'   Uc  Un
O@[        UR"                  5      S:X  a  USS2[        R*                  4   n[-        X!SS	9" U	5      n
[/        SUS-
  5       H  n[        R0                  " U R                   X{SS24   5      [        R0                  " U R2                  XSS24   5      -   X{S-   SS24'   [        R0                  " U R$                  X{SS24   5      [        R0                  " U R4                  XSS24   5      -   XSS24'   M     [        R0                  " U R$                  XuS-
  SS24   5      [        R0                  " U R4                  XS-
  SS24   5      -   XS-
  SS24'   U(       a  XU4$ X4$ )
a*  
Simulate output of a discrete-time linear system.

Parameters
----------
system : tuple of array_like or instance of `dlti`
    A tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

        * 1: (instance of `dlti`)
        * 3: (num, den, dt)
        * 4: (zeros, poles, gain, dt)
        * 5: (A, B, C, D, dt)

u : array_like
    An input array describing the input at each time `t` (interpolation is
    assumed between given times).  If there are multiple inputs, then each
    column of the rank-2 array represents an input.
t : array_like, optional
    The time steps at which the input is defined.  If `t` is given, it
    must be the same length as `u`, and the final value in `t` determines
    the number of steps returned in the output.
x0 : array_like, optional
    The initial conditions on the state vector (zero by default).

Returns
-------
tout : ndarray
    Time values for the output, as a 1-D array.
yout : ndarray
    System response, as a 1-D array.
xout : ndarray, optional
    Time-evolution of the state-vector.  Only generated if the input is a
    `StateSpace` system.

See Also
--------
lsim, dstep, dimpulse, cont2discrete

Examples
--------
A simple integrator transfer function with a discrete time step of 1.0
could be implemented as:

>>> import numpy as np
>>> from scipy import signal
>>> tf = ([1.0,], [1.0, -1.0], 1.0)
>>> t_in = [0.0, 1.0, 2.0, 3.0]
>>> u = np.asarray([0.0, 0.0, 1.0, 1.0])
>>> t_out, y = signal.dlsim(tf, u, t=t_in)
>>> y.T
array([[ 0.,  0.,  0.,  1.]])

z7dlsim can only be used with discrete-time dlti systems.Nr   rI   r   r   r  )r   )k)rW   r   r  r   r"   rY   r   r   r_  rQ  r}   rq   rI   r1  floorr   r%  r   r'  r   r   r  r   r  r   r&  r(  )r7   r   r   r   is_ss_inputout_samplesstoptimer  r  toutu_dtr  s               r:   r)   r)     s   r &# ( ) 	)%%vcr{2vbz2 VZ0K]]_F
aAvv{MM!y!f!Ovyy0R5"((8ii#789A= 88[((.."345D88[((.."345D;;sH6D 
zXXvxx~~a023QT
ZZ^QT
 	yqww<1!RZZ- A!!!,T2 1kAo&vxxd4vxxd45qS!VffVXXtqDz2ffVXXtqDz23T
 ' !ffVXXtM14D/EF ffVXXtM14D/EFGDQ	 4zr<   c                 D   [        U [        5      (       a  U R                  5       n O?[        U [        5      (       a  [	        S5      e[        U SS SU S   06R                  5       n Uc  SnUc$  [
        R                  " SX0R                  -  USS9nO[
        R                  " U5      nSn[        SU R                  5       HZ  n[
        R                  " UR                  S   U R                  45      nS	USU4'   [        XX!S
9nUc  US   4nOXGS   4-   nUS   nM\     WU4$ )a  
Impulse response of discrete-time system.

Parameters
----------
system : tuple of array_like or instance of `dlti`
    A tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

        * 1: (instance of `dlti`)
        * 3: (num, den, dt)
        * 4: (zeros, poles, gain, dt)
        * 5: (A, B, C, D, dt)

x0 : array_like, optional
    Initial state-vector.  Defaults to zero.
t : array_like, optional
    Time points.  Computed if not given.
n : int, optional
    The number of time points to compute (if `t` is not given).

Returns
-------
tout : ndarray
    Time values for the output, as a 1-D array.
yout : tuple of ndarray
    Impulse response of system.  Each element of the tuple represents
    the output of the system based on an impulse in each input.

See Also
--------
impulse, dstep, dlsim, cont2discrete

Examples
--------
>>> import numpy as np
>>> from scipy import signal
>>> import matplotlib.pyplot as plt

>>> butter = signal.dlti(*signal.butter(3, 0.5))
>>> t, y = signal.dimpulse(butter, n=25)
>>> plt.step(t, np.squeeze(y))
>>> plt.grid()
>>> plt.xlabel('n [samples]')
>>> plt.ylabel('Amplitude')

z:dimpulse can only be used with discrete-time dlti systems.Nr   rI   r   r   Fendpointr  r   r   r   )rW   r   rY   r   r  r   r   rI   r   r  rA   r   r   r)   	r7   r   r   r   r  r  r   
one_outputrD  s	            r:   r+   r+   N  s#   d &$	FC	 	  - . 	. vcr{2vbz299; 	y 	yKK1yy=!e<JJqM D1fmm$HHaggaj&--01!Q$61
<qM#Da=**D!} % :r<   c                    [        U [        5      (       a  U R                  5       n O?[        U [        5      (       a  [	        S5      e[        U SS SU S   06R                  5       n Uc  SnUc$  [
        R                  " SX0R                  -  USS9nO[
        R                  " U5      nSn[        SU R                  5       H~  n[
        R                  " UR                  S   U R                  45      n[
        R                  " UR                  S   45      USS2U4'   [        XX!S	9nUc  US
   4nOXGS
   4-   nUS   nM     WU4$ )a  
Step response of discrete-time system.

Parameters
----------
system : tuple of array_like
    A tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

        * 1: (instance of `dlti`)
        * 3: (num, den, dt)
        * 4: (zeros, poles, gain, dt)
        * 5: (A, B, C, D, dt)

x0 : array_like, optional
    Initial state-vector.  Defaults to zero.
t : array_like, optional
    Time points.  Computed if not given.
n : int, optional
    The number of time points to compute (if `t` is not given).

Returns
-------
tout : ndarray
    Output time points, as a 1-D array.
yout : tuple of ndarray
    Step response of system.  Each element of the tuple represents
    the output of the system based on a step response to each input.

See Also
--------
step, dimpulse, dlsim, cont2discrete

Examples
--------
>>> import numpy as np
>>> from scipy import signal
>>> import matplotlib.pyplot as plt

>>> butter = signal.dlti(*signal.butter(3, 0.5))
>>> t, y = signal.dstep(butter, n=25)
>>> plt.step(t, np.squeeze(y))
>>> plt.grid()
>>> plt.xlabel('n [samples]')
>>> plt.ylabel('Amplitude')
z7dstep can only be used with discrete-time dlti systems.Nr   rI   r   r   FrG  rI  r   )rW   r   rY   r   r  r   r   rI   r   r  rA   r   r   r   r)   rJ  s	            r:   r*   r*     s6   b &$	FC	 	  ( ) 	) vcr{2vbz299; 	y 	yKK1yy=!e<JJqM D1fmm$HHaggaj&--01''1771:-(!Q$61
<qM#Da=**D!} % :r<   c                    [        U [        5      (       d1  [        U [        5      (       a  [        S5      e[        U SS SU S   06n [        U [        5      (       a  U R                  5       n [        U [        [        45      (       d  [        S5      eU R                  S:w  d  U R                  S:w  a  [        S5      eUb  UnOUn[        U [        5      (       aI  [        R                  U R                  R                  5       U R                  5      u  pV[        XVXCS9u  pX4$ [        U [        5      (       a-  [!        U R"                  U R$                  U R&                  UUS9u  pUW4$ )	aE  
Calculate the frequency response of a discrete-time system.

Parameters
----------
system : an instance of the `dlti` class or a tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

        * 1 (instance of `dlti`)
        * 2 (numerator, denominator, dt)
        * 3 (zeros, poles, gain, dt)
        * 4 (A, B, C, D, dt)

w : array_like, optional
    Array of frequencies (in radians/sample). Magnitude and phase data is
    calculated for every value in this array. If not given a reasonable
    set will be calculated.
n : int, optional
    Number of frequency points to compute if `w` is not given. The `n`
    frequencies are logarithmically spaced in an interval chosen to
    include the influence of the poles and zeros of the system.
whole : bool, optional
    Normally, if 'w' is not given, frequencies are computed from 0 to the
    Nyquist frequency, pi radians/sample (upper-half of unit-circle). If
    `whole` is True, compute frequencies from 0 to 2*pi radians/sample.

Returns
-------
w : 1D ndarray
    Frequency array [radians/sample]
H : 1D ndarray
    Array of complex magnitude values

Notes
-----
If (num, den) is passed in for ``system``, coefficients for both the
numerator and denominator should be specified in descending exponent
order (e.g. ``z^2 + 3z + 5`` would be represented as ``[1, 3, 5]``).

.. versionadded:: 0.18.0

Examples
--------
Generating the Nyquist plot of a transfer function

>>> from scipy import signal
>>> import matplotlib.pyplot as plt

Construct the transfer function
:math:`H(z) = \frac{1}{z^2 + 2z + 3}` with a sampling time of 0.05
seconds:

>>> sys = signal.TransferFunction([1], [1, 2, 3], dt=0.05)

>>> w, H = signal.dfreqresp(sys)

>>> plt.figure()
>>> plt.plot(H.real, H.imag, "b")
>>> plt.plot(H.real, -H.imag, "r")
>>> plt.show()

z6dfreqresp can only be used with discrete-time systems.Nr   rI   zUnknown system typer   z?dfreqresp requires a SISO (single input, single output) system.)r  r   )rW   r   r   r  r"   r`   r    r!   ru   rA   rB   r   r   r  r   r   r   r   rT   r  )r7   r   r   r   r  r   r   r  s           r:   r,   r,     sF   @ fd##fc""  ": ; ; vcr{2vbz2&*%%f/@AA.//}}V^^q0 + , 	, 	}&*++ $..vzz/?/?/A6::NSD6 4K	 
FN	+	+v||V[[t$& a4Kr<   c                 4   [        XUS9u  p[        U [        5      (       a  U R                  nOU S   nS[        R
                  " [        U5      5      -  n[        R                  " [        R                  " [        R                  " U5      5      5      nX-  XV4$ )u  
Calculate Bode magnitude and phase data of a discrete-time system.

Parameters
----------
system :
    An instance of the LTI class `dlti` or a tuple describing the system.
    The number of elements in the tuple determine the interpretation, i.e.:

    1. ``(sys_dlti)``:  Instance of LTI class `dlti`. Note that derived instances,
       such as instances of `TransferFunction`, `ZerosPolesGain`, or `StateSpace`,
       are allowed as well.
    2. ``(num, den, dt)``: Rational polynomial as described in `TransferFunction`.
       The coefficients of the polynomials should be specified in descending
       exponent order,  e.g., z² + 3z + 5 would be represented as ``[1, 3, 5]``.
    3. ``(zeros, poles, gain, dt)``:  Zeros, poles, gain form as described
       in `ZerosPolesGain`.
    4. ``(A, B, C, D, dt)``: State-space form as described in `StateSpace`.

w : array_like, optional
    Array of frequencies normalized to the Nyquist frequency being π, i.e.,
    having unit radiant / sample. Magnitude and phase data is calculated for every
    value in this array. If not given, a reasonable set will be calculated.
n : int, optional
    Number of frequency points to compute if `w` is not given. The `n`
    frequencies are logarithmically spaced in an interval chosen to
    include the influence of the poles and zeros of the system.

Returns
-------
w : 1D ndarray
    Array of frequencies normalized to the Nyquist frequency being ``np.pi/dt``
    with ``dt`` being the sampling interval of the `system` parameter.
    The unit is rad/s assuming ``dt`` is in seconds.
mag : 1D ndarray
    Magnitude array in dB
phase : 1D ndarray
    Phase array in degrees

Notes
-----
This function is a convenience wrapper around `dfreqresp` for extracting
magnitude and phase from the calculated complex-valued amplitude of the
frequency response.

.. versionadded:: 0.18.0

See Also
--------
dfreqresp, dlti, TransferFunction, ZerosPolesGain, StateSpace


Examples
--------
The following example shows how to create a Bode plot of a 5-th order
Butterworth lowpass filter with a corner frequency of 100 Hz:

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from scipy import signal
...
>>> T = 1e-4  # sampling interval in s
>>> f_c, o = 1e2, 5  # corner frequency in Hz (i.e., -3 dB value) and filter order
>>> bb, aa = signal.butter(o, f_c, 'lowpass', fs=1/T)
...
>>> w, mag, phase = signal.dbode((bb, aa, T))
>>> w /= 2*np.pi  # convert unit of frequency into Hertz
...
>>> fg, (ax0, ax1) = plt.subplots(2, 1, sharex='all', figsize=(5, 4),
...                               tight_layout=True)
>>> ax0.set_title("Bode Plot of Butterworth Lowpass Filter " +
...               rf"($f_c={f_c:g}\,$Hz, order={o})")
>>> ax0.set_ylabel(r"Magnitude in dB")
>>> ax1.set(ylabel=r"Phase in Degrees",
...         xlabel="Frequency $f$ in Hertz", xlim=(w[1], w[-1]))
>>> ax0.semilogx(w, mag, 'C0-', label=r"$20\,\log_{10}|G(f)|$")  # Magnitude plot
>>> ax1.semilogx(w, phase, 'C1-', label=r"$\angle G(f)$")  # Phase plot
...
>>> for ax_ in (ax0, ax1):
...     ax_.axvline(f_c, color='m', alpha=0.25, label=rf"${f_c=:g}\,$Hz")
...     ax_.grid(which='both', axis='x')  # plot major & minor vertical grid lines
...     ax_.grid(which='major', axis='y')
...     ax_.legend()
>>> plt.show()
r   r   r  )
r,   rW   r   rI   r   r  r  rad2degr  angle)r7   r   r   r  rI   r  r  s          r:   r-   r-   _  su    l VA&DA&$YYBZ
#a&!
!CJJryy!-.E63r<   )NTr   r   r   )r  gMbP?   )NNr   )Fr   r%  scipy.linalgr   r  scipyr   scipy.interpolater   _filter_designr   r   r	   r
   r   r   r   _lti_conversionr   r   r   r   r   r   r   numpyr   r   r   r   r   r   r   r   r   r   r   __all__r/   r   r   r    rr   r   r!   rs   r   r"   rt   r   r#   r  r$   r%   r&   r'   r  r  r  r  r  r  r  r  r(   r)   r+   r*   r,   r-   r   r<   r:   <module>rY     s  * 
 $  0' ' 'A A A 3 3 3 !S  S ld3
 d3N|6 |6~t* tnJ'!13 J'Z>	/ >	BG+( G+TC~s CLC	^T C	LQ#$ Q#hG!:s G!T;	T ;	|T"n>CL@F=@Rn# #
0f(!#^>B0;fx"v"4\~
qhTnSlaH`r<   