diff options
| author | Warrick Lo <warrick.s.z.lo@gmail.com> | 2026-02-20 15:22:50 -0800 |
|---|---|---|
| committer | Warrick Lo <warrick.s.z.lo@gmail.com> | 2026-02-20 15:22:50 -0800 |
| commit | f7fcb400fc0964adcb3d9d0c0670373b58604246 (patch) | |
| tree | 0240ffab50a54646028ae5819b45ad03c3766ddc | |
| parent | Prepare waveform data (diff) | |
Add power analyser code
Signed-off-by: Warrick Lo <warrick.s.z.lo@gmail.com>
| -rw-r--r-- | matlab/poweran.m | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/matlab/poweran.m b/matlab/poweran.m new file mode 100644 index 0000000..e5f00df --- /dev/null +++ b/matlab/poweran.m @@ -0,0 +1,57 @@ +function poweran(filename, pf_angle) + % Power analyser + % + % Analyse and plot the instantaneous, real, and reactive + % power of a waveform given by a tab-separated value + % (TSV) file. + % + % Required measurements to input: + % power factor angle (degree) + % + % 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); + + t = linspace(0, data.time(end), L); + p = data.v1(1:L) .* data.i1(1:L); + + % Sum over a full period to get the RMS values. + n = length(data.time(data.time < 1e3/60)); + vrms = sqrt(sum(data.v1(data.time < 1e3/60).^2)/n); + irms = sqrt(sum(data.i1(data.time < 1e3/60).^2)/n); + + % These are constant in time so we extrapolate with two points + % to save compute time. + real_power = ones(2) * vrms * irms * cos(deg2rad(pf_angle)); + reactive_power = ones(2) * vrms * irms * sin(deg2rad(pf_angle)); + + display(vrms); + display(irms); + + figure(1); + + subplot(3, 1, 1); + plot(t, p, "r", LineWidth=2); + title("Instantaneous Power versus Time"); + xlabel("Time, $t$ (ms)", Interpreter="latex"); + ylabel("Power, $p(t)$ (W)", Interpreter="latex"); + + subplot(3, 1, 2); + plot([0 data.time(end)], real_power, "g", LineWidth=2); + title("Real Power versus Time"); + xlabel("Time, $t$ (ms)", Interpreter="latex"); + ylabel("Real Power, $P$ (W)", Interpreter="latex"); + + subplot(3, 1, 3); + plot([0 data.time(end)], reactive_power, "b", LineWidth=2); + title("Reactive Power versus Time"); + xlabel("Time, $t$ (ms)", Interpreter="latex"); + ylabel("Reactive Power, $Q$ (VAR)", Interpreter="latex"); +end
\ No newline at end of file |