R/validation.R

Defines functions H KKT

Documented in H

KKT <- function(df){

  # punto i
  N <- 2^11     # N potencia de 2 en este caso 2048 puntos
  # interpolacion spline cubica de 2048 puntos igualmente espaciados
  imag_spline <-spline(df$freq , df$imag, n = N, xmin = min(df$freq), xmax = max(df$freq))
  real_spline <-spline(df$freq , df$ireal, n = N, xmin = min(df$freq), xmax = max(df$freq))

  # punto ii  calcula fft a los datos anteriores la primer mitad tau > 0 , la segunda
  # tau < 0
  fft_imag_spline <- fft(imag_spline$y, inverse = FALSE)
  fft_real_spline <- fft(real_spline$y, inverse = FALSE)

  data.frame(freq = imag_spline$x, real = real_spline$y, imag = imag_spline$y)

#   # punto iii  OJO!!
#
#   real_from_imag <- complex( real = Im(c(fft_imag_spline[1:1024] * 1i*2/pi, fft_imag_spline[1024:2048] * 1i*-2/pi )), imaginary = 0)
#
#   imag_from_real <- complex( real = 0, imaginary = -1*(Re(c(fft_real_spline[1:1024] * 1i*2/pi, fft_real_spline[1024:2048] * 1i*-2/pi ))))
#
#   # punto iv
#   # inv_fft_real_from_imag <- (fft(real_from_imag, inverse =TRUE) / length(real_from_imag)) * pi*N
#   # inv_fft_imag_from_real <- (fft(imag_from_real, inverse =TRUE) / length(imag_from_real))* pi*N
#
#   inv_fft_real_from_imag <- (fft(real_from_imag, inverse = TRUE) / length(real_from_imag))
#   inv_fft_imag_from_real <- (fft(imag_from_real, inverse = TRUE) / length(imag_from_real))
#
#   kk <- data.frame( real = Re(inv_fft_real_from_imag), imag = Im(inv_fft_imag_from_real))
#
#   return(kk)
 }

#' The Hilbert-transformation
#'
#' The Hilbert-transform is a phase shifter, which represents the complex complement
#' to a real vauled signal. It is calculated in the complex frequency space of the
#' signal by using the Fourier transform. Finally, calculating \eqn{f = y + i*H(y)}
#' gives the analytic signal, with a one sided spectrum. (See \code{\link{analyticFunction}})
#'
#' @usage H(x)
#'
#' @param x real valued time series
#' @return A numeric real valued vector is returned
#' @export
H <- function(x)
{
  x <- as.numeric(x)
  # first calculate the normalized FFT
  X <- fft(x) / length(x)

  # then we need a virtual spatial vector which is symmetric with respect to
  # f = 0. The signum function will do that. The advantage is, that we need not
  # take care of the odd-/evenness of the length of our dataset
  xf <- 0:(length(X) - 1)
  xf <- xf - mean(xf)

  # because the negative Frequencies are located in the upper half of the
  # FFT-data vector it is nesccesary to use "-sign". This will mirror the relation
  # The "-0.5" effect is that the Nyquist frequency, in case of odd data set lenghts,
  # is not rejected.
  Xh <- -1i * X * -sign(xf - 0.5)
  return(fft(Xh, inverse = T))
}
mendivilg/eisr documentation built on June 17, 2020, 12:41 a.m.