I have a first order linear filter $$ H(s) = \frac{1}{1+\tau\,s} $$
With the impulse response function, found using the inverse Laplace transform as $$ h(t) = \frac{1}{\tau}\,\exp\left({-\frac{t}{\tau}}\right) $$
I'm trying to match the output from the frequency response to the impulse response of the filter. For that I'm applying the discrete Fourier transform on the impulse response, evaluating $t$ as $t/f_s$. However, the phase I'm getting from the impulse response is significantly different from that of the frequency response (figures below). Any idea why is this happening and how to get an identical magnitude and phase from the impulse response function.
To reproduce the figures in R:
library(pracma)
# First order filter definition
H1st <- function(w, tau, descritize = FALSE, Ts) {
s <- 1i*w
z <- exp(1i*w*Ts)
if (descritize) {
s <- 2/Ts * (z-1)/(z+1)
}
1 /(1 + tau * s)
}
# impulse response function: inverse laplace transform of the function H1st
h1st <- function(t, tau) {
exp(-t/tau) / tau
}
sampling_freq <- 20
N <- 2000
f_seq <- seq(0, sampling_freq/2, length.out = N)
omega_seq <- 2*pi*f_seq
Ts <- 1/sampling_freq
tau1 <- 0.8
h1st_calc <- h1st(1:(N*2)*Ts, tau = tau1)
# Normalize
h1st_calc <- h1st_calc/sum(h1st_calc)
# Magnitude plot
plot((abs(H1st(omega_seq, tau = tau1,
descritize = FALSE, Ts = Ts))) ~ f_seq, ty ='l', log = 'xy',
ylab = 'magnitude, |H|', xlab = 'frequency [Hz]')
lines((abs(H1st(omega_seq, tau = tau1,
descritize = TRUE, Ts = Ts))) ~ f_seq,
ty ='l', lty = 2, col = 'blue')
lines(abs(fft(h1st_calc)[1:N]) ~ f_seq, ty ='l', col = 'red')
legend("bottomleft", c("evaluate s directly",
'bilinear transform', 'abs of fft of h(t)'), lty = 1,
col = c('black', 'blue', 'red'))
# Evaluate phase
plot((angle(H1st(omega_seq, tau = tau1,
descritize = FALSE, Ts = Ts))) ~ f_seq, ty ='l', log = 'x',
ylab = 'angle', xlab = 'frequency [Hz]')
lines((angle(H1st(omega_seq, tau = tau1,
descritize = TRUE, Ts = Ts))) ~ f_seq,
ty ='l', lty = 2, col = 'blue')
lines(angle(fft(h1st_calc))[1:(N)] ~ f_seq, ty ='l', log = 'x', col = 'red')
legend("bottomleft", c("evaluate s directly",
'bilinear transform', 'angle of fft of h(t)'), lty = 1,
col = c('black', 'blue', 'red'))