docstr: Test output length for audio to spectrogram.
  
  Create signals of arbitrary length and check output
  length is matching the actual transform length.
code: def test_audio_to_spec(self, fft_length: int, num_channels: int):
      """Test output length for audio to spectrogram.
  
      Create signals of arbitrary length and check output
      length is matching the actual transform length.
      """
      hop_lengths = [fft_length // 2, fft_length // 3, fft_length // 4]
      batch_size = 4
      num_examples = 20
      random_seed = 42
      atol = 1e-6
  
      _rng = np.random.default_rng(seed=random_seed)
  
      for n in range(num_examples):
          # Generate time-domain examples with different length
          input_length = _rng.integers(
              low=fft_length, high=100 * fft_length, size=batch_size
          )  # in samples
          x = _rng.normal(
              size=(batch_size, num_channels, np.max(input_length))
          )
          x = torch.tensor(x)
          for b in range(batch_size):
              x[b, :, input_length[b] :] = 0
  
          for hop_length in hop_lengths:
              # Prepare transform
              audio2spec = AudioToSpectrogram(
                  fft_length=fft_length, hop_length=hop_length
              )
  
              # Transform the whole batch
              batch_spec, batch_spec_len = audio2spec(
                  input=x, input_length=torch.tensor(input_length)
              )
  
              for b in range(batch_size):
                  # Transform just the current example
                  b_spec, b_spec_len = audio2spec(
                      input=x[b : b + 1, :, : input_length[b]]
                  )
                  actual_len = b_spec.size(-1)
  
                  # Check lengths
                  assert (
                      actual_len == b_spec_len
                  ), f"Output length not matching for example ({n}, {b}) with length {input_length[n]} (hop_length={hop_length}): true {actual_len} vs calculated {b_spec_len}."
  
                  assert (
                      actual_len == batch_spec_len[b]
                  ), f"Output length not matching for example ({n}, {b}) with length {input_length[n]} (hop_length={hop_length}): true {actual_len} vs calculated batch len {batch_spec_len[b]}."
  
                  # Make sure transforming a batch is the same as transforming individual examples
                  assert torch.allclose(
                      batch_spec[b, ..., :actual_len], b_spec, atol=atol
                  ), f"Spectrograms not matching for example ({n}, {b}) with length {input_length[b]} (hop_length={hop_length})"
  
file_path: NeMo/tests/collections/asr/test_audio_preprocessing.py
imports: import importlib
  import numpy
  import pytest
  import torch
  from nemo.collections.asr.modules.audio_preprocessing import AudioToSpectrogram, SpectrogramToAudio
label_desc: Use image node_id 1 for calling the TestAudioSpectrogram obj's underlying member method code with example usage: obj.test_audio_to_spec(fft_length, num_channels) without return types