這是我第一次在這裏問一個問題,所以我希望你能理解我的問題。x減x不是0在R
事情是,我想要做我自己的fft(),而不使用R.中給定的那個。 到目前爲止,它對seq(1,5)這樣的系列效果很好。
但是對於c(1,1)發生了一些奇怪的事情。就我所能指出的而言,似乎x-x在這種情況下不是0。代碼 這裏行:
series <- c(1,1) # defining the Serie
nr_samples <- length(series) # getting the length
#################
# Calculating the harmonic frequncy
#################
harmonic <- seq(0,(nr_samples-1))
harmonic <- 2*pi*harmonic
harmonic <- harmonic/nr_samples
#################
# Exponential funktion needed for summing up
#################
exponential <- function(index, omega){
result <- exp(-((0+1i)*omega*index))
return(result)
}
#################
# The sum for calculating the fft
#################
my_fft <- function(time_series, omega){
nr_samples <- length(time_series)
summand <- 0
# In the next loop the mistakes Happens
# While running this loop for harmonic[2]
# the rseult should be 0 because
# summand = - exp_factor
# The result for summand + exp_factor
# is 0-1.22464679914735e-16i
for (i in 1:nr_samples){
exp_factor <- exponential((i-1), omega)
summand <- summand + time_series[i]*exp_factor
print(paste("Summand", summand, "Exp", exp_factor))
}
return(summand)
}
transform <- sapply(harmonic, function(x){my_fft(series,x)})
fft_transform <- fft(series)
df <- data.frame(transform, fft_transform)
print(df)
誰能告訴我,爲什麼被加數+ exp_factor,諧波[2]是不是零?
感謝您的回答,認爲這應該有很大的幫助。 – 2014-11-09 11:59:15