cabbage972/GitChameleon-2.0 huggingface dataset:
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: numpy==1.16.0 scipy==1.1.0

Problem: Complete the function to compute the root mean square value for each frame. 

Starting code:
import librosa
import numpy as np

def compute_rms(y: np.ndarray) -> np.float32:
    

Solution:


    return librosa.feature.rms(y=y)

Test:

duration = 2.0 
frequency = 440 
sr = 22050 

t = np.linspace(0, duration, int(sr * duration), endpoint=False)

y = 0.5 * np.sin(2 * np.pi * frequency * t)
expect = np.array([
    [0.35406065, 0.35311503, 0.35384659, 0.35345521, 0.35343952, 0.35385957,
     0.35310695, 0.3540624,  0.35306794, 0.35393072, 0.35334823, 0.35355084,
     0.3537631,  0.35317223, 0.35403977, 0.35304415, 0.35399675, 0.35325105,
     0.35366224, 0.35365654, 0.35325578, 0.35399382, 0.35304479, 0.35404153,
     0.35316836, 0.3537684,  0.35354501, 0.35335359, 0.35392676, 0.35306983,
     0.35406292, 0.35310412, 0.35386422, 0.35343384, 0.35346095, 0.35384179,
     0.35311807, 0.35405989, 0.35306143, 0.35394511, 0.35332838, 0.3535727,
     0.35374297, 0.35318719, 0.35403259, 0.35304234, 0.35400722, 0.35323367,
     0.3536835,  0.35363504, 0.35327386, 0.35398233, 0.35304777, 0.35404758,
     0.35315429, 0.35378803, 0.35352315, 0.35337391, 0.3539115,  0.35307747,
     0.35406426, 0.35309403, 0.35388128, 0.35341269, 0.35348254, 0.35382349,
     0.35312999, 0.35405645, 0.35305581, 0.35395879, 0.35330893, 0.35359453,
     0.35372251, 0.35320281, 0.35402454, 0.35304147, 0.35401687, 0.35321688,
     0.35370452, 0.35361339, 0.35329244, 0.35397005, 0.35305168, 0.35405273,
     0.35314094, 0.35398808, 0.35359185]
])

assertion_value = np.allclose(expect, compute_rms(y))
assert assertion_value
Library: librosa==0.6.0 (Python 3.7, released 2018-02)
Additional dependencies: numpy==1.16.0 scipy==1.1.0

Problem: Complete the function to fill the off diagonal with a value of 0 with the constraint region being a Sakoe-Chiba band of radius 0.25. 

Starting code:
import librosa
import numpy as np

def compute_fill_diagonal(mut_x: np.ndarray, radius: float) -> np.ndarray:
    

Solution:


    return librosa.fill_off_diagonal(mut_x,  radius)

Test:

mut_x = np.ones((8, 12))
radius = 0.25

assertion_value = np.array_equal(librosa.fill_off_diagonal(mut_x,  radius), compute_fill_diagonal(mut_x, radius))
assert assertion_value
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: numpy==1.16.0 scipy==1.1.0

Problem: Complete the function to fill the off diagonal with a value of 0 with the constraint region being a Sakoe-Chiba band of radius 0.25. 

Starting code:
import librosa
import numpy as np

def compute_fill_diagonal(mut_x: np.ndarray, radius: float) -> np.ndarray:
    

Solution:


    return librosa.util.fill_off_diagonal(mut_x,  radius)

Test:

mut_x = np.ones((8, 12))
radius = 0.25
assertion_value = np.array_equal(librosa.util.fill_off_diagonal(mut_x,  radius), compute_fill_diagonal(mut_x, radius))
assert assertion_value
Library: librosa==0.6.0 (Python 3.7, released 2018-02)
Additional dependencies: numpy==1.16.0 scipy==1.1.0

Problem: Complete the function to extract melspectrogram from waveform y. After it is computed, determine if it is of type float64. Return both values as a tuple.

Starting code:
import librosa
import numpy as np
from typing import Tuple

def compute_extraction(y: np.ndarray, sr: int) -> Tuple[np.ndarray, bool]:
    

