コード例 #1
0
 def _compare(self, signals, norm, dct_type, atol=5e-4, rtol=5e-4):
   """Compares (I)DCT to SciPy (if available) and a NumPy implementation."""
   np_dct = NP_DCT[dct_type](signals, norm)
   tf_dct = dct_ops.dct(signals, type=dct_type, norm=norm).eval()
   self.assertAllClose(np_dct, tf_dct, atol=atol, rtol=rtol)
   np_idct = NP_IDCT[dct_type](signals, norm)
   tf_idct = dct_ops.idct(signals, type=dct_type, norm=norm).eval()
   self.assertAllClose(np_idct, tf_idct, atol=atol, rtol=rtol)
   if fftpack:
     scipy_dct = fftpack.dct(signals, type=dct_type, norm=norm)
     self.assertAllClose(scipy_dct, tf_dct, atol=atol, rtol=rtol)
     scipy_idct = fftpack.idct(signals, type=dct_type, norm=norm)
     self.assertAllClose(scipy_idct, tf_idct, atol=atol, rtol=rtol)
   # Verify inverse(forward(s)) == s, up to a normalization factor.
   tf_idct_dct = dct_ops.idct(
       tf_dct, type=dct_type, norm=norm).eval()
   tf_dct_idct = dct_ops.dct(
       tf_idct, type=dct_type, norm=norm).eval()
   if norm is None:
     if dct_type == 1:
       tf_idct_dct *= 0.5 / (signals.shape[-1] - 1)
       tf_dct_idct *= 0.5 / (signals.shape[-1] - 1)
     else:
       tf_idct_dct *= 0.5 / signals.shape[-1]
       tf_dct_idct *= 0.5 / signals.shape[-1]
   self.assertAllClose(signals, tf_idct_dct, atol=atol, rtol=rtol)
   self.assertAllClose(signals, tf_dct_idct, atol=atol, rtol=rtol)
コード例 #2
0
ファイル: dct_ops_test.py プロジェクト: Harryi0/tinyML
 def _compare(self, signals, n, norm, dct_type, atol, rtol):
     """Compares (I)DCT to SciPy (if available) and a NumPy implementation."""
     np_dct = NP_DCT[dct_type](signals, n=n, norm=norm)
     tf_dct = dct_ops.dct(signals, n=n, type=dct_type, norm=norm)
     self.assertEqual(tf_dct.dtype.as_numpy_dtype, signals.dtype)
     self.assertAllClose(np_dct, tf_dct, atol=atol, rtol=rtol)
     np_idct = NP_IDCT[dct_type](signals, n=None, norm=norm)
     tf_idct = dct_ops.idct(signals, type=dct_type, norm=norm)
     self.assertEqual(tf_idct.dtype.as_numpy_dtype, signals.dtype)
     self.assertAllClose(np_idct, tf_idct, atol=atol, rtol=rtol)
     if fftpack and dct_type != 4:
         scipy_dct = fftpack.dct(signals, n=n, type=dct_type, norm=norm)
         self.assertAllClose(scipy_dct, tf_dct, atol=atol, rtol=rtol)
         scipy_idct = fftpack.idct(signals, type=dct_type, norm=norm)
         self.assertAllClose(scipy_idct, tf_idct, atol=atol, rtol=rtol)
     # Verify inverse(forward(s)) == s, up to a normalization factor.
     # Since `n` is not implemented for IDCT operation, re-calculating tf_dct
     # without n.
     tf_dct = dct_ops.dct(signals, type=dct_type, norm=norm)
     tf_idct_dct = dct_ops.idct(tf_dct, type=dct_type, norm=norm)
     tf_dct_idct = dct_ops.dct(tf_idct, type=dct_type, norm=norm)
     if norm is None:
         if dct_type == 1:
             tf_idct_dct *= 0.5 / (signals.shape[-1] - 1)
             tf_dct_idct *= 0.5 / (signals.shape[-1] - 1)
         else:
             tf_idct_dct *= 0.5 / signals.shape[-1]
             tf_dct_idct *= 0.5 / signals.shape[-1]
     self.assertAllClose(signals, tf_idct_dct, atol=atol, rtol=rtol)
     self.assertAllClose(signals, tf_dct_idct, atol=atol, rtol=rtol)
