def test_inverse_stft_window_fn(self):
        """Test that inverse_stft_window_fn has unit gain at each window phase."""
        # Tuples of (frame_length, frame_step).
        test_configs = [
            (256, 32),
            (256, 64),
            (128, 25),
            (127, 32),
            (128, 64),
        ]

        for (frame_length, frame_step) in test_configs:
            hann_window = window_ops.hann_window(frame_length,
                                                 dtype=dtypes.float32)
            inverse_window_fn = spectral_ops.inverse_stft_window_fn(frame_step)
            inverse_window = inverse_window_fn(frame_length,
                                               dtype=dtypes.float32)

            with self.cached_session(use_gpu=True) as sess:
                hann_window, inverse_window = self.evaluate(
                    [hann_window, inverse_window])

            # Expect unit gain at each phase of the window.
            product_window = hann_window * inverse_window
            for i in range(frame_step):
                self.assertAllClose(1.0, np.sum(product_window[i::frame_step]))
    def test_inverse_stft_window_fn_special_case(self):
        """Test inverse_stft_window_fn in special overlap = 3/4 case."""
        # Cases in which frame_length is an integer multiple of 4 * frame_step are
        # special because they allow exact reproduction of the waveform with a
        # squared Hann window (Hann window in both forward and reverse transforms).
        # In the case where frame_length = 4 * frame_step, that combination
        # produces a constant gain of 1.5, and so the corrected window will be the
        # Hann window / 1.5.

        # Tuples of (frame_length, frame_step).
        test_configs = [
            (256, 64),
            (128, 32),
        ]

        for (frame_length, frame_step) in test_configs:
            hann_window = window_ops.hann_window(frame_length,
                                                 dtype=dtypes.float32)
            inverse_window_fn = spectral_ops.inverse_stft_window_fn(frame_step)
            inverse_window = inverse_window_fn(frame_length,
                                               dtype=dtypes.float32)

            with self.cached_session(use_gpu=True) as sess:
                hann_window, inverse_window = self.evaluate(
                    [hann_window, inverse_window])

            self.assertAllClose(hann_window, inverse_window * 1.5)
  def test_inverse_stft_window_fn_special_case(self):
    """Test inverse_stft_window_fn in special overlap = 3/4 case."""
    # Cases in which frame_length is an integer multiple of 4 * frame_step are
    # special because they allow exact reproduction of the waveform with a
    # squared Hann window (Hann window in both forward and reverse transforms).
    # In the case where frame_length = 4 * frame_step, that combination
    # produces a constant gain of 1.5, and so the corrected window will be the
    # Hann window / 1.5.

    # Tuples of (frame_length, frame_step).
    test_configs = [
        (256, 64),
        (128, 32),
    ]

    for (frame_length, frame_step) in test_configs:
      hann_window = window_ops.hann_window(frame_length, dtype=dtypes.float32)
      inverse_window_fn = spectral_ops.inverse_stft_window_fn(frame_step)
      inverse_window = inverse_window_fn(frame_length, dtype=dtypes.float32)

      with self.cached_session(use_gpu=True) as sess:
        hann_window, inverse_window = self.evaluate(
            [hann_window, inverse_window])

      self.assertAllClose(hann_window, inverse_window * 1.5)
def compute_spectrogram_tf(
        waveform,
        frame_length=2048, frame_step=512,
        spec_exponent=1., window_exponent=1.):
    """ Compute magnitude / power spectrogram from waveform as
    a n_samples x n_channels tensor.

    :param waveform:        Input waveform as (times x number of channels)
                            tensor.
    :param frame_length:    Length of a STFT frame to use.
    :param frame_step:      HOP between successive frames.
    :param spec_exponent:   Exponent of the spectrogram (usually 1 for
                            magnitude spectrogram, or 2 for power spectrogram).
    :param window_exponent: Exponent applied to the Hann windowing function
                            (may be useful for making perfect STFT/iSTFT
                            reconstruction).
    :returns:   Computed magnitude / power spectrogram as a
                (T x F x n_channels) tensor.
    """
    stft_tensor = tf.transpose(
        stft(
            tf.transpose(waveform),
            frame_length,
            frame_step,
            window_fn=lambda f, dtype: hann_window(
                f,
                periodic=True,
                dtype=waveform.dtype) ** window_exponent),
        perm=[1, 2, 0])
    return tf.abs(stft_tensor) ** spec_exponent
Exemple #5
0
  def test_inverse_stft_window_fn(self, frame_length, frame_step):
    """Test that inverse_stft_window_fn has unit gain at each window phase."""
    hann_window = window_ops.hann_window(frame_length, dtype=dtypes.float32)
    inverse_window_fn = spectral_ops.inverse_stft_window_fn(frame_step)
    inverse_window = inverse_window_fn(frame_length, dtype=dtypes.float32)
    hann_window, inverse_window = self.evaluate([hann_window, inverse_window])

    # Expect unit gain at each phase of the window.
    product_window = hann_window * inverse_window
    for i in range(frame_step):
      self.assertAllClose(1.0, np.sum(product_window[i::frame_step]))
Exemple #6
0
 def test_inverse_stft_window_fn_special_case(self, frame_length, frame_step):
   """Test inverse_stft_window_fn in special overlap = 3/4 case."""
   # Cases in which frame_length is an integer multiple of 4 * frame_step are
   # special because they allow exact reproduction of the waveform with a
   # squared Hann window (Hann window in both forward and reverse transforms).
   # In the case where frame_length = 4 * frame_step, that combination
   # produces a constant gain of 1.5, and so the corrected window will be the
   # Hann window / 1.5.
   hann_window = window_ops.hann_window(frame_length, dtype=dtypes.float32)
   inverse_window_fn = spectral_ops.inverse_stft_window_fn(frame_step)
   inverse_window = inverse_window_fn(frame_length, dtype=dtypes.float32)
   self.assertAllClose(hann_window, inverse_window * 1.5)
  def test_inverse_stft_window_fn(self):
    """Test that inverse_stft_window_fn has unit gain at each window phase."""
    # Tuples of (frame_length, frame_step).
    test_configs = [
        (256, 32),
        (256, 64),
        (128, 25),
        (127, 32),
        (128, 64),
    ]

    for (frame_length, frame_step) in test_configs:
      hann_window = window_ops.hann_window(frame_length, dtype=dtypes.float32)
      inverse_window_fn = spectral_ops.inverse_stft_window_fn(frame_step)
      inverse_window = inverse_window_fn(frame_length, dtype=dtypes.float32)

      with self.cached_session(use_gpu=True) as sess:
        hann_window, inverse_window = self.evaluate(
            [hann_window, inverse_window])

      # Expect unit gain at each phase of the window.
      product_window = hann_window * inverse_window
      for i in range(frame_step):
        self.assertAllClose(1.0, np.sum(product_window[i::frame_step]))