Skip to content

Instantly share code, notes, and snippets.

View dbetchkal's full-sized avatar
👂

Davyd Halyn Betchkal dbetchkal

👂
View GitHub Profile
@dbetchkal
dbetchkal / contiguous_regions.py
Created April 16, 2024 00:02
Given a binary condition, this function returns the bounding indices where the condition is true. Useful for simplifying time series or matrices.
def contiguous_regions(condition):
"""
Finds contiguous True regions of an input array.
Parameters
----------
condition : `np.ndarray` of dtype "bool" or `np.ndarray` and conditional logic statement to produce the boolean array
e.g., arr or arr > 5.5
@dbetchkal
dbetchkal / plot_image_fft.py
Created February 10, 2024 05:06
Plot the frequency-domain representation of an image using PIL and numpy
def plot_image_fft(img_path, n_labels=7, d=1):
'''
Plot an image alongside its frequency-domain representation.
Parameters
----------
img_path: (str) the path to an image
n_labels: (int) the number of labels to space out along each axis, default=7
@dbetchkal
dbetchkal / contrast_2d.py
Last active December 28, 2023 21:28
Calculate spectral contrast angle from a spectrogram
def contrast_2d(arr, spectrum):
"""
For an arbitrary 2D spectrogram and a characteristic 1D spectrum,
find the magnitude of each Bivector rotor separating them in degrees.
Paramters:
arr: (`pd.DataFrame`) a 2D (t,f) matrix of spectrogram data with `pd.index.DateTimeIndex`.
spectrum: (`np.array`) a vector (f) of sound levels representing a characteristic spectrum.
@dbetchkal
dbetchkal / cardinal_to_degree.py
Created February 4, 2023 19:04
Cardinal direction to degrees
def cardinal_to_degree(cardinal_direction, bounds=False):
'''
Convert cardinal direction (i.e., "ESE") to degrees.
Output may be returned either as bounds [low, high] or an average bearing.
Inputs
------
cardinal_direction (str): initials of a cardinal (e.g., "N"), intercardinal (e.g. "NW"),
or secondary intercardinal (e.g., "NNW") direction.
@dbetchkal
dbetchkal / Wittekind_function.py
Last active January 7, 2023 00:36
Wittekind vessel noise source model
def vessel_Lw(octave_f_c, v, v_c, C_B, D, E_num, E_mass, E_mount_spec):
'''
The Wittekind vessel noise source model
Wittekind, D. K.: A simple model for the underwater
noise source level of ships, J. Sh. Prod. Des., 30, 1–8,
https://doi.org/10.5957/JSPD.30.1.120052, 2014.
from equations published in Jalkanen et al. 2018,
@dbetchkal
dbetchkal / offset_SPL.py
Created December 5, 2022 23:57
Sound Level of Road Vehicle Pass-by At Distance Using Mechanical Specifications
def offset_SPL(d_offset, V, speed_MPH, m, r, n_tires, A, Cd=0.8, ρ=1.1, surface_condition='dry'):
'''
SOUND LEVEL OF A COMBUSTION-ENGINE DRIVEN ROAD VEHICLE
PROXIMAL TO THE ROAD SURFACE
T. Priede 1970,
"...the main criterion which determines [automotive] noise is the operational speed
or how short the time interval is within which the operation of one cycle of events
@dbetchkal
dbetchkal / Basic Attenuation Functions for Acoustics of Flowing Water.py
Last active September 22, 2021 19:29
A set of functions to compute attenuation of river sound, including spreading loss, ground effect, atmospheric absorption, and diffraction around the riverbank.
import numpy as np
np.set_printoptions(suppress=True)
import pandas as pd
import matplotlib.pyplot as plt
def get_third_octave_limits():
'''
Scrape limits of one-third octave bands from Engineering Toolbox HTML table.
'''
@dbetchkal
dbetchkal / 202110625_Rasterize.py
Created June 26, 2021 00:35
A very simple function for rasterizing point data using another raster as a template.
def Rasterize(geodataframe, inras, outras, field):
'''
Burn a geodataframe to a raster using an existing raster
as a template.
'''
# open the template raster
with rasterio.open(inras) as src:
@dbetchkal
dbetchkal / 20210130_BeatMatching_Dannenberg2005.py
Last active January 31, 2021 02:14
20210130_BeatMatching_Dannenberg2005
# Re-writing the first steps in a beat-matching algorithm described by Roger Dannenberg:
#
# https://www.cs.cmu.edu/~rbd/papers/dannenberg-ismir2005-beat-tracking.pdf
#
#
# Particularly helpful is the function `conditional_threshold` which could be applied
# more generally in thresholding situations where sections/chunks of a time series have
# very different peaking behavior.
#
@dbetchkal
dbetchkal / 20210122_HourlyTimeAudible_RasterPlot.py
Created January 23, 2021 01:44
20210122_HourlyTimeAudible_RasterPlot
# Imports
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt, rcParams, rc
import matplotlib.dates as mdates
import datetime as dt
import sys
import os
# You'll need to clone two Github repositories to use this code