Skip to content

Instantly share code, notes, and snippets.

@akashrajkn
Last active October 1, 2022 15:29
Show Gist options
  • Save akashrajkn/215b0bba02d9a1ce43fbc9842dead43a to your computer and use it in GitHub Desktop.
Save akashrajkn/215b0bba02d9a1ce43fbc9842dead43a to your computer and use it in GitHub Desktop.
Python function to compute room impulse response using the exponential sine sweep method with librosa
import librosa
import numpy as np
def compute_impulse_response(ess_rec_path, ess_inv_path, target_ir_path):
"""
Computes impulse response using the Exponential sine sweep (ESS) method.
Code based on IR Capture project: http://tulrich.com/recording/ir_capture/
Args:
ess_rec_path : Location to recorded ESS sweep (Assumes stereo)
ess_inv_path : Location to the inverse sweep signal
target_ir_path : Path to save the computed impulse response
"""
inv_data, inv_sr = librosa.load(ess_inv_path, sr=None, mono=True)
ess_data, ess_sr = librosa.load(ess_rec_path, sr=None, mono=False)
assert inv_sr == ess_sr, f"Sweep recording samplerate: {ess_sr} is not equal to inverse sweep samplerate: {inv_sr}"
channel_1 = np.convolve(ess_data[0], inv_data)
channel_2 = np.convolve(ess_data[1], inv_data)
scale = 0.5 / max(np.amax(channel_1), -np.amin(channel_1), np.amax(channel_2), np.amin(channel_2))
channel_1 *= scale
channel_2 *= scale
ir = np.vstack((channel_1, channel_2)).T
librosa.output.write_wav(target_ir_path, ir, sr=ess_sr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment