
    bi2                        d dl Zd dlmZ d dl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 d dlmZmZ d dlmZmZ d d	lmZ ej4                  j6                  Zdd
Z G d d      ZdZ ee       G d d             Z ee      d        Z y)    N)product)raises)array_api_extra)xp_assert_closearray_namespace_xp_copy_to_numpyis_cupymake_xp_test_case)numpy)upfirdnfirwin)_output_len_upfirdn_modes)	_pad_testc                 <   t        j                  |       } t        j                  |      }t        j                  t        |       |z  | j                        }| |dd|<   t        j
                  ||      dd|   dt        t        |      t        |       ||       }|S )zpNaive upfirdn processing in Python.

    Note: arg order (x, h) differs to facilitate apply_along_axis use.
    N)npasarrayzeroslendtypeconvolver   )xhupdownouts        Z/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/scipy/signal/tests/test_upfirdn.pyupfirdn_naiver   7   s    
 	

1A


1A
((3q6B;
(CC"I
++a
ff
%&L{3q63q62t'L
MCJ    c                   *    e Zd ZdZdddZd ZddZy)UpFIRDnCasezTest _UpFIRDn objectNxpc                    |t         }|| _        || _        t        j                  |      | _        || _        t        j                  j                  d      | _	        || _
        y )N   )	np_compatr   r   r   
atleast_1dr   x_dtyperandomRandomStaterngr#   )selfr   r   r   r(   r#   s         r   __init__zUpFIRDnCase.__init__F   sN    :B	q!99((,r   c                 l   | j                  t        j                  d| j                               | j                  t        j                  d| j                               | j                  j                  d      j                  | j                        }| j                  t        j                  t        j                  fv r!|d| j                  j                  d      z  z  }| j                  |       | j                  t        j                  d      j                  | j                               t        | j                        ry d} | j                  j
                  | j                  | j                        }| j                  t        j                  t        j                  fv r|d | j                  j
                  | z  z  }t        t        |            D ]  }| j                  ||        |d d d d ddd df   j                  }t        t        |            D ]  }| j                  ||        y )N   
                 ?)         axisr2   r3   )scrubr   onesr(   r+   randnastype	complex64
complex128aranger	   r#   ranger   T)r,   r   sizer6   s       r   __call__zUpFIRDnCase.__call__P   s   

2771dll+,

2772t||,-HHNN2%%dll3<<BLL"--88dhhnnR(((A

1

299R=''56477DHHNND!((6<<BLL"--88ndhhnnd+++A#d)$ 	%DJJqtJ$	%a1addlO#d)$ 	%DJJqtJ$	%r   c                    | j                   }t        j                  t        ||| j                  | j
                  | j                        }t        t        | j                        |j                  |   | j
                  | j                        }|j                  |   |k(  sJ t        |j                  | j                        |j                  |      | j
                  | j                  |      }|j                  |   |k(  sJ |j                  |j                  k(  sJ | j                  j                  |j                  f}t        d |D              r|j                  |j                  k(  sJ t        j                  |v r-t        j                  |v r|j                  |j                  k(  sJ t        d |D              r|j                  |j                  k(  s\J t        j                   |v st        j                  |v r|j                  |j                   k(  sJ |j                  |j"                  k(  sJ |j                  ||j                        }t%        ||       y )Nr5   c              3   B   K   | ]  }|t         j                  k(    y wN)r   r;   .0ds     r   	<genexpr>z$UpFIRDnCase.scrub.<locals>.<genexpr>u   s     1QqBLL 1   c              3   B   K   | ]  }|t         j                  k(    y wrD   )r   float32rE   s     r   rH   z$UpFIRDnCase.scrub.<locals>.<genexpr>y   s     1Qbjj1rI   r   )r#   r   apply_along_axisr   r   r   r   r   r   shaper   r   r   allr;   rK   r<   float64r   )r,   r   r6   r#   yrwant_lenydtypess           r   r7   zUpFIRDnCase.scrubj   s   WW  a!%$))=s466{AGGDM477DIINxx~)))BJJtvv&

1tww		wwt}(((ww"(("""&&,,(1&1177bll***\\V#

f(<77bll***1&1177bjj(((]]f$(>77bmm+++77bjj(((ZZ!''Z*Ar   ))__name__
__module____qualname____doc__r-   rA   r7    r   r   r!   r!   D   s    37 %4r   r!   int64rK   r;   rP   r<   c                   "   e Zd Z edd      d        Zej                  j                  dg d      ej                  j                  dg d      d               Zd	 Z	ej                  j                  d
ddddg dfddddg dfddddg dfddddg dfddddg dfg      d        Z
ej                  j                  dg d      ej                  j                  dg d      d               Zej                  j                  de      ej                  j                  dd      ej                  j                  dg d       d!                      Zej                  j                  de      ej                  j                  d"e      ej                  j                  d# e ed$d$                  d%                      Zd& Zej                  j                  d'e      d(        Zej                  j                  d) ed*gg d+eg d,            d-        Zy.)/TestUpfirdnTzenough to only test on numpy)np_onlyreasonc                     t        t        t        dgdgdd       t        t        t        g dgdd       t        t        t        dggdgdd       y )Nr/   r   )assert_raises
ValueErrorr   )r,   r#   s     r   test_valid_inputzTestUpfirdn.test_valid_input   sD    j'A3Q:j'2sAq9j'QC51#q!<r   len_h)r/   r2   r3      r4   len_xc                    |j                  |      }t        j                  |      |dz     j                  d      }|j	                  |      }t        ||dd      }t        j                  ||dz  |dz
  dz  fd|      }t        ||       y )Nr2         ?r/   constantr"   )r   xpxatsetr8   r   padr   )r,   re   rg   r#   r   r   rS   wants           r   test_singletonzTestUpfirdn.test_singleton   s     HHUOFF1Ieqj!%%b)GGENAq!Qwwq5A:	a'78*L4 r   c                 f   t        |j                  ddg      |j                  dg      dd      }t        ||j                  ddg|j                               t        |j                  ddg      |j                  ddg      dd      }t        ||j                  g d|j                               y )Nr/   ri   rL           )rr   ri   ri   )r   r   r   rP   )r,   r#   rS   s      r   test_shift_xzTestUpfirdn.test_shift_x   s    BJJ1v&

B4(8!Q?rzz3*BJJz7	
 BJJ1v&

B8(<aC2::oRZZ:HIr   z len_h, len_x, up, down, expectedr2   r4   )r/   r   r   r   r3      )r/   r   r/   r   r/   rf   )r/   r   r   r   r/   )r/   r   r   r/   r      )r/   r   r   r/   r   r   r/   c                    |j                  |      }t        j                  |      d   j                  d      }|j	                  ||j
                        }t        ||||      }	|j                  ||j
                        }t        |	|       y )Nr   ri   rL   )	r   rk   rl   rm   r8   rP   r   r   r   )
r,   re   rg   r   r   expectedr#   r   r   rS   s
             r   test_length_factorszTestUpfirdn.test_length_factors   ss     HHUOFF1IaLR GGEG,Aq"d#::hbjj:98$r   r   r[   zdown, want_len))r2   i  )ru   i  )O      c                    t         j                  j                  d      }d}t        t         |      }|j	                  |      j                  |      }|t         j                  t         j                  fv r|d|j	                  |      z  z  }t        ||      }|j                  ||      }|j                  t        dd|z  d            }	|j                  t        t        |      t        |	      d	|            }
t        |	|d	|
      }|j                  |fk(  sJ |
j                  d   |j                  d   k(  sJ t        |
|dd       y )Nr%   i'  r1   rL      ri   hamming)windowr/   )r   r   r   gHz>atolrtol)r   r)   r*   getattrr9   r:   r;   r<   r   r   r   r   r   rN   r   )r,   r   rR   r   r#   random_stater@   np_dtyper   r   ylrS   s               r   test_vs_convolvezTestUpfirdn.test_vs_convolve   s&    yy,,R02u%t$++H5bmm44l((...AE"JJqJ&JJvb"t)I>?ZZ+A.0A!0DaN
 AqQT*ww8+%%%xx{aggaj(((ADt4r   r(   r   )ri   r1   zup, down)r/   r/   )r2   r2   )r3   r2   )r2   r3   c                 .     t        |||||              y Nr"   )r!   )r,   r(   r   r   r   r#   s         r   test_vs_naive_deltazTestUpfirdn.test_vs_naive_delta   s     	1BaR02r   h_dtypezp_max, q_max)r0   d   c                 L    | j                  |||||      }|D ]	  } |         y r   )_random_factors)r,   r(   r   p_maxq_maxr#   teststests           r   test_vs_naivezTestUpfirdn.test_vs_naive   s3    
 $$UE7G$K 	DF	r   c          
         d}d}t         j                  j                  d      }g }	t        |      D ]  }
||kD  r|nd}||kD  r|nd}|j	                  |      |z   }|j	                  |      |z   }|j	                  |      dz   }t        j
                  |j	                  |            }|j                  |      }|t        u r|d|j	                  |      z  z  }|	j                  t        |||||              |	S )Nr3      r%   r/   r1   r"   )
