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.
library(signal)
# Butterworth filter
# Gain formula from wikipedia
Butterworth_gain <- function(freq, cutoff_frequency, n = 1) {
1/(1+(freq/cutoff_frequency)^(2*n))
}
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')