o
    iP9                     @   s   d dl Zd dlmZmZmZ d dlZd dlmZ	 d dlm
Z
mZmZmZmZ d dlmZmZmZmZmZ G dd dZG dd	 d	ZG d
d dZdS )    N)assert_array_almost_equalassert_almost_equalxp_assert_close)cont2discrete)dlsimss2tfss2zpklsimlti)tf2ssimpulsedimpulsestepdstepc                   @   s|   e Zd Zejjdd Zdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )TestC2Dc                 C   s   t jdt jd}t jddt jd}t ddgddgddgg}t dgdgd	gg}d
t d }t dd}d}t||||f|dd\}}	}
}}t|| t||	 t||
 t|| t|| d S )N   )Zdtyper            ?      ?      ?      ?        Qտ)a?g6<S?zohmethod)npeyeZfloat64fullarrayc2dr   r   )selfacbcccdcad_truthbd_truthdt_requestedadbdcddddt r0   _/home/kim/smarthome/.venv/lib/python3.10/site-packages/scipy/signal/tests/test_cont2discrete.pytest_zoh   s    



zTestC2D.test_zohc                 C   s   t d}t dd}t ddgddgddgg}t dgdgdgg}d	t d }t dd
}|}t dgdgdgg}d}	t||||f|	dd\}
}}}}t||
 t|| t|| t|| t|	| d S )Nr   r   r   r   r   r   r   r   r   gm?g>."?g4pL	?g.Ȫq¿fohr   r   r   r    r!   r"   r   r   r#   r$   r%   r&   r'   r(   r)   cd_truthdd_truthr*   r+   r,   r-   r.   r/   r0   r0   r1   test_foh$   s$   
 



zTestC2D.test_fohc                 C   s   t d}t dd}t ddgddgddgg}t dgdgdgg}dt d }t dd	}|}t d
gdgdgg}d}	t||||f|	dd\}
}}}}t||
 t|| t|| t|| t|	| d S )Nr   r   r   r   r   r   r   r   g)a?g      ?g      ?r   r   r4   r5   r0   r0   r1   test_impulse;   s"   




zTestC2D.test_impulsec                 C   s   t d}t dd}t ddgddgddgg}t dgdgdgg}d}d	}d
t d }t dd}t ddgddgddgg}	t dgdgdgg}
t||||f|d|d\}}}}}t|| t|| t|	| t|
| d S )Nr   r   r   r   r   r   r   r   UUUUUU?g?333333??333333?ffffff?皙?=
ףp=ʿgbtr   alphar   r   r    r!   r"   r   )r#   r$   r%   r&   r'   r*   rC   r(   r)   r6   r7   r+   r,   r-   r.   r/   r0   r0   r1   test_gbtQ   s.   




zTestC2D.test_gbtc                 C   s   t d}t dd}t ddgddgddgg}t dgdgdgg}d}d	t d }t dd}t ddgddgddgg}|}	t||||f|d
d\}
}}}}t||
 t|| t|| t|	| t|| d S )Nr   r   r   r   r   r   r   r         ?Zeulerr   r4   r#   r$   r%   r&   r'   r*   r(   r)   r6   r7   r+   r,   r-   r.   r/   r0   r0   r1   
test_eulerk   s(   





zTestC2D.test_eulerc                 C   s   t d}t dd}t ddgddgddgg}t dgdgdgg}d}d	t d }t dd}t d
d	gd	d	gd	dgg}t dgdgdgg}	t||||f|dd\}
}}}}t||
 t|| t|| t|	| d S )Nr   r   r   r   r   r   r   r   g       @rF   g      ?gzG?Zbackward_diffr   rD   rG   r0   r0   r1   test_backward_diff   s,   




zTestC2D.test_backward_diffc                 C   s  t d}t dd}t ddgddgddgg}t dgdgdgg}d}d	t d }t dd
}t ddgddgdd
gg}t dgd
gdgg}	t||||f|dd\}
}}}}t||
 t|| t|| t|	| t|| dt d }t dd}t ddgddgddgg}t dgdgdgg}	d
}t||||f|dd\}
}}}}t||
 t|| t|| t|	| t|| d S )Nr   r   r   r   r   r   r   r   g?r:   gUUUUUU?g?g>X%bilinearr   gffffff?r?   r<   r=   r;   r>   r@   r4   rG   r0   r0   r1   test_bilinear   sH   










zTestC2D.test_bilinearc           	      C   sx   t g d}t g d}t g dg}t g d}d}t||f|dd\}}}t|| t|| t|| d S )N)r   r   r   )r   r   r   )r:   gNZۿgϱ)_?)r   g]Ogoh?r   r   r   )r   r!   r"   r   r   )	r#   ZnumcZdencZnumdZdendr*   numdenr/   r0   r0   r1   test_transferfunction   s   

zTestC2D.test_transferfunctionc                 C   s   t ddg}t dt d dt d g}d}ddg}d	d
g}d}d}t|||f|dd\}}	}
}t|| t||	 t||
 t|| d S )Nr   g      y              ?r   y             r   gwRN?g.-.?y\9O?(?y\9O?(ֿr   r   )r   r!   sqrtr"   r   r   )r#   Zzeros_cZpoles_cZk_cZzeros_dZpolls_dZk_dr*   ZzerosZpoleskr/   r0   r0   r1   test_zerospolesgain   s    "


zTestC2D.test_zerospolesgainc                 C   s2  d}d}d}d}t ||||\}}t||||\}}}	d}
d}dd| |
 |  d||
 |   }|
| d||
 |   }|d||
 |   }||| |  }t ||||\}}t||f|
d|d\}}}t|| t|| t||||\}}}t|||	f|
d|d\}}}}t|| t|| t|| dS )	z7Test method='gbt' with alpha=0.25 for tf and zpk cases.g      r   r   r   r   rA   rB   N)r   r   r"   r   )r#   ABCDZcnumZcdenczcpZckhrC   ZAdZBdZCdZDdZdnumZddenZc2dnumZc2ddenr/   ZdzZdpZdkZc2dzZc2dpZc2dkr0   r0   r1   test_gbt_with_sio_tf_and_zpk   s*   $



z$TestC2D.test_gbt_with_sio_tf_and_zpkc                 C   s  dd }t dgg}t dgg}t dgg}t dgg}d}t ddd}|d	 |d  }||}	t||||f||	|d
\}}
}t||||f|dd}d|	dd |	d	d   }|dd }t||dd	||d\}}}d|
dd |
d	d   }t| |dd dS )a  
        Test that the solution to the discrete approximation of a continuous
        system actually approximates the solution to the continuous system.
        This is an indirect test of the correctness of the implementation
        of cont2discrete.
        c                 S   s   t d|  S )Ng      @)r   sin)tr0   r0   r1   u  s   z'TestC2D.test_discrete_approx.<locals>.ug{Gzr   r?   r   g      $@e   r   )TUZX0rJ   r   r   N)r\   r[   x0g-C6?Zrtol)	r   r!   Zlinspacer	   r"   r   Zreshaper   ravel)r#   r\   abcdra   r[   r/   u1ZyoutZxoutZdsysu2t2Ztd2Zyd2Zxd2Zymidr0   r0   r1   test_discrete_approx  s    zTestC2D.test_discrete_approxc                 C   sd   ddgddggddgf}t |d\}}}|dksJ t|ddgdd t|ddgddggdd d S )	Nr   r   {Gz?gUxegMbP?rb   r`   gT(|)r"   r   )r#   tfrL   rM   r/   r0   r0   r1   test_simo_tf=  s
   zTestC2D.test_simo_tfc                 C   s   d}ddgddggddgf}t ||\}}}|d d |d f}t ||\}}}	|d d |d f}
t |
|\}}}||	ks@J ||ksFJ t|t||fdd t||dd t||dd d S )Nrl   r      r   gvIh%<=rb   )r"   r   r   Zvstack)r#   tsrm   rL   rM   r/   Ztf1Znum1Zden1Zdt1Ztf2Znum2Zden2Zdt2r0   r0   r1   test_multioutputF  s   zTestC2D.test_multioutputN)__name__
__module____qualname__pytestmarkZthread_unsafer2   r8   r9   rE   rH   rI   rK   rN   rQ   rY   rk   rn   rr   r0   r0   r0   r1   r      s    
,*(	r   c                   @   s   e Zd Zdd Zdd ZdS )
TestC2dLtic           	      C   s   t ddgddgg}t dgdgg}t ddgg}d}t ddgd	d
gg}t dgdgg}t||||}|d}t|j| t|j| t|j| t|jt 	|j d S )Ng333333ӿ皙?r?   gffffffr   r   gГ<?gs?g?g{Kp?g1vM ?g_ǷJ(?g?)
r   r!   r
   to_discreter   rR   rS   rT   rU   Z
zeros_like)	r#   rR   rS   rT   rU   ZA_resZB_resZsys_sscZsys_ssdr0   r0   r1   test_c2d_ss_  s   

zTestC2dLti.test_c2d_ssc                 C   s\   t ddgddg}|d}tddg}tddg}t|j|dd	 t|j|dd	 d S )
Nr   r;   r   g?g{Gzt?gi߿gY쭛]g{Gz?)Zatol)r
   rz   r   r!   r   rM   rL   )r#   sysZnum_resZden_resr0   r0   r1   test_c2d_tfr  s   
zTestC2dLti.test_c2d_tfN)rs   rt   ru   r{   r}   r0   r0   r0   r1   rx   ^  s    rx   c                   @   s   e Zd Zeddgg dddfeddgg dddfedg d	ddfgZejd
edd Zejd
edd Z	ejd
edd Z
dS )TestC2dInvariantsr   )r   rF   r   r   
   r   )r   rF      r   r   ry   )r   r   r   r   zsys,sample_time,samples_numberc                 C   sZ   t || }t||d\}}tt||ddt|d\}}t||  |d   d S )Nr^   r   r   nr   )r   aranger   r   r"   lenr   rc   r#   r|   Zsample_timeZsamples_numbertime_Z	yout_contZ	yout_discr0   r0   r1   test_impulse_invariant  s   
z(TestC2dInvariants.test_impulse_invariantc                 C   sV   t || }t||d\}}tt||ddt|d\}}t| |d   d S )Nr   r   r   r   r   )r   r   r   r   r"   r   r   rc   r   r0   r0   r1   test_step_invariant  s   z%TestC2dInvariants.test_step_invariantc                 C   sT   t || }t|||d\}}}tt||dd|d\}}}t| |  d S )N)r^   r_   r3   r   )r\   )r   r   r	   r   r"   r   rc   r   r0   r0   r1   test_linear_invariant  s   z'TestC2dInvariants.test_linear_invariantN)rs   rt   ru   r   casesrv   rw   Zparametrizer   r   r   r0   r0   r0   r1   r~     s    

r~   )numpyr   Zscipy._lib._array_apir   r   r   rv   Zscipy.signalr   r"   r   r   r   r	   r
   r   r   r   r   r   r   rx   r~   r0   r0   r0   r1   <module>   s      Q"