
    (ph>                         S 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Jr  SSKJrJrJr  / SQrS rS	 rS
 rS rS rS rSS jrSS jrS rSS jrSS jrg)zr
ltisys -- a collection of functions to convert linear time invariant systems
from one representation to another.
    N)	r_eye
atleast_2dpolydotasarrayzerosarrayouter)linalg   )tf2zpkzpk2tf	normalize)tf2ssabcd_normalizess2tfzpk2ssss2zpkcont2discretec                 B   [        X5      u  p[        U R                  5      nUS:X  a  [        U /U R                  5      n U R                  S   n[        U5      nX4:  a  Sn[        U5      eUS:X  d  US:X  a>  [        / [        5      [        / [        5      [        / [        5      [        / [        5      4$ [        R                  " [        R                  " U R                  S   XC-
  4U R                  S9U 45      n U R                  S   S:  a  [        U SS2S4   5      nO[        S//[        5      nUS:X  aZ  UR                  U R                  5      n[        S5      [        SUR                  S   45      [        UR                  S   S45      U4$ [        USS /5      * n[        U[        US-
  US-
  5      4   n[        US-
  S5      n	U SS2SS24   [        U SS2S4   USS 5      -
  n
UR                  U
R                  S   U	R                  S   45      nXX4$ )	a)  Transfer function to state-space representation.

Parameters
----------
num, den : array_like
    Sequences representing the coefficients of the numerator and
    denominator polynomials, in order of descending degree. The
    denominator needs to be at least as long as the numerator.

Returns
-------
A, B, C, D : ndarray
    State space representation of the system, in controller canonical
    form.

Examples
--------
Convert the transfer function:

.. math:: H(s) = \frac{s^2 + 3s + 3}{s^2 + 2s + 1}

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

to the state-space representation:

.. math::

    \dot{\textbf{x}}(t) =
    \begin{bmatrix} -2 & -1 \\ 1 & 0 \end{bmatrix} \textbf{x}(t) +
    \begin{bmatrix} 1 \\ 0 \end{bmatrix} \textbf{u}(t) \\

    \textbf{y}(t) = \begin{bmatrix} 1 & 2 \end{bmatrix} \textbf{x}(t) +
    \begin{bmatrix} 1 \end{bmatrix} \textbf{u}(t)

>>> from scipy.signal import tf2ss
>>> A, B, C, D = tf2ss(num, den)
>>> A
array([[-2., -1.],
       [ 1.,  0.]])
>>> B
array([[ 1.],
       [ 0.]])
>>> C
array([[ 1.,  2.]])
>>> D
array([[ 1.]])
r   z7Improper transfer function. `num` is longer than `den`.r   )dtypeN)r   r      )r   lenshaper   r   
ValueErrorr
   floatnphstackr	   r   reshaper   r   r   )numdennnMKmsgDfrowABCs              O/var/www/html/venv/lib/python3.13/site-packages/scipy/signal/_lti_conversion.pyr   r      s   p "HC	SYYB	QwseSYY'		!ACAuGoAvab% %E"2E"e4Db% " 	" ))RXXsyy|QU3399EsK
LC
yy}qs1a4y! A3%AvIIcii fua_5qwwqz1o&+ 	+ 3qr7)D
4QUAE""#AAE1AAqrE
U3q!t9c!"g..A			1771:qwwqz*+A:    c                 "    U c  [        S5      $ U $ )Nr   r   )r	   args    r-   _none_to_empty_2dr3   s   s    
{V}
r.   c                      U b  [        U 5      $ g N)r   r1   s    r-   _atleast_2d_or_noner6   z   s    
# r.   c                 "    U b  U R                   $ g)N)NN)r   )r%   s    r-   _shape_or_noner8      s    }wwr.   c                  $    U  H
  nUc  M  Us  $    g r5    )argsr2   s     r-   _choice_not_noner<      s    ?J r.   c                 r    U R                   S:X  a  [        U5      $ U R                   U:w  a  [        S5      eU $ )Nr0   z*The input arrays have incompatible shapes.)r   r	   r   )r%   r   s     r-   _restorer>      s5    ww&U|77eIJJr.   c                    [        [        XX#45      u  pp#[        U 5      u  pE[        U5      u  pg[        U5      u  p[        U5      u  p[        XFU	5      n[        X{5      n[        X5      nUb  Ub  Uc  [	        S5      e[        [
        XX#45      u  pp#[        XU45      n [        XU45      n[        X.U45      n[        X>U45      nXX#4$ )ak  Check state-space matrices and ensure they are 2-D.

If enough information on the system is provided, that is, enough
properly-shaped arrays are passed to the function, the missing ones
are built from this information, ensuring the correct number of
rows and columns. Otherwise a ValueError is raised.

Parameters
----------
A, B, C, D : array_like, optional
    State-space matrices. All of them are None (missing) by default.
    See `ss2tf` for format.

Returns
-------
A, B, C, D : array
    Properly shaped state-space matrices.

Raises
------
ValueError
    If not enough information on the system was provided.

z%Not enough information on the system.)mapr6   r8   r<   r   r3   r>   )r*   r+   r,   r(   MANAMBNBMCNCMDNDpqrs                  r-   r   r      s    2 (1,7JA!AFBAFBAFBAFB$A A AyAI@AA&q5JA!FAFAFAFA:r.   c                    [        XX#5      u  pp#UR                  u  pVXF:  a  [        S5      eUSS2XDS-   24   nUSS2XDS-   24   n [        U 5      nUR                  S:X  aK  UR                  S:X  a;  [
        R                  " U5      nUR                  S:X  a  U R                  S:X  a  / nX4$ U R                  S   n	U SS2S4   USS2S4   -   USSS24   -   U-   S-   n
[
        R                  " XYS-   4U
R                  5      n[        U5       H8  n[        X+SS24   5      n[        U [        X5      -
  5      X;   S-
  U-  -   X'   M:     X4$ ! [         a    Sn GNf = f)a  State-space to transfer function.

A, B, C, D defines a linear state-space system with `p` inputs,
`q` outputs, and `n` state variables.

Parameters
----------
A : array_like
    State (or system) matrix of shape ``(n, n)``
B : array_like
    Input matrix of shape ``(n, p)``
C : array_like
    Output matrix of shape ``(q, n)``
D : array_like
    Feedthrough (or feedforward) matrix of shape ``(q, p)``
input : int, optional
    For multiple-input systems, the index of the input to use.

Returns
-------
num : 2-D ndarray
    Numerator(s) of the resulting transfer function(s). `num` has one row
    for each of the system's outputs. Each row is a sequence representation
    of the numerator polynomial.
den : 1-D ndarray
    Denominator of the resulting transfer function(s). `den` is a sequence
    representation of the denominator polynomial.

Examples
--------
Convert the state-space representation:

.. math::

    \dot{\textbf{x}}(t) =
    \begin{bmatrix} -2 & -1 \\ 1 & 0 \end{bmatrix} \textbf{x}(t) +
    \begin{bmatrix} 1 \\ 0 \end{bmatrix} \textbf{u}(t) \\

    \textbf{y}(t) = \begin{bmatrix} 1 & 2 \end{bmatrix} \textbf{x}(t) +
    \begin{bmatrix} 1 \end{bmatrix} \textbf{u}(t)

>>> A = [[-2, -1], [1, 0]]
>>> B = [[1], [0]]  # 2-D column vector
>>> C = [[1, 2]]    # 2-D row vector
>>> D = 1

to the transfer function:

.. math:: H(s) = \frac{s^2 + 3s + 3}{s^2 + 2s + 1}

>>> from scipy.signal import ss2tf
>>> ss2tf(A, B, C, D)
(array([[1., 3., 3.]]), array([ 1.,  2.,  1.]))
z)System does not have the input specified.Nr   r           )r   r   r   r   sizer   ravelemptyr   ranger   r   )r*   r+   r,   r(   inputnoutninr#   r"   
num_states	type_testkCks                r-   r   r      sj   t  a+JA!ID|DEE 	
!U19_
A	!U19_
A1g 	
!!&&A+hhqkFFaKaffkCxJ!Q$!AqD'!AadG+a/#5I
((Dq.)9??
;C4[Q$ a#a*n%S(88  8O!  s   	E E E c                 &    [        [        XU5      6 $ )a
  Zero-pole-gain representation to state-space representation

Parameters
----------
z, p : sequence
    Zeros and poles.
k : float
    System gain.

Returns
-------
A, B, C, D : ndarray
    State space representation of the system, in controller canonical
    form.

)r   r   )zrI   rW   s      r-   r   r     s    " &q/""r.   c           
      $    [        [        XX#US96 $ )aV  State-space representation to zero-pole-gain representation.

A, B, C, D defines a linear state-space system with `p` inputs,
`q` outputs, and `n` state variables.

Parameters
----------
A : array_like
    State (or system) matrix of shape ``(n, n)``
B : array_like
    Input matrix of shape ``(n, p)``
C : array_like
    Output matrix of shape ``(q, n)``
D : array_like
    Feedthrough (or feedforward) matrix of shape ``(q, p)``
input : int, optional
    For multiple-input systems, the index of the input to use.

Returns
-------
z, p : sequence
    Zeros and poles.
k : float
    System gain.

)rR   )r   r   )r*   r+   r,   r(   rR   s        r-   r   r   1  s    6 5q5122r.   c                 $	   [        U 5      S:X  a  U R                  5       $ [        U 5      S:X  a9  [        [        U S   U S   5      XUS9n[	        US   US   US   US   5      U4-   $ [        U 5      S:X  a=  [        [        U S   U S   U S   5      UX#S9n[        US   US   US   US   5      U4-   $ [        U 5      S:X  a  U u  pVpxO[        S5      eUS:X  a%  Uc  [        S
5      eUS:  d  US:  a  [        S5      eUS:X  a  [        R                  " UR                  S   5      X1-  U-  -
  n	[        R                  " U	[        R                  " UR                  S   5      SU-
  U-  U-  -   5      n
[        R                  " XU-  5      n[        R                  " U	R                  5       UR                  5       5      nUR                  5       nX[        R                  " X{5      -  -   nGOUS:X  d  US:X  a  [        XSSS9$ US:X  d  US:X  a  [        XSSS9$ US:X  a  [        XSSS9$ US:X  Ga  [        R                  " XV45      n[        R                  " [        R                   " UR                  S   UR                  S   45      [        R                   " UR                  S   UR                  S   45      45      n[        R"                  " X45      n[        R$                  " UU-  5      nUS	UR                  S   2S	S	24   nUS	S	2SUR                  S   24   n
US	S	2UR                  S   S	24   nUnUnGOMUS:X  a  UR                  S   nUR                  S   n[        R&                  " [        R(                  " XV/5      U-  [        R                  " U5      5      n[!        UUSU-  -   45      n[        R(                  " U/U//5      n[        R$                  " U5      nUS	U2SU24   nUS	U2UUU-   24   nUS	U2UU-   S	24   nUn
UU-
  UU-  -   nUnXU-  -   nOeUS:X  aP  [        R*                  " US5      (       d  [        S5      e[        R$                  " XQ-  5      n
X-  U-  nUnXv-  U-  nO[        SU S35      eXXU4$ )a(  
Transform a continuous to a discrete state-space system.

Parameters
----------
system : a tuple describing the system or an instance of `lti`
    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)

dt : float
    The discretization time step.
method : str, optional
    Which method to use:

        * gbt: generalized bilinear transformation
        * bilinear: Tustin's approximation ("gbt" with alpha=0.5)
        * euler: Euler (or forward differencing) method ("gbt" with alpha=0)
        * backward_diff: Backwards differencing ("gbt" with alpha=1.0)
        * zoh: zero-order hold (default)
        * foh: first-order hold (*versionadded: 1.3.0*)
        * impulse: equivalent impulse response (*versionadded: 1.3.0*)

alpha : float within [0, 1], optional
    The generalized bilinear transformation weighting parameter, which
    should only be specified with method="gbt", and is ignored otherwise

Returns
-------
sysd : tuple containing the discrete system
    Based on the input type, the output will be of the form

    * (num, den, dt)   for transfer function input
    * (zeros, poles, gain, dt)   for zeros-poles-gain input
    * (A, B, C, D, dt) for state-space system input

Notes
-----
By default, the routine uses a Zero-Order Hold (zoh) method to perform
the transformation. Alternatively, a generalized bilinear transformation
may be used, which includes the common Tustin's bilinear approximation,
an Euler's method technique, or a backwards differencing technique.

The Zero-Order Hold (zoh) method is based on [1]_, the generalized bilinear
approximation is based on [2]_ and [3]_, the First-Order Hold (foh) method
is based on [4]_.

References
----------
.. [1] https://en.wikipedia.org/wiki/Discretization#Discretization_of_linear_state_space_models

.. [2] http://techteach.no/publications/discretetime_signals_systems/discrete.pdf

.. [3] G. Zhang, X. Chen, and T. Chen, Digital redesign via the generalized
    bilinear transformation, Int. J. Control, vol. 82, no. 4, pp. 741-754,
    2009.
    (https://www.mypolyuweb.hk/~magzhang/Research/ZCC09_IJC.pdf)

.. [4] G. F. Franklin, J. D. Powell, and M. L. Workman, Digital control
    of dynamic systems, 3rd ed. Menlo Park, Calif: Addison-Wesley,
    pp. 204-206, 1998.

Examples
--------
We can transform a continuous state-space system to a discrete one:

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from scipy.signal import cont2discrete, lti, dlti, dstep

Define a continuous state-space system.

>>> A = np.array([[0, 1],[-10., -3]])
>>> B = np.array([[0],[10.]])
>>> C = np.array([[1., 0]])
>>> D = np.array([[0.]])
>>> l_system = lti(A, B, C, D)
>>> t, x = l_system.step(T=np.linspace(0, 5, 100))
>>> fig, ax = plt.subplots()
>>> ax.plot(t, x, label='Continuous', linewidth=3)

Transform it to a discrete state-space system using several methods.

>>> dt = 0.1
>>> for method in ['zoh', 'bilinear', 'euler', 'backward_diff', 'foh', 'impulse']:
...    d_system = cont2discrete((A, B, C, D), dt, method=method)
...    s, x_d = dstep(d_system)
...    ax.step(s, np.squeeze(x_d), label=method, where='post')
>>> ax.axis([t[0], t[-1], x[0], 1.4])
>>> ax.legend(loc='best')
>>> fig.tight_layout()
>>> plt.show()

r   r   r   )methodalpha      zKFirst argument must either be a tuple of 2 (tf), 3 (zpk), or 4 (ss) arrays.gbtNzUAlpha parameter must be specified for the generalized bilinear transform (gbt) methodzDAlpha parameter must be within the interval [0,1] for the gbt methodg      ?bilineartusting      ?eulerforward_diffrM   backward_diffzohfohimpulsez<Impulse method is only applicable to strictly proper systemszUnknown transformation method '')r   to_discreter   r   r   r   r   r   r   r   r   r   solve	transposer   r    r	   vstackexpm
block_diagblockallclose)systemdtr]   r^   sysdabcdimaadbdcdddem_upperem_loweremmsnmms11ms12ms13s                          r-   r   r   O  s   F 6{a!!##
6{aU6!9fQi8"#(*T!Wd1gtAwQ8B5@@	V	VF1Ivay&)Db$*9d1gtAwQa9REAA	V	
a 6 7 	7 = K L LQY%!) 8 9 9 ffQWWQZ 58A:-\\#rvvaggaj1SYN14DDE\\#!t$ \\#--/1;;=9\\^rvva}$$	:	8!3VSAA	7	f6VSAA	?	"VSAA	599aV$ 99bhh
AGGAJ'?@ hh
AGGAJ'?@B C YY+,[[b! Q1QWWQZ< 1771:;	5GGAJGGAJ $$RXXqf%5%:BFF1IF!QQY(XXzH:./[[_ "1"ac'{"1"aAg+"1"a!ef*~D[4$;&T\	9	{{1a   : ; ; [[ Vb[URZ :6(!DEE22r.   )NNNN)r   )rg   N)__doc__numpyr   r   r   r   r   r   r   r	   r
   r   scipyr   _filter_designr   r   r   __all__r   r3   r6   r8   r<   r>   r   r   r   r   r   r:   r.   r-   <module>r      sk   
 1 1 1  5 5^B
,^Vr#(3<Fr.   