aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWarrick Lo <warrick.s.z.lo@gmail.com>2026-02-16 03:08:14 -0800
committerWarrick Lo <warrick.s.z.lo@gmail.com>2026-02-16 03:08:14 -0800
commit0aeeca161c38124b99c0c75fd16351642705ce6b (patch)
tree441721fa768f9b75fe2559d0a4e203c514c76904
Add MATLAB code for processing dataHEADmaster
Signed-off-by: Warrick Lo <warrick.s.z.lo@gmail.com>
-rw-r--r--LICENSE14
-rw-r--r--phasor.m15
-rwxr-xr-xprep.sh26
-rw-r--r--specan.m53
4 files changed, 108 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..cc41c7a
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,14 @@
+BSD Zero Clause License
+
+Copyright (c) 2026 Warrick Lo
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
diff --git a/phasor.m b/phasor.m
new file mode 100644
index 0000000..a75cc0e
--- /dev/null
+++ b/phasor.m
@@ -0,0 +1,15 @@
+function phasor(modulus, argument)
+ % Wrapper for quiver() that plots phasor diagrams.
+ %
+ % Expects the modulus and argument (in degrees)
+ % of a complex number.
+
+ r = modulus;
+ phi = argument * pi / 180;
+
+ % Automatic scaling is disabled.
+ quiver(0, 0, r*cos(phi), r*sin(phi), 0);
+
+ xlim([-r r]);
+ ylim([-r r]);
+end
diff --git a/prep.sh b/prep.sh
new file mode 100755
index 0000000..2dbc67e
--- /dev/null
+++ b/prep.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+# Prepare all waveform data for MATLAB processing.
+#
+# This script will:
+# 1. sanitise the header before running MATLAB's readtable();
+# 2. convert line endings from DOS to UNIX; and
+# 3. rename files to have *.tsv file extensions.
+#
+# This script assumes all waveform data is tab-delimited and
+# stored in *.txt files.
+#
+# Usage: ./prep.sh
+
+files=$(ls -- *.txt)
+
+for i in $files; do
+ printf "Preparing %s\n" "$i"
+
+ # Replace the header and convert DOS line endings (CRLF)
+ # to UNIX line endings (LF).
+ sed -i -e "1c time\tv1\tv2\tv3\ti1\ti2\ti3" -e "s/\x1D$//" "$i"
+
+ # Rename files to have TSV extensions.
+ mv "$i" "${i%.*}.tsv"
+done
diff --git a/specan.m b/specan.m
new file mode 100644
index 0000000..d1d52ad
--- /dev/null
+++ b/specan.m
@@ -0,0 +1,53 @@
+function specan(filename)
+ % Spectrum analyser
+ %
+ % Analyse and plot the frequency spectrum of a waveform
+ % given by a tab-separated value (TSV) file.
+ %
+ % The following column headers for the TSV are assumed:
+ % time, v1, v2, v3, i1, i2, i3
+ %
+ % Time measurements are assumed to be in milliseconds.
+ % All voltages and currents are assumed to be given
+ % in Volts and Amperes, respectively.
+
+ data = readtable(filename, FileType="text", Delimiter="\t");
+
+ L = length(data.time);
+ % Assumes data is sampled at regular intervals.
+ T = 1e-3*(data.time(end)-data.time(1))/(L-1);
+ fs = 1/T;
+ % Frequency axis for plotting.
+ f = fs/L*(0:L-1);
+
+ Y1 = abs(fft(data.v1)/L);
+ Y2 = abs(fft(data.v2)/L);
+ Y3 = abs(fft(data.v3)/L);
+ Y4 = abs(fft(data.i1)/L);
+ Y5 = abs(fft(data.i2)/L);
+ Y6 = abs(fft(data.i3)/L);
+
+ figure(1);
+
+ subplot(2, 1, 1);
+ hold on;
+ plot(f, Y1, "r", LineWidth=2);
+ plot(f, Y2, "g", LineWidth=2);
+ plot(f, Y3, "b", LineWidth=2);
+ title("Frequency Spectrum of Voltage Measurements");
+ xlim([0 1e3]);
+ xlabel("Frequency, $f$ (Hz)", Interpreter="latex");
+ ylabel("$\left|\mathcal{F}(V)\right|$ (V)", Interpreter="latex");
+ legend("V_1", "V_2", "V_3");
+
+ subplot(2, 1, 2);
+ hold on;
+ plot(f, Y4, "r", LineWidth=2);
+ plot(f, Y5, "g", LineWidth=2);
+ plot(f, Y6, "b", LineWidth=2);
+ title("Frequency Spectrum of Current Measurements");
+ xlim([0 1e3]);
+ xlabel("Frequency, $f$ (Hz)", Interpreter="latex");
+ ylabel("$\left|\mathcal{F}(I)\right|$ (A)", Interpreter="latex");
+ legend("I_1", "I_2", "I_3");
+end