librosa.tone

librosa.tone(frequency, *, sr=22050, length=None, duration=None, phi=None)[source]

Construct a pure tone (cosine) signal at a given frequency.

Parameters:
frequencyfloat > 0

frequency

srnumber > 0

desired sampling rate of the output signal

lengthint > 0

desired number of samples in the output signal. When both duration and length are defined, length takes priority.

durationfloat > 0

desired duration in seconds. When both duration and length are defined, length takes priority.

phifloat or None

phase offset, in radians. If unspecified, defaults to -np.pi * 0.5.

Returns:
tone_signalnp.ndarray [shape=(length,), dtype=float64]

Synthesized pure sine tone signal

Raises:
ParameterError
  • If frequency is not provided.

  • If neither length nor duration are provided.

Examples

Generate a pure sine tone A4

>>> tone = librosa.tone(440, duration=1, sr=22050)

Or generate the same signal using length

>>> tone = librosa.tone(440, sr=22050, length=22050)

Display spectrogram

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> S = librosa.feature.melspectrogram(y=tone)
>>> librosa.display.specshow(librosa.power_to_db(S, ref=np.max),
...                          x_axis='time', y_axis='mel', ax=ax)
../_images/librosa-tone-1.png