-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource_fun.R
79 lines (68 loc) · 2.5 KB
/
source_fun.R
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
# title: "source_fun"
# author: "Tobias Kuerschner"
# date: "30 June 2021"
###############################################################################################################################################
# Function 1
#
# "PhaseDiff" - This function takes two timeseries (TSerA, TSerB) and the time (timeObj) and calculates the phasedifference of those two series
#
###############################################################################################################################################
PhaseDiff <- function(TSerA, TSerB, timeObj)
{
library(psd) #required for spectral analyis
t <- timeObj #time
y1 <- TSerA #timeseries A
y2 <- TSerB #timeseries B
if (base::sum(y1) == 0 ||
(base::sum(y2) == 0))
#catching failed runs
{
return(NA) #throwing na for failed runs
} else{
# spectral analysis
out1 <- psd::pspectrum(y1)
out2 <- psd::pspectrum(y2)
# frequency with the highest peak in the spectrum
f1 <- out1$freq[base::which.max(out1$spec)]
f2 <- out2$freq[base::which.max(out2$spec)]
if (f1 >= f2)
{
f <- f1
} else{
f <- f2
}
# fitting procedure:
fit1 <-
base::lm(y1 ~ base::sin(2 * pi * f * t) + base::cos(2 * pi * f * t))
fit2 <-
base::lm(y2 ~ base::sin(2 * pi * f * t) + base::cos(2 * pi * f * t))
#calculation of phase of y1:
a1 <- fit1$coefficients[2]
b1 <- fit1$coefficients[3]
ph1 <- base::atan(b1 / a1)
#calculation of phase of y2:
a2 <- fit2$coefficients[2]
b2 <- fit2$coefficients[3]
ph2 <- base::atan(b2 / a2)
phase_difference <- base::as.numeric((ph2 - ph1) / pi)
# result:
phase_difference
return(phase_difference)
}
}
#########################################
# End function 1 #
#########################################
###############################################################################################################################################
# Function 2
#
# "MassDecompTrend" - helper function to return only the "trend" part of a decomposed time series
#
###############################################################################################################################################
MassDecompTrend<-function(ts){
temp1<-stats::decompose(ts)
return(temp1$trend)
}
#########################################
# End function 2 #
#########################################