Chord Recognition (libfmp.c5)

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/C5/C5.html

libfmp.c5.c5s1_basic_theory_harmony.generate_sinusoid_chord(pitches=[69], duration=1, Fs=4000, amplitude_max=0.5)[source]

Generate synthetic sound of chord using sinusoids

Notebook: C5/C5S1_Chords.ipynb

Parameters
  • pitches (list) – List of pitches (MIDI note numbers) (Default value = [69])

  • duration (float) – Duration (seconds) (Default value = 1)

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

  • amplitude_max (float) – Amplitude (Default value = 0.5)

Returns

x (np.ndarray) – Synthesized signal

libfmp.c5.c5s1_basic_theory_harmony.generate_sinusoid_scale(pitches=[69], duration=0.5, Fs=4000, amplitude_max=0.5)[source]

Generate synthetic sound of scale using sinusoids

Notebook: C5/C5S1_Scales_CircleFifth.ipynb

Parameters
  • pitches (list) – List of pitchs (MIDI note numbers) (Default value = [69])

  • duration (float) – Duration (seconds) (Default value = 0.5)

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

  • amplitude_max (float) – Amplitude (Default value = 0.5)

Returns

x (np.ndarray) – Synthesized signal

libfmp.c5.c5s2_chord_rec_template.chord_recognition_template(X, norm_sim='1', nonchord=False)[source]

Conducts template-based chord recognition with major and minor triads (and possibly nonchord)

Notebook: C5/C5S2_ChordRec_Templates.ipynb

Parameters
  • X (np.ndarray) – Chromagram

  • norm_sim (str) – Specifies norm used for normalizing chord similarity matrix (Default value = ‘1’)

  • nonchord (bool) – If “True” then add nonchord template (Default value = False)

Returns
  • chord_sim (np.ndarray) – Chord similarity matrix

  • chord_max (np.ndarray) – Binarized chord similarity matrix only containing maximizing chord

libfmp.c5.c5s2_chord_rec_template.compute_chromagram_from_filename(fn_wav, Fs=22050, N=4096, H=2048, gamma=None, version='STFT', norm='2')[source]

Compute chromagram for WAV file specified by filename

Notebook: C5/C5S2_ChordRec_Templates.ipynb

Parameters
  • fn_wav (str) – Filenname of WAV

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

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

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

  • gamma (float) – Constant for logarithmic compression (Default value = None)

  • version (str) – Technique used for front-end decomposition (‘STFT’, ‘IIS’, ‘CQT’) (Default value = ‘STFT’)

  • norm (str) – If not ‘None’, chroma vectors are normalized by norm as specified (‘1’, ‘2’, ‘max’) (Default value = ‘2’)

Returns
  • X (np.ndarray) – Chromagram

  • Fs_X (scalar) – Feature reate of chromagram

  • x (np.ndarray) – Audio signal

  • Fs (scalar) – Sampling rate of audio signal

  • x_dur (float) – Duration (seconds) of audio signal

libfmp.c5.c5s2_chord_rec_template.compute_eval_measures(I_ref, I_est)[source]

Compute evaluation measures including precision, recall, and F-measure

Notebook: C5/C5S2_ChordRec_Eval.ipynb

Parameters
  • I_ref (np.ndarray) – Reference set of items

  • I_est (np.ndarray) – Set of estimated items

Returns
  • P (float) – Precision

  • R (float) – Recall

  • F (float) – F-measure

  • num_TP (int) – Number of true positives

  • num_FN (int) – Number of false negatives

  • num_FP (int) – Number of false positives

libfmp.c5.c5s2_chord_rec_template.convert_chord_ann_matrix(fn_ann, chord_labels, Fs=1, N=None, last=False)[source]

Convert segment-based chord annotation into various formats

Notebook: C5/C5S2_ChordRec_Eval.ipynb

Parameters
  • fn_ann (str) – Filename of segment-based chord annotation

  • chord_labels (list) – List of chord labels

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

  • N (int) – Number of frames to be generated (by cutting or extending). Only enforced for ann_matrix, ann_frame, ann_seg_frame (Default value = None)

  • last (bool) – If ‘True’ uses for extension last chord label, otherwise uses nonchord label ‘N’ (Default value = False)

Returns
  • ann_matrix (np.ndarray) – Encoding of label sequence in form of a binary time-chord representation

  • ann_frame (list) – Label sequence (specified on the frame level)

  • ann_seg_frame (list) – Encoding of label sequence as segment-based annotation (given in indices)

  • ann_seg_ind (list) – Segment-based annotation with segments (given in indices)

  • ann_seg_sec (list) – Segment-based annotation with segments (given in seconds)

libfmp.c5.c5s2_chord_rec_template.convert_chord_label(ann)[source]

Replace for segment-based annotation in each chord label the string ‘:min’ by ‘m’ and convert flat chords into sharp chords using enharmonic equivalence

Notebook: C5/C5S2_ChordRec_Eval.ipynb

Parameters

ann (list) – Segment-based annotation with chord labels

Returns

ann_conv (list) – Converted segment-based annotation with chord labels

libfmp.c5.c5s2_chord_rec_template.convert_sequence_ann(seq, Fs=1)[source]

Convert label sequence into segment-based annotation

Notebook: C5/C5S2_ChordRec_Eval.ipynb

Parameters
  • seq (list) – Label sequence

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

Returns

ann (list) – Segment-based annotation for label sequence

libfmp.c5.c5s2_chord_rec_template.generate_chord_templates(nonchord=False)[source]

Generate chord templates of major and minor triads (and possibly nonchord)

Notebook: C5/C5S2_ChordRec_Templates.ipynb

Parameters

nonchord (bool) – If “True” then add nonchord template (Default value = False)

Returns

chord_templates (np.ndarray) – Matrix containing chord_templates as columns

libfmp.c5.c5s2_chord_rec_template.get_chord_labels(ext_minor='m', nonchord=False)[source]

Generate chord labels for major and minor triads (and possibly nonchord label)

Notebook: C5/C5S2_ChordRec_Templates.ipynb

Parameters
  • ext_minor (str) – Extension for minor chords (Default value = ‘m’)

  • nonchord (bool) – If “True” then add nonchord label (Default value = False)

Returns

chord_labels (list) – List of chord labels

libfmp.c5.c5s2_chord_rec_template.plot_chromagram_annotation(ax, X, Fs_X, ann, color_ann, x_dur, cmap='gray_r', title='')[source]

Plot chromagram and annotation

Notebook: C5/C5S2_ChordRec_Templates.ipynb

Parameters
  • ax – Axes handle

  • X – Feature representation

  • Fs_X – Feature rate

  • ann – Annotations

  • color_ann – Color for annotations

  • x_dur – Duration of feature representation

  • cmap – Color map for imshow (Default value = ‘gray_r’)

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

libfmp.c5.c5s2_chord_rec_template.plot_matrix_chord_eval(I_ref, I_est, Fs=1, xlabel='Time (seconds)', ylabel='Chord', title='', chord_labels=None, ax=None, grid=True, figsize=(9, 3.5))[source]

Plots TP-, FP-, and FN-items in a color-coded form in time–chord grid

Notebook: C5/C5S2_ChordRec_Eval.ipynb

Parameters
  • I_ref – Reference set of items

  • I_est – Set of estimated items

  • Fs – Feature rate (Default value = 1)

  • xlabel – Label for x-axis (Default value = ‘Time (seconds)’)

  • ylabel – Label for y-axis (Default value = ‘Chord’)

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

  • chord_labels – List of chord labels used for vertical axis (Default value = None)

  • ax – Array of axes (Default value = None)

  • grid – If “True” the plot grid (Default value = True)

  • figsize – Size of figure (if axes are not specified) (Default value = (9, 3.5))

Returns
  • fig – The created matplotlib figure or None if ax was given.

  • ax – The used axes

  • im – The image plot

libfmp.c5.c5s3_chord_rec_hmm.chord_recognition_all(X, ann_matrix, p=0.15, filt_len=None, filt_type='mean')[source]

Conduct template- and HMM-based chord recognition and evaluates the approaches

Notebook: C5/C5S3_ChordRec_Beatles.ipynb

Parameters
  • X (np.ndarray) – Chromagram

  • ann_matrix (np.ndarray) – Reference annotation as given as time-chord binary matrix

  • p (float) – Self-transition probability used for HMM (Default value = 0.15)

  • filt_len (int) – Filter length used for prefilitering (Default value = None)

  • filt_type (str) – Filter type used for prefilitering (Default value = ‘mean’)

Returns
  • result_Tem (tuple) – Chord recogntion evaluation results ([P, R, F, TP, FP, FN]) for template-based approach

  • result_HMM (tuple) – Chord recogntion evaluation results ([P, R, F, TP, FP, FN]) for HMM-based approach

  • chord_Tem (np.ndarray) – Template-based chord recogntion result given as binary matrix

  • chord_HMM (np.ndarray) – HMM-based chord recogntion result given as binary matrix

  • chord_sim (np.ndarray) – Chord similarity matrix

libfmp.c5.c5s3_chord_rec_hmm.estimate_hmm_from_o_s(O, S, I, K)[source]

Estimate the state transition and output probability matrices from a given observation and state sequence

Notebook: C5/C5S3_HiddenMarkovModel.ipynb

Parameters
  • O (np.ndarray) – Observation sequence of length N

  • S (np.ndarray) – State sequence of length N

  • I (int) – Number of states

  • K (int) – Number of observation symbols

Returns
  • A_est (np.ndarray) – State transition probability matrix of dimension I x I

  • B_est (np.ndarray) – Output probability matrix of dimension I x K

libfmp.c5.c5s3_chord_rec_hmm.generate_sequence_hmm(N, A, C, B, details=False)[source]

Generate observation and state sequence from given HMM

Notebook: C5/C5S3_HiddenMarkovModel.ipynb

Parameters
  • N (int) – Number of observations to be generated

  • A (np.ndarray) – State transition probability matrix of dimension I x I

  • C (np.ndarray) – Initial state distribution of dimension I

  • B (np.ndarray) – Output probability matrix of dimension I x K

  • details (bool) – If “True” then shows details (Default value = False)

Returns
  • O (np.ndarray) – Observation sequence of length N

  • S (np.ndarray) – State sequence of length N

libfmp.c5.c5s3_chord_rec_hmm.matrix_chord24_trans_inv(A)[source]

Computes transposition-invariant matrix for transition matrix based 12 major chords and 12 minor chords

Notebook: C5/C5S3_ChordRec_HMM.ipynb

Parameters

A (np.ndarray) – Input transition matrix

Returns

A_ti (np.ndarray) – Output transition matrix

libfmp.c5.c5s3_chord_rec_hmm.matrix_circular_mean(A)[source]

Computes circulant matrix with mean diagonal sums

Notebook: C5/C5S3_ChordRec_HMM.ipynb

Parameters

A (np.ndarray) – Square matrix

Returns

A_mean (np.ndarray) – Circulant output matrix

libfmp.c5.c5s3_chord_rec_hmm.plot_transition_matrix(A, log=True, ax=None, figsize=(6, 5), title='', xlabel='State (chord label)', ylabel='State (chord label)', cmap='gray_r', quadrant=False)[source]

Plot a transition matrix for 24 chord models (12 major and 12 minor triads)

Notebook: C5/C5S3_ChordRec_HMM.ipynb

Parameters
  • A – Transition matrix

  • log – Show log probabilities (Default value = True)

  • ax – Axis (Default value = None)

  • figsize – Width, height in inches (only used when ax=None) (Default value = (6, 5))

  • title – Title for plot (Default value = ‘’)

  • xlabel – Label for x-axis (Default value = ‘State (chord label)’)

  • ylabel – Label for y-axis (Default value = ‘State (chord label)’)

  • cmap – Color map (Default value = ‘gray_r’)

  • quadrant – Plots additional lines for C-major and C-minor quadrants (Default value = False)

Returns
  • fig – The created matplotlib figure or None if ax was given.

  • ax – The used axes.

  • im – The image plot

libfmp.c5.c5s3_chord_rec_hmm.uniform_transition_matrix(p=0.01, N=24)[source]

Computes uniform transition matrix

Notebook: C5/C5S3_ChordRec_HMM.ipynb

Parameters
  • p (float) – Self transition probability (Default value = 0.01)

  • N (int) – Column and row dimension (Default value = 24)

Returns

A (np.ndarray) – Output transition matrix

libfmp.c5.c5s3_chord_rec_hmm.viterbi(A, C, B, O)[source]

Viterbi algorithm for solving the uncovering problem

Notebook: C5/C5S3_Viterbi.ipynb

Parameters
  • A (np.ndarray) – State transition probability matrix of dimension I x I

  • C (np.ndarray) – Initial state distribution of dimension I

  • B (np.ndarray) – Output probability matrix of dimension I x K

  • O (np.ndarray) – Observation sequence of length N

Returns
  • S_opt (np.ndarray) – Optimal state sequence of length N

  • D (np.ndarray) – Accumulated probability matrix

  • E (np.ndarray) – Backtracking matrix

libfmp.c5.c5s3_chord_rec_hmm.viterbi_log(A, C, B, O)[source]

Viterbi algorithm (log variant) for solving the uncovering problem

Notebook: C5/C5S3_Viterbi.ipynb

Parameters
  • A (np.ndarray) – State transition probability matrix of dimension I x I

  • C (np.ndarray) – Initial state distribution of dimension I

  • B (np.ndarray) – Output probability matrix of dimension I x K

  • O (np.ndarray) – Observation sequence of length N

Returns
  • S_opt (np.ndarray) – Optimal state sequence of length N

  • D_log (np.ndarray) – Accumulated log probability matrix

  • E (np.ndarray) – Backtracking matrix

libfmp.c5.c5s3_chord_rec_hmm.viterbi_log_likelihood(A, C, B_O)[source]

Viterbi algorithm (log variant) for solving the uncovering problem

Notebook: C5/C5S3_Viterbi.ipynb

Parameters
  • A (np.ndarray) – State transition probability matrix of dimension I x I

  • C (np.ndarray) – Initial state distribution of dimension I

  • B_O (np.ndarray) – Likelihood matrix of dimension I x N

Returns
  • S_opt (np.ndarray) – Optimal state sequence of length N

  • S_mat (np.ndarray) – Binary matrix representation of optimal state sequence

  • D_log (np.ndarray) – Accumulated log probability matrix

  • E (np.ndarray) – Backtracking matrix