コード例 #3
0
 def _compare(self, signals, norm, dct_type, atol=5e-4, rtol=5e-4):
     """Compares (I)DCT to SciPy (if available) and a NumPy implementation."""
     np_dct = NP_DCT[dct_type](signals, norm)
     tf_dct = dct_ops.dct(signals, type=dct_type, norm=norm).eval()
     self.assertAllClose(np_dct, tf_dct, atol=atol, rtol=rtol)
     np_idct = NP_IDCT[dct_type](signals, norm)
     tf_idct = dct_ops.idct(signals, type=dct_type, norm=norm).eval()
     self.assertAllClose(np_idct, tf_idct, atol=atol, rtol=rtol)
     if fftpack:
         scipy_dct = fftpack.dct(signals, type=dct_type, norm=norm)
         self.assertAllClose(scipy_dct, tf_dct, atol=atol, rtol=rtol)
         scipy_idct = fftpack.idct(signals, type=dct_type, norm=norm)
         self.assertAllClose(scipy_idct, tf_idct, atol=atol, rtol=rtol)
     # Verify inverse(forward(s)) == s, up to a normalization factor.
     tf_idct_dct = dct_ops.idct(tf_dct, type=dct_type, norm=norm).eval()
     tf_dct_idct = dct_ops.dct(tf_idct, type=dct_type, norm=norm).eval()
     if norm is None:
         if dct_type == 1:
             tf_idct_dct *= 0.5 / (signals.shape[-1] - 1)
             tf_dct_idct *= 0.5 / (signals.shape[-1] - 1)
         else:
             tf_idct_dct *= 0.5 / signals.shape[-1]
             tf_dct_idct *= 0.5 / signals.shape[-1]
     self.assertAllClose(signals, tf_idct_dct, atol=atol, rtol=rtol)
     self.assertAllClose(signals, tf_dct_idct, atol=atol, rtol=rtol)
コード例 #4
0
def inverse_stdct(stdcts,
                 frame_length,
                 frame_step,
                 fft_length=None,
                 window_fn=window_ops.hann_window,
                 name=None):
  """
	Inverse short-time discrete cosine transform.

	Argument/s:

	Returns:
  """
  with ops.name_scope(name, 'inverse_stdct', [stdcts]):
    stdcts = ops.convert_to_tensor(stdcts, name='stdcts')
    stdcts.shape.with_rank_at_least(2)
    frame_length = ops.convert_to_tensor(frame_length, name='frame_length')
    frame_length.shape.assert_has_rank(0)
    frame_step = ops.convert_to_tensor(frame_step, name='frame_step')
    frame_step.shape.assert_has_rank(0)
    if fft_length is None:
      fft_length = _enclosing_power_of_two(frame_length)
    else:
      fft_length = ops.convert_to_tensor(fft_length, name='fft_length')
      fft_length.shape.assert_has_rank(0)

    frames = dct_ops.idct(stdcts, n=fft_length)

    # frame_length may be larger or smaller than fft_length, so we pad or
    # truncate frames to frame_length.
    frame_length_static = tensor_util.constant_value(frame_length)
    # If we don't know the shape of frames's inner dimension, pad and
    # truncate to frame_length.
    if (frame_length_static is None or frames.shape.ndims is None or
        frames.shape.as_list()[-1] is None):
      frames = frames[..., :frame_length]
      frames_rank = array_ops.rank(frames)
      frames_shape = array_ops.shape(frames)
      paddings = array_ops.concat(
          [array_ops.zeros([frames_rank - 1, 2],
                           dtype=frame_length.dtype),
           [[0, math_ops.maximum(0, frame_length - frames_shape[-1])]]], 0)
      frames = array_ops.pad(frames, paddings)
    # We know frames's last dimension and frame_length statically. If they
    # are different, then pad or truncate frames to frame_length.
    elif frames.shape.as_list()[-1] > frame_length_static:
      frames = frames[..., :frame_length_static]
    elif frames.shape.as_list()[-1] < frame_length_static:
      pad_amount = frame_length_static - frames.shape.as_list()[-1]
      frames = array_ops.pad(frames,
                                  [[0, 0]] * (frames.shape.ndims - 1) +
                                  [[0, pad_amount]])

    # The above code pads the inner dimension of frames to frame_length,
    # but it does so in a way that may not be shape-inference friendly.
    # Restore shape information if we are able to.
    if frame_length_static is not None and frames.shape.ndims is not None:
      frames.set_shape([None] * (frames.shape.ndims - 1) +
                            [frame_length_static])

    # Optionally window and overlap-add the inner 2 dimensions of frames
    # into a single [samples] dimension.
    if window_fn is not None:
      window = window_fn(frame_length, dtype=stdcts.dtype.real_dtype)
      frames *= window
    return reconstruction_ops.overlap_and_add(frames, frame_step)