TB066

Análisis de Circuitos (TB066)
Index Commits Files Refs
   1 clc; clear all; close all;
   2 
   3 %% Figure light theme
   4 
   5 set(0, "defaultAxesColorOrder", [
   6         1.0000 0.0000 0.0000;  # red
   7         0.0000 0.0000 1.0000;  # blue
   8         1.0000 0.4980 0.0549;  # orange
   9         0.0000 0.7490 1.0000;  # 9. Sky Blue / Cyan (New)
  10         0.1725 0.6275 0.1725;  # green
  11         0.5451 0.7020 0.0000;  # 15. Olive / Acid Green (New - Warm, yellow-green with great contrast)
  12         0.0471 0.4000 0.1373;  # 13. Forest Green (New - Deep, dark green)
  13         0.4000 0.7608 0.5020;  # 14. Mint / Sage Green (New - Lighter, fresh green)
  14         0.6350 0.0780 0.1840;  # 12. Burgundy / Dark Red (New)
  15         0.0000 0.5882 0.5333;  # 10. Teal (New)
  16         0.8510 0.6471 0.1255;  # 11. Gold / Mustard (New - High-contrast alternative to yellow)
  17         0.5490 0.3373 0.2941;  # brown
  18         0.8902 0.4667 0.7608;  # pink
  19         0.5804 0.4039 0.7412;  # purple
  20         0.4980 0.4980 0.4980;  # gray
  21 ]);
  22 
  23 set(0, "DefaultFigureColor", [1 1 1]);
  24 set(0, "DefaultAxesColor", [1 1 1]);
  25 set(0, "DefaultAxesXColor", [0 0 0]);
  26 set(0, "DefaultAxesYColor", [0 0 0]);
  27 set(0, "DefaultTextColor", [0 0 0]);
  28 
  29 set(0, "DefaultAxesGridColor", [0 0 0]);
  30 set(0, 'DefaultAxesGridAlpha', 1.00);
  31 
  32 set(0, "DefaultAxesMinorGridColor", [0 0 0]);
  33 set(0, 'DefaultAxesMinorGridAlpha', 0.20);
  34 
  35 set(0, "DefaultLineLinewidth", 3.00);
  36 set(0, "DefaultAxesFontSize", 16);
  37 set(0, "DefaultTextFontSize", 16);
  38 set(0, "DefaultAxesLineWidth", 1.00);
  39 
  40 set(0, "DefaultAxesXGrid", "on");
  41 set(0, "DefaultAxesYGrid", "on");
  42 set(0, "DefaultAxesZGrid", "on");
  43 set(0, "DefaultAxesXMinorGrid", "on");
  44 set(0, "DefaultAxesYMinorGrid", "on");
  45 set(0, "DefaultAxesXMinorTick", "on");
  46 set(0, "DefaultAxesYMinorTick", "on");
  47 
  48 set(0, 'DefaultAxesGridAlpha', 0.50);
  49 set(0, "defaultAxesFontName", "Nimbus Sans");
  50 
  51 % exported osc data
  52 filename = "bode.csv";
  53 
  54 fid = fopen(filename, "r");
  55 
  56 % Skip header
  57 fgetl(fid);
  58 
  59 % Read frequency and complex string
  60 data = csvread(filename, 1, 0);
  61 
  62 freq    = data(:, 1);
  63 mag_osc = data(:, 2);
  64 
  65 mag_osc = squeeze(mag_osc);
  66 mag_osc = 20*log10(mag_osc);
  67 mag_osc = movmean(mag_osc, 3);
  68 
  69 % for k = 1:N
  70 %     tokens = regexp(response{k}, ...
  71 %         '\(([-+0-9.eE]+)dB,([-+0-9.eE]+)°\)', ...
  72 %         'tokens');
  73 
  74 %     mag_model(k)   = str2double(tokens{1}{1});
  75 % end
  76 
  77 figure("visible", "off");
  78 grid on;
  79 set(gcf, "paperunits", "inches");
  80 set(gcf, "papersize", [10 4]);
  81 set(gcf, "paperposition", [0 0 10 4]);
  82 % theorical transfer function
  83 
  84 H1 = tf([1 250], [1 500])
  85 H2 = tf([4000^2], [1 4000^2/9000 4000^2])
  86 H = 12*H1*H2
  87 [mag_the, pha_the, freq] = bode(H, freq);
  88 
  89 % bode functions returns linear scale y-axis
  90 mag_the = squeeze(mag_the);
  91 pha_the = squeeze(pha_the);
  92 mag_the = 20*log10(mag_the);
  93 
  94 
  95 % theorical normalized transfer function
  96 
  97 H1_norm = tf([1 252.52], [1 505.05])
  98 H2_norm = tf([4014.80^2], [1 4014.80/2.264 4014.80^2])
  99 H_norm = 12*H1_norm*H2_norm
 100 [mag_the_norm, pha_the_norm, freq] = bode(H_norm, freq);
 101 
 102 % bode functions returns linear scale y-axis
 103 mag_the_norm = squeeze(mag_the_norm);
 104 pha_the_norm = squeeze(pha_the_norm);
 105 mag_the_norm = 20*log10(mag_the_norm);
 106 
 107 %% Magnitude
 108 % ax1 = figure()
 109 hold on;
 110 y1 = semilogx(freq, mag_the, "LineWidth", 3.75);
 111 y2 = semilogx(freq, mag_the_norm, "LineWidth", 3);
 112 y3 = semilogx(freq*2*pi, mag_osc, "LineWidth", 2.25);
 113 ylim([-35 35])
 114 yticks([-20 0 20]);
 115 xlim([1 1e5])
 116 xlabel("FRECUENCIA [rad/s]");
 117 ylabel("MAGNITUD [dB]");
 118 % title("Bode Magnitude");
 119 grid on;
 120 
 121 % Remove x tick labels from upper plot
 122 % set(ax1, "XTickLabel", []);
 123 
 124 % %% Phase
 125 % ax2 = subplot(2, 1, 2)
 126 % hold on;
 127 % semilogx(freq, pha_the, "LineWidth", 3.75);
 128 % semilogx(freq, pha_the_norm, "LineWidth", 3);
 129 % semilogx(freq*2*pi, phase, "LineWidth", 2.25);
 130 % semilogx(freq*2*pi, phase_model, "LineWidth", 1.5);
 131 % 
 132 % grid on;
 133 % ylim([-225 45])
 134 % yticks([-180 -90 0]);
 135 % xlim([1 1e5])
 136 % xlabel("FRECUENCIA  [rad/s]");
 137 % ylabel("FASE  [grados]");
 138 % title("Bode Phase");
 139 % legend("Normalizado", "Teórica", "Location", "northeast");
 140 
 141 % Invisible axes covering the whole figure
 142 axL = axes("Position", [0 0 1 1], ...
 143            "Visible", "off", ...
 144            "Units", "normalized");
 145 
 146 % Create legend on top axes
 147 lgd = legend([y1 y2 y3], ...
 148                 {"H", "H normalizada", "Placa"}, ...
 149                 "Location", "southwest");
 150 
 151 % Move legend
 152 set(lgd, "position", [0.16 0.23 0.10 0.05]);
 153 % set(lgd, "numcolumns", 1);
 154 
 155 % Reduce vertical spacing
 156 % set(ax1, "Position", [0.13 0.58 0.80 0.38]);
 157 
 158 print("bode.png", "-dpng", "-r500");
 159 
 160 freq_osc_rad = freq*2*pi;
 161 mag_osc_interp = interp1(freq_osc_rad, mag_osc, freq);
 162 
 163 data = [freq(:), mag_the(:), mag_osc_interp(:)];
 164 
 165 fid = fopen("bode_data_exported.csv", "w");
 166 fprintf(fid, "freq_rad_s,mag_the,mag_the_norm,mag_osc\n");
 167 fclose(fid);
 168 
 169 dlmwrite("bode_data_exported.csv", data, "-append", ...
 170     "delimiter", ",", "precision", "%2.1f");