Ë
    âQ(h¹>  ã                   ó®   — d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZ g d¢Zd„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zdd„Zdd„Zd„ Zdd„Zdd„Zy)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                 óD  — t        | |«      \  } }t        | j                  «      }|dk(  rt        | g| j                  «      } | j                  d   }t        |«      }||kD  rd}t        |«      ‚|dk(  s|dk(  r>t        g t        «      t        g t        «      t        g t        «      t        g t        «      fS t        j                  t        j                  | j                  d   ||z
  f| j                  ¬«      | f«      } | j                  d   dkD  rt        | dd…df   «      }nt        dggt        «      }|dk(  rZ|j                  | j                  «      }t        d«      t        d|j                  d   f«      t        |j                  d   df«      |fS t        |dd g«       }t        |t        |dz
  |dz
  «      f   }t        |dz
  d«      }	| dd…dd…f   t        | dd…df   |dd «      z
  }
|j                  |
j                  d   |	j                  d   f«      }||	|
|fS )	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   )ÚdtypeéÿÿÿÿN)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              úZ/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/scipy/signal/_lti_conversion.pyr   r      sþ  € ôp ˜˜cÓ"�H€CˆÜ	ˆS�Y‰Y‹€BØ	ˆQ‚wÜ�s�e˜SŸY™YÓ'ˆØ�	‰	�!‰€AÜˆC‹€AØˆ1‚uØGˆÜ˜‹oÐØˆA‚v��a’Ü�bœ%Ó ¤%¨¬EÓ"2´E¸"¼eÓ4DÜ�bœ%Ó ð"ð 	"ô �)‰)”R—X‘X˜sŸy™y¨™|¨Q°©UÐ3¸3¿9¹9ÔEÀsÐKÓ
L€Cà
‡y�y��}�qÒÜ�sš1˜a˜4‘yÓ!‰ô �A�3�%œÓˆàˆA‚vØ�I‰I�c—i‘iÓ ˆä�f“œu a¨¯©°© _Ó5Ü�q—w‘w˜q‘z 1�oÓ&¨ð+ð 	+ô �3�q�r�7�)ÓÐ€DÜ
ˆ4”�Q˜‘U˜A ™EÓ"Ð"Ñ#€AÜˆA�‰E�1‹€AØŠAˆq‰rˆE‰
”U˜3šq !˜t™9 c¨!¨" gÓ.Ñ.€AØ	�	‰	�1—7‘7˜1‘:˜qŸw™w q™zÐ*Ó+€Aàˆa��Aˆ:Ðó    c                 ó    — | €t        d«      S | S )N©r   r   )r	   ©Úargs    r-   Ú_none_to_empty_2dr3   s   s   € Ø
€{Ü�V‹}Ðàˆ
r.   c                 ó   — | �t        | «      S y ©N)r   r1   s    r-   Ú_atleast_2d_or_noner6   z   s   € Ø
€Ü˜#‹Ðð r.   c                 ó    — | �| j                   S y)N)NN)r   )r%   s    r-   Ú_shape_or_noner8      s   € Ø€}Ø�w‰wˆàr.   c                  ó   — | D ]  }|€Œ|c S  y r5   © )Úargsr2   s     r-   Ú_choice_not_noner<   †   s   € Øò ˆØ‰?ØŠJñr.   c                 ón   — | j                   dk(  rt        |«      S | j                   |k7  rt        d«      ‚| S )Nr0   z*The input arrays have incompatible shapes.)r   r	   r   )r%   r   s     r-   Ú_restorer>   Œ   s5   € Ø‡w�w�&ÒÜ�U‹|Ðà�7‰7�eÒÜÐIÓJÐJØˆr.   c                 ó¾  — t        t        | |||f«      \  } }}}t        | «      \  }}t        |«      \  }}t        |«      \  }}	t        |«      \  }
}t        |||	«      }t        ||«      }t        ||
«      }|�|�|€t	        d«      ‚t        t
        | |||f«      \  } }}}t        | ||f«      } t        |||f«      }t        |||f«      }t        |||f«      }| |||fS )a³  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¨a°°A¨,Ó7�J€A€qˆ!ˆQä˜AÓ�F€BˆÜ˜AÓ�F€BˆÜ˜AÓ�F€BˆÜ˜AÓ�F€Bˆä˜˜R Ó$€AÜ˜˜RÓ €AÜ˜˜RÓ €AØ€y�A�I  ÜÐ@ÓAÐAäÔ&¨¨A¨q°!¨Ó5�J€A€qˆ!ˆQÜ��Q˜�FÓ€AÜ��Q˜�FÓ€AÜ��Q˜�FÓ€AÜ��Q˜�FÓ€Aàˆa��Aˆ:Ðr.   c                 óÐ  — t        | |||«      \  } }}}|j                  \  }}||k\  rt        d«      ‚|dd…||dz   …f   }|dd…||dz   …f   }	 t        | «      }|j                  dk(  rH|j                  dk(  r9t        j                  |«      }|j                  dk(  r| j                  dk(  rg }||fS | j                  d   }	| dd…df   |dd…df   z   |ddd…f   z   |z   dz   }
