o
    Ki                     @   sp   d dl Z d dlZd dlZd dlZdd Zdd fddZdd fd	d
Zdd Zdd ZdddZ	dddZ
dS )    Nc                 C   s    t t| jtdtjdS )N1)rounding)intdecimalDecimalquantizeROUND_HALF_UP)number r
   R/home/ubuntu/.local/lib/python3.10/site-packages/python_speech_features/sigproc.pyround_half_up	   s    r   c                 C      t | fS Nnumpyonesxr
   r
   r   <lambda>       r   c                 C   s   t | }tt|}tt|}||krd}ndttd| | |  }t|d | | }t|| f}t| |f}tt	d||dftt	d|| ||dfj
 }	tj|	tjd}	||	 }
t|||df}|
| S )a  Frame a signal into overlapping frames.

    :param sig: the audio signal to frame.
    :param frame_len: length of each frame measured in samples.
    :param frame_step: number of samples after the start of the previous frame that the next frame should begin.
    :param winfunc: the analysis window to apply to each frame. By default no window is applied.
    :returns: an array of frames. Size is NUMFRAMES by frame_len.
             ?r   dtype)lenr   r   mathceilr   zerosconcatenatetilearangeTarrayint32)sig	frame_len
frame_stepwinfuncslen	numframespadlenr   	padsignalindicesframeswinr
   r
   r   framesig   s   	8r/   c                 C   r   r   r   r   r
   r
   r   r   *   r   c                 C   s@  t |}t |}t| d }t| d |ksJ dttd||dfttd|| ||dfj }tj|tjd}|d | | }|dkrN|}t|f}t|f}	||}
t	d|D ]2}|	||ddf  |
 d |	||ddf < |||ddf  | |ddf  |||ddf < qc||	 }|d| S )a	  Does overlap-add procedure to undo the action of framesig.

    :param frames: the array of frames.
    :param siglen: the length of the desired signal, use 0 if unknown. Output will be truncated to siglen samples.
    :param frame_len: length of each frame measured in samples.
    :param frame_step: number of samples after the start of the previous frame that the next frame should begin.
    :param winfunc: the analysis window to apply to each frame. By default no window is applied.
    :returns: a 1-D signal.
    r   r   z@"frames" matrix is wrong size, 2nd dim is not equal to frame_lenr   NgV瞯<)
r   r   shaper   r    r!   r"   r#   r   range)r-   siglenr%   r&   r'   r)   r,   r*   
rec_signalwindow_correctionr.   ir
   r
   r   
deframesig*   s    
8,6r6   c                 C   sB   t | d |krtdt | d | t j| |}t |S )a  Compute the magnitude spectrum of each frame in frames. If frames is an NxD matrix, output will be Nx(NFFT/2+1).

    :param frames: the array of frames. Each row is a frame.
    :param NFFT: the FFT length to use. If NFFT > frame_len, the frames are zero-padded.
    :returns: If frames is an NxD matrix, output will be Nx(NFFT/2+1). Each row will be the magnitude spectrum of the corresponding frame.
    r   zaframe length (%d) is greater than FFT size (%d), frame will be truncated. Increase NFFT to avoid.)r   r0   loggingwarnfftrfftabsolute)r-   NFFTcomplex_specr
   r
   r   magspecJ   s   
r>   c                 C   s   d| t t| | S )a  Compute the power spectrum of each frame in frames. If frames is an NxD matrix, output will be Nx(NFFT/2+1).

    :param frames: the array of frames. Each row is a frame.
    :param NFFT: the FFT length to use. If NFFT > frame_len, the frames are zero-padded.
    :returns: If frames is an NxD matrix, output will be Nx(NFFT/2+1). Each row will be the power spectrum of the corresponding frame.
    r   )r   squarer>   )r-   r<   r
   r
   r   powspecV   s   r@   r   c                 C   s:   t | |}d||dk< dt| }|r|t| S |S )a  Compute the log power spectrum of each frame in frames. If frames is an NxD matrix, output will be Nx(NFFT/2+1).

    :param frames: the array of frames. Each row is a frame.
    :param NFFT: the FFT length to use. If NFFT > frame_len, the frames are zero-padded.
    :param norm: If norm=1, the log power spectrum is normalised so that the max value (across all frames) is 0.
    :returns: If frames is an NxD matrix, output will be Nx(NFFT/2+1). Each row will be the log power spectrum of the corresponding frame.
    gKH9
   )r@   r   log10max)r-   r<   normpslpsr
   r
   r   
logpowspec_   s   
rG   ffffff?c                 C   s(   t | d | dd || dd   S )zperform preemphasis on the input signal.

    :param signal: The signal to filter.
    :param coeff: The preemphasis coefficient. 0 is no filter, default is 0.95.
    :returns: the filtered signal.
    r   r   N)r   append)signalcoeffr
   r
   r   preemphasiso   s   (rM   )r   )rH   )r   r   r   r7   r   r/   r6   r>   r@   rG   rM   r
   r
   r
   r   <module>   s    
	