# Python scipy firwin2 has a low frequency error when designing arbitrary amplitude filter

I've been trying to design an arbitrary amplitude FIR filter which has a specific attenuation for each frequency. However I get an error at low frequency when comparing the results to my expected filter response as seen in the figure below. It doesn't change when raising the filter order. Can anyone help me? Thank you! The code is as follows:

import colorednoise as cn
from matplotlib import pyplot
import numpy
import scipy.signal as signal

# Generates a 3 seconds pink noise
x_raw=cn.powerlaw_psd_gaussian(1, 3*44100)
x_max=numpy.amax(numpy.abs(x_raw))
x=x_raw/x_max
fs=44100

# Vectors of the freq abnds and the corresponding attenuations
freq=numpy.array([0, 25.12, 31.62, 39.81, 50.12, 63.1, 79.43, 100, 125.89, 158.49, 199.53, 251.19, 316.23, 398.11, 501.19, 630.96, 794.33, 1000, 1258.93, 1584.89, 1995.25, 2511.89, 3162.28, 3981.07, 5011.87, 5012, 6309.57, 7943.28, 10000, 12589.25, 15848.93, 19952.62, 22050]);
att=numpy.array([-5.7,-5.7,-5.7,-5.7,-5.7,-2.8,-4.5,-3.9,-7.3,-11.2,-10.4,-11,-10.8,-10.1,-9.8,-7.9,-8.3,-7.7,-10.3,-12.8,-13.9,-15.1,-15.6,-17.3,-18.8,-18.8,-18.8,-18.8,-18.8,-18.8,-18.8,-18.8 ,-18.8])

gain=numpy.power(10,att/20)

b=signal.firwin2(8093,freq,gain,nyq=fs/2)

w,h=signal.freqz(b)
w,h=signal.freqz(b)
f=w*fs/(2*numpy.pi)

####### Plots the filter magnitude response ##########
pyplot.figure(1)
pyplot.plot(f,20*numpy.log10(numpy.abs(h)),label='Filter response')
pyplot.xscale('log')
pyplot.xlabel('Frequência [Hz]')
pyplot.ylabel('Magnitude [dB]')
pyplot.xlim((20,20000))
pyplot.plot(freq,att,label='Desired response')
pyplot.legend()
pyplot.show()


• Can you plot it with more frequency resolution? – endolith Feb 22 at 17:30
• I am facing the same issue, have you by any chance solved your problem. I'd be thrilled to get some tips to solve that issue. Thanks! Romain – Romain DEDIEU Apr 18 at 9:19