Search
 
SCRIPT & CODE EXAMPLE
 

R

fourier in R

dat   <- read.csv("Baguio.csv", header=FALSE)
y     <- dat$V1
t     <- 1:73
rg    <- diff(range(y))

nff = function(x = NULL, n = NULL, up = 10L, plot = TRUE, add = FALSE, main = NULL, ...){
  #The direct transformation
  #The first frequency is DC, the rest are duplicated
  dff = fft(x)
  #The time
  t = seq(from = 1, to = length(x))
  #Upsampled time
  nt = seq(from = 1, to = length(x)+1-1/up, by = 1/up)
  #New spectrum
  ndff = array(data = 0, dim = c(length(nt), 1L))
  ndff[1] = dff[1] #Always, it's the DC component
  if(n != 0){
    ndff[2:(n+1)] = dff[2:(n+1)] #The positive frequencies always come first
    #The negative ones are trickier
    ndff[length(ndff):(length(ndff) - n + 1)] = dff[length(x):(length(x) - n + 1)]
  }
  #The inverses
  indff = fft(ndff/73, inverse = TRUE)
  idff = fft(dff/73, inverse = TRUE)
  if(plot){
    if(!add){
      plot(x = t, y = x, pch = 16L, xlab = "Time", ylab = "Measurement",
        main = ifelse(is.null(main), paste(n, "harmonics"), main))
      lines(y = Mod(idff), x = t, col = adjustcolor(1L, alpha = 0.5))
    }
    lines(y = Mod(indff), x = nt, ...)
  }
  ret = data.frame(time = nt, y = Mod(indff))
  return(ret)
}
Comment

PREVIOUS NEXT
Code Example
R :: data table R select several columns 
R :: r combine strings 
R :: create dataframe or table in r 
R :: R rename singl edf column 
R :: ggplot - blank title of axis 
R :: how to read a vector input in r 
R :: sort in descending order in r 
R :: nls in r 
R :: replace na with 0 in r 
R :: Getting rid of row names in R 
R :: na by column r 
R :: r type of object 
R :: r number of blanks in the data 
R :: ggplot abline thickness 
R :: for loop in R dictionary 
R :: How to calculate standardized residuals in R 
R :: finding chi square value in r 
R :: run regression for certain groups in r 
R :: ts object to data frame 
R :: Use regex to extract row in R (solution 1) 
R :: negate R 
R :: hypergeometric distribution on r 
R :: R gap between date add missing value 
R :: r - if value in a df is between two number then add 1 
Rust :: read file buffer rust 
Rust :: rust setup in vim 
Rust :: rust copy to clipboard 
Rust :: How to print out a variable in rust 
Rust :: armanriazi•rust•unsafe•rawpointer 
Rust :: armanriazi•rust•static 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =