Skip to content

Instantly share code, notes, and snippets.

@mattlogan
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattlogan/9822720 to your computer and use it in GitHub Desktop.
Save mattlogan/9822720 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
# Load the text file
# This gives us an Nx4 matrix with columns: t, left, t, right
data = np.loadtxt("jeremih.txt")
print('Num samples: ' + str(len(data)))
# Make Nx2 matrix with columns: t, mean(left, right)
amp_vs_time = [[row[0], np.mean(row[1], row[3])] for row in data]
# Make 2xN matrix with rows: t, mean(left, right)
amp_vs_time = np.transpose(amp_vs_time)
# Define function to get first peak in some range
def t_of_first_peak_in_range(min_t, max_t):
for i in range(len(amp_vs_time[0])):
t = amp_vs_time[0][i]
if min_t < t < max_t and amp_vs_time[1][i] > 0.5:
return t
# Determine times for beat one and the and of two
beat_one = t_of_first_peak_in_range(1.5, 2)
beat_two_and = t_of_first_peak_in_range(3, 3.5)
print(str(beat_two_and - beat_one))
# Make a list of all 32nd notes from beat one to the and of two
thirtysecond_notes = np.linspace(beat_one, beat_two_and, 13)
# Determine time of 2nd recorded 32nd note
second_thirtysecond_rec = t_of_first_peak_in_range(thirtysecond_notes[1],
thirtysecond_notes[2])
# Determine difference in time between recorded time and rhythmic time
rec_rhythm_diff = second_thirtysecond_rec - thirtysecond_notes[1]
print ('Offset (s): ' + str(rec_rhythm_diff) + ' s')
# Determine percent offset
percent_offset = rec_rhythm_diff / (thirtysecond_notes[1]
- thirtysecond_notes[0])
print('Percent offset: ' + str(percent_offset * 100) + '%')
# Plot amplitude vs time
plt.plot(amp_vs_time[0], amp_vs_time[1])
# Overlay some vertical lines where the 32nd notes SHOULD be
plt.vlines(thirtysecond_notes, -1, 1, color='red')
# Show me the money!
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment