original_docstring: Use torch.nn.functional to perform 1d padding and conv.
  
      Arguments
      ---------
      waveform : tensor
          The tensor to perform operations on.
      kernel : tensor
          The filter to apply during convolution.
      padding : int or tuple
          The padding (pad_left, pad_right) to apply.
          If an integer is passed instead, this is passed
          to the conv1d function and pad_type is ignored.
      pad_type : str
          The type of padding to use. Passed directly to
          `torch.nn.functional.pad`, see PyTorch documentation
          for available options.
      stride : int
          The number of units to move each time convolution is applied.
          Passed to conv1d. Has no effect if `use_fft` is True.
      groups : int
          This option is passed to `conv1d` to split the input into groups for
          convolution. Input channels should be divisible by the number of groups.
      use_fft : bool
          When `use_fft` is passed `True`, then compute the convolution in the
          spectral domain using complex multiply. This is more efficient on CPU
          when the size of the kernel is large (e.g. reverberation). WARNING:
          Without padding, circular convolution occurs. This makes little
          difference in the case of reverberation, but may make more difference
          with different kernels.
      rotation_index : int
          This option only applies if `use_fft` is true. If so, the kernel is
          rolled by this amount before convolution to shift the output location.
  
      Returns
      -------
      The convolved waveform.
  
      Example
      -------
      from speechbrain.dataio.dataio import read_audio
      signal = read_audio('samples/audio_samples/example1.wav')
      signal = signal.unsqueeze(0).unsqueeze(2)
      kernel = torch.rand(1, 10, 1)
      signal = convolve1d(signal, kernel, padding=(9, 0))
      
docstring: Use torch.nn.functional to perform 1d padding and conv.
  Arguments
  
  waveform : tensor
  The tensor to perform operations on.
  kernel : tensor
  The filter to apply during convolution.
  padding : int or tuple
  The padding (pad_left, pad_right) to apply.
  If an integer is passed instead, this is passed
  to the conv1d function and pad_type is ignored.
  pad_type : str
  The type of padding to use. Passed directly to
  `torch.nn.functional.pad`, see PyTorch documentation
  for available options.
  stride : int
  The number of units to move each time convolution is applied.
  Passed to conv1d. Has no effect if `use_fft` is True.
  groups : int
  This option is passed to `conv1d` to split the input into groups for
  convolution. Input channels should be divisible by the number of groups.
  use_fft : bool
  When `use_fft` is passed `True`, then compute the convolution in the
  spectral domain using complex multiply. This is more efficient on CPU
  when the size of the kernel is large . WARNING:
  Without padding, circular convolution occurs. This makes little
  difference in the case of reverberation, but may make more difference
  with different kernels.
  rotation_index : int
  This option only applies if `use_fft` is true. If so, the kernel is
  rolled by this amount before convolution to shift the output location.
  
  Returns
  
  The convolved waveform.
  
  Example
  
  
docstring_tokens: Use torch . nn . functional to perform 1d padding and conv . Arguments waveform : tensor The tensor to perform operations on . kernel : tensor The filter to apply during convolution . padding : int or tuple The padding ( pad_left pad_right ) to apply . If an integer is passed instead this is passed to the conv1d function and pad_type is ignored . pad_type : str The type of padding to use . Passed directly to ` torch . nn . functional . pad ` see PyTorch documentation for available options . stride : int The number of units to move each time convolution is applied . Passed to conv1d . Has no effect if ` use_fft ` is True . groups : int This option is passed to ` conv1d ` to split the input into groups for convolution . Input channels should be divisible by the number of groups . use_fft : bool When ` use_fft ` is passed ` True ` then compute the convolution in the spectral domain using complex multiply . This is more efficient on CPU when the size of the kernel is large . WARNING : Without padding circular convolution occurs . This makes little difference in the case of reverberation but may make more difference with different kernels . rotation_index : int This option only applies if ` use_fft ` is true . If so the kernel is rolled by this amount before convolution to shift the output location . Returns The convolved waveform . Example
code: def convolve1d(
          waveform,
          kernel,
          padding=0,
          pad_type="constant",
          stride=1,
          groups=1,
          use_fft=False,
          rotation_index=0,
  ):
      """Use torch.nn.functional to perform 1d padding and conv.
  
      Arguments
      ---------
      waveform : tensor
          The tensor to perform operations on.
      kernel : tensor
          The filter to apply during convolution.
      padding : int or tuple
          The padding (pad_left, pad_right) to apply.
          If an integer is passed instead, this is passed
          to the conv1d function and pad_type is ignored.
      pad_type : str
          The type of padding to use. Passed directly to
          `torch.nn.functional.pad`, see PyTorch documentation
          for available options.
      stride : int
          The number of units to move each time convolution is applied.
          Passed to conv1d. Has no effect if `use_fft` is True.
      groups : int
          This option is passed to `conv1d` to split the input into groups for
          convolution. Input channels should be divisible by the number of groups.
      use_fft : bool
          When `use_fft` is passed `True`, then compute the convolution in the
          spectral domain using complex multiply. This is more efficient on CPU
          when the size of the kernel is large (e.g. reverberation). WARNING:
          Without padding, circular convolution occurs. This makes little
          difference in the case of reverberation, but may make more difference
          with different kernels.
      rotation_index : int
          This option only applies if `use_fft` is true. If so, the kernel is
          rolled by this amount before convolution to shift the output location.
  
      Returns
      -------
      The convolved waveform.
  
      Example
      -------
      from speechbrain.dataio.dataio import read_audio
      signal = read_audio('samples/audio_samples/example1.wav')
      signal = signal.unsqueeze(0).unsqueeze(2)
      kernel = torch.rand(1, 10, 1)
      signal = convolve1d(signal, kernel, padding=(9, 0))
      """
      if len(waveform.shape) != 3:
          raise ValueError("Convolve1D expects a 3-dimensional tensor")
  
      # Move time dimension last, which pad and fft and conv expect.
      waveform = waveform.transpose(2, 1)
      kernel = kernel.transpose(2, 1)
  
      # Padding can be a tuple (left_pad, right_pad) or an int
      if isinstance(padding, tuple):
          waveform = torch.nn.functional.pad(
              input=waveform, pad=padding, mode=pad_type,
          )
  
      # This approach uses FFT, which is more efficient if the kernel is large
      if use_fft:
  
          # Pad kernel to same length as signal, ensuring correct alignment
          zero_length = waveform.size(-1) - kernel.size(-1)
  
          # Handle case where signal is shorter
          if zero_length < 0:
              kernel = kernel[..., :zero_length]
              zero_length = 0
  
          # Perform rotation to ensure alignment
          zeros = torch.zeros(
              kernel.size(0), kernel.size(1), zero_length, device=kernel.device
          )
          after_index = kernel[..., rotation_index:]
          before_index = kernel[..., :rotation_index]
          kernel = torch.cat((after_index, zeros, before_index), dim=-1)
  
          # Multiply in frequency domain to convolve in time domain
          if version.parse(torch.__version__) > version.parse("1.6.0"):
              import torch.fft as fft
  
              result = fft.rfft(waveform) * fft.rfft(kernel)
              convolved = fft.irfft(result, n=waveform.size(-1))
          else:
              f_signal = torch.rfft(waveform, 1)
              f_kernel = torch.rfft(kernel, 1)
              sig_real, sig_imag = f_signal.unbind(-1)
              ker_real, ker_imag = f_kernel.unbind(-1)
              f_result = torch.stack(
                  [
                      sig_real * ker_real - sig_imag * ker_imag,
                      sig_real * ker_imag + sig_imag * ker_real,
                  ],
                  dim=-1,
              )
              convolved = torch.irfft(
                  f_result, 1, signal_sizes=[waveform.size(-1)]
              )
  
      # Use the implementation given by torch, which should be efficient on GPU
      else:
          convolved = torch.nn.functional.conv1d(
              input=waveform,
              weight=kernel,
              stride=stride,
              groups=groups,
              padding=padding if not isinstance(padding, tuple) else 0,
          )
  
      # Return time dimension to the second dimension.
      return convolved.transpose(2, 1)
code_tokens: def convolve1d ( waveform , kernel , padding = 0 , pad_type = "constant" , stride = 1 , groups = 1 , use_fft = False , rotation_index = 0 , ) : if len ( waveform . shape ) != 3 : raise ValueError ( "Convolve1D expects a 3-dimensional tensor" ) waveform = waveform . transpose ( 2 , 1 ) kernel = kernel . transpose ( 2 , 1 ) if isinstance ( padding , tuple ) : waveform = torch . nn . functional . pad ( input = waveform , pad = padding , mode = pad_type , ) if use_fft : zero_length = waveform . size ( - 1 ) - kernel . size ( - 1 ) if zero_length < 0 : kernel = kernel [ ... , : zero_length ] zero_length = 0 zeros = torch . zeros ( kernel . size ( 0 ) , kernel . size ( 1 ) , zero_length , device = kernel . device ) after_index = kernel [ ... , rotation_index : ] before_index = kernel [ ... , : rotation_index ] kernel = torch . cat ( ( after_index , zeros , before_index ) , dim = - 1 ) if version . parse ( torch . __version__ ) > version . parse ( "1.6.0" ) : import torch . fft as fft result = fft . rfft ( waveform ) * fft . rfft ( kernel ) convolved = fft . irfft ( result , n = waveform . size ( - 1 ) ) else : f_signal = torch . rfft ( waveform , 1 ) f_kernel = torch . rfft ( kernel , 1 ) sig_real , sig_imag = f_signal . unbind ( - 1 ) ker_real , ker_imag = f_kernel . unbind ( - 1 ) f_result = torch . stack ( [ sig_real * ker_real - sig_imag * ker_imag , sig_real * ker_imag + sig_imag * ker_real , ] , dim = - 1 , ) convolved = torch . irfft ( f_result , 1 , signal_sizes = [ waveform . size ( - 1 ) ] ) else : convolved = torch . nn . functional . conv1d ( input = waveform , weight = kernel , stride = stride , groups = groups , padding = padding if not isinstance ( padding , tuple ) else 0 , ) return convolved . transpose ( 2 , 1 )
short_docstring: Use torch.nn.functional to perform 1d padding and conv.
short_docstring_tokens: Use torch . nn . functional to perform 1d padding and conv .
comment: """Use torch.nn.functional to perform 1d padding and conv.
  
      Arguments
      ---------
      waveform : tensor
          The tensor to perform operations on.
      kernel : tensor
          The filter to apply during convolution.
      padding : int or tuple
          The padding (pad_left, pad_right) to apply.
          If an integer is passed instead, this is passed
          to the conv1d function and pad_type is ignored.
      pad_type : str
          The type of padding to use. Passed directly to
          `torch.nn.functional.pad`, see PyTorch documentation
          for available options.
      stride : int
          The number of units to move each time convolution is applied.
          Passed to conv1d. Has no effect if `use_fft` is True.
      groups : int
          This option is passed to `conv1d` to split the input into groups for
          convolution. Input channels should be divisible by the number of groups.
      use_fft : bool
          When `use_fft` is passed `True`, then compute the convolution in the
          spectral domain using complex multiply. This is more efficient on CPU
          when the size of the kernel is large (e.g. reverberation). WARNING:
          Without padding, circular convolution occurs. This makes little
          difference in the case of reverberation, but may make more difference
          with different kernels.
      rotation_index : int
          This option only applies if `use_fft` is true. If so, the kernel is
          rolled by this amount before convolution to shift the output location.
  
      Returns
      -------
      The convolved waveform.
  
      Example
      -------
      from speechbrain.dataio.dataio import read_audio
      signal = read_audio('samples/audio_samples/example1.wav')
      signal = signal.unsqueeze(0).unsqueeze(2)
      kernel = torch.rand(1, 10, 1)
      signal = convolve1d(signal, kernel, padding=(9, 0))
      """$SEP$# Move time dimension last, which pad and fft and conv expect.$SEP$# Padding can be a tuple (left_pad, right_pad) or an int$SEP$# This approach uses FFT, which is more efficient if the kernel is large$SEP$# Pad kernel to same length as signal, ensuring correct alignment$SEP$# Handle case where signal is shorter$SEP$# Perform rotation to ensure alignment$SEP$# Multiply in frequency domain to convolve in time domain$SEP$# Use the implementation given by torch, which should be efficient on GPU$SEP$# Return time dimension to the second dimension.