I'm trying to calculate the Butterworth filter gain. If I use the formula mentioned on Wikipedia: $$ G^2(\omega) = \frac{G_0^2} {1+\left(\frac{j\omega}{j\omega_c}\right)^{2n}} $$

I don't get a matching result from calculating the gain directly from the filter's magnitude using R's signal package.


# Butterworth filter

# Gain formula from wikipedia
Butterworth_gain <- function(freq, cutoff_frequency, n = 1) {

bf <- butter(n = 2, W = .6, type = "low")
bfr <- freqz(bf)

plot(bfr$f, abs(bfr$h)^2, ty ='l')
lines(bfr$f, Butterworth_gain(bfr$f,  pi*.6, n = 2), col = 'red')

enter image description here


1 Answer 1


The function butter() computes the coefficients of a discrete-time ("digital") Butterworth filter, whereas the gain formula you used is valid for a continuous-time ("analog") Butterworth filter. According to the R documentation you can use butter() to compute an analog filter using the plane argument.

The gain of a discrete-time Butterworth filter is obtained by the bilinear transform, which substitutes the "analog" frequency variable by $\tan(\omega/2)$, where $\omega$ is the frequency in radians, normalized by the sampling frequency. For a discrete-time unit gain lowpass filter of order $n$, the squared gain is given by

$$G^2(\omega)=\frac{1}{1+\left(\frac{\tan(\omega/2)}{\tan(\omega_c/2)}\right)^{2n}},\qquad |\omega|\le\pi\tag{1}$$

  • 1
    $\begingroup$ @Crataegus: You mean the gain of a digital Butterworth filter? $\endgroup$
    – Matt L.
    Feb 23, 2021 at 12:51
  • $\begingroup$ yes exactly, ideally I want to have an expression similar to the one mentioned in Wikipedia but for a digital filter. $\endgroup$
    – Crataegus
    Feb 23, 2021 at 13:00
  • 1
    $\begingroup$ @Crataegus: I've added the formula for the discrete-time case. $\endgroup$
    – Matt L.
    Feb 23, 2021 at 14:06
  • $\begingroup$ It looks like OP is using the squared formula, but is the built-in one also squared? $\endgroup$ Feb 23, 2021 at 16:54
  • $\begingroup$ @aconcernedcitizen: He plots the squared magnitude: abs(bfr$h)^2, so it's correct. $\endgroup$
    – Matt L.
    Feb 23, 2021 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.