Feature audio
Calcolare descrittori, rappresentazioni Mel, MFCC e chroma.
In questa sezione
mel_spectrogram— Mel spectrogram in decibels (reference: the maximum of the clip).stft_power— Power spectrogram|X[k, t]|^2and the frequencies of its rows.mel_filterbank— The Mel filter bank: one row per band, one column per FFT bin.mel_frequencies— The centre frequencies of the Mel bands, in Hz.log_mel— Log-Mel spectrogram in decibels, with a fixed reference.mfcc_matrix— MFCC matrix (coefficients x frames) from the log-Mel spectrogram.delta— First derivative of the coefficients along time: the delta features.add_noise— Add white noise at a chosen signal-to-noise ratio.chroma_profile— Average chroma profile: twelve pitch classes, one number each.plot_mel_filterbank— The Mel filter banks plotted with Plotly: how the spectrum is folded toplot_mfcc— The MFCC coefficients as a time-coefficient heat map, Plotly style.plot_chroma— The chroma as a time-pitch-class heat map, Plotly style.time_features— Two numbers from the time domain: RMS and zero crossings per second.spectral_features— Four numbers describing the shape of the spectrum: centroid, bandwidth,mfcc_features— The vector of MFCCs averaged over time: the aggregation used by theall_features— All the features together: time, spectral and MFCC concatenated.chord_templates— The 24 chord templates: 12 major and 12 minor, as binary vectors on therecognize_chord— The chord most similar to a chroma profile, by cosine similarity with the
Modulo features
mel_spectrogram
al.mel_spectrogram(signal, sr, n_mels=64, n_fft=1024, hop=512)Mel spectrogram in decibels (reference: the maximum of the clip).
Fixed shape n_mels times number of windows: the “image” the network receives.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | Obbligatorio | The sampling rate in Hz. |
n_mels |
int | 64 |
Number of Mel bands. |
n_fft |
int | 1024 |
Analysis window length. |
hop |
int | 512 |
Hop between windows. |
Risultato
- numpy.ndarray: The matrix (Mel bands x windows), in dB (
float32).
Esempio
S = al.mel_spectrogram(al.load_clip(path, sr=22050), sr=22050)
print(S.shape)stft_power
al.stft_power(signal, sr, n_fft=1024, hop=512)Power spectrogram |X[k, t]|^2 and the frequencies of its rows.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | Obbligatorio | The sampling rate in Hz. |
n_fft |
int | 1024 |
Analysis window length. |
hop |
int | 512 |
Hop between windows. |
Risultato
- (numpy.ndarray, numpy.ndarray): The frequencies in Hz (513 values with
n_fft=1024) and the power matrix (frequencies x windows).
Esempio
f, P = al.stft_power(y, 22050)
print(P.shape)mel_filterbank
al.mel_filterbank(sr, n_mels=64, n_fft=1024, htk=False, norm='slaney')The Mel filter bank: one row per band, one column per FFT bin.
The matrix to multiply by the power spectrogram: B @ P gives the Mel bands. The default convention is Slaney (htk=False), as in librosa.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
sr |
int | Obbligatorio | The sampling rate in Hz. |
n_mels |
int | 64 |
Number of Mel bands. |
n_fft |
int | 1024 |
Analysis window length. |
htk |
bool | False |
Use the HTK Mel formula instead of the Slaney one. |
norm |
str or None | 'slaney' |
Area normalisation of the triangles. |
Risultato
- numpy.ndarray: The matrix (Mel bands x FFT bins).
Esempio
B = al.mel_filterbank(22050, n_mels=6, n_fft=1024)
print(B.shape) # (6, 513)mel_frequencies
al.mel_frequencies(n_mels=64, sr=44100, fmin=0.0, fmax=None)The centre frequencies of the Mel bands, in Hz.
Returns n_mels + 2 values (the two edges included), as librosa does.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
n_mels |
int | 64 |
Number of Mel bands. |
sr |
int | 44100 |
Sampling rate. |
fmin |
float | 0.0 |
Lowest frequency of the bank. |
fmax |
float or None | None |
Highest frequency; with None, sr / 2. |
Risultato
- numpy.ndarray: The frequencies in Hz.
Esempio
f = al.mel_frequencies(12, 22050)
print(int((f < 1000).sum()), "bande sotto 1 kHz")log_mel
al.log_mel(signal, sr, n_mels=64, n_fft=1024, hop=512, ref=1.0, top_db=80)Log-Mel spectrogram in decibels, with a fixed reference.
The fixed reference (ref=1.0) keeps the levels comparable between clips; top_db sets the floor, 80 dB below the maximum of the matrix.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | Obbligatorio | The sampling rate in Hz. |
n_mels |
int | 64 |
Number of Mel bands. |
n_fft |
int | 1024 |
Analysis window length. |
hop |
int | 512 |
Hop between windows. |
ref |
float | 1.0 |
Reference of the decibel scale. |
top_db |
float | 80 |
Dynamic range kept below the maximum. |
Risultato
- numpy.ndarray: The matrix (Mel bands x windows), in dB.
Esempio
S = al.log_mel(y, 22050)
print(S.shape, S.min().round(1), S.max().round(1))mfcc_matrix
al.mfcc_matrix(signal, sr, n_mfcc=13, n_mels=64, n_fft=1024, hop=512)MFCC matrix (coefficients x frames) from the log-Mel spectrogram.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | Obbligatorio | The sampling rate in Hz. |
n_mfcc |
int | 13 |
Number of coefficients kept. |
n_mels |
int | 64 |
Number of Mel bands. |
n_fft |
int | 1024 |
Analysis window length. |
hop |
int | 512 |
Hop between windows. |
Risultato
- numpy.ndarray: The matrix (coefficients x frames); row 0 is the overall energy.
Esempio
C = al.mfcc_matrix(y, 22050)
print(C.shape)delta
al.delta(coeffs, width=9)First derivative of the coefficients along time: the delta features.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
coeffs |
numpy.ndarray | Obbligatorio | The matrix (coefficients x frames). |
width |
int | 9 |
Number of frames of the regression window (odd). |
Risultato
- numpy.ndarray: The deltas, same shape as the input.
Esempio
D = al.delta(al.mfcc_matrix(y, 22050))add_noise
al.add_noise(signal, snr_db=10.0, seed=0)Add white noise at a chosen signal-to-noise ratio.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The clean signal. |
snr_db |
float | 10.0 |
Signal-to-noise ratio, in dB. |
seed |
int | 0 |
Seed of the noise generator. |
Risultato
- numpy.ndarray: The noisy signal, same length as the input.
Esempio
rumoroso = al.add_noise(y, snr_db=10)chroma_profile
al.chroma_profile(signal, sr, hop=512)Average chroma profile: twelve pitch classes, one number each.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | Obbligatorio | The sampling rate in Hz. |
hop |
int | 512 |
Hop between windows. |
Risultato
- numpy.ndarray: The twelve values (C, C#, D, …), averaged over time.
Esempio
profilo = al.chroma_profile(accordo, 22050)
print(al.recognize_chord(profilo))plot_mel_filterbank
al.plot_mel_filterbank(sr, n_mels=12, n_fft=2048, up_to=None, title='Banchi di filtri Mel', *, show: bool = True)The Mel filter banks plotted with Plotly: how the spectrum is folded to the Mel scale.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
sr |
int | Obbligatorio | The sampling rate in Hz. |
n_mels |
int | 12 |
Number of banks. |
n_fft |
int | 2048 |
FFT length the banks refer to. |
up_to |
float or None | None |
Upper limit of the frequency axis, in Hz. |
title |
str | 'Banchi di filtri Mel' |
Title of the figure. |
show |
bool | True |
Display the figure in the notebook; with show=False the function only returns it. |
Risultato
- plotly.graph_objects.Figure: The figure when
show=False; with the defaultshow=Truethe figure is displayed andNoneis returned.
Esempio
al.plot_mel_filterbank(al.SAMPLE_RATE, n_mels=16)plot_mfcc
al.plot_mfcc(signal, sr, n_mfcc=13, n_fft=2048, hop=None, with_delta=False, columns=None, title='MFCC nel tempo', *, show: bool = True)The MFCC coefficients as a time-coefficient heat map, Plotly style.
With with_delta=True the figure has three stacked panels: MFCC, delta and delta-delta, on the same time axis. It accepts a single signal, a pair (name, signal) or a list of pairs: with several signals you get a grid, one panel per signal (with columns the number of columns is fixed).
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
ndarray or (str, ndarray) or list of pairs | Obbligatorio | The signal(s) to analyse, with their names (used as titles). |
sr |
int | Obbligatorio | The sampling rate in Hz. |
n_mfcc |
int | 13 |
Number of coefficients. |
n_fft |
int | 2048 |
Analysis window length. |
hop |
int or None | None |
Hop between windows; when absent, one quarter of n_fft. |
with_delta |
bool | False |
If True, also show delta and delta-delta in two panels below (single signal only). |
columns |
int or None | None |
Number of columns of the grid when there is more than one signal. |
title |
str | 'MFCC nel tempo' |
Title of the figure. |
show |
bool | True |
Display the figure in the notebook; with show=False the function only returns it. |
Risultato
- plotly.graph_objects.Figure: The figure when
show=False; with the defaultshow=Truethe figure is displayed andNoneis returned.
Esempio
signal, sr = al.load_audio(al.AUDIO_DIR / "speech1.wav")
al.plot_mfcc(signal, sr, with_delta=True)
al.plot_mfcc([("flauto", ya), ("archi", yb)], al.SAMPLE_RATE, columns=1)plot_chroma
al.plot_chroma(signal, sr, hop=512, with_means=False, method='stft', title='Chroma: le dodici classi di altezza', *, show: bool = True)The chroma as a time-pitch-class heat map, Plotly style.
With with_means=True it adds the average profile per class below; method chooses the chroma estimate (“stft” or “cqt”).
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | Obbligatorio | The sampling rate in Hz. |
hop |
int | 512 |
Hop between windows, in samples. |
with_means |
bool | False |
If True, also show the per-class means in a bar panel. |
method |
str | 'stft' |
“stft” or “cqt”. |
title |
str | 'Chroma: le dodici classi di altezza' |
Title of the figure. |
show |
bool | True |
Display the figure in the notebook; with show=False the function only returns it. |
Risultato
- plotly.graph_objects.Figure: The figure when
show=False; with the defaultshow=Truethe figure is displayed andNoneis returned.
Esempio
signal, sr = al.load_audio(al.AUDIO_DIR / "C-major-scale.wav")
al.plot_chroma(signal, sr, method="cqt", with_means=True)time_features
al.time_features(signal, sr=44100)Two numbers from the time domain: RMS and zero crossings per second.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | 44100 |
Sampling rate. |
Risultato
- numpy.ndarray: The vector [RMS, crossings per second].
Esempio
print(al.time_features(al.synthetic_timbre("percussioni"), 22050))spectral_features
al.spectral_features(signal, sr=44100)Four numbers describing the shape of the spectrum: centroid, bandwidth, flatness and rolloff, averaged over time.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | 44100 |
Sampling rate. |
Risultato
- numpy.ndarray: The vector [centroid, bandwidth, flatness, rolloff].
Esempio
print(al.spectral_features(al.synthetic_timbre("archi"), 22050))mfcc_features
al.mfcc_features(signal, sr=44100, n_mfcc=13)The vector of MFCCs averaged over time: the aggregation used by the classifier.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | 44100 |
Sampling rate. |
n_mfcc |
int | 13 |
Number of coefficients. |
Risultato
- numpy.ndarray: The vector of the mean coefficients.
Esempio
v = al.mfcc_features(al.synthetic_timbre("clarinetto", midi_note=64), 22050)all_features
al.all_features(signal, sr=44100, n_mfcc=13)All the features together: time, spectral and MFCC concatenated.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
signal |
numpy.ndarray | Obbligatorio | The signal. |
sr |
int | 44100 |
Sampling rate. |
n_mfcc |
int | 13 |
Number of MFCC coefficients. |
Risultato
- numpy.ndarray: The vector [time, spectral, MFCC] (19 numbers with the defaults).
Esempio
print(len(al.all_features(al.synthetic_timbre("archi"), 22050)))chord_templates
al.chord_templates()The 24 chord templates: 12 major and 12 minor, as binary vectors on the twelve pitch classes (1 on the notes of the chord, 0 elsewhere).
Risultato
- dict: Chord name (for example “C maggiore”) and 12-component vector.
Esempio
templates = al.chord_templates()
print(list(templates)[:2])recognize_chord
al.recognize_chord(chroma_profile)The chord most similar to a chroma profile, by cosine similarity with the templates.
Parametri
| Parametro | Tipo | Valore predefinito | Descrizione |
|---|---|---|---|
chroma_profile |
numpy.ndarray | Obbligatorio | The 12-component profile (for example the mean of a chromagram). |
Risultato
- (str, float): The name of the recognised chord and the cosine similarity.
Esempio
chord = al.synthetic_chord([60, 64, 67], seed=2)
chroma = librosa.feature.chroma_stft(y=chord, sr=22050).mean(axis=1)
print(al.recognize_chord(chroma))