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.1725 0.6275 0.1725;  # green
        0.5804 0.4039 0.7412;  # purple
        0.5490 0.3373 0.2941;  # brown
        0.8902 0.4667 0.7608;  # pink
        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);

data = csvread("bode_rc_highpass_600_pts.csv", 1, 0);
freq = data(:, 1);
mag  = data(:, 2);

mag_db = 20 * log10(mag / 1.0);
mag_db_smooth = movmean(mag_db, 20);

fig = figure("visible", "off");
% semilogx(freq, mag_db, "LineWidth", 4);
% hold on;
yline(-3, ":k", "linewidth", 1.5, "color", [0.5 0.5 0.5]);
hold on;
semilogx(freq, mag_db_smooth, "LineWidth", 2.5);

% text(30000, -6.5, "-3 dB", ...
%     "fontsize", 10, ...
%     "fontname", "Helvetica", ...
%     "fontweight", "bold", ...
%     "verticalalignment", "bottom", ...
%     "horizontalalignment", "left");

grid on;
set(gcf, "paperunits", "inches");
set(gcf, "papersize", [6 4]);
set(gcf, "paperposition", [0 0 6 4]);

% hold(ax(1), "on");
% hold(ax(2), "on");

% Theoretical curves
% semilogx(ax(1), freq, mag_the, ".", "LineWidth", 2);
% semilogx(ax(2), freq, pha_the, ".", "LineWidth", 2);

% Log frequency axis
% set(ax(1), "XScale", "log");
% set(ax(2), "XScale", "log");

% Line widths
% set(hmag, "LineWidth", 3);
% set(hphase, "LineWidth", 3);

% Limits
xlim([1 1e5]);
ylim([-30 0.1])
yticks([-30 -20 -10 0]);

% grid(ax(1), "on");
% grid(ax(2), "on");

% Labels
xlabel("FRECUENCIA [rad/s]");
ylabel(gca, "MAGNITUD [dB]");

% legend([bode, line], ...
%        "Magnitud", "-3dB");

print("bode_rc_highpass_600_pts.png", "-dpng", "-r500");
close(fig)
