I am trying to figure out the mean frequency and the Q25 and Q75 values for a sound clip, but am running into issues (mainly due to my lack of mathematical and DSP knowledge).
I'm going off of this answer, and am running into issues combining the code in that answer with reading a .wav file.
Here is the code I'm using to record...
def record_sample(file):
# Audio Recording
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
pa = pyaudio.PyAudio()
# Record sample.
stream = pa.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
frames = []
for _ in range(int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
# Save to wave file.
wf = wave.open(file, "wb")
wf.setnchannels(CHANNELS)
wf.setsampwidth(pa.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
That all works fine. Here is the code I have for computing mean frequency, Q25, and Q75...
def spectral_properties(file):
# Note: scipy.io.wavfile.read
fs, data = wavfile.read(file)
spec = np.abs(np.fft.rfft(data))
freq = np.fft.rfftfreq(len(data), d=1 / fs)
spec = np.abs(spec)
amp = spec / spec.sum()
amp_cumsum = np.cumsum(amp)
Q25 = freq[len(amp_cumsum[amp_cumsum <= 0.25]) + 1]
Q75 = freq[len(amp_cumsum[amp_cumsum <= 0.75]) + 1]
print((freq * amp).sum(), Q25, Q75)
And the error that it is producing...
File "/home/horner/workspace/school/ML/machine-learning-project-mdx97/program/audio.py", line 65, in spectral_properties
Q75 = freq[len(amp_cumsum[amp_cumsum <= 0.75]) + 1]
IndexError: index 298981 is out of bounds for axis 0 with size 110081