-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchap6.R
More file actions
44 lines (40 loc) · 1.43 KB
/
Copy pathchap6.R
File metadata and controls
44 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Chap6 Adaptive Filters
# 参考wiki: https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Zurbenko_filter
# 发现竟然有两个R包和Python都有实现,这里给出使用2016年kza的一个demo
# Kolmogorov-Zurbenko Fourier Transform (KZFT)
# example taken from Wei Yang's KZFT package
# transfer function of the kzft(201,5) at frequency 0.025
library(kza)
lamda<-seq(-0.1,0.1,by=0.001)
tf1<-transfer_function(201,1,lamda,0.025)
tf2<-transfer_function(201,5,lamda,0.025)
matplot(lamda,cbind(log(tf1),log(tf2)),type="l",ylim=c(-15,0),
ylab="Natural log transformation of the coefficients",
xlab="Frequency (cycles/time unit)",
main="Transfer function of kzft(201,5) at frequency 0.025")
# example with missing values
set.seed(2)
period=101
f<-1/period
t<-1:2000
s<-1*sin(2*pi*f*t)
x<-s
noise<-3*rnorm(length(t))
x<-s+noise
m=101
rand_idx <- sample(t,100,replace=FALSE)
x[rand_idx]<-NA
x<-as.vector(na.omit(x))
system.time(z1<-kzft(x, m=m, k=1, f=f))
system.time(z2<-kzft(x, m=m, k=2, f=f))
system.time(z3<-kzft(x, m=m, k=3, f=f))
par(mfrow=c(2,2))
plot(x,type="l",main="Original time series",xlab="t", ylab="y")
lines(s,col="blue")
plot(2*Re(z1),type="l",main="kzft(101,1)",xlab="t", ylab="y", ylim=c(-6,6))
lines(s,col="blue")
plot(2*Re(z2),type="l",main="kzft(101,2)",xlab="t", ylab="y", ylim=c(-6,6))
lines(s,col="blue")
plot(2*Re(z3),type="l",main="kzft(101,3)",xlab="t", ylab="y", ylim=c(-6,6))
lines(s,col="blue")
par(mfrow=c(1,1))