toolbox/nt/transform/module_stft.py
def stft(
        time_signal,
        size: int=1024,
        shift: int=256,
        *,
        axis=-1,
        window: typing.Callable=signal.blackman,
        window_length: int=None,
        fading: bool=True,
        pad: bool=True,
        symmetric_window: bool=False,
) -> np.array:
    """
    ToDo: Open points:
     - sym_window need literature
     - fading why it is better?
     - should pad have more degrees of freedom?

    Calculates the short time Fourier transformation of a multi channel multi
    speaker time signal. It is able to add additional zeros for fade-in and
    fade out and should yield an STFT signal which allows perfect
    reconstruction.

    :param time_signal: Multi channel time signal with dimensions
        AA x ... x AZ x T x BA x ... x BZ.
    :param size: Scalar FFT-size.
    :param shift: Scalar FFT-shift, the step between successive frames in
        samples. Typically shift is a fraction of size.
    :param axis: Scalar axis of time.
        Default: None means the biggest dimension.
    :param window: Window function handle. Default is blackman window.
    :param fading: Pads the signal with zeros for better reconstruction.
    :param window_length: Sometimes one desires to use a shorter window than
        the fft size. In that case, the window is padded with zeros.
        The default is to use the fft-size as a window size.
    :param pad: If true zero pad the signal to match the shape, else cut
    :param symmetric_window: symmetric or periodic window. Assume window is
        periodic. Since the implementation of the windows in scipy.signal have a
        curious behaviour for odd window_length. Use window(len+1)[:-1]. Since
        is equal to the behaviour of MATLAB.
    :return: Single channel complex STFT signal with dimensions
        AA x ... x AZ x T' times size/2+1 times BA x ... x BZ.
    """
    time_signal = np.array(time_signal)

    axis = axis % time_signal.ndim

    if window_length is None:
        window_length = size

    # Pad with zeros to have enough samples for the window function to fade.
    if fading:
        pad_width = np.zeros((time_signal.ndim, 2), dtype=np.int)
        pad_width[axis, :] = window_length - shift
        time_signal = np.pad(time_signal, pad_width, mode='constant')

    if symmetric_window:
        window = window(window_length)
    else:
        # https://github.com/scipy/scipy/issues/4551
        window = window(window_length + 1)[:-1]

    time_signal_seg = segment_axis_v2(
        time_signal,
        window_length,
        shift=shift,
        axis=axis,
        end='pad' if pad else 'cut'
    )

    letters = string.ascii_lowercase[:time_signal_seg.ndim]
    mapping = letters + ',' + letters[axis + 1] + '->' + letters

    try:
        # ToDo: Implement this more memory efficient
        return rfft(
            np.einsum(mapping, time_signal_seg, window),
            n=size,
            axis=axis + 1
        )
    except ValueError as e:
        raise ValueError(
            f'Could not calculate the stft, something does not match.\n'
            f'mapping: {mapping}, '
            f'time_signal_seg.shape: {time_signal_seg.shape}, '
            f'window.shape: {window.shape}, '
            f'size: {size}'
            f'axis+1: {axis+1}'
        ) from e

    ToDo: Open points:
     - sym_window need literature
     - fading why it is better?
     - should pad have more degrees of freedom?

    Calculates the short time Fourier transformation of a multi channel multi
    speaker time signal. It is able to add additional zeros for fade-in and
    fade out and should yield an STFT signal which allows perfect
    reconstruction.

    :param time_signal: Multi channel time signal with dimensions
        AA x ... x AZ x T x BA x ... x BZ.
    :param size: Scalar FFT-size.
    :param shift: Scalar FFT-shift, the step between successive frames in
        samples. Typically shift is a fraction of size.
    :param axis: Scalar axis of time.
        Default: None means the biggest dimension.
    :param window: Window function handle. Default is blackman window.
    :param fading: Pads the signal with zeros for better reconstruction.
    :param window_length: Sometimes one desires to use a shorter window than
        the fft size. In that case, the window is padded with zeros.
        The default is to use the fft-size as a window size.
    :param pad: If true zero pad the signal to match the shape, else cut
    :param symmetric_window: symmetric or periodic window. Assume window is
        periodic. Since the implementation of the windows in scipy.signal have a
        curious behaviour for odd window_length. Use window(len+1)[:-1]. Since
        is equal to the behaviour of MATLAB.
    :return: Single channel complex STFT signal with dimensions
        AA x ... x AZ x T' times size/2+1 times BA x ... x BZ.
    
Open points:
sym_window need literature
fading why it is better.

Calculates the short time Fourier transformation of a multi channel multi
speaker time signal. It is able to add additional zeros for fade-in and
fade out and should yield an STFT signal which allows perfect
reconstruction.
Open points : sym_window need literature fading why it is better . Calculates the short time Fourier transformation of a multi channel multi speaker time signal . It is able to add additional zeros for fade - in and fade out and should yield an STFT signal which allows perfect reconstruction .
def stft(
        time_signal,
        size: int=1024,
        shift: int=256,
        *,
        axis=-1,
        window: typing.Callable=signal.blackman,
        window_length: int=None,
        fading: bool=True,
        pad: bool=True,
        symmetric_window: bool=False,
) -> np.array:
    time_signal = np.array(time_signal)
    axis = axis % time_signal.ndim
    if window_length is None:
        window_length = size
    if fading:
        pad_width = np.zeros((time_signal.ndim, 2), dtype=np.int)
        pad_width[axis, :] = window_length - shift
        time_signal = np.pad(time_signal, pad_width, mode='constant')
    if symmetric_window:
        window = window(window_length)
    else:
        window = window(window_length + 1)[:-1]
    time_signal_seg = segment_axis_v2(
        time_signal,
        window_length,
        shift=shift,
        axis=axis,
        end='pad' if pad else 'cut'
    )
    letters = string.ascii_lowercase[:time_signal_seg.ndim]
    mapping = letters + ',' + letters[axis + 1] + '->' + letters
    try:
        return rfft(
            np.einsum(mapping, time_signal_seg, window),
            n=size,
            axis=axis + 1
        )
    except ValueError as e:
        raise ValueError(
            f'Could not calculate the stft, something does not match.\n'
            f'mapping: {mapping}, '
            f'time_signal_seg.shape: {time_signal_seg.shape}, '
            f'window.shape: {window.shape}, '
            f'size: {size}'
            f'axis+1: {axis+1}'
        ) from e
def stft ( time_signal , size : int = 1024 , shift : int = 256 , * , axis = - 1 , window : typing . Callable = signal . blackman , window_length : int = None , fading : bool = True , pad : bool = True , symmetric_window : bool = False , ) -> np . array : time_signal = np . array ( time_signal ) axis = axis % time_signal . ndim if window_length is None : window_length = size if fading : pad_width = np . zeros ( ( time_signal . ndim , 2 ) , dtype = np . int ) pad_width [ axis , : ] = window_length - shift time_signal = np . pad ( time_signal , pad_width , mode = 'constant' ) if symmetric_window : window = window ( window_length ) else : window = window ( window_length + 1 ) [ : - 1 ] time_signal_seg = segment_axis_v2 ( time_signal , window_length , shift = shift , axis = axis , end = 'pad' if pad else 'cut' ) letters = string . ascii_lowercase [ : time_signal_seg . ndim ] mapping = letters + ',' + letters [ axis + 1 ] + '->' + letters try : return rfft ( np . einsum ( mapping , time_signal_seg , window ) , n = size , axis = axis + 1 ) except ValueError as e : raise ValueError ( f'Could not calculate the stft, something does not match.\n' f'mapping: {mapping}, ' f'time_signal_seg.shape: {time_signal_seg.shape}, ' f'window.shape: {window.shape}, ' f'size: {size}' f'axis+1: {axis+1}' ) from e
ToDo: Open points:
 sym_window need literature
 fading why it is better?
ToDo : Open points : sym_window need literature fading why it is better?
"""
    ToDo: Open points:
     - sym_window need literature
     - fading why it is better?
     - should pad have more degrees of freedom?

    Calculates the short time Fourier transformation of a multi channel multi
    speaker time signal. It is able to add additional zeros for fade-in and
    fade out and should yield an STFT signal which allows perfect
    reconstruction.

    :param time_signal: Multi channel time signal with dimensions
        AA x ... x AZ x T x BA x ... x BZ.
    :param size: Scalar FFT-size.
    :param shift: Scalar FFT-shift, the step between successive frames in
        samples. Typically shift is a fraction of size.
    :param axis: Scalar axis of time.
        Default: None means the biggest dimension.
    :param window: Window function handle. Default is blackman window.
    :param fading: Pads the signal with zeros for better reconstruction.
    :param window_length: Sometimes one desires to use a shorter window than
        the fft size. In that case, the window is padded with zeros.
        The default is to use the fft-size as a window size.
    :param pad: If true zero pad the signal to match the shape, else cut
    :param symmetric_window: symmetric or periodic window. Assume window is
        periodic. Since the implementation of the windows in scipy.signal have a
        curious behaviour for odd window_length. Use window(len+1)[:-1]. Since
        is equal to the behaviour of MATLAB.
    :return: Single channel complex STFT signal with dimensions
        AA x ... x AZ x T' times size/2+1 times BA x ... x BZ.
    """ # Pad with zeros to have enough samples for the window function to fade. # https://github.com/scipy/scipy/issues/4551 # ToDo: Implement this more memory efficient
Single channel complex STFT signal with dimensions
AA x ... x AZ x T' times size/2+1 times BA x ... x BZ.
Single channel complex STFT signal with dimensions AA x ... x AZ x T ' times size / 2 + 1 times BA x ... x BZ .
time_signal
Multi channel time signal with dimensions
AA x ...
Multi channel time signal with dimensions AA x ...
size
int
None
shift
int
Scalar FFT-shift, the step between successive frames in
samples. Typically shift is a fraction of size.
Scalar FFT - shift the step between successive frames in samples . Typically shift is a fraction of size .
axis
Scalar axis of time.
Default: None means the biggest dimension.
Scalar axis of time . Default : None means the biggest dimension .
window
typing.Callable
Window function handle. Default is blackman window.
Window function handle . Default is blackman window .
window_length
int
Sometimes one desires to use a shorter window than
the fft size. In that case, the window is padded with zeros.
The default is to use the fft-size as a window size.
Sometimes one desires to use a shorter window than the fft size . In that case the window is padded with zeros . The default is to use the fft - size as a window size .
fading
bool
Pads the signal with zeros for better reconstruction.
Pads the signal with zeros for better reconstruction .
pad
bool
If true zero pad the signal to match the shape, else cut
If true zero pad the signal to match the shape else cut
symmetric_window
bool
symmetric or periodic window. Assume window is
periodic. Since the implementation of the windows in scipy.signal have a
curious behaviour for odd window_length.
symmetric or periodic window . Assume window is periodic . Since the implementation of the windows in scipy . signal have a curious behaviour for odd window_length .