t        j                  ||	dz   f|
j                  «      }t        |«      D ];  }t        ||dd…f   «      }t        | t        ||«      z
  «      ||   dz
  |z  z   ||<   Œ= ||fS # t        $ r d}Y �Œ	w xY w)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   Ä   s�  € ôt    1 a¨Ó+�J€A€qˆ!ˆQà—‘�I€Dˆ#Ø�‚|ÜÐDÓEÐEð 	
Š!ˆU�5˜1‘9ˆ_Ð
Ñ€AØ	Š!ˆU�5˜1‘9ˆ_Ð
Ñ€AðÜ�1‹gˆð 	
�‰�!Š˜!Ÿ&™& Aš+Ü�h‰h�q‹kˆØ�F‰F�aŠK˜aŸf™f¨škØˆCØ�Cˆxˆà—‘˜‘€JØ’!�Q�$‘˜!šA˜q˜D™'Ñ! A aª d¡GÑ+¨aÑ/°#Ñ5€IÜ
�(‰(�D˜* q™.Ð)¨9¯?©?Ó
;€CÜ�4‹[ò 9ˆÜ˜˜!šQ˜$™Ó ˆÜ�aœ#˜a ›*‘nÓ%¨¨1©°©°SÑ(8Ñ8ˆˆAŠð9ð �ˆ8€Oøô! ò Ø‹ðús   ÁE ÅE%Å$E%c                 ó(   — t        t        | ||«      Ž S )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   € ô" ”&˜˜A˜q“/Ð"Ð"r.   c           	      ó.   — t        t        | ||||¬«      Ž S )aª  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˜˜A˜q !¨5Ô1Ð2Ð2r.   c                 ó	  — t        | «      dk(  r| j                  «       S t        | «      dk(  r=t        t        | d   | d   «      |||¬«      }t	        |d   |d   |d   |d   «      |fz   S t        | «      dk(  rAt        t        | d   | d   | d   «      |||¬«      }t        |d   |d   |d   |d   «      |fz   S t        | «      dk(  r| \  }}}}nt        d«      ‚|dk(  r"|€t        d
«      ‚|dk  s|dkD  rt        d«      ‚|dk(  ræt        j                  |j                  d   «      ||z  |z  z
  }	t        j                  |	t        j                  |j                  d   «      d|z
  |z  |z  z   «      }
t        j                  |	||z  «      }t        j                  |	j                  «       |j                  «       «      }|j                  «       }||t        j                  ||«      z  z   }�n™|dk(  s|dk(  rt        | |dd¬«      S |dk(  s|dk(  rt        | |dd¬«      S |dk(  rt        | |dd¬«      S |dk(  �r	t        j                  ||f«      }t        j                  t        j                   |j                  d   |j                  d   f«      t        j                   |j                  d   |j                  d   f«      f«      }t        j"                  ||f«      }t        j$                  ||z  «      }|d	|j                  d   …d	d	…f   }|d	d	…d|j                  d   …f   }
|d	d	…|j                  d   d	…f   }|}|}�nD|dk(  rß|j                  d   }|j                  d   }t        j&                  t        j(                  ||g«      |z  t        j                  |«      «      }t!        ||d|z  z   f«      }t        j(                  |g|gg«      }t        j$                  |«      }|d	|…d|…f   }|d	|…|||z   …f   }|d	|…||z   d	…f   }|}
||z
  ||z  z   }|}|||z  z   }n`|dk(  rLt        j*                  |d«      st        d«      ‚t        j$                  ||z  «      }
|
|z  |z  }|}||z  |z  }nt        d|› d�«      ‚|
||||fS )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ÒÜœU 6¨!¡9¨f°Q©iÓ8¸"ÀVØ#(ô*ˆä�T˜!‘W˜d 1™g t¨A¡w°°Q±Ó8¸B¸5Ñ@Ð@Ü	ˆV‹˜Ò	ÜœV F¨1¡I¨v°a©y¸&À¹)ÓDÀbØ$*°%ô9ˆä�d˜1‘g˜t A™w¨¨Q©°°a±Ó9¸R¸EÑAÐAÜ	ˆV‹˜Ò	Ø‰
ˆˆ1ˆa‘äð 6ó 7ð 	7ð �‚Øˆ=Üð Kó Lð Là�QŠY˜% !š)Üð 8ó 9ð 9ð �‚ä�f‰f�Q—W‘W˜Q‘ZÓ  5¨¡8¨A¡:Ñ-ˆÜ�\‰\˜#œrŸv™v a§g¡g¨a¡jÓ1°S¸±YÀ±NÀ1Ñ4DÑDÓEˆÜ�\‰\˜#˜r !™tÓ$ˆô �\‰\˜#Ÿ-™-›/¨1¯;©;«=Ó9ˆØ�\‰\‹^ˆØ�”r—v‘v˜a “}Ñ$Ñ$Šà	�:Ò	 ¨8Ò!3Ü˜V R°¸SÔAÐAà	�7Ò	˜f¨Ò6Ü˜V R°¸SÔAÐAà	�?Ò	"Ü˜V R°¸SÔAÐAà	�5‹ä—9‘9˜a ˜VÓ$ˆô —9‘9œbŸh™h¨¯©°©
°A·G±G¸A±JÐ'?Ó@Ü Ÿh™h¨¯©°©
°A·G±G¸A±JÐ'?Ó@ðBó Cˆô �Y‰Y˜ (Ð+Ó,ˆÜ�[‰[˜˜b™Ó!ˆð ��—‘˜‘�šQ�Ñˆà’�1�Q—W‘W˜Q‘Z�<�Ñ ˆØ’�1—7‘7˜1‘:‘;�ÑˆàˆØŠà	�5Šà�G‰G�A‰JˆØ�G‰G�A‰Jˆô ×$Ñ$¤R§X¡X¨q°!¨fÓ%5¸Ñ%:¼B¿F¹FÀ1»IÓFˆÜ˜!˜Q  Q¡™Y˜Ó(ˆÜ�X‰X˜�z H :Ð.Ó/ˆä�[‰[˜‹_ˆð �"�1�"�a˜�c�'‰{ˆØ�"�1�"�a˜˜A™�g�+‰ˆØ�"�1�"�a˜!‘e‘f�*‰~ˆàˆØ�D‰[˜4 $™;Ñ&ˆØˆØ��T‘‰\‰à	�9Ò	Ü�{‰{˜1˜aÔ Üð :ó ;ð ;ô �[‰[˜˜R™Ó ˆØ�!‰V�b‰[ˆØˆØ�‰U�R‰Z‰ô Ð:¸6¸(À!ÐDÓEÐEàˆr�2�r˜2ÐÐ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.   