import pyfar as pf
import numpy as np
hptf = pf.signals.files.headphone_impulse_responses()[0, 0].flatten()
regularization = pf.dsp.filter.low_shelf(
    pf.signals.impulse(hptf.n_samples), 4e3, -20, 2, 'II')
target = pf.dsp.filter.butterworth(
    pf.signals.impulse(hptf.n_samples), 4, [100, 16e3], 'bandpass')
target = pf.dsp.time_shift(target, 125, 'cyclic')
Inversion = pf.dsp.RegularizedSpectrumInversion.from_magnitude_spectrum(
    hptf, regularization, beta=.1, target=target)
inverted = Inversion.invert
ax = pf.plot.time_freq(hptf, label='HpTF')
pf.plot.time_freq(inverted, label='HpTF inverted')
pf.plot.time_freq(hptf * inverted, label='Equalized HpTF')
pf.plot.time_freq(target, linestyle='--', label='Target')
pf.plot.freq(regularization, ax=ax[1], label='Regularization')
ax[0].set_xlim(0 , .005)
ax[1].set_ylim(-40 , 15)
ax[1].legend(loc='lower center', ncols=3)
