parameters: (x, expk=None)
return_statement: return y[..., 0]+y[..., 1]
docstring: Batch Discrete Cosine Transformation without normalization to coefficients.
      Compute y_u = \sum_i  x_i cos(pi*(2i+1)*u/(2N)),
      Impelements the 2N padding trick to solve DCT with FFT in the following link,
      https://dsp.stackexchange.com/questions/2807/fast-cosine-transform-via-fft
  
      1. Pad x by zeros
      2. Perform FFT
      3. Multiply by 2*exp(-1j*pi*u/(2N))
      4. Extract the real part
docstring_summary: Batch Discrete Cosine Transformation without normalization to coefficients.
      Compute y_u = \sum_i  x_i cos(pi*(2i+1)*u/(2N)),
      Impelements the 2N padding trick to solve DCT with FFT in the following link,
      https://dsp.stackexchange.com/questions/2807/fast-cosine-transform-via-fft
docstring_tokens: Batch Discrete Cosine Transformation without normalization to coefficients . Compute y_u = \ sum_i x_i cos ( pi * ( 2i + 1 ) * u / ( 2N )) Impelements the 2N padding trick to solve DCT with FFT in the following link https : // dsp . stackexchange . com / questions / 2807 / fast - cosine - transform - via - fft
function: def dct_2N(x, expk=None):
      """ Batch Discrete Cosine Transformation without normalization to coefficients.
      Compute y_u = \sum_i  x_i cos(pi*(2i+1)*u/(2N)),
      Impelements the 2N padding trick to solve DCT with FFT in the following link,
      https://dsp.stackexchange.com/questions/2807/fast-cosine-transform-via-fft
  
      1. Pad x by zeros
      2. Perform FFT
      3. Multiply by 2*exp(-1j*pi*u/(2N))
      4. Extract the real part
      """
      # last dimension
      N = x.size(-1)
      # pad last dimension
      x_pad = F.pad(x, (0, N), 'constant', 0)
  
      # the last dimension here becomes -2 because complex numbers introduce a new dimension
      y = torch_fft_api.rfft(x_pad, signal_ndim=1, normalized=False, onesided=True)[..., 0:N, :]
      y.mul_(1.0/N)
  
      if expk is None:
          expk = get_expk(N, dtype=x.dtype, device=x.device)
  
      # get real part
      y.mul_(expk)
  
      # I found add is much faster than sum
      #y = y.sum(dim=-1)
      return y[..., 0]+y[..., 1]
function_tokens: def dct_2N ( x , expk = None ) : # last dimension N = x . size ( - 1 ) # pad last dimension x_pad = F . pad ( x , ( 0 , N ) , 'constant' , 0 ) # the last dimension here becomes -2 because complex numbers introduce a new dimension y = torch_fft_api . rfft ( x_pad , signal_ndim = 1 , normalized = False , onesided = True ) [ ... , 0 : N , : ] y . mul_ ( 1.0 / N ) if expk is None : expk = get_expk ( N , dtype = x . dtype , device = x . device ) # get real part y . mul_ ( expk ) # I found add is much faster than sum #y = y.sum(dim=-1) return y [ ... , 0 ] + y [ ... , 1 ]