L iX ddlmZddlmZddlmZmZmZddlZddlm Z ddl m Z m Z m Z gdZedZe e d e d d iZd ed eegeffdZdededej(dej*d df dZeddj.dHieddddej0ddddedeedededeej(dej*deej6ded e fdZed d!j.dHieddej0ddd"dededeej(dej*deej6ded e fd#Zed$d%j.dHiedddej0ddd&ded'ededeej(dej*deej6ded e fd(Zed)d*j.dHied+ddej0ddd,ded-ededeej(dej*deej6ded e fd.Zed/d0j.dHieddej0ddd"dededeej(dej*deej6ded e fd1Z ed2d3j.dHieddej0ddd"dededeej(dej*deej6ded e fd4Z!ed5d6j.dHieddej0ddd"dededeej(dej*deej6ded e fd7Z"ed8d9j.dHieddej0ddd"dededeej(dej*deej6ded e fd:Z#ed;dZ$ed?d@j.dHiedAddej0dddBdCededeej(dej*deej6ded e fdDZ%edEdFj.dHieddej0ddd"dededeej(dej*deej6ded e fdGZ&y)I)Iterable)sqrt)CallableOptionalTypeVarN)Tensor)factory_common_args merge_dicts parse_kwargs) bartlettblackmancosine exponentialgaussiangeneral_cosinegeneral_hamminghamminghannkaisernuttall_Ta6 M (int): the length of the window. In other words, the number of points of the returned window. sym (bool, optional): If `False`, returns a periodic window suitable for use in spectral analysis. If `True`, returns a symmetric window suitable for use in filter design. Default: `True`. normalizationzThe window is normalized to 1 (maximum value is 1). However, the 1 doesn't appear if :attr:`M` is even and :attr:`sym` is `True`.argsreturnc,dtdtffd }|S)a8Adds docstrings to a given decorated function. Specially useful when then docstrings needs string interpolation, e.g., with str.format(). REMARK: Do not use this function if the docstring doesn't need string interpolation, just write a conventional docstring. Args: args (str): orc4dj|_|S)N)join__doc__)rrs b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/signal/windows/windows.py decoratorz_add_docstr..decorator8sGGDM )r)rr"s` r! _add_docstrr$,sRB r# function_nameMdtypelayoutc|dkrt|d||tjurt|d||tjtjfvrt|d|y)aPerforms common checks for all the defined windows. This function should be called before computing any window. Args: function_name (str): name of the window function. M (int): length of the window. dtype (:class:`torch.dtype`): the desired data type of returned tensor. layout (:class:`torch.layout`): the desired layout of returned tensor. rz, requires non-negative window length, got M=z/ is implemented for strided tensors only, got: z) expects float32 or float64 dtypes, got: N) ValueErrortorchstridedfloat32float64)r%r&r'r(s r!_window_function_checksr/?s 1uoI! M  U]]"oLVH U   U]]EMM22oFug N  3r#z Computes a window with an exponential waveform. Also known as Poisson window. The exponential window is defined as follows: .. math:: w_n = \exp{\left(-\frac{|n - c|}{\tau}\right)} where `c` is the ``center`` of the window. aF {normalization} Args: {M} Keyword args: center (float, optional): where the center of the window will be located. Default: `M / 2` if `sym` is `False`, else `(M - 1) / 2`. tau (float, optional): the decay value. Tau is generally associated with a percentage, that means, that the value should vary within the interval (0, 100]. If tau is 100, it is considered the uniform window. Default: 1.0. {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric exponential window of size 10 and with a decay value of 1.0. >>> # The center will be at (M - 1) / 2, where M is 10. >>> torch.signal.windows.exponential(10) tensor([0.0111, 0.0302, 0.0821, 0.2231, 0.6065, 0.6065, 0.2231, 0.0821, 0.0302, 0.0111]) >>> # Generates a periodic exponential window and decay factor equal to .5 >>> torch.signal.windows.exponential(10, sym=False,tau=.5) tensor([4.5400e-05, 3.3546e-04, 2.4788e-03, 1.8316e-02, 1.3534e-01, 1.0000e+00, 1.3534e-01, 1.8316e-02, 2.4788e-03, 3.3546e-04]) ?TF)centertausymr'r(device requires_gradr1r2r3r4r5c |tj}td||||dkrtd|d|r | td|dk(rtjd||||S||s|dkDr|n|dz d z }d|z }tj | |z| |dz z|z||||| } tj tj|  S) NrrzTau must be positive, got: instead.z)Center must be None for symmetric windowsrr'r(r4r5@startendstepsr'r(r4r5)r+get_default_dtyper/r*emptylinspaceexpabs) r&r1r2r3r'r(r4r5constantks r!rrYsn }'')M1eV< ax6se9EFF v!DEEAv{{ fV=  ~1q5!a!es:3wH g WA ( *# A 99eiil] ##r#a Computes a window with a simple cosine waveform, following the same implementation as SciPy. This window is also known as the sine window. The cosine window is defined as follows: .. math:: w_n = \sin\left(\frac{\pi (n + 0.5)}{M}\right) This formula differs from the typical cosine window formula by incorporating a 0.5 term in the numerator, which shifts the sample positions. This adjustment results in a window that starts and ends with non-zero values. a {normalization} Args: {M} Keyword args: {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric cosine window. >>> torch.signal.windows.cosine(10) tensor([0.1564, 0.4540, 0.7071, 0.8910, 0.9877, 0.9877, 0.8910, 0.7071, 0.4540, 0.1564]) >>> # Generates a periodic cosine window. >>> torch.signal.windows.cosine(10, sym=False) tensor([0.1423, 0.4154, 0.6549, 0.8413, 0.9595, 1.0000, 0.9595, 0.8413, 0.6549, 0.4154]) r3r'r(r4r5c D|tj}td||||dk(rtjd||||Sd}tj|s |dkDr|dzn|z }tj ||z||dz z|z|||||}tj |S)Nrrr8r9?r:r<)r+r@r/rApirBsin r&r3r'r(r4r5r=rErFs r!rrsd }'')Ha7Av{{ fV=   ExxA1q51=H h a!e_ (# A 99Q<r#z Computes a window with a gaussian waveform. The gaussian window is defined as follows: .. math:: w_n = \exp{\left(-\left(\frac{n}{2\sigma}\right)^2\right)} a  {normalization} Args: {M} Keyword args: std (float, optional): the standard deviation of the gaussian. It controls how narrow or wide the window is. Default: 1.0. {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric gaussian window with a standard deviation of 1.0. >>> torch.signal.windows.gaussian(10) tensor([4.0065e-05, 2.1875e-03, 4.3937e-02, 3.2465e-01, 8.8250e-01, 8.8250e-01, 3.2465e-01, 4.3937e-02, 2.1875e-03, 4.0065e-05]) >>> # Generates a periodic gaussian window and standard deviation equal to 0.9. >>> torch.signal.windows.gaussian(10, sym=False,std=0.9) tensor([1.9858e-07, 5.1365e-05, 3.8659e-03, 8.4658e-02, 5.3941e-01, 1.0000e+00, 5.3941e-01, 8.4658e-02, 3.8659e-03, 5.1365e-05]) )stdr3r'r(r4r5rMc x|tj}td||||dkrtd|d|dk(rtjd||||S|s|dkDr|n|dz dz }d|t d zz }tj ||z||dz z|z||||| } tj| d z S) Nrrz*Standard deviation must be positive, got: r7r8r9r:r;r<)r+r@r/r*rArrBrC) r&rMr3r'r(r4r5r=rErFs r!rrs` }'')J5&9 axEcU)TUUAv{{ fV=  q1ua!a% 03 6EC$q'M"H h a!e_ (# A 99q!tW r#aK Computes the Kaiser window. The Kaiser window is defined as follows: .. math:: w_n = I_0 \left( \beta \sqrt{1 - \left( {\frac{n - N/2}{N/2}} \right) ^2 } \right) / I_0( \beta ) where ``I_0`` is the zeroth order modified Bessel function of the first kind (see :func:`torch.special.i0`), and ``N = M - 1 if sym else M``. a {normalization} Args: {M} Keyword args: beta (float, optional): shape parameter for the window. Must be non-negative. Default: 12.0 {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric gaussian window with a standard deviation of 1.0. >>> torch.signal.windows.kaiser(5) tensor([4.0065e-05, 2.1875e-03, 4.3937e-02, 3.2465e-01, 8.8250e-01, 8.8250e-01, 3.2465e-01, 4.3937e-02, 2.1875e-03, 4.0065e-05]) >>> # Generates a periodic gaussian window and standard deviation equal to 0.9. >>> torch.signal.windows.kaiser(5, sym=False,std=0.9) tensor([1.9858e-07, 5.1365e-05, 3.8659e-03, 8.4658e-02, 5.3941e-01, 1.0000e+00, 5.3941e-01, 8.4658e-02, 3.8659e-03, 5.1365e-05]) g(@)betar3r'r(r4r5rPc h|tj}td||||dkrtd|d|dk(rtjd||||S|dk(rtj d||||Stj ||| }| }d |z|s|n|dz z }tj|||dz |zz} tj|| ||||| } tjtj||ztj| d z tj|z S) Nrrz beta must be non-negative, got: r7r8r9r:r:)r'r4r;r<rO) r+r@r/r*rAonestensorminimumrBi0rpow) r&rPr3r'r(r4r5r=rEr>rFs r!rrLs6b }'')Ha7 ax;D6KLLAv{{ fV=   Avzz fV=  <<E& 9D EETzcQq1u5H --eq1u&88 9C  # A 88EJJtd{UYYq!_<= >$ OOr#z Computes the Hamming window. The Hamming window is defined as follows: .. math:: w_n = \alpha - \beta\ \cos \left( \frac{2 \pi n}{M - 1} \right) a {normalization} Arguments: {M} Keyword args: {sym} alpha (float, optional): The coefficient :math:`\alpha` in the equation above. beta (float, optional): The coefficient :math:`\beta` in the equation above. {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric Hamming window. >>> torch.signal.windows.hamming(10) tensor([0.0800, 0.1876, 0.4601, 0.7700, 0.9723, 0.9723, 0.7700, 0.4601, 0.1876, 0.0800]) >>> # Generates a periodic Hamming window. >>> torch.signal.windows.hamming(10, sym=False) tensor([0.0800, 0.1679, 0.3979, 0.6821, 0.9121, 1.0000, 0.9121, 0.6821, 0.3979, 0.1679]) c$t||||||S)NrGrr&r3r'r(r4r5s r!rrs$Z  #  r#z Computes the Hann window. The Hann window is defined as follows: .. math:: w_n = \frac{1}{2}\ \left[1 - \cos \left( \frac{2 \pi n}{M - 1} \right)\right] = \sin^2 \left( \frac{\pi n}{M - 1} \right) a {normalization} Arguments: {M} Keyword args: {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric Hann window. >>> torch.signal.windows.hann(10) tensor([0.0000, 0.1170, 0.4132, 0.7500, 0.9698, 0.9698, 0.7500, 0.4132, 0.1170, 0.0000]) >>> # Generates a periodic Hann window. >>> torch.signal.windows.hann(10, sym=False) tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955]) c &t|d|||||S)NrIalphar3r'r(r4r5rYrZs r!rrs'X   # r#z Computes the Blackman window. The Blackman window is defined as follows: .. math:: w_n = 0.42 - 0.5 \cos \left( \frac{2 \pi n}{M - 1} \right) + 0.08 \cos \left( \frac{4 \pi n}{M - 1} \right) a {normalization} Arguments: {M} Keyword args: {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric Blackman window. >>> torch.signal.windows.blackman(5) tensor([-1.4901e-08, 3.4000e-01, 1.0000e+00, 3.4000e-01, -1.4901e-08]) >>> # Generates a periodic Blackman window. >>> torch.signal.windows.blackman(5, sym=False) tensor([-1.4901e-08, 2.0077e-01, 8.4923e-01, 8.4923e-01, 2.0077e-01]) c r|tj}td|||t|gd|||||S)Nr )gzG?rIg{Gz?ar3r'r(r4r5)r+r@r/rrZs r!r r sHV }'')J5&9   # r#a4 Computes the Bartlett window. The Bartlett window is defined as follows: .. math:: w_n = 1 - \left| \frac{2n}{M - 1} - 1 \right| = \begin{cases} \frac{2n}{M - 1} & \text{if } 0 \leq n \leq \frac{M - 1}{2} \\ 2 - \frac{2n}{M - 1} & \text{if } \frac{M - 1}{2} < n < M \\ \end{cases} a {normalization} Arguments: {M} Keyword args: {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric Bartlett window. >>> torch.signal.windows.bartlett(10) tensor([0.0000, 0.2222, 0.4444, 0.6667, 0.8889, 0.8889, 0.6667, 0.4444, 0.2222, 0.0000]) >>> # Generates a periodic Bartlett window. >>> torch.signal.windows.bartlett(10, sym=False) tensor([0.0000, 0.2000, 0.4000, 0.6000, 0.8000, 1.0000, 0.8000, 0.6000, 0.4000, 0.2000]) c \|tj}td||||dk(rtjd||||S|dk(rtjd||||Sd}d|s|n|dz z }tj |||dz |zz||||| }dtj |z S) Nr rr8r9r:rRrOr<)r+r@r/rArSrBrDrLs r!r r LsZ }'')J5&9Av{{ fV=   Avzz fV=   ESAa!e,H  QUh& &# A uyy| r#z Computes the general cosine window. The general cosine window is defined as follows: .. math:: w_n = \sum^{M-1}_{i=0} (-1)^i a_i \cos{ \left( \frac{2 \pi i n}{M - 1}\right)} a {normalization} Arguments: {M} Keyword args: a (Iterable): the coefficients associated to each of the cosine functions. {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric general cosine window with 3 coefficients. >>> torch.signal.windows.general_cosine(10, a=[0.46, 0.23, 0.31], sym=True) tensor([0.5400, 0.3376, 0.1288, 0.4200, 0.9136, 0.9136, 0.4200, 0.1288, 0.3376, 0.5400]) >>> # Generates a periodic general cosine window with 2 coefficients. >>> torch.signal.windows.general_cosine(10, a=[0.5, 1 - 0.5], sym=False) tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955]) r`c .|tj}td||||dk(rtjd||||S|dk(rtjd||||St |t s td|s tdd tjz|s|n|dz z }tjd|dz |z||||| }tjt|D cgc] \} } d | z| zc} } ||| } tj| jd| j| j | j" } | j%d tj&| j%d |zzj)dScc} } w)Nrrr8r9r:rRz!Coefficients must be a list/tuplezCoefficients cannot be emptyrOr<rb)r4r'r5)r'r4r5)r+r@r/rArS isinstancer TypeErrorr*rJrBrT enumeratearangeshaper'r4r5 unsqueezecossum) r&r`r3r'r(r4r5rErFiwa_is r!rrsZ }''),a?Av{{ fV=   Avzz fV=   a ";<< 788588|qQ7H  Uh # A ,,#,Q<041a"Q0#  C   ! iizz''  A MM"  !++b/A*= > > C CA FF 1s'F z Computes the general Hamming window. The general Hamming window is defined as follows: .. math:: w_n = \alpha - (1 - \alpha) \cos{ \left( \frac{2 \pi n}{M-1} \right)} a {normalization} Arguments: {M} Keyword args: alpha (float, optional): the window coefficient. Default: 0.54. {sym} {dtype} {layout} {device} {requires_grad} Examples:: >>> # Generates a symmetric Hamming window with the general Hamming window. >>> torch.signal.windows.general_hamming(10, sym=True) tensor([0.0800, 0.1876, 0.4601, 0.7700, 0.9723, 0.9723, 0.7700, 0.4601, 0.1876, 0.0800]) >>> # Generates a periodic Hann window with the general Hamming window. >>> torch.signal.windows.general_hamming(10, alpha=0.5, sym=False) tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955]) gHzG?r\r]c 0t||d|z g|||||S)Nr0r_r)r&r]r3r'r(r4r5s r!rrs0Z  #+  # r#z Computes the minimum 4-term Blackman-Harris window according to Nuttall. .. math:: w_n = 1 - 0.36358 \cos{(z_n)} + 0.48917 \cos{(2z_n)} - 0.13659 \cos{(3z_n)} + 0.01064 \cos{(4z_n)} where :math:`z_n = \frac{2 \pi n}{M}`. a {normalization} Arguments: {M} Keyword args: {sym} {dtype} {layout} {device} {requires_grad} References:: - A. Nuttall, "Some windows with very good sidelobe behavior," IEEE Transactions on Acoustics, Speech, and Signal Processing, vol. 29, no. 1, pp. 84-91, Feb 1981. https://doi.org/10.1109/TASSP.1981.1163506 - Heinzel G. et al., "Spectrum and spectral density estimation by the Discrete Fourier transform (DFT), including a comprehensive list of window functions and some new flat-top windows", February 15, 2002 https://holometer.fnal.gov/GH_FFT.pdf Examples:: >>> # Generates a symmetric Nutall window. >>> torch.signal.windows.general_hamming(5, sym=True) tensor([3.6280e-04, 2.2698e-01, 1.0000e+00, 2.2698e-01, 3.6280e-04]) >>> # Generates a periodic Nuttall window. >>> torch.signal.windows.general_hamming(5, sym=False) tensor([3.6280e-04, 1.1052e-01, 7.9826e-01, 7.9826e-01, 1.1052e-01]) c *t|gd|||||S)N)gzD?g;%N?g1|?gC˅?r_rprZs r!rr-s'j  6 # r#)'collections.abcrmathrtypingrrrr+rtorch._torch_docsr r r __all__rwindow_common_argsstrr$intr'r(r/formatr,floatboolr4rrrrrrr r rrrrrr#r!r~s$.. LL  T]  7"sxb1&   ',{{