Lab moderna: add Franck-Hertz experiment

Change-Id: I8bcd498618d60d2ddbcdade6d789b8a310b6c0cd
diff --git a/quad12/labmoderna/franckhertz/r/main.r b/quad12/labmoderna/franckhertz/r/main.r
new file mode 100644
index 0000000..0af5e69
--- /dev/null
+++ b/quad12/labmoderna/franckhertz/r/main.r
@@ -0,0 +1,117 @@
+library(ggplot2)
+library(svglite)
+library(zoo)
+library(dplyr)
+
+inflect <- function(x, threshold = 1){
+  up   <- sapply(1:threshold, function(n) c(x[-(seq(n))], rep(NA, n)))
+  down <-  sapply(-1:-threshold, function(n) c(rep(NA,abs(n)), x[-seq(length(x), length(x) - abs(n) + 1)]))
+  a    <- cbind(x,up,down)
+  list(minima = which(apply(a, 1, min) == a[,1]), maxima = which(apply(a, 1, max) == a[,1]))
+}
+
+drawMinimums <- TRUE
+drawSmoothedCurve <- FALSE
+if (drawMinimums) {
+  filenames <- c('160K_uh8.5_2', '170K_uh8.5', '180K_uh8.5', '190K_uh8.5', '190K_uh7.5', '200K_uh8.5')
+} else {
+  filenames <- c('160K_uh7.5', '160K_uh8.5_3', '170K_uh7.5', '180K_uh7.5', '190K_uh7.5', '200K_uh8.5', '160K_uh8.5_2', '160K_uh8.5', '170K_uh8.5', '180K_uh8.5', '190K_uh8.5')
+}
+dependanceOnTFilenames <- c('160K_uh8.5_2', '170K_uh8.5', '180K_uh8.5',
+                            #'190K_uh8.5',
+                            '190K_uh7.5', '200K_uh8.5')
+
+for (filename in filenames) {
+  temp <- gsub("(\\d+)K_.*", "\\1", filename)
+  uh <- gsub(".*_uh([^_]+).*", "\\1", filename)
+  table <- read.table(paste0('dades/', filename, '.txt'), sep="\t", dec=',', header=TRUE)
+  table$Smooth_IA <- rollmean(table$IA, k = 60, align = "center", fill = NA)
+  bottoms <- inflect(table$Smooth_IA, threshold = 2)$minima
+  #print(bquote("T =" ~ .(temp) ~ "K;" ~ "U"[h]~"=" ~ .(uh) ~ "V"))
+  #print(table$U1[bottoms])
+  pplot <- ggplot(table, aes(x = U1, y = Smooth_IA))
+  if (drawMinimums) {
+    min_vals <- read.table(paste0('dades/', filename, '_minims.txt'), sep="\t", dec=",", header=TRUE)
+    if (drawSmoothedCurve) {
+      pplot <- pplot +
+        geom_line(aes(x = U1, y = IA), colour = "#FCC8C5") +
+        geom_line(colour = "#F8766D") +
+        geom_vline(data = min_vals, aes(xintercept = U1), linetype = "dashed", color="#FAA49E")
+    } else {
+      pplot <- pplot +
+        geom_line(aes(x = U1, y = IA), colour = "#F8766D") +
+        geom_vline(data = min_vals, aes(xintercept = U1), linetype = "dashed", color="#FAA49E")
+    }
+  } else {
+    pplot <- pplot +
+      geom_line(aes(x = U1, y = IA), colour = "#F8766D")
+  }
+  pplot <- pplot +
+    xlab(bquote(U["1"] ~ "(V)")) +
+    ylab(bquote(I[A] ~ "(nA)")) +
+    ggtitle(bquote("T =" ~ .(temp) ~ "ºC;" ~ "U"[h]~"=" ~ .(uh) ~ "V")) +
+    theme(legend.position = "none", text = element_text(family = "Montserrat"))
+  outfilenamesuffix <- ""
+  if (drawMinimums) {
+    outfilenamesuffix <- "_minims"
+  }
+  ggsave(paste0('output/', filename, outfilenamesuffix, '.svg'), pplot, width = 6.6, height = 3.75, bg = "transparent")
+}
+
+abs_data <- NA
+for (filename in dependanceOnTFilenames) {
+  temp <- gsub("(\\d+)K_.*", "\\1", filename)
+  min_vals <- read.table(paste0('dades/', filename, '_minims.txt'), sep="\t", dec=",", header=TRUE)
+  ur_diffs <- diff(min_vals$U1)
+  ns <- min_vals$n[-1]
+  ur_diffs_data <- data.frame(n = ns, UR_diff = ur_diffs)
+  ur_diffs_data$T <- temp
+  if (!is.data.frame(abs_data)) {
+    abs_data <- ur_diffs_data
+  } else {
+    abs_data <- rbind(abs_data, ur_diffs_data)
+  }
+  print(paste0('T=', temp))
+  print(paste0('Mean: ', mean(ur_diffs)))
+  print(paste0('Unc. mean: ', sd(ur_diffs)/sqrt(length(ur_diffs))))
+}
+
+urplot <- ggplot(data = abs_data, aes(x = n, y = UR_diff, color = T)) +
+  geom_line() +
+  scale_x_continuous(breaks = function(x) unique(floor(pretty(3:11)))) +
+  xlab("n") +
+  ylab(bquote(U[R] ~ "(V)")) +
+  ylim(4, 5.4) +
+  labs(color = "T (ºC)") +
+  theme(text = element_text(family = "Montserrat"))
+ggsave('output/ur_for_different_ts.svg', urplot, width = 6.6, height = 3.75, bg = "transparent")
+
+boxp1plot <- ggplot(data = abs_data, aes(x = T, y = UR_diff, color = T)) +
+  geom_boxplot() +
+  xlab("T (ºC)") +
+  ylab(bquote(U[R] ~ "(V)")) +
+  #ylim(4, 5.4) +
+  theme(legend.position = "none", text = element_text(family = "Montserrat"))
+ggsave('output/mean_ur_vs_t_boxplot.svg', boxp1plot, width = 6.6, height = 3.75, bg = "transparent")
+
+abs_data.summary <- abs_data %>%
+  group_by(T) %>%
+  summarise(
+    sd = sd(UR_diff, na.rm = TRUE)/sqrt(n()),
+    UR_diff = mean(UR_diff)
+  )
+boxp2plot <- ggplot(data = abs_data, aes(x = T, y = UR_diff, color = T)) +
+  geom_jitter(
+    position = position_jitter(0.2), color = "darkgray"
+  ) +
+  geom_point(size = 2.5, data = abs_data.summary) +
+  geom_errorbar(
+    aes(ymin = UR_diff - sd, ymax = UR_diff + sd),
+    width = 0.15,
+    data = abs_data.summary
+  ) +
+  xlab("T (ºC)") +
+  ylab(bquote(U[R] ~ "(V)")) +
+  #ylim(4, 5.4) +
+  theme(legend.position = "none", text = element_text(family = "Montserrat"))
+ggsave('output/mean_ur_vs_t_boxplot2.svg', boxp2plot, width = 6.6, height = 3.75, bg = "transparent")