class_docstr: Implementation of the imperceptible attack against a speech recognition model.
  
  | Paper link: http://proceedings.mlr.press/v97/qin19a.html
docstr: Approximate the power spectral density for a perturbation `perturbation` in PyTorch.
  
  See also `ImperceptibleASR._approximate_power_spectral_density_tf`.
code: def _approximate_power_spectral_density_torch(
      self, perturbation: "PTensor", psd_maximum_stabilized: "PTensor"
  ) -> "PTensor":
      """
      Approximate the power spectral density for a perturbation `perturbation` in PyTorch.
  
      See also `ImperceptibleASR._approximate_power_spectral_density_tf`.
      """
      import torch
  
      # compute short-time Fourier transform (STFT)
      # pylint: disable=W0212
      stft_matrix = torch.view_as_real(
          torch.stft(
              perturbation,
              n_fft=self._window_size,
              hop_length=self._hop_size,
              win_length=self._window_size,
              center=False,
              window=torch.hann_window(self._window_size).to(
                  self.estimator._device
              ),
              return_complex=True,
          ).to(self.estimator._device)
      )
  
      # compute power spectral density (PSD)
      # note: fixes implementation of Qin et al. by also considering the square root of gain_factor
      gain_factor = np.sqrt(8.0 / 3.0)
      stft_matrix_abs = torch.sqrt(
          torch.sum(
              torch.square(
                  gain_factor * stft_matrix / self._window_size
              ),
              -1,
          )
      )
      psd_matrix = torch.square(stft_matrix_abs)
  
      # approximate normalized psd: psd_matrix_approximated = 10^((96.0 - psd_matrix_max + psd_matrix)/10)
      psd_matrix_approximated = (
          pow(10.0, 9.6)
          / psd_maximum_stabilized.reshape(-1, 1, 1)
          * psd_matrix
      )
  
      # return PSD matrix such that shape is (batch_size, window_size // 2 + 1, frame_length)
      return psd_matrix_approximated
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  from typing import TYPE_CHECKING, Optional, Tuple, Union
  import numpy
  import scipy.signal
  from art.attacks.attack import EvasionAttack
  from art.estimators.estimator import BaseEstimator, LossGradientsMixin, NeuralNetworkMixin
  from art.estimators.pytorch import PyTorchEstimator
  from art.estimators.speech_recognition.speech_recognizer import SpeechRecognizerMixin
  from art.estimators.tensorflow import TensorFlowV2Estimator
  from art.utils import pad_sequence_input
label_desc: Use image node_id 11 for calling the ImperceptibleASR obj's underlying member method code with example usage: obj._approximate_power_spectral_density_torch(perturbation, psd_maximum_stabilized) and returns: psd_matrix_approximated