Solution:


    M_from_y = librosa.feature.melspectrogram(y=y, sr=sr) 
    return M_from_y, M_from_y.dtype == np.float64

Test:

duration = 2.0 
frequency = 440
sr = 22050 

t = np.linspace(0, duration, int(sr * duration), endpoint=False)

y = 0.5 * np.sin(2 * np.pi * frequency * t)
y = y.astype(np.float32)

sol=librosa.feature.melspectrogram(y=y, sr=sr) 
M_from_y, float64_bool = compute_extraction(y, sr)
assert np.array_equal(sol, M_from_y)
assert float64_bool
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: numpy==1.16.0 scipy==1.1.0

Problem: Complete the function to extract melspectrogram from waveform y. After it is computed, determine if it is of type float32.

Starting code:
import librosa
import numpy as np
from typing import Tuple

def compute_extraction(y: np.ndarray, sr: int) -> Tuple[np.ndarray, bool]:
    

Solution:


    M_from_y = librosa.feature.melspectrogram(y=y, sr=sr) 
    return M_from_y, M_from_y.dtype == np.float32


Test:

duration = 2.0 
frequency = 440 
sr = 22050 
t = np.linspace(0, duration, int(sr * duration), endpoint=False)

y = 0.5 * np.sin(2 * np.pi * frequency * t)
y = y.astype(np.float32)

sol=librosa.feature.melspectrogram(y=y, sr=sr) 
M_from_y, float32_bool = compute_extraction(y, sr)
assert np.array_equal(sol, M_from_y)
assert float32_bool
Library: librosa==0.6.0 (Python 3.7, released 2018-02)
Additional dependencies: scikit-learn==0.21.0 numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to iterate over an audio file using a stream and calculate the STFT on each mono channel.

Starting code:
import librosa
import numpy as np
import soundfile as sf 


# Save the stream in variable stream. Save each stream block with the array stream_blocks
def compute_stream(filename, y, sr, n_fft, hop_length):
    stream_blocks = []

Solution:


    stream = sf.blocks(filename, blocksize=n_fft + 15 * hop_length, overlap=n_fft - hop_length,  fill_value=0)

    for c, block in enumerate(stream):
        y = librosa.to_mono(block.T)
        D = librosa.stft(y, n_fft=n_fft, hop_length=hop_length, center=False)
        stream_blocks.append(D)

    return stream, stream_blocks

Test:


filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)

n_fft = 4096
hop_length = n_fft // 2

stream, stream_blocks = compute_stream(filename, y, sr, n_fft, hop_length)
sol_stream = sf.blocks(filename, blocksize=n_fft + 15 * hop_length, overlap=n_fft - hop_length, fill_value=0)
sol_blocks = []
for c, block in enumerate(sol_stream):
    y = librosa.to_mono(block.T)
    D = librosa.stft(y, n_fft=n_fft, hop_length=hop_length, center=False)
    sol_blocks.append(D)
for i in range(0, len(stream_blocks)):
    assert np.array_equal(sol_blocks[i], stream_blocks[i])
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: scikit-learn==0.21.0 numpy==1.16.0 scipy==1.1.0

Problem: Complete the function to complete the function to iterate over an audio file using a stream and calculate the STFT on each mono channel. Frame_length is given by n_fft. Save the stream and each stream block.

Starting code:
import librosa
import numpy as np

# Save the stream in variable stream. Save each stream block with the array stream_blocks
def compute_stream(y, sr, n_fft, hop_length):
    stream_blocks = []

Solution:



    stream =  librosa.stream(filename, block_length=16,
                        frame_length=n_fft,
                        hop_length=hop_length,
                        mono=True,
                        fill_value=0)

    for c, y_block in enumerate(stream):
        stream_blocks.append(librosa.stft(y_block, n_fft=n_fft, hop_length=hop_length, center=False))
    return stream, stream_blocks

Test:

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)

