Tempo and Beat Tracking (libfmp.c6)

The FMP notebooks provide detailed textbook-like explanations of central techniques and algorithms implemented in the libfmp. The part of FMP related to this module is available at the following URL:

https://www.audiolabs-erlangen.de/resources/MIR/FMP/C6/C6.html

libfmp.c6.c6s1_onset_detection.average_nov_dic(nov_dic, time_max_sec, Fs_out=100, norm=True, sigma=None)[source]

Average respamples set of novelty functions

Notebook: C6/C6S1_NoveltyComparison.ipynb

Parameters
  • nov_dic (dict) – Dictionary of novelty functions

  • time_max_sec (float) – Duration of output signals (given in seconds)

  • Fs_out (scalar) – Sampling rate of output signal (Default value = 100)

  • norm (bool) – Apply max norm (if norm==True) (Default value = True)

  • sigma (float) – Standard deviation for smoothing Gaussian kernel (Default value = None)

Returns
  • nov_matrix (np.ndarray) – Matrix containing resampled output signal (last one is average)

  • Fs_out (scalar) – Sampling rate of output signals

libfmp.c6.c6s1_onset_detection.compute_local_average(x, M)[source]

Compute local average of signal

Notebook: C6/C6S1_NoveltySpectral.ipynb

Parameters
  • x (np.ndarray) – Signal

  • M (int) – Determines size (2M+1) in samples of centric window used for local average

Returns

local_average (np.ndarray) – Local average signal

libfmp.c6.c6s1_onset_detection.compute_novelty_complex(x, Fs=1, N=1024, H=64, gamma=10.0, M=40, norm=True)[source]

Compute complex-domain novelty function

Notebook: C6/C6S1_NoveltyComplex.ipynb

Parameters
  • x (np.ndarray) – Signal

  • Fs (scalar) – Sampling rate (Default value = 1)

  • N (int) – Window size (Default value = 1024)

  • H (int) – Hop size (Default value = 64)

  • gamma (float) – Parameter for logarithmic compression (Default value = 10.0)

  • M (int) – Determines size (2M+1) in samples of centric window used for local average (Default value = 40)

  • norm (bool) – Apply max norm (if norm==True) (Default value = True)

Returns
  • novelty_complex (np.ndarray) – Energy-based novelty function

  • Fs_feature (scalar) – Feature rate

libfmp.c6.c6s1_onset_detection.compute_novelty_energy(x, Fs=1, N=2048, H=128, gamma=10.0, norm=True)[source]

Compute energy-based novelty function

Notebook: C6/C6S1_NoveltyEnergy.ipynb

Parameters
  • x (np.ndarray) – Signal

  • Fs (scalar) – Sampling rate (Default value = 1)

  • N (int) – Window size (Default value = 2048)

  • H (int) – Hop size (Default value = 128)

  • gamma (float) – Parameter for logarithmic compression (Default value = 10.0)

  • norm (bool) – Apply max norm (if norm==True) (Default value = True)

Returns
  • novelty_energy (np.ndarray) – Energy-based novelty function

  • Fs_feature (scalar) – Feature rate

libfmp.c6.c6s1_onset_detection.compute_novelty_phase(x, Fs=1, N=1024, H=64, M=40, norm=True)[source]

Compute phase-based novelty function

Notebook: C6/C6S1_NoveltyPhase.ipynb

Parameters
  • x (np.ndarray) – Signal

  • Fs (scalar) – Sampling rate (Default value = 1)

  • N (int) – Window size (Default value = 1024)

  • H (int) – Hop size (Default value = 64)

  • M (int) – Determines size (2M+1) in samples of centric window used for local average (Default value = 40)

  • norm (bool) – Apply max norm (if norm==True) (Default value = True)

Returns
  • novelty_phase (np.ndarray) – Energy-based novelty function

  • Fs_feature (scalar) – Feature rate

libfmp.c6.c6s1_onset_detection.compute_novelty_spectrum(x, Fs=1, N=1024, H=256, gamma=100.0, M=10, norm=True)[source]

Compute spectral-based novelty function

Notebook: C6/C6S1_NoveltySpectral.ipynb

Parameters
  • x (np.ndarray) – Signal

  • Fs (scalar) – Sampling rate (Default value = 1)

  • N (int) – Window size (Default value = 1024)

  • H (int) – Hop size (Default value = 256)

  • gamma (float) – Parameter for logarithmic compression (Default value = 100.0)

  • M (int) – Size (frames) of local average (Default value = 10)

  • norm (bool) – Apply max norm (if norm==True) (Default value = True)

Returns
  • novelty_spectrum (np.ndarray) – Energy-based novelty function

  • Fs_feature (scalar) – Feature rate

libfmp.c6.c6s1_onset_detection.principal_argument(v)[source]

Principal argument function

Parameters

v (float or np.ndarray) – Value (or vector of values)

Returns

w (float or np.ndarray) – Principle value of v

libfmp.c6.c6s1_onset_detection.read_annotation_pos(fn_ann, label='', header=True, print_table=False)[source]

Read and convert file containing either list of pairs (number,label) or list of (number)

Notebook: C6/C6S1_OnsetDetection.ipynb

Parameters
  • fn_ann (str) – Name of file

  • label (str) – Name of label (Default value = ‘’)

  • header (bool) – Assumes header (True) or not (False) (Default value = True)

  • print_table (bool) – Prints table if True (Default value = False)

Returns
  • ann (list) – List of annotations

  • label_keys (dict) – Dictionaries specifying color and line style used for labels

libfmp.c6.c6s1_onset_detection.resample_signal(x_in, Fs_in, Fs_out=100, norm=True, time_max_sec=None, sigma=None)[source]

Resample and smooth signal

Notebook: C6/C6S1_NoveltyComparison.ipynb

Parameters
  • x_in (np.ndarray) – Input signal

  • Fs_in (scalar) – Sampling rate of input signal

  • Fs_out (scalar) – Sampling rate of output signal (Default value = 100)

  • norm (bool) – Apply max norm (if norm==True) (Default value = True)

  • time_max_sec (float) – Duration of output signal (given in seconds) (Default value = None)

  • sigma (float) – Standard deviation for smoothing Gaussian kernel (Default value = None)

Returns
  • x_out (np.ndarray) – Output signal

  • Fs_out (scalar) – Feature rate of output signal

libfmp.c6.c6s1_peak_picking.peak_picking_MSAF(x, median_len=16, offset_rel=0.05, sigma=4.0)[source]

