// The mask selects out constant regions + active borders
mask = ~np.pad(matches[:, t], 1, mode="constant")
starts = np.argwhere(matches[:, t] & mask[:-2]).squeeze()
ends = np.argwhere(matches[:, t] & mask[2:]).squeeze()
// Set up inner loop
frqs = np.zeros_like(starts, dtype=float)
mags = np.zeros_like(starts, dtype=float)
for i, (u, v) in enumerate(zip(starts, ends)):
// Weight frequencies by energy
weights = np.abs(D[u:v+1, t])
mags[i] = weights.sum()
// Compute the weighted average frequency.
// FIXME: is this the right thing to do?
// These are frquencies... shouldn"t be a
// weighted geometric average?
frqs[i] = weights.dot(if_gram[u:v+1, t])
if mags[i] > 0:
frqs[i] /= mags[i]
// Clip outside the ramp zones
idx = (fmax[-1] < frqs) | (frqs < fmin[0])
mags[idx] = 0
frqs[idx] = 0
// Ramp down at the high end
idx = (fmax[-1] > frqs) & (frqs > fmax[0])
mags[idx] *= (fmax[-1] - frqs[idx]) / (fmax[-1] - fmax[0])
// Ramp up from the bottom end
idx = (fmin[-1] > frqs) & (frqs > fmin[0])
mags[idx] *= (frqs[idx] - fmin[0]) / (fmin[-1] - fmin[0])
// Assign pitch and magnitude to their center bin
bins = np.round(0.5 * (starts+ends)).astype(int)
pitches[bins, t] = frqs
magnitudes[bins, t] = mags
return pitches, magnitudes, D
After Change
// The mask selects out constant regions + active borders
mask = ~np.pad(matches[:, t], 1, mode="constant")
starts = np.argwhere(matches[:, t] & mask[:-2])
ends = np.argwhere(matches[:, t] & mask[2:])
// Set up inner loop
frqs = np.zeros_like(starts, dtype=float)
mags = np.zeros_like(starts, dtype=float)
for i, (u, v) in enumerate(zip(starts, ends)):
// Weight frequencies by energy
weights = np.abs(D[u:v+1, t])
mags[i] = weights.sum()
// Compute the weighted average frequency.
// FIXME: is this the right thing to do?
// These are frquencies... shouldn"t be a
// weighted geometric average?
frqs[i] = weights.dot(if_gram[u:v+1, t])
if mags[i] > 0:
frqs[i] /= mags[i]
// Clip outside the ramp zones
idx = (fmax[-1] < frqs) | (frqs < fmin[0])
mags[idx] = 0
frqs[idx] = 0
// Ramp down at the high end
idx = (fmax[-1] > frqs) & (frqs > fmax[0])
mags[idx] *= (fmax[-1] - frqs[idx]) / (fmax[-1] - fmax[0])
// Ramp up from the bottom end
idx = (fmin[-1] > frqs) & (frqs > fmin[0])
mags[idx] *= (frqs[idx] - fmin[0]) / (fmin[-1] - fmin[0])
// Assign pitch and magnitude to their center bin
bins = np.round(0.5 * (starts+ends)).astype(int)
pitches[bins, t] = frqs
magnitudes[bins, t] = mags
return pitches, magnitudes, D