SimulationsSwitchingsFun.R
3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#' 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))
}