alignmentUtils

alignmentUtils

pyampact.alignmentUtils.dp(M)[source]

Use dynamic programming to find a min-cost path through matrix M. Return state sequence in p,q.

Parameters:

M – 2D numpy array, the input matrix.

Returns:

A tuple containing three elements: - p: List, the sequence of row indices for the min-cost path. - q: List, the sequence of column indices for the min-cost path. - D: 2D numpy array, the cost matrix.

pyampact.alignmentUtils.f0_est_weighted_sum(x, f, f0i, fMax=5000, fThresh=None)[source]

Calculate F0, power, and spectrum for an inputted spectral representation.

Parameters:
  • x – FxT matrix of complex spectrogram values.

  • f – FxT matrix of frequencies of each of those spectrogram values.

  • f0i – 1xT vector of initial estimates of f0 for each time.

  • fMax – Maximum frequency to consider in weighted sum.

  • fThresh – Maximum distance in Hz from each harmonic to consider.

Returns:

A tuple containing three elements: - f0: Vector of estimated f0s from noteStart_s to noteEnd_s. - p: Vector of corresponding “powers” from f0EstWeightedSum. - strips: Estimated spectrum for each partial.

pyampact.alignmentUtils.f0_est_weighted_sum_spec(filename, noteStart_s, noteEnd_s, midiNote, y, sr, useIf=True)[source]

Calculate F0, power, and spectrum for a single note.

Parameters:
  • fileName – Name of wav file to analyze.

  • noteStart_s – Start position (in seconds) of note to analyze.

  • noteEnd_s – End position (in seconds) of note to analyze.

  • noteMidi – Midi note number of note to analyze.

  • useIf – If true, use instantaneous frequency, else spectrogram frequencies.

Returns:

A tuple containing three elements: - f0: Vector of estimated f0s from noteStart_s to noteEnd_s. - p: Vector of corresponding “powers” from f0EstWeightedSum. - M: Estimated spectrum.

pyampact.alignmentUtils.fill_priormat_gauss(Nobs, ons, offs, Nstates)[source]

Creates a prior matrix based on the DTW alignment (supplied by the input variables ons and offs. A rectangular window with half a Gaussian on each side over the onsets and offsets estimated by the DTW alignment.

Params Nobs:

Number of observations

Params ons:

Vector of onset times predicted by DTW alignment

Params offs:

Vector of offset times predicted by DTW alignment

Params Nstates:

Number of states in the hidden Markov model

Return prior:

Prior matrix based on DTW alignment

pyampact.alignmentUtils.fill_trans_mat(trans_seed, notes)[source]

Makes a transition matrix from a seed transition matrix. The seed matrix is composed of the states: steady state, transient, silence, transient, steady state, but the full transition matrix starts and ends with silence, so the seed with be chopped up on the ends. Notes is the number of times to repeat the seed. Transseed’s first and last states should be equivalent, as they will be overlapped with each other.

Parameters:
  • trans_seed – Transition matrix seed.

  • notes – Number of notes being aligned.

Return trans:

Transition matrix

pyampact.alignmentUtils.flatTopGaussIdx(x, b1, bi1, t1, ti1, t2, ti2, b2, bi2)[source]

Create a window function that is zeros, going up to 1s with the left half of a gaussian, then ones, then going back down to zeros with the right half of another gaussian.

Parameters:
  • x – 1D numpy array, the set of points over which this is to be calculated.

  • b1 – Float, the x coordinate 2 stddevs out from the mean of the first gaussian.

  • bi1 – Integer, the index in b1.

  • t1 – Float, the x coordinate of the mean of the first gaussian.

  • ti1 – Integer, the index in t1.

  • t2 – Float, the x coordinate of the mean of the second gaussian.

  • ti2 – Integer, the index in t2.

  • b2 – Float, the x coordinate 2 stddevs out from the mean of the second gaussian.

  • bi2 – Integer, the index in b2.

Returns:

1D numpy array, the window function.

The points should be in that order. Vectors are indexed intelligently, so you don’t have to worry about overflows or underflows.

pyampact.alignmentUtils.flatTopGaussian(x, b1, t1, t2, b2)[source]

Create a window function that is zeros, going up to 1s with the left half of a gaussian, then ones, then going back down to zeros with the right half of another gaussian.

Parameters:
  • x – 1D numpy array, the set of points over which this is to be calculated.

  • b1 – Float, the x coordinate 2 stddevs out from the mean of the first gaussian.

  • t1 – Float, the x coordinate of the mean of the first gaussian.

  • t2 – Float, the x coordinate of the mean of the second gaussian.

  • b2 – Float, the x coordinate 2 stddevs out from the mean of the second gaussian.

Returns:

1D numpy array, the window function.

The points should be in that order. If any of [b1, t1, t2] > any of [t1, t2, b2], a message ‘Endpoints are not in order: b1, t1, t2, b2’ will be printed.

pyampact.alignmentUtils.g(vec, idx, domain)[source]

Get an element from vec, checking bounds. Domain is the set of points that vec is a subset of.

Parameters:
  • vec – 1D numpy array, the input vector.

  • idx – Integer, the index in vec.

  • domain – 1D numpy array, the set of points that vec is a subset of.

Returns:

The element from vec at index idx if idx is within the bounds of vec, otherwise the first element of domain if idx is less than 1, or the last element of domain if idx is greater than the length of vec.

pyampact.alignmentUtils.gh(v1, i1, v2, i2, domain, frac=0.5)[source]

Get an element that is frac fraction of the way between v1[i1] and v2[i2], but check bounds on both vectors. frac of 0 returns v1[i1], frac of 1 returns v2[i2], frac of 0.5 (the default) returns halfway between them.

Parameters:
  • v1 – 1D numpy array, the first vector.

  • i1 – Integer, the index in v1.

  • v2 – 1D numpy array, the second vector.

  • i2 – Integer, the index in v2.

  • domain – Domain to check bounds on both vectors.

  • frac – Float, the fraction of the way between v1[i1] and v2[i2]. Default is 0.5.

Returns:

Integer, the element that is frac fraction of the way between v1[i1] and v2[i2].

pyampact.alignmentUtils.maptimes(t, intime, outtime)[source]

Map the times in t according to the mapping that each point in intime corresponds to that value in outtime.

Parameters:
  • t – 1D numpy array, input times.

  • intime – 1D numpy array, input time points.

  • outtime – 1D numpy array, output time points.

Returns:

u, a 2D numpy array of mapped times.

pyampact.alignmentUtils.orio_simmx(M, D)[source]

Calculate an Orio&Schwartz-style (Peak Structure Distance) similarity matrix.

Parameters:
  • M – Binary mask where each column corresponds to a row in the output matrix S.

  • D – Regular spectrogram, where columns of S correspond to columns of D.

Returns:

S, the similarity matrix.

pyampact.alignmentUtils.simmx(A, B)[source]

Calculate a similarity matrix between feature matrices A and B.

Parameters:
  • A – The first feature matrix.

  • B – The second feature matrix. If not provided, B will be set to A.

Returns:

The similarity matrix between A and B.

pyampact.alignmentUtils.viterbi_path(prior, transmat, obslik)[source]

VITERBI Find the most-probable (Viterbi) path through the HMM state trellis. path = viterbi(prior, transmat, obslik)

Parameters:
  • prior(i) – Pr(Q(1) = i)

  • transmat(i,j) – Pr(Q(t+1)=j | Q(t)=i)

  • obslik(i,t) – Pr(y(t) | Q(t)=i)

Returns:

  • path(t): q(t), where q1 … qT is the argmax of the above expression.

  • delta(j,t) = prob. of the best sequence of length t-1 and then going to state j, and O(1:t)

  • psi(j,t) = the best predecessor state, given that we ended up in state j at t