-
Notifications
You must be signed in to change notification settings - Fork 25
/
porc.py
executable file
·374 lines (307 loc) · 12.4 KB
/
porc.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/python -OO
#
# Python Open Room Correction (PORC)
# Copyright (c) 2012 Mason A. Green
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# More details about the parallel filter can be found in the papers
#
# Balazs Bank, "Perceptually Motivated Audio Equalization Using Fixed-Pole Parallel
# Second-Order Filters", IEEE Signal Processing Letters, 2008.
# http://www.acoustics.hut.fi/go/spl08-parfilt
#
# Balazs Bank, "Direct Design of Parallel Second-order Filters for
# Instrument Body Modeling", International Computer Music Conference,
# Copenhagen, Denmark, Aug. 2007.
# http://www.acoustics.hut.fi/go/icmc07-parfilt
#
# For Mixed-Phase Compensation, see:
# "Mixed Time-Frequency approach for Mulitpoint Room Response Equalization," by
# Alberto Carini, et al.
# Python libs
import sys
import textwrap
import wave
from contextlib import closing
import struct
# Scipy, Numpy, and matplotlibs
import numpy as np
import scipy as sp
import scipy.io as sio
import scipy.signal as sig
from scipy.fftpack import ifft, fft
from scipy.interpolate import pchip
from scipy.io import wavfile
from scipy.signal import convolve as conv
from scipy.stats import kurtosis
from scipy.stats import norm as Gaussian
from scipy import nanstd
import matplotlib.pyplot as plt
# PORC source files
from parfiltid import parfiltid
from tfplot import tfplot, tfplots, debug_log_plot
from freqpoles import freqpoles
# Ignore warnings
import warnings; warnings.filterwarnings('ignore')
# MiniDSP's OpenDRC box likes 6144 taps
def rceps(x):
y = sp.real(ifft(sp.log(sp.absolute(fft(x)))))
n = len(x)
if (n%2) == 1:
ym = np.hstack((y[0], 2*y[1:n/2], np.zeros(n/2-1)))
else:
ym = np.hstack((y[0], 2*y[1:n/2], y[n/2+1], np.zeros(n/2-1)))
ym = sp.real(ifft(sp.exp(fft(ym))))
return (y, ym)
def parfilt(Bm, Am, FIR, x):
y = np.zeros(x.size)
for k in range(Am.shape[1]):
y += np.ravel(sig.lfilter(Bm[:,k], Am[:,k], x))
y += np.ravel(sig.lfilter(np.hstack([FIR]), np.hstack([1]), x))
return y
# Normalize signal
def norm(y): return y/np.fabs(y).max()
def dB2Mag(dB):
return 10**((dB)/20.)
# The Median Absolute Deviation along given axis of an array
# From statsmodels lib
def mad(a, c=Gaussian.ppf(3/4.), axis=0): # c \approx .6745
a = np.asarray(a)
return np.median((np.fabs(a))/c, axis=axis)
def roomcomp(impresp, filter, target, ntaps, mixed_phase, opformat, trim, nsthresh, noplot):
print("Loading impulse response")
# Read impulse response
Fs, data = wavfile.read(impresp)
data = norm(np.hstack(data))
if trim:
print("Removing leading silence")
for spos,sval in enumerate(data):
if abs(sval)>nsthresh:
lzs=max(spos-1,0)
ld =len(data)
print('Impulse starts at position ', spos, '/', len(data))
print('Trimming ', float(lzs)/float(Fs), ' seconds of silence')
data=data[lzs:len(data)] #remove everything before sample at spos
break
print("\nSample rate = ", Fs)
print("\nGenerating correction filter")
###
## Logarithmic pole positioning
###
fplog = np.hstack((sp.logspace(sp.log10(20.), sp.log10(200.), 14.), sp.logspace(sp.log10(250.),
sp.log10(20000.), 13.)))
plog = freqpoles(fplog, Fs)
###
## Preparing data
###
# making the measured response minumum-phase
cp, minresp = rceps(data)
# Impulse response
imp = np.zeros(len(data), dtype=np.float64)
imp[0]=1.0
# Target
outf = []
db = []
if target == 'flat':
# Make the target output a bandpass filter
Bf, Af = sig.butter(4, 30/(Fs/2), 'high')
outf = sig.lfilter(Bf, Af, imp)
else:
# load target file
t = np.loadtxt(target)
frq = t[:,0]; pwr = t[:,1]
# calculate the FIR filter via windowing method
fir = sig.firwin2(501, frq, np.power(10, pwr/20.0), nyq = frq[-1])
# Minimum phase, zero padding
cp, outf = rceps(np.append(fir, np.zeros(len(minresp) - len(fir))))
###
## Filter design
###
#Parallel filter design
(Bm, Am, FIR) = parfiltid(minresp, outf, plog)
# equalized loudspeaker response - filtering the
# measured transfer function by the parallel filter
equalizedresp = parfilt(Bm, Am, FIR, data)
# Equalizer impulse response - filtering a unit pulse
equalizer = norm(parfilt(Bm, Am, FIR, imp))
# Windowing with a half hanning window in time domain
han = np.hanning(ntaps*2)[-ntaps:]
equalizer = han * equalizer[:ntaps]
###
## Mixed-phase compensation
## Based on the paper "Mixed Time-Frequency approach for Multipoint
## Room Rosponse Equalization," by A. Carini et al.
## To use this feature, your Room Impulse Response should have all
## the leading zeros removed.
###
if mixed_phase is True:
# prototype function
hp = norm(np.real(equalizedresp))
# time integration of the human ear is ~24ms
# See "Measuring the mixing time in auditoria," by Defrance & Polack
hop_size = 0.024
samples = hop_size * Fs
bins = np.int(np.ceil(len(hp) / samples))
tmix = 0
# Kurtosis method
for b in range(bins):
start = np.int(b * samples)
end = np.int((b+1) * samples)
k = kurtosis(hp[start:end])
if k <= 0:
tmix = b * hop_size
break
# truncate the prototype function
taps = np.int(tmix*Fs)
print("\nmixing time(secs) = ", tmix, "; taps = ", taps)
if taps > 0:
# Time reverse the array
h = hp[:taps][::-1]
# create all pass filter
phase = np.unwrap(np.angle(h))
H = np.exp(1j*phase)
# convert from db to linear
mixed = np.power(10, np.real(H)/20.0)
# create filter's impulse response
mixed = np.real(ifft(mixed))
# convolve and window to desired length
equalizer = conv(equalizer, mixed)
equalizer = han * equalizer[:ntaps]
#data = han * data[:ntaps]
#eqresp = np.real(conv(equalizer, data))
else:
print("zero taps; skipping mixed-phase computation")
if opformat in ('wav', 'wav24'):
# Write data
wavwrite_24(filter, Fs, norm(np.real(equalizer)))
print('\nOutput format is wav24')
print('Output filter length =', len(equalizer), 'taps')
print('Output filter written to ' + filter)
print("\nUse sox to convert output .wav to raw 32 bit IEEE floating point if necessary,")
print("or to merge left and right channels into a stereo .wav")
print("\nExample: sox leq48.wav -t f32 leq48.bin")
print(" sox -M le148.wav req48.wav output.wav\n")
elif opformat == 'wav32':
wavwrite_32(filter, Fs, norm(np.real(equalizer)))
print('\nOutput format is wav32')
print('Output filter length =', len(equalizer), 'taps')
print('Output filter written to ' + filter)
print("\nUse sox to convert output .wav to raw 32 bit IEEE floating point if necessary,")
print("or to merge left and right channels into a stereo .wav")
print("\nExample: sox leq48.wav -t f32 leq48.bin")
print(" sox -M le148.wav req48.wav output.wav\n")
elif opformat == 'bin':
# direct output to bin avoids float64->pcm16->float32 conversion by going direct
#float64->float32
f = open(filter, 'wb')
norm(np.real(equalizer)).astype('float32').tofile(f)
f.close()
print('\nOutput filter length =', len(equalizer), 'taps')
print('Output filter written to ' + filter)
else:
print('Output format not recognized, no file generated.')
###
## Plots
###
if not noplot:
data *= 500
# original loudspeaker-room response
tfplot(data, Fs, avg = 'abs')
# 1/3 Octave smoothed
tfplots(data, Fs, 'r')
#tfplot(mixed, Fs, 'r')
# equalizer transfer function
tfplot(0.75*equalizer, Fs, 'g')
# indicating pole frequencies
plt.vlines(fplog, -2, 2, color='k', linestyles='solid')
# equalized loudspeaker-room response
tfplot(equalizedresp*0.01, Fs, avg = 'abs')
# 1/3 Octave smoothed
tfplots(equalizedresp*0.01, Fs, 'r')
# Add labels
# May need to reposition these based on input data
plt.text(325,30,'Unequalized loudspeaker-room response')
plt.text(100,-15,'Equalizer transfer function')
plt.text(100,-21,'(Black lines: pole locations)')
plt.text(130,-70,'Equalized loudspeaker-room response')
a = plt.gca()
a.set_xlim([20, 20000])
a.set_ylim([-80, 80])
plt.ylabel('Amplitude (dB)', color='b')
plt.xlabel('Frequency (Hz)')
plt.grid()
plt.legend()
plt.show()
def wavwrite_24(fname, fs, data):
data_as_bytes = (struct.pack('<i', int(samp*(2**23-1))) for samp in data)
with closing(wave.open(fname, 'wb')) as wavwriter:
wavwriter.setnchannels(1)
wavwriter.setsampwidth(3)
wavwriter.setframerate(fs)
for data_bytes in data_as_bytes:
wavwriter.writeframes(data_bytes[0:3])
def wavwrite_32(fname, fs, data):
data_as_bytes = (struct.pack('<i', int(samp*(2**31-1))) for samp in data)
with closing(wave.open(fname, 'wb')) as wavwriter:
wavwriter.setnchannels(1)
wavwriter.setsampwidth(4)
wavwriter.setframerate(fs)
for data_bytes in data_as_bytes:
wavwriter.writeframes(data_bytes[0:4])
def main():
print()
mtxt = textwrap.dedent('''\
Python Open Room Correction (PORC), version 0.1
Copyright (c) 2012 Mason A. Green
Based on the work of Dr. Balazs Bank
''')
bye = textwrap.dedent('''
Example:
./porc -t b&k.txt -n 8000 l48.wav leq48.bin
See the README for detailed instructions
''')
import argparse
from argparse import RawTextHelpFormatter
parser = argparse.ArgumentParser(description = mtxt, epilog=bye, formatter_class=RawTextHelpFormatter)
# Positionals
parser.add_argument('impresp', metavar='I', type=str, help='mesaured impulse response')
parser.add_argument('filter', metavar='F', type=str, help='output filter file name')
# Options
parser.add_argument("-t", dest="target", default='flat',
help="target curve", metavar="FILE")
parser.add_argument("-n", dest="ntaps", default = 6144,
help="filter length, in taps. Default = len(input)", type=int)
parser.add_argument('--mixed', action='store_true', default = False,
help="Implement mixed-phase compensation. see README for details")
parser.add_argument("-o", dest="opformat", default = 'bin',
help="Output file type, default bin optional wav", type=str)
parser.add_argument("-s", dest="nsthresh", default = 0.005,
help="Normalized silence threshold. Default = 0.05", type=float)
parser.add_argument('--trim', action='store_true', default = False,
help="Trim leading silence")
parser.add_argument('--noplot', action='store_true', default = False,
help="Do not polt the filter")
args = parser.parse_args()
roomcomp(args.impresp, args.filter, args.target, args.ntaps, args.mixed, args.opformat, args.trim, args.nsthresh, args.noplot)
if __name__=="__main__":
main()