x, sr = librosa.load('audio/c_strum.wav')
ipd.Audio(x, rate=sr)

plt.figure(figsize=(14, 5))
librosa.display.waveplot(x, sr)

"""
Explanation: &larr; Back to Index
Autocorrelation
The autocorrelation of a signal describes the similarity of a signal against a time-shifted version of itself. For a signal $x$, the autocorrelation $r$ is:
$$ r(k) = \sum_n x(n) x(n-k) $$
In this equation, $k$ is often called the lag parameter. $r(k)$ is maximized at $k = 0$ and is symmetric about $k$.
The autocorrelation is useful for finding repeated patterns in a signal. For example, at short lags, the autocorrelation can tell us something about the signal's fundamental frequency. For longer lags, the autocorrelation may tell us something about the tempo of a musical signal.
Let's load a file:
End of explanation
"""


# Because the autocorrelation produces a symmetric signal, we only care about the "right half".
r = numpy.correlate(x, x, mode='full')[len(x)-1:]
print(x.shape, r.shape)

"""
Explanation: numpy.correlate
There are two ways we can compute the autocorrelation in Python. The first method is numpy.correlate:
End of explanation
"""


plt.figure(figsize=(14, 5))
plt.plot(r[:10000])
plt.xlabel('Lag (samples)')
plt.xlim(0, 10000)

"""
Explanation: Plot the autocorrelation:
End of explanation
"""


r = librosa.autocorrelate(x, max_size=10000)
print(r.shape)

plt.figure(figsize=(14, 5))
plt.plot(r)
plt.xlabel('Lag (samples)')
plt.xlim(0, 10000)

"""
Explanation: librosa.autocorrelate
The second method is librosa.autocorrelate:
End of explanation
"""


x, sr = librosa.load('audio/oboe_c6.wav')
ipd.Audio(x, rate=sr)

"""
Explanation: librosa.autocorrelate conveniently only keeps one half of the autocorrelation function, since the autocorrelation is symmetric. Also, the max_size parameter prevents unnecessary calculations.
Pitch Estimation
The autocorrelation is used to find repeated patterns within a signal. For musical signals, a repeated pattern can correspond to a pitch period. We can therefore use the autocorrelation function to estimate the pitch in a musical signal.
End of explanation
"""


r = librosa.autocorrelate(x, max_size=5000)
plt.figure(figsize=(14, 5))
plt.plot(r[:200])

"""
Explanation: Compute and plot the autocorrelation:
End of explanation
"""


midi_hi = 120.0
midi_lo = 12.0
f_hi = librosa.midi_to_hz(midi_hi)
f_lo = librosa.midi_to_hz(midi_lo)
t_lo = sr/f_hi
t_hi = sr/f_lo

print(f_lo, f_hi)
print(t_lo, t_hi)

"""
Explanation: The autocorrelation always has a maximum at zero, i.e. zero lag. We want to identify the maximum outside of the peak centered at zero. Therefore, we might choose only to search within a range of reasonable pitches:
End of explanation
"""


r[:int(t_lo)] = 0
r[int(t_hi):] = 0

plt.figure(figsize=(14, 5))
plt.plot(r[:1400])

"""
Explanation: Set invalid pitch candidates to zero:
End of explanation
"""


t_max = r.argmax()
print(t_max)

"""
Explanation: Find the location of the maximum:
End of explanation
"""


float(sr)/t_max

"""
Explanation: Finally, estimate the pitch in Hertz:
End of explanation
"""


librosa.midi_to_hz(84)

"""
Explanation: Indeed, that is very close to the true frequency of C6:
End of explanation
"""


