#' Interference values #' #' Simulation of interference values at times t. Using the simple formula derived #' analytically. #' #' @param t numerical vector #' @param pc 1/switching frequency #' @param duty duty cycle #' @param t0 a numerical vector of initial starts ( assumed to be U(-pc, 0) ) #' #' @return a numerical vector of values of interference at times t #' @export #' #' @examples Simulation <- function(t, pc, duty, t0, Np, A, f, lambda) { ii <- (seq_len(Np) - 1) * pc # Calculated parameters. A1 <- purrr::map_dbl(t0, function(.t0) sum( cos(2 * pi * f * (ii - .t0)) * exp(-lambda * (ii - .t0)) )) B1 <- purrr::map_dbl(t0, function(.t0) sum( sin(2 * pi * f * (ii - .t0)) * exp(-lambda * (ii - .t0)) )) A2 <- purrr::map_dbl(t0, function(.t0) sum( cos(2 * pi * f * (ii - duty * pc - .t0)) * exp(-lambda * (ii - duty * pc - .t0)) )) B2 <- purrr::map_dbl(t0, function(.t0) sum( sin(2 * pi * f * (ii - duty * pc - .t0)) * exp(-lambda * (ii - duty * pc - .t0)) )) y <- vector(mode = "numeric", length = length(t)) for(nk in seq_along(t0)) { # From the reduced equation t1 <- t - floor((t - t0[nk]) / pc) * pc t2 <- t - floor((t - duty * pc - t0[nk]) / pc) * pc C1 <- A * exp(-lambda * (t1)) C2 <- A * exp(-lambda * (t2)) f1 <- C1 * (A1[nk] * sin(2 * pi * f * (t1)) + B1[nk] * cos(2 * pi * f * (t1))) f2 <- C2 * (A2[nk] * sin(2 * pi * f * (t2)) + B2[nk] * cos(2 * pi * f * (t2))) y <- y + f1 - f2 } return(y) } # Assuming that t0 depicts the only the moments before the first bit sent. SimulationNormal <- function(t, pc, duty, t0, Np, FunInter) { t1 <- purrr::map(t0, function(.t1) .t1 + seq(-2 * Np - 1, ceiling(max(t) / pc)) * pc) t1 <- sort(purr::flatten_dbl(t1)) KierWsp <- c("+" = 1, "-" = -1) KierV <- c("+" = 0, "-" = 1) y <- vector(mode = "numeric", length = length(t)) for(.t1 in t1) { for(kier in c("+", "-")) { y <- y + KierWsp[kier] * FunInter(t - .t1 - KierV[kier] * duty * pc) } } return(y) } #' Instead of looping through switching instants, loop is over t #' Since I want to check randomly chosen t for the error checking purpose. SimulationNormalTimes <- function(t, pc, duty, t0, Np, FunInter) { # max(t) may induce a lot of switching instants t1 <- purrr::map(t0, function(.t1) .t1 + seq(-2 * Np - 1, ceiling(max(t) / pc)) * pc) t1 <-sort(flatten_dbl(t1)) KierWsp <- c("+" = 1, "-" = -1) KierV <- c("+" = 0, "-" = 1) y <- vector(mode = "numeric", length = length(t)) i <- 1 for(.t in t) { # print(i) for(kier in c("+", "-")) { y[i] <- y[i] + sum(KierWsp[kier] * FunInter(.t - t1 - KierV[kier] * duty * pc)) } i <- i + 1 } return(y) } #' Simulation to check, if error comes from N_p selected values or from the equations. #' #' SimulationNormalTimesNp <- function(t, pc, duty, t0, Np, FunInter) { # max(t) may induce a lot of switching instants # NN <- length(t0) t1 <- purrr::map(t0, function(.t1) .t1 + seq(-2 * Np - 1, ceiling(max(t) / pc)) * pc) t1 <- sort(flatten_dbl(t1)) KierWsp <- c("+" = 1, "-" = -1) KierV <- c("+" = 0, "-" = 1) y <- vector(mode = "numeric", length = length(t)) i <- 1 for(.t in t) { for(kier in c("+", "-")) { # Take only the last Np values. .t1 <- t1[ (t1 + KierV[kier] * duty * pc) < .t & (t1 + KierV[kier] * duty * pc) >= .t - Np * pc] + KierV[kier] * duty * pc # cat("Length .t1:", length(.t1), "- NN * Np:", NN * Np, "\n") y[i] <- y[i] + sum(KierWsp[kier] * FunInter(.t - .t1)) } i <- i + 1 } return(y) } NpFun <- function(tol, A, lambda, pc) { ceiling( (log(A) - log(tol)) / (lambda * pc) ) } sqwave <- function(t, t_on, duty, pc) { yy <- rep(c(1, 0), times = length(t_on)) tt <- sort(c(t_on, t_on + duty * pc)) f1 <- approxfun(tt, yy, method = "constant") return(f1(t)) }