#windowed sinc low pass filter #y - vector to filter #t - time interval between measurements (s) #f - low pass frequency (Hz) wlpf <- function( y, t, f ) { #sinc function of frequency f sinc <- function( x, f ) ifelse(x==0, 2*pi*f, sin(2*pi*f*x)/x) #Blackman window from 0..m Blackman <- function( m ) 0.42-0.5*cos(2*pi*(0:m)/m)+0.08*cos(4*pi*(0:m)/m) m <- min(floor(length(y)/2), 500) rk <- sinc(-m:m, f*t) bk <- Blackman(2*m) * rk k <- c(bk, rep(0,length(y)-length(bk))) fy <- fft(fft(k)*fft(y), inverse=TRUE) return(Re(fy)) } ecg <- scan("http://biostatmatt.com/csv/ecg.csv") ecg <- ( ecg - mean(ecg) ) / sd(ecg)