code: def autocorrelation(input, dim=0):
      """
      Computes the autocorrelation of samples at dimension ``dim``.
  
      Reference: https://en.wikipedia.org/wiki/Autocorrelation#Efficient_computation
  
      :param torch.Tensor input: the input tensor.
      :param int dim: the dimension to calculate autocorrelation.
      :returns torch.Tensor: autocorrelation of ``input``.
      """
      # Adapted from Stan implementation
      # https://github.com/stan-dev/math/blob/develop/stan/math/prim/mat/fun/autocorrelation.hpp
      N = input.size(dim)
      M = next_fast_len(N)
      M2 = 2 * M
  
      # transpose dim with -1 for Fourier transform
      input = input.transpose(dim, -1)
  
      # centering and padding x
      centered_signal = input - input.mean(dim=-1, keepdim=True)
  
      # Fourier transform
      freqvec = torch.view_as_real(rfft(centered_signal, n=M2))
      # take square of magnitude of freqvec (or freqvec x freqvec*)
      freqvec_gram = freqvec.pow(2).sum(-1)
      # inverse Fourier transform
      autocorr = irfft(freqvec_gram, n=M2)
  
      # truncate and normalize the result, setting autocorrelation to 1 for all
      # constant channels
      autocorr = autocorr[..., :N]
      autocorr = autocorr / torch.tensor(
          range(N, 0, -1), dtype=input.dtype, device=input.device
      )
      variance = autocorr[..., :1]
      constant = (variance == 0).expand_as(autocorr)
      autocorr = autocorr / variance.clamp(
          min=torch.finfo(variance.dtype).tiny
      )
      autocorr[constant] = 1
  
      # transpose back to original shape
      return autocorr.transpose(dim, -1)
  
imports: import math
  import numbers
  import torch
  from torch.fft import irfft, rfft
  from .tensor_utils import next_fast_len
label_desc: Use image node_id 4 for calling a global function with example usage: autocorrelation(input, dim) and returns: autocorr