wip
This commit is contained in:
parent
d6b2fbd069
commit
6d7acd11ee
1 changed files with 17 additions and 8 deletions
25
process
25
process
|
@ -21,6 +21,7 @@ ADJACENCY_FACTOR = 2 # area to look for noise around the target frequency
|
||||||
AMPLITUDE_THRESHOLD = 200 # relative DB (rDB) (because not calibrated)
|
AMPLITUDE_THRESHOLD = 200 # relative DB (rDB) (because not calibrated)
|
||||||
BLOCK_SECONDS = 3 # seconds (longer means more frequency resolution, but less time resolution)
|
BLOCK_SECONDS = 3 # seconds (longer means more frequency resolution, but less time resolution)
|
||||||
DETECTION_DISTANCE = 30 # seconds (minimum time between detections)
|
DETECTION_DISTANCE = 30 # seconds (minimum time between detections)
|
||||||
|
BLOCK_OVERLAP = 0.8 # overlap between blocks (0.8 means 80% overlap)
|
||||||
|
|
||||||
|
|
||||||
def process_recording(filename):
|
def process_recording(filename):
|
||||||
|
@ -30,23 +31,31 @@ def process_recording(filename):
|
||||||
date_string_from_filename = os.path.splitext(filename)[0]
|
date_string_from_filename = os.path.splitext(filename)[0]
|
||||||
recording_date = datetime.datetime.strptime(date_string_from_filename, "%Y-%m-%d_%H-%M-%S.%f%z")
|
recording_date = datetime.datetime.strptime(date_string_from_filename, "%Y-%m-%d_%H-%M-%S.%f%z")
|
||||||
|
|
||||||
# get samplerate and blocksize
|
# get data and metadata from recording
|
||||||
path = os.path.join(RECORDINGS_DIR, filename)
|
path = os.path.join(RECORDINGS_DIR, filename)
|
||||||
info = soundfile.info(path)
|
sound, samplerate = soundfile.read(path)
|
||||||
samplerate = info.samplerate
|
samples_per_block = int(BLOCK_SECONDS * samplerate)
|
||||||
block_samples = int(BLOCK_SECONDS * samplerate)
|
overlap = int(samples_per_block * BLOCK_OVERLAP)
|
||||||
|
|
||||||
|
# get median amplitude for normalization
|
||||||
|
complex_amplitudes_global = rfft(sound)
|
||||||
|
median_amplitude = np.median(np.abs(complex_amplitudes_global))
|
||||||
|
print(f'median amplitude: {median_amplitude:.5f}rDB')
|
||||||
|
|
||||||
# initialize to a very old date
|
# initialize to a very old date
|
||||||
last_detection_at = datetime.datetime.min.replace(tzinfo=recording_date.tzinfo)
|
last_detection_at = datetime.datetime.min.replace(tzinfo=recording_date.tzinfo)
|
||||||
is_detecting = False
|
is_detecting = False
|
||||||
|
|
||||||
# iterate blocks
|
# read blocks of audio data with overlap from sound variable
|
||||||
for num, block in enumerate(soundfile.blocks(path, blocksize=block_samples, overlap=int(block_samples*0.8))):
|
for num in range(0, len(sound) // (samples_per_block - overlap)):
|
||||||
|
start = num * (samples_per_block - overlap)
|
||||||
|
end = start + samples_per_block
|
||||||
|
block = sound[start:end]
|
||||||
|
|
||||||
block_date = recording_date + datetime.timedelta(seconds=num * BLOCK_SECONDS)
|
block_date = recording_date + datetime.timedelta(seconds=num * BLOCK_SECONDS)
|
||||||
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)
|
absolute_amplitudes = np.abs(complex_amplitudes)
|
||||||
median_amplitude = np.median(absolute_amplitudes)
|
|
||||||
amplitudes = absolute_amplitudes / median_amplitude
|
amplitudes = absolute_amplitudes / median_amplitude
|
||||||
|
|
||||||
# get amplitudes only between 100 and 1000 Hz
|
# get amplitudes only between 100 and 1000 Hz
|
||||||
|
@ -79,7 +88,7 @@ def process_recording(filename):
|
||||||
last_detection_at = block_date
|
last_detection_at = block_date
|
||||||
print("🔊")
|
print("🔊")
|
||||||
|
|
||||||
print(f'{block_date}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz (noise {noise:.3f}rDB, median {median_amplitude:.3f}rDB)')
|
print(f'{block_date}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz (noise {noise:.3f}rDB)')
|
||||||
else:
|
else:
|
||||||
is_detecting = False
|
is_detecting = False
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue