clc; clear all; close all;

%% Figure light theme

set(0, "defaultAxesColorOrder", [
        1.0000 0.0000 0.0000;  # red
        0.0000 0.0000 1.0000;  # blue
        1.0000 0.4980 0.0549;  # orange
        0.0000 0.7490 1.0000;  # cyan
        0.1725 0.6275 0.1725;  # green
        0.5451 0.7020 0.0000;  # olive
        0.0471 0.4000 0.1373;  # deep green
        0.4000 0.7608 0.5020;  # mint
        0.6350 0.0780 0.1840;  # dark red
        0.0000 0.5882 0.5333;  # teal
        0.8510 0.6471 0.1255;  # gold
        0.5490 0.3373 0.2941;  # brown
        0.8902 0.4667 0.7608;  # pink
        0.5804 0.4039 0.7412;  # purple
        0.4980 0.4980 0.4980;  # gray
]);

set(0, "DefaultFigureColor", [1 1 1]);
set(0, "DefaultAxesColor", [1 1 1]);
set(0, "DefaultAxesXColor", [0 0 0]);
set(0, "DefaultAxesYColor", [0 0 0]);
set(0, "DefaultTextColor", [0 0 0]);

set(0, "DefaultAxesGridColor", [0 0 0]);
set(0, 'DefaultAxesGridAlpha', 1.00);

set(0, "DefaultAxesMinorGridColor", [0 0 0]);
set(0, 'DefaultAxesMinorGridAlpha', 0.20);

set(0, "DefaultLineLinewidth", 3.00);
set(0, "DefaultAxesFontSize", 16);
set(0, "DefaultTextFontSize", 16);
set(0, "DefaultAxesLineWidth", 1.00);

set(0, "DefaultAxesXGrid", "on");
set(0, "DefaultAxesYGrid", "on");
set(0, "DefaultAxesZGrid", "on");
set(0, "DefaultAxesXMinorGrid", "on");
set(0, "DefaultAxesYMinorGrid", "on");
set(0, "DefaultAxesXMinorTick", "on");
set(0, "DefaultAxesYMinorTick", "on");

set(0, 'DefaultAxesGridAlpha', 0.50);
set(0, "defaultAxesFontName", "Nimbus Sans");

function dt = get_time_interval(filename)
    fid = fopen(filename, "r");

    % Read line 9
    for i = 1:9
        line = fgetl(fid);
    endfor

    fclose(fid);

    % Extract the value after the comma
    value = strtrim(strsplit(line, ","){2});

    % Convert the value to seconds
    dt = sscanf(value, "%f");

    if strfind(value, "uS")
        dt *= 1e-6;
    elseif strfind(value, "mS")
        dt *= 1e-3;
    elseif strfind(value, "nS")
        dt *= 1e-9;
    elseif strfind(value, "S")
        % already in seconds
    endif
endfunction

args = argv();

if isempty(args)
    error("Usage: %s <filename>", program_name());
endif

filename = args{1};
basename = tolower(strrep(filename, ".CSV", ""));
png_savename = [basename, ".png"]
fft_savename = [basename, "_fft.png"]

data = dlmread(filename, ",", 10, 0);

figure("visible", "off");
grid on;

set(gcf, "paperunits", "inches");
set(gcf, "papersize", [10 4]);
set(gcf, "paperposition", [0 0 10 4]);

index = data(:, 1);
voltage = data(:, 2);

dt = get_time_interval(filename)
% dt = 0.80e-6;

n = length(voltage)
trigger_offset = 50;
time = ((0:n-1) - (n/2 - trigger_offset)) * dt;

t_max = ((n-1)/2*dt)*1e6
v_max = max(voltage)

plot(time*1e3, voltage);

xlim([-(t_max-trigger_offset)/1.05/1e3 (t_max-trigger_offset)/1.05/1e3])
ylim([-v_max*1.10 v_max*1.10])

xlabel("TIEMPO  [ms]");
ylabel("VOLTAJE  [V]");
grid on;

print(png_savename, "-dpng", "-r600");
close

% FFT
figure("visible", "off");
grid on;

set(gcf, "paperunits", "inches");
set(gcf, "papersize", [10 4]);
set(gcf, "paperposition", [0 0 10 4]);

Y = fft(voltage);

% Two-sided magnitude spectrum
P2 = abs(Y / n);

% Single-sided spectrum
P1 = P2(1:floor(n/2)+1);
P1(2:end-1) = 2 * P1(2:end-1);

% Frequency axis
f = (0:floor(n/2)) / (n * dt);

% Plot
plot(f / 1000, P1, "linewidth", 1.5);

xlabel("FRECUENCIA [kHz]");
ylabel("MAGNITUDE  [V]");
grid on;

xlim([0 200])

print(fft_savename, "-dpng", "-r600");
