Skip to content

Instantly share code, notes, and snippets.

@wiccy46
Last active July 23, 2019 08:25
Show Gist options
  • Save wiccy46/b5cb5e3f5d674a5f4b48047d18a19837 to your computer and use it in GitHub Desktop.
Save wiccy46/b5cb5e3f5d674a5f4b48047d18a19837 to your computer and use it in GitHub Desktop.
[env_detec]Envelope Detections of signal #python #dsp
"""1: Peak Detection"""
def envfol_peak_detection(sig, sr, chunk=4, order=6, lpfreq=150):
"""Envelope follower based on peak detection"""
s = sig.shape[0] // chunk
new_sig = np.array_split(np.abs(sig), s)
result = []
for i in new_sig:
result.append(np.max(i))
result = np.array(result)
nyq = 0.5 * sr
normal_cutoff = lpfreq / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
result = lfilter(b, a, result)
return result
"""2: Lowpass filter"""
def envfol_filter(sig, sr, smooth_sec=0.125):
"""Envelop follower based on lowpass filter.
Parameters
----------
sig : numpy.ndarray
signal array
sr : int
sampling rate
smooth_sec : float
Smoothing parameter in second. (sensitivity)
Returns
-------
wav_env : numpy.ndarray
Signal envelop in amplitude.
"""
smooth_hz = 1 / smooth_sec
cutoff = smooth_hz/(sr / 2.)
b, a = butter(1, cutoff, btype='low', analog=False)
wav_env = np.sqrt(lfilter(b, a, sig*sig)) # Rectify signal
return wav_env
def convert_SPL(sig):
PA_ref = 20e-6
SPL_dB = 20.0 * np.log((sig / PA_ref))
return SPL_dB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment