Add the function detect_open
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 7 May 2024 14:00:30 +0000 (16:00 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 7 May 2024 14:00:30 +0000 (16:00 +0200)
This function returns an array of logicals, where the value is true only
at the index, which corresponds with the time the valve only just opened.
It should work like this:
  t(detect_open(t, p)) = t_when_valve_opened

detect_open.m [new file with mode: 0644]

diff --git a/detect_open.m b/detect_open.m
new file mode 100644 (file)
index 0000000..4c20f01
--- /dev/null
@@ -0,0 +1,17 @@
+function opened = detect_open(t, p, threshold = 0.2, min_dist = 10)
+       dp = (p(2:end) - p(1:end - 1)) ./ (t(2:end) - t(1:end - 1));
+       dp = dp / max(dp);
+       opened = dp > threshold;
+       for i = (length(opened):-1:1)
+               j = i - 1;
+               while (j > 0 && t(j) + min_dist > t(i))
+                       if (opened(j) == 1)
+                               opened(i) = 0;
+                               break;
+                       end
+                       j -= 1;
+               end
+       end
+       opened = [0 opened];
+       opened = logical(opened);
+end