Skip to content Skip to sidebar Skip to footer

Specify Minimum Trigger Frequency For Recording Audio In Python

I'm writing a script for sound-activated recording in Python using pyaudio. I want to trigger a 5s recording after a sound that is above a prespecified volume and frequency. I've m

Solution 1:

To retrieve the frequency of a signal you can compute Fourier transform, thus switching to frequency domain (freq in the code). Your next step is to compute relative amplitude of the signal (amp) . The latter is proportional to the sound volume.

spec = np.abs(np.fft.rfft(audio_array))
freq = np.fft.rfftfreq(len(audio_array), d=1 / sampling_freq)
spec = np.abs(spec)
amp = spec / spec.sum()

Mind that 3000 isn't a sound volume either. The true sound volume information was lost when the signal was digitalised. Now you only work with relative numbers, so you can just check if e.g. 1/3 of energy in a frame is above 10 khz.

Here's some code to illustrate the concept:

idx_above_10khz = np.argmax(freq > 10000)
amp_below_10k = amp[:idx_above_10khz].sum()
amp_above_10k = amp[idx_above_10khz:].sum()

Now you could specify that from certain ratio of amp_below_10k / amp_above_10k you should trigger your program.

Solution 2:

@Thomas

Hello, have you been able to make this working with Lukasz code ?

BR - iulian

Post a Comment for "Specify Minimum Trigger Frequency For Recording Audio In Python"