-
Notifications
You must be signed in to change notification settings - Fork 0
/
deconvolve.py
349 lines (273 loc) · 9.93 KB
/
deconvolve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# coding:utf-8
"""
:module: deconvolve.py
:description: Deconvolve / process impulse responses functions
:author: Michel 'Mitch' Pecqueur
:date: 2024.04
"""
import math
import numpy as np
from numpy.fft import fft, ifft
def deconvolve(audio, reference, lambd=1e-3, mode='minmax_sum'):
"""
Deconvolve audio from a reference sound (typically a sweep) to an impulse response
:param np.array audio: Convolved audio
:param np.array reference: Reference audio
:param float lambd: Peak signal-to-noise ratio
:param str mode: Match length mode between audio and ref, "min", "max" or 'minmax_sum' (to alleviate wrap-around)
:return: Resulting IR
:rtype: np.array
"""
au_nch, ref_nch = audio.ndim, reference.ndim
result = None
for i in range(au_nch):
if au_nch > 1:
conv_data = audio[:, i]
else:
conv_data = audio
if ref_nch > 1:
ref_data = reference[:, i]
else:
ref_data = reference
# Match convolved and reference audio length
r_l, c_l = len(ref_data), len(conv_data)
mn_l, mx_l = min(r_l, c_l), max(r_l, c_l)
if mode == 'min':
length = mn_l
kernel = ref_data[:length]
conv_data = conv_data[:length]
else:
length = mx_l
pad_length = (length, length + mn_l)[mode == 'minmax_sum']
conv_data = np.pad(conv_data, (0, max(0, pad_length - c_l)), mode='constant', constant_values=0)
kernel = np.pad(ref_data, (0, max(0, pad_length - r_l)), mode='constant', constant_values=0)
# Wiener Deconvolution
# Taken fom "Example of Wiener deconvolution in Python"
# Written 2015 by Dan Stowell. Public domain.
fft_k = fft(kernel)
deconv = np.real(ifft(fft(conv_data) * np.conj(fft_k) / (fft_k * np.conj(fft_k) + lambd ** 2)))[:length]
if result is None:
result = deconv
else:
result = np.column_stack((result, deconv))
return result
def compensate_ir(audio, mode='rms', sr=48000):
"""
Compensate impulse response volume so convolved audio keeps approximately the same gain as original
:param np.array audio: Input impulse response
:param str mode: Normalization mode, 'peak' or 'rms'
:param int sr: Sampling rate
:return: processed IR
:rtype: np.array
"""
nch = audio.ndim
length = len(audio)
vol_func = {'peak': peak, 'rms': rms}[mode]
test_tone = generate_sweep(length / sr, sr, db=-6, start_freq=20, window=True, os=1)
values = []
for c in range(nch):
if nch > 1:
data = audio[:, c]
else:
data = audio
orig_vol = vol_func(test_tone)
conv = np_fftconvolve(test_tone, data, mode='full')[:length]
conv_vol = vol_func(conv)
factor = orig_vol / conv_vol
values.append(factor)
gain = np.mean(values)
return audio * gain
def generate_sweep(duration=4, sr=48000, db=-6, start_freq=20, window=True, os=1):
"""
Generate logarithmic sweep tone
:param float duration: in seconds
:param int sr: Sample Rate
:param float db: Volume
:param float start_freq: in Hz
:param nool window: Apply window
:param int os: Oversampling factor
:return: Generated audio
:rtype: np.array
"""
length = int(duration * sr)
end_freq = sr / 2
pad = np.array([0])
if window:
pad = freq_to_period(start_freq, sr) * np.array([1, 1], dtype=np.int32)
freq = np.logspace(np.log10(start_freq), np.log10(end_freq), (length - sum(pad)) * os, endpoint=True)
freq[[0, -1]] = [start_freq, end_freq]
freq = np.pad(freq, pad_width=pad * os, mode='edge')
phase = np.cumsum(2 * np.pi * freq / (sr * os))
phase -= phase[0] # Start from 0
sweep = np.sin(phase) * db_to_lin(db)
if window:
w = sweep_window(length * os, int(freq_to_period(start_freq, sr * os)))
sweep *= w
if os > 1:
sweep = np_decimate(sweep, os, n=None)
return sweep
def generate_impulse(duration=4, sr=48000, db=-0.5):
length = int(duration * sr)
impulse = np.zeros(length)
impulse[1] = db_to_lin(db)
return impulse
def convolve(audio, ir, comp_ir=True, wet=1.0, sr=48000):
"""
Convolve input audio with an impulse response
:param np.array audio: Input audio
:param np.array ir: Impulse response
:param bool comp_ir: Compensate IR gain
:param float wet: Blend between processed and original audio
:param int sr: Sample Rate
:return: Processed audio
:rtype: np.array
"""
au_nch, ir_nch = audio.ndim, ir.ndim
au_l, ir_l = len(audio), len(ir)
if comp_ir:
ir = compensate_ir(ir, mode='rms', sr=sr)
# Match number of channels between IR and audio
if ir_nch > au_nch:
audio = np.tile(audio[:, np.newaxis], (1, ir_nch))
elif ir_nch < au_nch:
ir = np.tile(ir[:, np.newaxis], (1, au_nch))
# Adjust length so both audio and IR match
length = au_l + ir_l
audio = np.pad(audio, pad_width=((0, length - au_l), (0, 0)), mode='constant', constant_values=0)
ir = np.pad(ir, pad_width=((0, length - ir_l), (0, 0)), mode='constant', constant_values=0)
result = None
for c in range(ir_nch):
if ir_nch > 1:
ir_chn = ir[:, c]
au_chn = audio[:, c]
else:
ir_chn = ir
au_chn = audio
conv = np_fftconvolve(au_chn, ir_chn, mode='full')[:length]
if result is None:
result = conv
else:
result = np.column_stack((result, conv))
result = lerp(audio, result, wet)
return result
def trim_end(data, trim_db=-120, fade_db=-96, min_silence=512, min_length=None):
"""
Remove trailing silence
:param np.array data: Input audio
:param float or None trim_db: Silence threshold in dB
:param float or None fade_db: Fade threshold in dB, value should be higher than trim_db
:param int or None min_silence: Minimum silence length in samples for 1st pass
:param int or None min_length: Minimum length in samples
:return: processed audio
:rtype: np.array
"""
if trim_db is None:
return data
nch = data.ndim
if nch > 1:
mono = data.mean(axis=1)
else:
mono = data
# Trim 1st pass - trim using peak envelope
if min_silence is not None:
hop = 512
window = hop * 2
steps = np.arange(math.ceil(len(mono) / hop))
y = np.array([peak(mono[i * hop:i * hop + window]) for i in steps]) # windowed peak
th = y > db_to_lin(trim_db) # Thresholding
mn = math.ceil(min_silence / hop)
th_mx = np.array([np.max(th[i:i + mn]) for i in steps]) # Blob small silences by applying windowed max
# Detect cue indices by finding where the values change and rescale result to original length
cues = np.clip(np.where(np.diff(th_mx, prepend=False, append=False))[0] * hop, 0, len(mono) - 1)
regions = cues.reshape(-1, 2) # Pair cues to get regions
if len(regions):
# Use end of 1st region or end of 2nd last region if more than 2 are found
end = regions[-min(len(regions), 2)][1]
mono = mono[:end + 1]
length = len(mono)
# Trim 2nd pass - tighten trimming
idx = np.where(np.abs(mono) > db_to_lin(trim_db))[0]
if len(idx):
length = idx[-1] + 1
if min_length is not None:
length = max(min(len(mono), min_length), length)
data = data[:length]
# Apply pseudo-log fade out
if fade_db is not None:
idx = np.where(np.abs(mono) > db_to_lin(fade_db))[0]
if len(idx):
fade_cue = idx[-1]
fo = np.append(np.ones(fade_cue), np.linspace(1, 0, length - fade_cue))
fo = np_log(fo)
if nch > 1:
fo = fo.reshape(-1, 1)
fo = np.repeat(fo, nch, axis=1)
data *= fo
return data
def half_cosine(x):
return 0.5 - 0.5 * np.cos(np.pi * x)
def sweep_window(length, fade=512):
f_l = min(fade, length // 2)
x = half_cosine(np.linspace(0, 1, f_l))
result = np.concatenate([x, np.ones(length - 2 * f_l), x[::-1]])
return result
def freq_to_period(f, sr=48000):
return np.round(sr / np.array(f)).astype(np.int16)
def lerp(a, b, x):
return a + (b - a) * x
def db_to_lin(db):
return np.power(10, db / 20)
def lin_to_db(lin):
return 20 * np.log10(lin)
def rms(x):
return np.sqrt(np.mean(x ** 2))
def peak(x):
return np.max(np.abs(x))
def np_log(array):
"""
Pseudo log function for fades
:param np.array array:
:return:
:rtype: np.array
"""
return np.subtract(1, np.subtract(1, array) ** 4)
# Numpy implementations of scipy.signal functions
def np_fftconvolve(a, b, mode='full'):
"""
Perform fft convolution of array a by array b
:param np.array a:
:param np.array b:
:param str mode: 'full', 'same' or 'valid'
:return: Convolved result
:rtype: np.array
"""
n = a.size + b.size - 1
a_fft = fft(a, n=n)
b_fft = fft(b, n=n)
result_fft = a_fft * b_fft
result = ifft(result_fft).real
match mode:
case 'full':
return result
case 'same':
start = (len(result) - len(a)) // 2
return result[start:start + len(a)]
case 'valid':
valid_size = len(a) - len(b) + 1
return result[len(b) - 1:len(b) - 1 + valid_size]
case _:
raise ValueError("mode must be 'full', 'same', or 'valid'")
def np_decimate(x, q, n=None):
"""
Decimate x by an integer factor q using a simple low-pass filter.
:param np.array x: input signal
:param int q: Decimate factor
:param int or None n: moving average filter size, default is 8 times decimate factor
:return: Decimated result
:rtype: np.array
"""
if n is None:
n = 8 * q
kernel = np.ones(n) / n
result = np.convolve(x, kernel, mode='same')
return result[::q]