n_fft = 4096
hop_length = n_fft // 2
stream, stream_blocks = compute_stream(y, sr, n_fft, hop_length)
sol_stream =  librosa.stream(filename, block_length=16,
                        frame_length=n_fft,
                        hop_length=hop_length,
                        mono=True,
                        fill_value=0)
for c, y_block in enumerate(sol_stream):
    assertion_value = np.array_equal(librosa.stft(y_block, n_fft=n_fft, hop_length=hop_length, center=False), stream_blocks[c])
    assert  assertion_value
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to compute an approximate magnitude spectrogram inversion using the Griffin-Lim algorithm.

Starting code:
import librosa
import numpy as np
from librosa import istft, stft
from typing import Union, Optional

DTypeLike = Union[np.dtype, type]

def compute_griffinlim(y: np.ndarray, sr: int, S: np.ndarray, random_state: int, n_iter: int, hop_length: Optional[int], win_length: Optional[int], window: str, center: bool, dtype: DTypeLike, length: Optional[int], pad_mode: str, n_fft: int) -> np.ndarray:
    """
    Compute waveform from a linear scale magnitude spectrogram using the Griffin-Lim transformation.

    Parameters:
        y: Audio timeseries.
        sr: Sampling rate.
        S: short-time Fourier transform magnitude matrix.
        random_state: Random state for the random number generator.
        n_iter: Number of iterations.
        hop_length: Hop length.
        win_length: Window length.
        window: Window function.
        center: If True, the signal y is padded so that frame t is centered at y[t * hop_length]. If False, then frame t begins at y[t * hop_length].
        dtype: Data type of the output.
        length: Length of the output signal.
        pad_mode: Padding mode.
        n_fft: FFT size.

    Returns:
        The Griffin-Lim waveform.        
    """    
    rng = np.random.RandomState(seed=random_state)


Solution:


    return librosa.griffinlim(S, n_iter, hop_length, win_length, window, center, dtype, length, pad_mode, momentum, random_state)

Test:

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
momentum = 0.99
S = np.abs(librosa.stft(y))
random_state = 0
rng = np.random.RandomState(seed=random_state)
n_iter=32
hop_length=None
win_length=None
window='hann'
center=True
dtype=np.float32
length=None
pad_mode='reflect'
n_fft = 2 * (S.shape[0] - 1)

sol = compute_griffinlim(y, sr, S, random_state, n_iter, hop_length, win_length, window, center, dtype, length, pad_mode, n_fft)

rng = np.random.RandomState(seed=random_state)
test_sol = librosa.griffinlim(S, n_iter, hop_length, win_length, window, center, dtype, length, pad_mode, momentum, random_state)
assert np.allclose(test_sol, sol)
Library: librosa==0.6.0 (Python 3.7, released 2018-02)
Additional dependencies: numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to compute Linear Prediction coefficents of input array y.

Starting code:
import librosa
import numpy as np

def compute_lpc_coef(y: np.ndarray, sr: int, order: int) -> np.ndarray:
    """
    Compute the Linear Prediction Coefficients of an audio signal.

    Parameters:
        y: The audio signal.
        sr: The sampling rate of the audio signal in Hertz.
        order: Order of the linear filter.

    Returns:
        LP prediction error coefficients, i.e. filter denominator polynomial.
    """
    

Solution:

    
    dtype = y.dtype.type
    ar_coeffs = np.zeros(order+1, dtype=dtype)
    ar_coeffs[0] = dtype(1)
    ar_coeffs_prev = np.zeros(order+1, dtype=dtype)
    ar_coeffs_prev[0] = dtype(1)
    fwd_pred_error = y[1:]
    bwd_pred_error = y[:-1]
    den = np.dot(fwd_pred_error, fwd_pred_error) \
          + np.dot(bwd_pred_error, bwd_pred_error)
    for i in range(order):
        if den <= 0:
            raise FloatingPointError('numerical error, input ill-conditioned?')
        reflect_coeff = dtype(-2) * np.dot(bwd_pred_error, fwd_pred_error) / dtype(den)
        ar_coeffs_prev, ar_coeffs = ar_coeffs, ar_coeffs_prev
        for j in range(1, i + 2):
            ar_coeffs[j] = ar_coeffs_prev[j] + reflect_coeff * ar_coeffs_prev[i - j + 1]
        fwd_pred_error_tmp = fwd_pred_error
        fwd_pred_error = fwd_pred_error + reflect_coeff * bwd_pred_error
        bwd_pred_error = bwd_pred_error + reflect_coeff * fwd_pred_error_tmp
        q = dtype(1) - reflect_coeff**2
        den = q*den - bwd_pred_error[-1]**2 - fwd_pred_error[0]**2
        fwd_pred_error = fwd_pred_error[1:]
        bwd_pred_error = bwd_pred_error[:-1]
    return ar_coeffs

Test:

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
order=2

sol = compute_lpc_coef(y, sr, order)
dtype = y.dtype.type
ar_coeffs = np.zeros(order+1, dtype=dtype)
ar_coeffs[0] = dtype(1)
ar_coeffs_prev = np.zeros(order+1, dtype=dtype)
ar_coeffs_prev[0] = dtype(1)
fwd_pred_error = y[1:]
bwd_pred_error = y[:-1]
den = np.dot(fwd_pred_error, fwd_pred_error) \
      + np.dot(bwd_pred_error, bwd_pred_error)
for i in range(order):
    if den <= 0:
        raise FloatingPointError('numerical error, input ill-conditioned?')
    reflect_coeff = dtype(-2) * np.dot(bwd_pred_error, fwd_pred_error) / dtype(den)
    ar_coeffs_prev, ar_coeffs = ar_coeffs, ar_coeffs_prev
    for j in range(1, i + 2):
        ar_coeffs[j] = ar_coeffs_prev[j] + reflect_coeff * ar_coeffs_prev[i - j + 1]
    fwd_pred_error_tmp = fwd_pred_error
    fwd_pred_error = fwd_pred_error + reflect_coeff * bwd_pred_error
    bwd_pred_error = bwd_pred_error + reflect_coeff * fwd_pred_error_tmp
    q = dtype(1) - reflect_coeff**2
    den = q*den - bwd_pred_error[-1]**2 - fwd_pred_error[0]**2
    fwd_pred_error = fwd_pred_error[1:]
    bwd_pred_error = bwd_pred_error[:-1]

test_sol = ar_coeffs
assert np.array_equal(test_sol, sol)
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to compute Linear Prediction coefficents of input array y.

Starting code:
import librosa
import numpy as np

def compute_lpc_coef(y: np.ndarray, sr: int, order: int) -> np.ndarray:
    """
    Compute the Linear Prediction Coefficients of an audio signal.

    Parameters:
        y: The audio signal.
        sr: The sampling rate of the audio signal in Hertz.
        order: Order of the linear filter.

    Returns:
        LP prediction error coefficients, i.e. filter denominator polynomial.
    """


Solution:


    return librosa.lpc(y, order)

Test:

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
order=2

sol = compute_lpc_coef(y, sr, order)
test_sol = librosa.lpc(y, order)
assert np.array_equal(test_sol, sol)
Library: librosa==0.6.0 (Python 3.7, released 2018-02)
Additional dependencies: scikit-learn==0.21.0 numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to compute local onset autocorrelation in order to create a fourier tempogram.

Starting code:
import librosa
import numpy as np
from librosa.core.spectrum import stft

def compute_fourier_tempogram(oenv: np.ndarray, sr: int, hop_length: int) -> np.ndarray:
    """
    Compute the Fourier tempogram: the short-time Fourier transform of the onset strength envelope.

    Parameters:
       oenv: The onset strength envelope.
       sr: The sampling rate of the audio signal in Hertz.
       hop_length: The number of samples between successive frames.

    Returns:
       The computed Fourier tempogram.
    """

Solution:


    return stft(oenv, n_fft=384, hop_length=1, center=True, window="hann")

Test:

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
hop_length = 512
oenv = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length)

sol = compute_fourier_tempogram(oenv, sr, hop_length)
test_sol = stft(oenv, n_fft=384, hop_length=1, center=True, window="hann")
assert np.array_equal(test_sol, sol)
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to compute local onset autocorrelation using fourier_tempogram.

Starting code:
import librosa
import numpy as np

def compute_fourier_tempogram(oenv: np.ndarray, sr: int, hop_length: int) -> np.ndarray:
    """
    Compute the Fourier tempogram: the short-time Fourier transform of the onset strength envelope.

    Parameters:
       oenv: The onset strength envelope.
       sr: The sampling rate of the audio signal in Hertz.
       hop_length: The number of samples between successive frames.

    Returns:
       The computed Fourier tempogram.
    """
    

Solution:


    return librosa.feature.fourier_tempogram(onset_envelope=oenv, sr=sr, hop_length=hop_length)

Test:

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
hop_length = 512
oenv = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length)

sol = compute_fourier_tempogram(oenv, sr, hop_length)
test_sol = librosa.feature.fourier_tempogram(onset_envelope=oenv, sr=sr, hop_length=hop_length)
assert np.array_equal(test_sol, sol)
Library: librosa==0.6.0 (Python 3.7, released 2018-02)
Additional dependencies: scikit-learn==0.21.0 numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to compute the predominant local pulse (PLP) estimation of y.

Starting code:
import librosa
import numpy as np
from librosa.core.spectrum import stft, istft
from typing import Optional


def compute_plp(
    y: np.ndarray,
    sr: int,
    hop_length: int,
    win_length: int,
    tempo_min: Optional[float],
    tempo_max: Optional[float],
    onset_env: np.ndarray
) -> np.ndarray:
    """
    Compute the Predominant Local Pulse (PLP) of an audio signal.
    
    Parameters:
        y: The audio signal.
        sr: The sampling rate of the audio signal in Hertz.
        hop_length: The number of samples between successive frames.
        win_length: The length (in samples) of the analysis window.
        tempo_min: The minimum tempo (in BPM) for consideration.
        tempo_max: The maximum tempo (in BPM) for consideration.
        onset_env: The onset envelope of the audio signal.
        
    Returns:
        The computed PLP (Predominant Local Pulse) values.
    """

Solution:



    ftgram = stft(onset_env, n_fft=win_length, hop_length=1, center=True, window="hann")
    
    tempo_frequencies = np.fft.rfftfreq(n=win_length, d=(sr * 60 / float(hop_length)))

    ftmag = np.abs(ftgram)
    peak_values = ftmag.max(axis=0, keepdims=True)
    ftgram[ftmag < peak_values] = 0

    ftgram[:] /= peak_values

    pulse = istft(ftgram, hop_length=1, length=len(onset_env))

    np.clip(pulse, 0, None, pulse)
    return librosa.util.normalize(pulse)

Test:
filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
hop_length=512
win_length=384
tempo_min = None
tempo_max = None
onset_env = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length)


sol = compute_plp(y, sr, hop_length, win_length, tempo_min, tempo_max, onset_env)

ftgram = stft(onset_env, n_fft=win_length, hop_length=1, center=True, window="hann")

tempo_frequencies = np.fft.rfftfreq(n=win_length, d=(sr * 60 / float(hop_length)))

ftmag = np.abs(ftgram)
peak_values = ftmag.max(axis=0, keepdims=True)
ftgram[ftmag < peak_values] = 0

ftgram[:] /= peak_values

pulse = istft(ftgram, hop_length=1, length=len(onset_env))

np.clip(pulse, 0, None, pulse)
test_sol = librosa.util.normalize(pulse)
assert np.array_equal(test_sol, sol)
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to compute the predominant local pulse (PLP) estimation of y.

Starting code:
import librosa
import numpy as np
from librosa.core.spectrum import stft, istft
from typing import Optional


def compute_plp(
    y: np.ndarray,
    sr: int,
    hop_length: int,
    win_length: int,
    tempo_min: Optional[float],
    tempo_max: Optional[float],
    onset_env: np.ndarray
) -> np.ndarray:
    """
    Compute the Predominant Local Pulse (PLP) of an audio signal.
    
    Parameters:
        y: The audio signal.
        sr: The sampling rate of the audio signal in Hertz.
        hop_length: The number of samples between successive frames.
        win_length: The length (in samples) of the analysis window.
        tempo_min: The minimum tempo (in BPM) for consideration.
        tempo_max: The maximum tempo (in BPM) for consideration.
        onset_env: The onset envelope of the audio signal.
        
    Returns:
        The computed PLP (Predominant Local Pulse) values.
    """