r   r)   r*   r>   randintr'   r:   complexappendr!   )r,   r   r   r   r(   r#   n_rep	longest_hr   r   _p_addq_addpqre   r   s                    r   r   zTestUpfirdn._random_factors   s    	yy,,R0u 	?A"U]EE"U]EE$$U+e3A$$U+e3A !((3a7El2259:A!A'!R,..u555LLQ1g"=>	?  r   modec                 <   t        j                  g dt         j                        }d\  }}t        ||||      }|j                  |      }|j                  |      }|dk(  r|j                  g d      }n|dk(  r|j                  g d      }n|d	k(  r|j                  g d
      }n|dk(  r|d   |d   z
  |j                  d   dz
  z  }|d   |j                  | dd|j                        |z  z   }	|d   |j                  d|dz   |j                        |z  z   }
t        |	      j                  } ||	||
f      }n4t        j                  t        |      ||f|      }|j                  |      }|j                  ||j                        }t        ||       y)z@Test vs. manually computed results for modes not in numpy's pad.)r/   r2   r3   r/   rL   )rt   rt   nprenpostr   antisymmetric)g      @r/   rU   rU   r/   r2   r3   r/   rU   r   r   rU   r/   r2   antireflect)ri   r2   r3   r/   rU   r   r/   r2   r3   r/   rU   r   r/   r2   r3   r/   smooth)g      r   r   rU   r   r/   r2   r3   r/   rU   r   iiilinerU   r   r/   r   N)r   r   rP   r   rN   r=   r   concatrn   r   r   )r,   r   r#   r   r   r   rS   
y_expected	lin_slopeleftrightr   s               r   test_extensionszTestUpfirdn.test_extensions   s    JJ|2::6ead%d;JJqMJJqM?"JLJ]"DFJXNPJV^21!''!*q.9IQ4"))TE1arzz)BYNNDbEBIIa"**IE	QQE$T*11Fq% 01J 1! 4tUm$OJJ/JZZ
"**Z=
:&r   zsize, h_len, mode, dtype   )rf   r4      )rK   rP   r;   r<   c                 4   t        |      r|dk7  rt        j                  d       t        t        |      }t        ||      }t        j
                  j                  d      }|j                  |      j                  |      }	|dv r|	d|j                  |      z  z  }	t	        j                  dd|z   |	j                  j                        }
|j                  |	|      }	|j                  |
      }
t        |
|	dd|	      }|dz
  }|d
v rt        t        |	      |||      }n!t	        j                   t        |	      ||      }|j                  |      }t        |
|ddd	      }|||  }|j#                  |      j$                  dz  x}}t'        ||||       y )Nrj   z&only mode='constant' supported by CuPy)r`   r4   )r;   r<   r1   r/   rL   )r   r   r   )r   r   r   r   r   r   g      Y@r   )r	   pytestskipr   r   r)   r*   r9   r:   r=   realr   r   r   r   r   rn   finfoepsr   )r,   r@   h_lenr   r   r#   dtype_npdtype_xpr   r   r   rS   npadxpadypadr   r   r   s                     r   
test_modeszTestUpfirdn.test_modes  sr    2;4:-KKGH2u%2u%yy,,Q/t$++H5//l((...AIIaU!&&,,7JJqJ)JJqMAqQQT2qyEE.q1DtTD66+A.4@Dzz$q$11:>$u%
hhx(,,s22t:Dt<r   N)rV   rW   rX   skip_xp_backendsrd   r   markparametrizerp   rs   rx   r   _UPFIRDN_TYPESr   listr   r   r   r   r   r   rZ   r   r   r^   r^      sG    d+IJ= K=
 [[Wo6[[Wo6! 7 7!J [[?	
Aq!\"	
Aq!_%	
Aq!_%	
Aq!_%	
B1+,B %% [[K [[- 0 
55, [[Y7[[S(+[[Z)IJ3 K , 83 [[Y7[[Y7[[^!')Y"?@BB 8 8
0 [[V^4' 5'< [["C=		
==r   r^   c                 >    d}d}d}d}t        ||||      }|dk(  sJ y )Ni  i i@  i  i6S)r   )r#   re   in_lenr   r   out_lens         r   test_output_len_long_inputr   C  s8    
 EF	BD%T2G hr   r   )!r   r   	itertoolsr   r   r   rb   
scipy._libr   rk   scipy._lib._array_apir   r   r   r	   r
   scipy._lib.array_api_compatr&   scipy.signalr   r   scipy.signal._upfirdnr   r   scipy.signal._upfirdn_applyr   r   r   r   r!   r   r^   r   rZ   r   r   <module>r      s   F   *  -  ; ( = 1;;// 
< <~ L 7y= y= y=x 7 r   