wip
This commit is contained in:
parent
0ef1bdfb4b
commit
7a7059f5c7
1 changed files with 4 additions and 16 deletions
20
process
20
process
|
@ -33,32 +33,20 @@ def process_recording(filename):
|
||||||
|
|
||||||
# get data and metadata from recording
|
# get data and metadata from recording
|
||||||
path = os.path.join(RECORDINGS_DIR, filename)
|
path = os.path.join(RECORDINGS_DIR, filename)
|
||||||
sound, samplerate = soundfile.read(path)
|
samplerate = soundfile.info(path).samplerate
|
||||||
samples_per_block = int(BLOCK_SECONDS * samplerate)
|
samples_per_block = int(BLOCK_SECONDS * samplerate)
|
||||||
overlapping_samples = int(samples_per_block * BLOCK_OVERLAP_FACTOR)
|
overlapping_samples = int(samples_per_block * BLOCK_OVERLAP_FACTOR)
|
||||||
|
|
||||||
# calculate a base amplitude for normalization
|
|
||||||
complex_amplitudes_global = rfft(sound)
|
|
||||||
base_amplitude = np.mean(np.abs(complex_amplitudes_global))
|
|
||||||
print(f'base amplitude: {base_amplitude:.5f}rDB')
|
|
||||||
|
|
||||||
# chache data about current event
|
# chache data about current event
|
||||||
current_event = None
|
current_event = None
|
||||||
|
|
||||||
# read blocks of audio data with overlap from sound variable
|
# read blocks of audio data with overlap from sound variable
|
||||||
block_num = 0
|
for block_num, block in enumerate(soundfile.blocks(path, blocksize=samples_per_block, overlap=overlapping_samples)):
|
||||||
while block_num < (len(sound) // (samples_per_block - overlapping_samples)):
|
|
||||||
# get block of audio data
|
|
||||||
start_sample = block_num * (samples_per_block - overlapping_samples)
|
|
||||||
end_sample = start_sample + samples_per_block
|
|
||||||
block = sound[start_sample:end_sample]
|
|
||||||
|
|
||||||
# get block date and calculate FFT
|
# get block date and calculate FFT
|
||||||
block_date = recording_date + datetime.timedelta(seconds=block_num * (samples_per_block - overlapping_samples) / samplerate)
|
block_date = recording_date + datetime.timedelta(seconds=block_num * (samples_per_block - overlapping_samples) / samplerate)
|
||||||
labels = rfftfreq(len(block), d=1/samplerate)
|
labels = rfftfreq(len(block), d=1/samplerate)
|
||||||
complex_amplitudes = rfft(block)
|
complex_amplitudes = rfft(block)
|
||||||
absolute_amplitudes = np.abs(complex_amplitudes)
|
amplitudes = np.abs(complex_amplitudes)
|
||||||
amplitudes = absolute_amplitudes / base_amplitude
|
|
||||||
|
|
||||||
# get amplitudes only between 100 and 1000 Hz
|
# get amplitudes only between 100 and 1000 Hz
|
||||||
adjacent_amplitudes = amplitudes[(labels >= DETECT_FREQUENCY_FROM/ADJACENCY_FACTOR) & (labels <= DETECT_FREQUENCY_TO*ADJACENCY_FACTOR)]
|
adjacent_amplitudes = amplitudes[(labels >= DETECT_FREQUENCY_FROM/ADJACENCY_FACTOR) & (labels <= DETECT_FREQUENCY_TO*ADJACENCY_FACTOR)]
|
||||||
|
@ -98,7 +86,7 @@ def process_recording(filename):
|
||||||
'end_freq': max_freq,
|
'end_freq': max_freq,
|
||||||
'max_amplitude': max(max_amplitude, current_event['max_amplitude']),
|
'max_amplitude': max(max_amplitude, current_event['max_amplitude']),
|
||||||
})
|
})
|
||||||
#print(f'{block_date}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz (noise {noise:.3f}rDB)')
|
print(f'- {block_date.strftime('%Y-%m-%d %H:%M:%S')}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz (noise {noise:.3f}rDB)')
|
||||||
else:
|
else:
|
||||||
# not detecting an event
|
# not detecting an event
|
||||||
if current_event:
|
if current_event:
|
||||||
|
|
Loading…
Reference in a new issue