a
    ùc  ã                   @   sÄ   d Z ddlZddlmZ ddlmZmZmZm	Z	 G dd„ deƒZ
ddd„Zed	krÀdd
lmZ ddlmZ ejeddƒd eddƒd f Ze ddd¡Ze
eedZe ¡  ejdejjd dS )z#
Linear Time Frequency Processing.
é    N)ÚBaseTFRepresentation)Únearest_oddÚdividerÚmoduloÚizakc                       s<   e Zd ZdZdZd‡ fdd„	Zdd„ Zd‡ fdd„	Z‡  ZS )ÚShortTimeFourierTransformzShort time Fourier transform.ZstftNc                    s   t t| ƒj||||d dS )a}  Create a ShortTimeFourierTransform object.

        :param signal: Signal to be analyzed.
        :param timestamps: Time instants of the signal (default:
            ``np.arange(len(signal))``)
        :param n_fbins: Number of frequency bins (default: ``len(signal)``)
        :param fwindow: Frequency smoothing window (default: Hamming window of
            length ``len(signal) / 4``)
        :type signal: array-like
        :type timestamps: array-like
        :type n_fbins: int
        :type fwindow: array-like
        :return: ShortTimeFourierTransform object
        :Example:

        >>> from tftb.generators import fmconst
        >>> sig = np.r_[fmconst(128, 0.2)[0], fmconst(128, 0.4)[0]]
        >>> stft = ShortTimeFourierTransform(sig)
        >>> tfr, t, f = stft.run()
        >>> stft.plot() #doctest: +SKIP

        .. plot:: docstring_plots/processing/stft.py
        )ÚsignalÚn_fbinsÚ
timestampsÚfwindowN)Úsuperr   Ú__init__)Úselfr   r
   r	   r   ©Ú	__class__© úc/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/tftb/processing/linear.pyr      s
    ýz"ShortTimeFourierTransform.__init__c                 C   sd  | j jd d d }tt| jd ƒ|gƒ}tjtj|t | jj¡ t 	| jjd ¡d f dd 
t¡ }tjtj|t | jj¡ | jjd t 	| jjd ¡ f dd 
t¡}t | j ¡}t| jjd ƒD ]r}|| }|| }t 	||d ¡ 
t¡}	t | j|	 | j¡}
| j||	 d  
t¡ |||	  
t¡  | j|
|f< qÌtjj| jdd| _| j| j| jfS )zCompute the STFT according to:

        .. math:: X[m, w] = \sum_{n=-\infty}^{\infty}x[n]w[n - m]e^{-j\omega n}

        Where :math:`w` is a Hamming window.r   é   é   ç       @©Zaxis)r   ÚshapeÚminÚroundr	   ÚnpZc_ZonesÚtsÚarangeÚastypeÚintr   ZconjÚrangeÚtfrÚ	remainderÚfftÚfreqs)r   ZlhZrangeminZstartsZendsZconj_fwindowZicolÚstartÚendÚtauÚindexr   r   r   Úrun4   s6    ÿþþÿþþÿzShortTimeFourierTransform.runÚcmapTçš™™™™™©?c                    s”   | j dt| jd ƒ…dd…f | _ | jdt| jd ƒ… | _|rRt | j ¡d | _ t | j ¡| }d| j | j |k< tt| ƒj	f |||dœ|¤Ž dS )aÏ  Display the spectrogram of an STFT.

        :param ax: axes object to draw the plot on. If None(default), one will
            be created.
        :param kind: Choice of visualization type, either "cmap"(default) or "contour".
        :param sqmod: Whether to take squared modulus of TFR before plotting.
            (Default: True)
        :param threshold: Percentage of the maximum value of the TFR, below
            which all values are set to zero before plotting.
        :param **kwargs: parameters passed to the plotting function.
        :type ax: matplotlib.axes.Axes object
        :type kind: str
        :type sqmod: bool
        :type threshold: float
        :return: None
        :rtype: None
        Nr   r   g        )ÚaxÚkindÚ	threshold)
r    r   r	   r#   r   ÚabsZamaxr   r   Úplot)r   r+   r,   Zsqmodr-   ÚkwargsZ
_thresholdr   r   r   r/   M   s    "ÿþzShortTimeFourierTransform.plot)NNN)Nr)   Tr*   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer   r(   r/   Ú__classcell__r   r   r   r   r      s
   r   c              	   C   s  |du rt | jd ƒ\}}|du r.t |ƒ\}}|du rlt t d¡t ddt|ƒ¡d  ¡}|tj |¡ }t	|| jd  t
|ƒ ƒ}t	| jd t
|ƒ ƒ}t	| jd t
|ƒ ƒ}|jd }|d dkrÐtdƒ‚t d| jd  t
|ƒ d | d|  ¡}	t | jd f¡}
t | jd |d  d ¡|	 }t | jd | d d ¡|	 }||
t |d |¡ t	¡< |
jt	|ƒt	|ƒdd	}tjj|jdd
t |¡ }t ||f¡}tjd|d t
d}t|ƒD ]D}t||| |  |ƒ t	¡}|t ||d dd…f ¡d 7 }qÆd||t d¡k < || }t t|ƒ¡| jd  }tj| jd |ftd}t d| jd d ¡}t|ƒD ]X}t||| |  | jd ƒ t	¡d }tjj| tj || ¡ dd
|dd…|f< qt|tj| jd |d t	¡dd…f }t |¡d }|||fS )aÒ  Compute the Gabor representation of a signal.

    :param signal: Singal to be analyzed.
    :param n_coeff: number of Gabor coefficients in time.
    :param q_oversample: Degree of oversampling
    :param window: Synthesis window
    :type signal: array-like
    :type n_coeff: integer
    :type q_oversample: int
    :type window: array-like
    :return: Tuple of Gabor coefficients, biorthogonal window associated with the synthesis window.
    :rtype: tuple
    Nr   g{®Gázt?éÿÿÿÿr   r   z.The window function should have an odd length.ÚF)Úorderr   )Zdtype)Ústep)r   r   r   ÚexpÚlogÚlinspacer   ZlinalgZnormr   ÚfloatÚ
ValueErrorr   Úzerosr   r   Zreshaper"   ÚTÚsqrtr   r   r.   ÚspacingÚrealr   ÚcomplexZfftshift)r   Zn_coeffZq_oversampleZwindowÚ_ÚmZmbÚnbZnhÚalphaZhn1r$   r%   ZmsigZdzthZmzhÚxÚlÚmodZdztgamZgamZdgrn1ÚkÚnr'   Zdgrr    r   r   r   Úgaborj   sH    &
,  &&0&rO   Ú__main__)Úfmconsté€   gš™™™™™É?gš™™™™™Ù?r   é   )r
   T)Zshow_tfr)   )NNN)r4   Únumpyr   Ztftb.processing.baser   Z
tftb.utilsr   r   r   r   r   rO   r1   Ztftb.generatorsrQ   Zmatplotlib.pyplotZpyplotZpltZr_Úsigr=   r   r    r(   r/   ÚcmZviridisr   r   r   r   Ú<module>	   s   X
<"