chipmuenk / pyfda

Python Filter Design Analysis Tool
http://chipmuenk.github.io/
MIT License
651 stars 93 forks source link

Numerical percision issues with tight filter constraints #16

Closed josef-dunbar closed 9 years ago

josef-dunbar commented 9 years ago

I've noticed an issue in the calculation of group delay and magnitude response when designing a highly constrained filter: Lowpass, IIR (N=10), Chebychev 2, Fc = 0.01, Asb = 60 . This issue appears to be caused by the use of the transfer function polynomials in the calculation and I have been able to correct the group delay by changing the calculation to use zero-pole values instead of the polynomials (see code below). I did some testing and it looks like the magnitude response appears to be corrected if the filter is designed in the analogue domain however this affects subsequent frequency and group delay calculations and the coefficients need to be transformed to the digital domain.

'''
add to pyfda/plot_widgets/plot_tau_g.py (line 82) 
remove/comment out currently defined w and tau_g variables
'''
        zz = fb.fil[0]['zpk'][0]
        pp = fb.fil[0]['zpk'][1]

        G = np.zeros(rc.params['N_FFT'])
        w = np.arange(0,1, 1/rc.params['N_FFT']) * np.pi
        for i in range(zz.shape[0]):
            rz = np.abs(zz[i])
            az = np.angle(zz[i])
            G += (-rz*np.cos(w-az) + rz**2) / (rz**2 - 2*rz*np.cos(w-az) + 1)
        for i in range(pp.shape[0]):
            rp = np.abs(pp[i])
            ap = np.angle(pp[i])
            G -= (-rp*np.cos(w-ap) + rp**2) / (rp**2 - 2*rp*np.cos(w-ap) + 1)
        tau_g = G
chipmuenk commented 9 years ago

Tnanx for adding the issue - I've created the branch "group_delay" where you can choose between my (numerically ill-conditioned) group delay algorithm and the algorithm recently added in scipy 0.16. There is a checkbox in the groupdelay widget so that you can compare easily. Please experiment with this branch, you could also add / replace your algorithm for performance comparison, and tell me what you think.

chipmuenk commented 9 years ago

I have played around with the v0.16 scipy.signal algorithm, liked it and copied it to pyfda_lib. When scipy 0.16 has become "standard" it could be removed again. In the latest pre-release I've also improved the GUI - give it a try.