docstr: Test output length for spectrogram to audio.
  
  Create signals of arbitrary length and check output
  length is matching the actual transform length.
code: def test_spec_to_audio(self, fft_length: int, num_channels: int):
      """Test output length for spectrogram to audio.
  
      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 spectrogram examples with different lengths
          input_length = _rng.integers(
              low=10, high=100, size=batch_size
          )  # in frames
          input_shape = (
              batch_size,
              num_channels,
              fft_length // 2 + 1,
              np.max(input_length),
          )
          spec = _rng.normal(size=input_shape) + 1j * _rng.normal(
              size=input_shape
          )
          spec = torch.tensor(spec)
          spec[..., 0, :] = spec[..., 0, :].real
          spec[..., -1, :] = spec[..., -1, :].real
          for b in range(batch_size):
              spec[b, ..., input_length[b] :] = 0
  
          for hop_length in hop_lengths:
              # Prepare transform
              spec2audio = SpectrogramToAudio(
                  fft_length=fft_length, hop_length=hop_length
              )
  
              # Transform the whole batch
              batch_x, batch_x_len = spec2audio(
                  input=spec, input_length=torch.tensor(input_length)
              )
  
              for b in range(batch_size):
                  # Transform just the current example
                  b_x, b_x_len = spec2audio(
                      input=spec[b : b + 1, ..., : input_length[b]]
                  )
  
                  actual_len = b_x.size(-1)
  
                  # Check lengths
                  assert (
                      b_x_len == actual_len
                  ), f"Output length not matching for example ({n}, {b}) with {input_length[b]} frames (hop_length={hop_length}): true {actual_len} vs calculated {b_x_len}."
  
                  assert (
                      batch_x_len[b] == actual_len
                  ), f"Output length not matching for example ({n}, {b}) with {input_length[b]} frames (hop_length={hop_length}): true {actual_len} vs calculated batch {batch_x_len[b]}."
  
                  # Make sure transforming a batch is the same as transforming individual examples
                  if input_length[b] < spec.size(-1):
                      # Discard the last bit of the signal which differs due to number of frames in batch (with zero padded frames) vs individual (only valid frames).
                      # The reason for this difference is normalization with `window_sumsquare` of the inverse STFT. More specifically,
                      # batched and non-batched transform are using on a different number of frames.
                      tail_length = max(fft_length // 2 - hop_length, 0)
                  else:
                      tail_length = 0
                  valid_len = actual_len - tail_length
                  batch_x_valid = batch_x[b, :, :valid_len]
                  b_x_valid = b_x[..., :valid_len]
                  assert torch.allclose(
                      batch_x_valid, b_x_valid, atol=atol
                  ), f"Signals not matching for example ({n}, {b}) with length {input_length[b]} (hop_length={hop_length}): max abs diff {torch.max(torch.abs(batch_x_valid-b_x_valid))} at {torch.argmax(torch.abs(batch_x_valid-b_x_valid))}"
  
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 2 for calling the TestAudioSpectrogram obj's underlying member method code with example usage: obj.test_spec_to_audio(fft_length, num_channels) without return types