Non-negative coefficients.ΒΆ

To illustrate how bounds can be put on coefficients, a filter without negative coefficients is designed.

import matplotlib.pyplot as plt
from mplsignal import freqz_fir

from fird.constraint import CoefficientBoundConstraint
from fird.designer import FIRDesigner
from fird.objective import PiecewiseLinearObjective

o = PiecewiseLinearObjective([0, 0.2, 0.5, 1], [1, 1, 0, 0], [1, 10])
c = CoefficientBoundConstraint(lower=0)
d = FIRDesigner(21, o, c)
d.solve()
h = d.get_impulse_response()
fig, ax = plt.subplots(2, 1, layout="constrained")
freqz_fir(h, ax=ax[0], style="magnitude")
ax[1].stem(h)
fig.show()
nonnegativecoefficients

This given quite bad results, but for narrow-band filters it can be possible to get decent results.

o = PiecewiseLinearObjective([0, 0.05, 0.3, 1], [1, 1, 0, 0], [1, 10])
c = CoefficientBoundConstraint(lower=0)
d = FIRDesigner(21, o, c)
d.solve()
h = d.get_impulse_response()
fig, ax = plt.subplots(2, 1, layout="constrained")
freqz_fir(h, ax=ax[0], style="magnitude")
ax[1].stem(h)
fig.show()
nonnegativecoefficients

Total running time of the script: (0 minutes 0.599 seconds)

Gallery generated by Sphinx-Gallery