Peak picking strategy following MSFA using an adaptive threshold (https://github.com/urinieto/msaf)

Notebook: C6/C6S1_PeakPicking.ipynb

Parameters
  • x (np.ndarray) – Input function

  • median_len (int) – Length of media filter used for adaptive thresholding (Default value = 16)

  • offset_rel (float) – Additional offset used for adaptive thresholding (Default value = 0.05)

  • sigma (float) – Variance for Gaussian kernel used for smoothing the novelty function (Default value = 4.0)

Returns
  • peaks (np.ndarray) – Peak positions

  • x (np.ndarray) – Local threshold

  • threshold_local (np.ndarray) – Filtered novelty curve

libfmp.c6.c6s1_peak_picking.peak_picking_boeck(activations, threshold=0.5, fps=100, include_scores=False, combine=False, pre_avg=12, post_avg=6, pre_max=6, post_max=6)[source]

Detects peaks.

Implements the peak-picking method described in:
“Evaluating the Online Capabilities of Onset Detection Methods”
Sebastian Boeck, Florian Krebs and Markus Schedl
Proceedings of the 13th International Society for Music Information Retrieval Conference (ISMIR), 2012

Modified by Jan Schlueter, 2014-04-24

Parameters
  • activations (np.nadarray) – Vector of activations to process

  • threshold (float) – Threshold for peak-picking (Default value = 0.5)

  • fps (scalar) – Frame rate of onset activation function in Hz (Default value = 100)

  • include_scores (bool) – Include activation for each returned peak (Default value = False)

  • combine (bool) – Only report 1 onset for N seconds (Default value = False)

  • pre_avg (float) – Use N past seconds for moving average (Default value = 12)

  • post_avg (float) – Use N future seconds for moving average (Default value = 6)

  • pre_max (float) – Use N past seconds for moving maximum (Default value = 6)

  • post_max (float) – Use N future seconds for moving maximum (Default value = 6)

Returns

peaks (np.ndarray) – Peak positions

libfmp.c6.c6s1_peak_picking.peak_picking_roeder(x, direction=None, abs_thresh=None, rel_thresh=None, descent_thresh=None, tmin=None, tmax=None)[source]
Computes the positive peaks of the input vector x
Python adaption from the Matlab Roeder_Peak_Picking script peaks.m from the internal Sync Toolbox
reckjn 2017
Parameters
  • x (np.nadarray) – Signal to be searched for (positive) peaks

  • direction (int) – +1 for forward peak searching, -1 for backward peak searching. default is dir == -1. (Default value = None)

  • abs_thresh (float) – Absolute threshold signal, i.e. only peaks satisfying x(i)>=abs_thresh(i) will be reported. abs_thresh must have the same number of samples as x. a sensible choice for this parameter would be a global or local average or median of the signal x. If omitted, half the median of x will be used. (Default value = None)

  • rel_thresh (float) – Relative threshold signal. Only peak positions i with an uninterrupted positive ascent before position i of at least rel_thresh(i) and a possibly interrupted (see parameter descent_thresh) descent of at least rel_thresh(i) will be reported. rel_thresh must have the same number of samples as x. A sensible choice would be some measure related to the global or local variance of the signal x. if omitted, half the standard deviation of W will be used.

  • descent_thresh (float) – Descent threshold. during peak candidate verfication, if a slope change from negative to positive slope occurs at sample i BEFORE the descent has exceeded rel_thresh(i), and if descent_thresh(i) has not been exceeded yet, the current peak candidate will be dropped. this situation corresponds to a secondary peak occuring shortly after the current candidate peak (which might lead to a higher peak value)! | | The value descent_thresh(i) must not be larger than rel_thresh(i). | | descent_thresh must have the same number of samples as x. a sensible choice would be some measure related to the global or local variance of the signal x. if omitted, 0.5*rel_thresh will be used. (Default value = None)

  • tmin (int) – Index of start sample. peak search will begin at x(tmin). (Default value = None)

  • tmax (int) – Index of end sample. peak search will end at x(tmax). (Default value = None)

Returns

peaks (np.nadarray) – Array of peak positions

libfmp.c6.c6s1_peak_picking.peak_picking_simple(x, threshold=None)[source]

Peak picking strategy looking for positions with increase followed by descrease

Notebook: C6/C6S1_PeakPicking.ipynb

Parameters
  • x (np.ndarray) – Input function

  • threshold (float) – Lower threshold for peak to survive

Returns

peaks (np.ndarray) – Array containing peak positions

libfmp.c6.c6s2_tempo_analysis.compute_autocorrelation_local(x, Fs, N, H, norm_sum=True)[source]

Compute local autocorrelation [FMP, Section 6.2.3]

Notebook: C6/C6S2_TempogramAutocorrelation.ipynb

Parameters
  • x (np.ndarray) – Input signal

  • Fs (scalar) – Sampling rate

  • N (int) – Window length

  • H (int) – Hop size

  • norm_sum (bool) – Normalizes by the number of summands in local autocorrelation (Default value = True)

Returns
  • A (np.ndarray) – Time-lag representation

  • T_coef (np.ndarray) – Time axis (seconds)

  • F_coef_lag (np.ndarray) – Lag axis

libfmp.c6.c6s2_tempo_analysis.compute_cyclic_tempogram(tempogram, F_coef_BPM, tempo_ref=30, octave_bin=40, octave_num=4)[source]

Compute cyclic tempogram

Notebook: C6/C6S2_TempogramCyclic.ipynb

Parameters
  • tempogram (np.ndarray) – Input tempogram

  • F_coef_BPM (np.ndarray) – Tempo axis (BPM)

  • tempo_ref (float) – Reference tempo (BPM) (Default value = 30)

  • octave_bin (int) – Number of bins per tempo octave (Default value = 40)

  • octave_num (int) – Number of tempo octaves to be considered (Default value = 4)

Returns
  • tempogram_cyclic (np.ndarray) – Cyclic tempogram tempogram_cyclic

  • F_coef_scale (np.ndarray) – Tempo axis with regard to scaling parameter

  • tempogram_log (np.ndarray) – Tempogram with logarithmic tempo axis

  • F_coef_BPM_log (np.ndarray) – Logarithmic tempo axis (BPM)

libfmp.c6.c6s2_tempo_analysis.compute_plot_tempogram_plp(fn_wav, Fs=22050, N=500, H=10, Theta=np.arange(30, 601), title='', figsize=(8, 4), plot_maxtempo=False)[source]

Compute and plot Fourier-based tempogram and PLP function

Notebook: C6/C6S3_PredominantLocalPulse.ipynb

Parameters
  • fn_wav – Filename of audio file

  • Fs – Sample rate (Default value = 22050)

  • N – Window size (Default value = 500)

  • H – Hop size (Default value = 10)

  • Theta – Set of tempi (given in BPM) (Default value = np.arange(30, 601))

  • title – Title of figure (Default value = ‘’)

  • figsize – Figure size (Default value = (8, 4))

  • plot_maxtempo – Visualize tempo with greatest coefficients in tempogram (Default value = False)

libfmp.c6.c6s2_tempo_analysis.compute_plp(X, Fs, L, N, H, Theta)[source]

Compute windowed sinusoid with optimal phase

Notebook: C6/C6S3_PredominantLocalPulse.ipynb

Parameters
  • X (np.ndarray) – Fourier-based (complex-valued) tempogram

  • Fs (scalar) – Sampling rate

  • L (int) – Length of novelty curve

  • N (int) – Window length

  • H (int) – Hop size

  • Theta (np.ndarray) – Set of tempi (given in BPM)

Returns

nov_PLP (np.ndarray) – PLP function

libfmp.c6.c6s2_tempo_analysis.compute_sinusoid_optimal(c, tempo, n, Fs, N, H)[source]

Compute windowed sinusoid with optimal phase

Notebook: C6/C6S2_TempogramFourier.ipynb

Parameters
  • c (complex) – Coefficient of tempogram (c=X(k,n))

  • tempo (float) – Tempo parameter corresponding to c (tempo=F_coef_BPM[k])

  • n (int) – Frame parameter of c

  • Fs (scalar) – Sampling rate

  • N (int) – Window length

  • H (int) – Hop size

Returns
  • kernel (np.ndarray) – Windowed sinusoid

  • t_kernel (np.ndarray) – Time axis (samples) of kernel

  • t_kernel_sec (np.ndarray) – Time axis (seconds) of kernel

libfmp.c6.c6s2_tempo_analysis.compute_tempogram_autocorr(x, Fs, N, H, norm_sum=False, Theta=np.arange(30, 601))[source]

Compute autocorrelation-based tempogram

Notebook: C6/C6S2_TempogramAutocorrelation.ipynb

Parameters
  • x (np.ndarray) – Input signal

  • Fs (scalar) – Sampling rate

  • N (int) – Window length

  • H (int) – Hop size

  • norm_sum (bool) – Normalizes by the number of summands in local autocorrelation (Default value = False)

  • Theta (np.ndarray) – Set of tempi (given in BPM) (Default value = np.arange(30, 601))

Returns
  • tempogram (np.ndarray) – Tempogram tempogram

  • T_coef (np.ndarray) – Time axis T_coef (seconds)

  • F_coef_BPM (np.ndarray) – Tempo axis F_coef_BPM (BPM)

  • A_cut (np.ndarray) – Time-lag representation A_cut (cut according to Theta)

  • F_coef_lag_cut (np.ndarray) – Lag axis F_coef_lag_cut

libfmp.c6.c6s2_tempo_analysis.compute_tempogram_fourier(x, Fs, N, H, Theta=np.arange(30, 601, 1))[source]

Compute Fourier-based tempogram [FMP, Section 6.2.2]

Notebook: C6/C6S2_TempogramFourier.ipynb

Parameters
  • x (np.ndarray) – Input signal

  • Fs (scalar) – Sampling rate

  • N (int) – Window length

  • H (int) – Hop size

  • Theta (np.ndarray) – Set of tempi (given in BPM) (Default value = np.arange(30, 601, 1))

Returns
  • X (np.ndarray) – Tempogram

  • T_coef (np.ndarray) – Time axis (seconds)

  • F_coef_BPM (np.ndarray) – Tempo axis (BPM)

libfmp.c6.c6s2_tempo_analysis.plot_signal_kernel(x, t_x, kernel, t_kernel, xlim=None, figsize=(8, 2), title=None)[source]

Visualize signal and local kernel

Notebook: C6/C6S2_TempogramFourier.ipynb

Parameters
  • x – Signal

  • t_x – Time axis of x (given in seconds)

  • kernel – Local kernel

  • t_kernel – Time axis of kernel (given in seconds)

  • xlim – Limits for x-axis (Default value = None)

  • figsize – Figure size (Default value = (8, 2))

  • title – Title of figure (Default value = None)

Returns

fig – Matplotlib figure handle

libfmp.c6.c6s2_tempo_analysis.plot_signal_local_lag(x, t_x, local_lag, t_local_lag, lag, xlim=None, figsize=(8, 1.5), title='')[source]

Visualize signal and local lag [FMP, Figure 6.14]

Notebook: C6/C6S2_TempogramAutocorrelation.ipynb

Parameters
  • x – Signal

  • t_x – Time axis of x (given in seconds)

  • local_lag – Local lag

  • t_local_lag – Time axis of kernel (given in seconds)

  • lag – Lag (given in seconds)

  • xlim – Limits for x-axis (Default value = None)

  • figsize – Figure size (Default value = (8, 1.5))

  • title – Title of figure (Default value = ‘’)

Returns

fig – Matplotlib figure handle

libfmp.c6.c6s2_tempo_analysis.set_yticks_tempogram_cyclic(ax, octave_bin, F_coef_scale, num_tick=5)[source]

Set yticks with regard to scaling parmater

Notebook: C6/C6S2_TempogramCyclic.ipynb

Parameters
  • ax (mpl.axes.Axes) – Figure axis

  • octave_bin (int) – Number of bins per tempo octave

  • F_coef_scale (np.ndarra) – Tempo axis with regard to scaling parameter

  • num_tick (int) – Number of yticks (Default value = 5)

libfmp.c6.c6s3_adaptive_windowing.adaptive_windowing(X, B, neigborhood=1, add_start=False, add_end=False)[source]

Apply adaptive windowing [FMP, Section 6.3.3]

Notebook: C6/C6S3_AdaptiveWindowing.ipynb

Parameters
  • X (np.ndarray) – Feature sequence

  • B (np.ndarray) – Beat sequence (spefied in frames)

  • neigborhood (float) – Parameter specifying relative range considered for windowing (Default value = 1)

  • add_start (bool) – Add first index of X to beat sequence (if not existent) (Default value = False)

  • add_end (bool) – Add last index of X to beat sequence (if not existent) (Default value = False)

Returns
  • X_adapt (np.ndarray) – Feature sequence adapted to beat sequence

  • B_s (np.ndarray) – Sequence specifying start (in frames) of window sections

  • B_t (np.ndarray) – Sequence specifying end (in frames) of window sections

libfmp.c6.c6s3_adaptive_windowing.compute_plot_adaptive_windowing(x, Fs, H, X, B, neigborhood=1, add_start=False, add_end=False)[source]

Compute and plot process for adaptive windowing [FMP, Section 6.3.3]

Notebook: C6/C6S3_AdaptiveWindowing.ipynb

Parameters
  • x (np.ndarray) – Signal

  • Fs (scalar) – Sample Rate

  • H (int) – Hop size

  • X (int) – Feature sequence

  • B (np.ndarray) – Beat sequence (spefied in frames)

  • neigborhood (float) – Parameter specifying relative range considered for windowing (Default value = 1)

  • add_start (bool) – Add first index of X to beat sequence (if not existent) (Default value = False)

  • add_end (bool) – Add last index of X to beat sequence (if not existent) (Default value = False)

Returns

X_adapt (np.ndarray) – Feature sequence adapted to beat sequence

libfmp.c6.c6s3_adaptive_windowing.plot_beat_grid(B_sec, ax, color='r', linestyle=':', linewidth=1)[source]

Plot beat grid (given in seconds) into axis

Notebook: C6/C6S3_AdaptiveWindowing.ipynb

Parameters
  • B_sec – Beat grid

  • ax – Axes for plotting

  • color – Color of lines (Default value = ‘r’)

  • linestyle – Style of lines (Default value = ‘:’)

  • linewidth – Width of lines (Default value = 1)

libfmp.c6.c6s3_beat_tracking.beat_period_to_tempo(beat, Fs)[source]

Convert beat period (samples) to tempo (BPM) [FMP, Section 6.3.2]

Notebook: C6/C6S3_BeatTracking.ipynb

Parameters
  • beat (int) – Beat period (samples)

  • Fs (scalar) – Sample rate

Returns

tempo (float) – Tempo (BPM)

libfmp.c6.c6s3_beat_tracking.compute_beat_sequence(novelty, beat_ref, penalty=None, factor=1.0, return_all=False)[source]
Compute beat sequence using dynamic programming [FMP, Section 6.3.2]
Note: Concatenation of ‘0’ because of Python indexing conventions

Notebook: C6/C6S3_BeatTracking.ipynb

Parameters
  • novelty (np.ndarray) – Novelty function

  • beat_ref (int) – Reference beat period

  • penalty (np.ndarray) – Penalty function (Default value = None)

  • factor (float) – Weight parameter for adjusting the penalty (Default value = 1.0)

  • return_all (bool) – Return details (Default value = False)

Returns
  • B (np.ndarray) – Optimal beat sequence

  • D (np.ndarray) – Accumulated score

  • P (np.ndarray) – Maximization information

libfmp.c6.c6s3_beat_tracking.compute_penalty(N, beat_ref)[source]
Compute penalty funtion used for beat tracking [FMP, Section 6.3.2]
Note: Concatenation of ‘0’ because of Python indexing conventions

Notebook: C6/C6S3_BeatTracking.ipynb

Parameters
  • N (int) – Length of vector representing penalty function

  • beat_ref (int) – Reference beat period (given in samples)

Returns

penalty (np.ndarray) – Penalty function

libfmp.c6.c6s3_beat_tracking.compute_plot_sonify_beat(x, Fs, nov, Fs_nov, beat_ref, factor, title=None, figsize=(6, 2))[source]

Compute, plot, and sonify beat sequence from novelty function [FMP, Section 6.3.2]

Notebook: C6/C6S3_BeatTracking.ipynb

Parameters
  • x – Novelty function

  • Fs – Sample rate

  • nov – Novelty function

  • Fs_nov – Rate of novelty function

  • beat_ref – Reference beat period

  • factor – Weight parameter for adjusting the penalty

  • title – Title of figure (Default value = None)

  • figsize – Size of figure (Default value = (6, 2))