(audio,
             n_fft=512,
             hop_length=None,
             mask=True,
             log_mag=True,
             re_im=False,
             dphase=True,
             mag_only=False)
return spec_real
Spectrogram using librosa.

  Args:
    audio: 1-D array of float32 sound samples.
    n_fft: Size of the FFT.
    hop_length: Stride of FFT. Defaults to n_fft/2.
    mask: Mask the phase derivative by the magnitude.
    log_mag: Use the logamplitude.
    re_im: Output Real and Imag. instead of logMag and dPhase.
    dphase: Use derivative of phase instead of phase.
    mag_only: Don't return phase.

  Returns:
    specgram: [n_fft/2 + 1, audio.size / hop_length, 2]. The first channel is
      the logamplitude and the second channel is the derivative of phase.
Spectrogram using librosa.
Spectrogram using librosa .
def specgram(audio,
             n_fft=512,
             hop_length=None,
             mask=True,
             log_mag=True,
             re_im=False,
             dphase=True,
             mag_only=False):
  """Spectrogram using librosa.

  Args:
    audio: 1-D array of float32 sound samples.
    n_fft: Size of the FFT.
    hop_length: Stride of FFT. Defaults to n_fft/2.
    mask: Mask the phase derivative by the magnitude.
    log_mag: Use the logamplitude.
    re_im: Output Real and Imag. instead of logMag and dPhase.
    dphase: Use derivative of phase instead of phase.
    mag_only: Don't return phase.

  Returns:
    specgram: [n_fft/2 + 1, audio.size / hop_length, 2]. The first channel is
      the logamplitude and the second channel is the derivative of phase.
  """
  if not hop_length:
    hop_length = int(n_fft / 2.)

  fft_config = dict(
      n_fft=n_fft, win_length=n_fft, hop_length=hop_length, center=True)

  spec = librosa.stft(audio, **fft_config)

  if re_im:
    re = spec.real[:, :, np.newaxis]
    im = spec.imag[:, :, np.newaxis]
    spec_real = np.concatenate((re, im), axis=2)

  else:
    mag, phase = librosa.core.magphase(spec)
    phase_angle = np.angle(phase)

    # Magnitudes, scaled 0-1
    if log_mag:
      mag = (librosa.power_to_db(
          mag**2, amin=1e-13, top_db=120., ref=np.max) / 120.) + 1
    else:
      mag /= mag.max()

    if dphase:
      #  Derivative of phase
      phase_unwrapped = np.unwrap(phase_angle)
      p = phase_unwrapped[:, 1:] - phase_unwrapped[:, :-1]
      p = np.concatenate([phase_unwrapped[:, 0:1], p], axis=1) / np.pi
    else:
      # Normal phase
      p = phase_angle / np.pi
    # Mask the phase
    if log_mag and mask:
      p = mag * p
    # Return Mag and Phase
    p = p.astype(np.float32)[:, :, np.newaxis]
    mag = mag.astype(np.float32)[:, :, np.newaxis]
    if mag_only:
      spec_real = mag[:, :, np.newaxis]
    else:
      spec_real = np.concatenate((mag, p), axis=2)
  return spec_real
def specgram ( audio , n_fft = 512 , hop_length = None , mask = True , log_mag = True , re_im = False , dphase = True , mag_only = False ) : if not hop_length : hop_length = int ( n_fft / 2. ) fft_config = dict ( n_fft = n_fft , win_length = n_fft , hop_length = hop_length , center = True ) spec = librosa . stft ( audio , * * fft_config ) if re_im : re = spec . real [ : , : , np . newaxis ] im = spec . imag [ : , : , np . newaxis ] spec_real = np . concatenate ( ( re , im ) , axis = 2 ) else : mag , phase = librosa . core . magphase ( spec ) phase_angle = np . angle ( phase ) # Magnitudes, scaled 0-1 if log_mag : mag = ( librosa . power_to_db ( mag ** 2 , amin = 1e-13 , top_db = 120. , ref = np . max ) / 120. ) + 1 else : mag /= mag . max ( ) if dphase : #  Derivative of phase phase_unwrapped = np . unwrap ( phase_angle ) p = phase_unwrapped [ : , 1 : ] - phase_unwrapped [ : , : - 1 ] p = np . concatenate ( [ phase_unwrapped [ : , 0 : 1 ] , p ] , axis = 1 ) / np . pi else : # Normal phase p = phase_angle / np . pi # Mask the phase if log_mag and mask : p = mag * p # Return Mag and Phase p = p . astype ( np . float32 ) [ : , : , np . newaxis ] mag = mag . astype ( np . float32 ) [ : , : , np . newaxis ] if mag_only : spec_real = mag [ : , : , np . newaxis ] else : spec_real = np . concatenate ( ( mag , p ) , axis = 2 ) return spec_real