-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnote_filter_test.py
More file actions
70 lines (50 loc) · 1.52 KB
/
note_filter_test.py
File metadata and controls
70 lines (50 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## Found code from: http://stackoverflow.com/questions/18625085/how-to-plot-a-wav-file
import matplotlib.pyplot as plt
import numpy as np
import wave
import sys
def filterNote(noteFreq, omega, transform):
filterRange = 10
minFreq = noteFreq - filterRange
maxFreq = noteFreq + filterRange
filteredSignal = []
for index, frequency in enumerate(omega):
if (frequency > minFreq and frequency < maxFreq):
filteredSignal.append(transform[index])
else:
filteredSignal.append(0)
return filteredSignal
#Open the wave file
spf = wave.open('52_piano_notes.wav','r')
#Extract Raw Audio from Wav File
#Becuase this particular file is too big.
#For smaller files, use -1
print "reading frames"
signal = spf.readframes(-1)
#Make the frames into an array of integers
print "converting to integer array"
signal = np.fromstring(signal, 'Int16')
#Get the frame rate of the file
print "getting frame rate"
fs = spf.getframerate()
#Get an array of time values. We know the frame rate
#And the number of frames, so the array is easy
Time=np.linspace(0, len(signal)/fs, num=len(signal))
#Plot the wave
plt.figure(1)
plt.title('Signal Wave...')
plt.plot(Time,signal)
#plt.xlim([3.1,3.105])
#Transforms yay!
print "transforming"
transform = np.fft.fft(signal)
omega=np.linspace(-20000, 20000, num=len(transform))
plt.figure(2)
plt.title('Fourier Transform of Signal')
plt.plot(omega, transform)
print "filtering"
noteTransform = filterNote(440,omega,transform)
plt.figure(3)
plt.title('Filtered Note')
plt.plot(omega,noteTransform)
plt.show()