import pyfar as pf
import numpy as np
from scipy.signal import remez
import matplotlib.pyplot as plt
# generate linear and minimum phase signal
freq = [0, 0.2, 0.3, 1.0]
desired = [1, 0]
h_linear = pf.Signal(remez(151, freq, desired, Hz=2.), 44100)
h_minimum, ratio = pf.dsp.minimum_phase(h_linear,
    method='homomorphic', return_magnitude_ratio=True)
# plot signals and difference between them
fig, axs = plt.subplots(2, figsize=(8, 4))
pf.plot.freq(h_linear, linestyle='-', ax=axs[0])
pf.plot.freq(h_minimum, linestyle='--', ax=axs[0])
pf.plot.freq(ratio, linestyle='-', ax=axs[1])
mask = np.abs(h_linear.freq) < 10**(-60/20)
ratio_masked = pf.FrequencyData(
    ratio.freq[mask], ratio.frequencies[mask[0]])
pf.plot.freq(ratio_masked, color='k', linestyle='--', ax=axs[1])
axs[1].set_ylabel('Magnitude error in dB')
axs[0].legend(['Linear phase', 'Minimum phase'])
axs[1].legend(['Broadband', 'Linear-phase < -60 dB'])
axs[1].set_ylim((-5, 105))
