diff options
| author | Warrick Lo <warrick.s.z.lo@gmail.com> | 2026-02-11 07:16:45 -0800 |
|---|---|---|
| committer | Warrick Lo <warrick.s.z.lo@gmail.com> | 2026-02-11 07:16:45 -0800 |
| commit | f1a9a85c6138715c3933c957c695d0482b901154 (patch) | |
| tree | f2d6ca37aa60eb35737f31a4a8ab3a202e07f01c /matlab/specan.m | |
Finish lab report up to Task 2B
Signed-off-by: Warrick Lo <warrick.s.z.lo@gmail.com>
Diffstat (limited to 'matlab/specan.m')
| -rw-r--r-- | matlab/specan.m | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/matlab/specan.m b/matlab/specan.m new file mode 100644 index 0000000..751dba9 --- /dev/null +++ b/matlab/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
\ No newline at end of file |