Learn R Programming

kzfs (version 1.0.1.5)

kz.ft: Kolmogorov-Zurbenko Fourier Transform Function

Description

kz.ft is improved version of Wei Yang's kzft::kzft. It is modified to handle missing values in the data. Its outputs also include the KZ periodogram.

kz.ftc is an experimental version of KZFT for signals sampled on continual time/space points with irregular intervals. Missing is common in this scheme. However, you may need large window size for reconstruction of the signals.

Usage

kz.ft(x, m, ...)

kz.ftc(x, m, ...)

Arguments

x

The data vector. Missing values are allowed.

m

The window size for a regular Fourier transform

...

Other arguments.

  • k : Integer. The iterations number of KZFT.

  • f : Vector. Selected frequencies. Default value is c(1:m)/m

  • n : The sampling frequency rate as a multiplication of the Fourier frequencies

  • p : The distance between two successive intervals as a percentage of the total length of the data series.

  • adpt : Logic. Flag for using adaptive window size, or not. Default is TRUE.

  • phase : Logic. Flag for correcting phase shift, or not. Default is TRUE.

Value

List. It includes data frame for Fourier transform matrix tfmatrix, column means of Fourier transform matrix fft, vector pg and f for KZ-periodogram values and corresponding frequencies.

Details

If 2*m*f is not an interger, the recovered signal may have included a phase shift. However, if the option of "phase shift correction is enabled, the related errors caused by the phase shift can be limited to an acceptable level.

Another way to reduce the errors caused by unmatched m and emphf is to use the option of "adaptive window size". It will help you select the best window size for the given frequencies and the data length automatically. But it only works well when there is m for integer values 2*m*f.

These two options haven't been implemented for kz.ftc.

See Also

kzft, kz.smpg, kzp2

Examples

Run this code
## Adapted from kzft::kzp example 2
t <- 1:2000
y <- 1.1*sin(2*pi*0.0339*t)+7*sin(2*pi*0.0366*t)+5*rnorm(length(t),0,1)
y[sample(t,100,replace=FALSE)] <- NA
ft <- kz.ft(y, f=c(0.0339, 0.0366), k=2, m=1000, n=10)
# It may take 10 ~ 20 seconds
 # system.time(ft <- kz.ft(y, k=2, m=1000, n=10)) 
plot(y=log(ft$pg+1), x=ft$f, type="l", xlim=c(0.02,0.05))
abline(v=c(0.0339, 0.0366), lty=21, col="red")

## recover signal
	t <- 1:2000
y <- 1.1*sin(2*pi*0.01*t)+2*sin(2*pi*0.03*t)
y[sample(t,500,replace=FALSE)] <- NA
noise <- rnorm(2000,0,1)
system.time(ft <- kz.ft(y + 1.0*noise, k=5, f=c(0.01,0.03), m=100))
yr <- 2*Re(rowSums(ft$tf))
cor(yr, y[1:length(yr)], use="pairwise.complete.obs")
plot(yr, type="p", cex=0.5)
points(y[1:length(yr)],type="l", col="red")
## Example for kz.ftc

	t <- runif(2000)*2000
f <- c(0.15, 0.1)
x <- sin(2*pi*f[1]*t + pi/4)
y <- sin(2*pi*f[2]*t + pi/12)
y <- y[order(t)]
x <- x[order(t)]
tr <- t[order(t)]
noise <- rnorm(length(tr),0,1)
plot(y=y+x, x=tr, type="l")

ft <- kz.ftc(x+y+2*noise, xt=tr, k=2, m=1000)
plot(y=ft$pg, x=ft$f, type="l")
abline(v=f, col="grey", lty=21)
mtext("Spectrum of Longitudinal Data, Selected f")

ft <- kz.ftc(x+y+noise, xt=tr, f=f, k=1, m=1900)
 	yr <- rowSums(2*Re(ft$tf))
iv <- 0:60
plot(y=(x+y+noise)[iv], x=tr[iv], type="p", col="grey")
xt <- (0:8000)/100
yt <- sin(2*pi*f[1]*xt+pi/4) + sin(2*pi*f[2]*xt+pi/12)
y2 <- sin(2*pi*f[1]*iv+pi/4) + sin(2*pi*f[2]*iv+pi/12)
	points(yt, x=xt, col="grey", cex=0.5, lwd=1, type="l")
	points(y2, x=iv, col="blue", cex=0.75, lwd=1, type="p")
points(y=yr, x=0:(length(yr)-1), type="p", cex=0.5, lwd=1, col="red")
mtext("k=1, m=1900, x+y+noise, red for reconstruction, grey points for data input", cex=0.75)

Run the code above in your browser using DataLab