Solution:


    return librosa.beat.plp(onset_envelope=onset_env, sr=sr, tempo_min=tempo_min, tempo_max=tempo_max)

Test:
filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
hop_length=512
win_length=384
tempo_min = None
tempo_max = None
onset_env = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length)


sol = compute_plp(y, sr, hop_length, win_length, tempo_min, tempo_max, onset_env)

ftgram = stft(onset_env, n_fft=win_length, hop_length=1, center=True, window="hann")

tempo_frequencies = np.fft.rfftfreq(n=win_length, d=(sr * 60 / float(hop_length)))

ftmag = np.abs(ftgram)
peak_values = ftmag.max(axis=0, keepdims=True)
ftgram[ftmag < peak_values] = 0

ftgram[:] /= peak_values

pulse = istft(ftgram, hop_length=1, length=len(onset_env))

np.clip(pulse, 0, None, pulse)
test_sol = librosa.util.normalize(pulse)
assert np.array_equal(test_sol, sol)
Library: librosa==0.6.0 (Python 3.7, released 2018-02)
Additional dependencies: scikit-learn==0.21.0 numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to return an array of time values to match the time axis from a feature matrix.

Starting code:
import librosa
import numpy as np

def compute_times_like(y: np.ndarray, sr: int, hop_length: int, D: np.ndarray) -> np.ndarray:
    """
    Compute the times vector of a spectrogram.

    Parameters:
        y: The audio signal.
        sr: The sampling rate of the audio signal in Hertz.
        hop_length: The number of samples between successive frames.
        D: The spectrogram.

    Returns:
        The computed times vector.
    """
    

Solution:


    if np.isscalar(D):
        frames = np.arange(D) # type: ignore
    else:
        frames = np.arange(D.shape[-1]) # type: ignore
    offset = 0
    samples = (np.asanyarray(frames) * hop_length + offset).astype(int)

    return np.asanyarray(samples) / float(sr)

Test:

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
D = librosa.stft(y)
hop_length = 512 

sol = compute_times_like(y, sr, hop_length, D)
    
if np.isscalar(D):
    frames = np.arange(D) # type: ignore
else:
    frames = np.arange(D.shape[-1]) # type: ignore
offset = 0
samples = (np.asanyarray(frames) * hop_length + offset).astype(int)

test_sol = np.asanyarray(samples) / float(sr)
assert np.array_equal(test_sol, sol)
Library: librosa==0.7.0 (Python 3.7, released 2019-07)
Additional dependencies: numpy==1.16.0 scipy==1.1.0 soundfile==0.10.2

Problem: Complete the function to return an array of time values to match the time axis from a feature matrix.

Starting code:
import librosa
import numpy as np

def compute_times_like(y: np.ndarray, sr: int, hop_length: int, D: np.ndarray) -> np.ndarray:
    """
    Compute the times vector of a spectrogram.

    Parameters:
        y: The audio signal.
        sr: The sampling rate of the audio signal in Hertz.
        hop_length: The number of samples between successive frames.
        D: The spectrogram.

    Returns:
        The computed times vector.
    """
    

Solution:


    return librosa.times_like(D, sr=sr)

Test:

filename = librosa.util.example_audio_file()
y, sr = librosa.load(filename)
D = librosa.stft(y)
hop_length = 512 

sol = compute_times_like(y, sr, hop_length, D)
    
if np.isscalar(D):
    frames = np.arange(D) # type: ignore
else:
    frames = np.arange(D.shape[-1]) # type: ignore
offset = 0
samples = (np.asanyarray(frames) * hop_length + offset).astype(int)

test_sol = np.asanyarray(samples) / float(sr)
assert np.array_equal(test_sol, sol)