Skip to content

Commit 0ef2ddc

Browse files
committed
v1.0.0
Science Fair is right around the corner and overtonic is complete! (For now, anyways) -- if overtonic makes it up to school-wide science fair I may make a few more edits to spruce it up a bit.
1 parent ff705cb commit 0ef2ddc

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

overtonic.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# overtonic.py
1+
# overtonic.py (v1.0.0)
22
# Using Fast Fourier Transforms (FFTs) to determine an instrument based on the musical overtones of its sound
33

44
import numpy as np
@@ -16,13 +16,18 @@
1616
# If you change this, remember to change the savefig location to match your directory tree!
1717
soundfile = 'sound/sine-a4.wav'
1818

19+
soundfilename = soundfile.split('/')[1] # Don't touch
20+
1921
# Set to True if you want the output figure (with peaks) to save!
2022
# Usually best to leave at false except for demos, etc.
2123
# Split is a bit questionable, figure out how to do it the right way
2224
# This will need to be changed if a different directory tree is used
2325
savefig = False
24-
savefigloc = 'images/' + soundfile.split('/')[1] + '.png'
26+
savefigloc = 'images/' + soundfilename + '.png'
2527

28+
# Set to True if you want the output figure (with peaks) to be displayed
29+
# in a matplotlib window!
30+
showfig = True
2631

2732
##################################################
2833

@@ -48,34 +53,37 @@
4853

4954
y2 = y + np.polyval([0.002,-0.08,5], x)
5055
base = peakutils.baseline(y2, 2) # remove the baseline for a cleaner reading
56+
y3 = y2-base
5157

5258
# TODO: Conditional-ize the thres and min_dist per sound if possible
5359
indexes = peakutils.indexes(y, thres=0.7, min_dist=100)
5460

61+
y4 = y3[indexes]
62+
5563
print("Indexes", indexes)
5664
print("array of Indexes", array[indexes])
65+
print("y of Indexes (amplitude for array of indexes)", y4) # amplitude
5766

58-
peakutils.plot.plot(x, y2-base, indexes) # plot x and y with removed baseline, peaks (indexes)
59-
plt.title('Peaks after Removed Baseline')
67+
peakutils.plot.plot(x, y3, indexes) # plot x and y with removed baseline, peaks (indexes)
68+
plt.title('Peaks after Removed Baseline: ' + soundfilename)
6069
xlabel('Frequency (Hz)')
6170
ylabel('Amplitude (dB) without Baseline')
6271

63-
# Save figure, use for demo only
72+
# Save figure, use for demo only (see program variables)
6473
if savefig == True:
6574
plt.savefig(savefigloc, bbox_inches='tight')
75+
# Show figure, best to leave on (see program variables)
76+
if showfig == True:
77+
plt.show()
6678

67-
plt.show()
68-
69-
# TODO:
70-
# Set the conditions for the if/else block
71-
# Add a "more or less" part to ensure that this works with other files of the same sound source
72-
79+
# TODO (future) - make this whole if/else thing into a neural network so long conditionals don't need to be a thing
80+
7381
inst = None
7482
if len(indexes) == 1: # Sine waves have one peak
7583
inst = 'a SINE WAVE'
7684
elif len(indexes) == 2: # Whistles have two peaks
7785
inst = 'a WHISTLE'
78-
elif len(indexes) == 5: # Pianos have five peaks, but not really and this should be changed! (TODO)
86+
elif len(indexes) == 5 and round(y4[0] / y4[1], 2) == 1.24 and round(y4[0] / y4[2], 2) == 1.38 and round(y4[0] / y4[3], 2) == 1.24: # it's sketchy but it works
7987
inst = 'a PIANO'
8088

8189
if inst:

0 commit comments

Comments
 (0)