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 
  50 
  51 set(0, "defaultAxesFontName", "Nimbus Sans");
  52 
  53 % exported ltspice data
  54 filename = "trans_comp_valores2_utf8.txt";
  55 
  56 fid = fopen(filename, "r");
  57 
  58 % Skip header
  59 fgetl(fid);
  60 
  61 % Read frequency and complex string
  62 data = textscan(fid, "%f %s", "Delimiter", "\t");
  63 
  64 fclose(fid);
  65 
  66 freq = data{1};
  67 response = data{2};
  68 
  69 N = numel(freq);
  70 
  71 mag = zeros(N,1);
  72 phase = zeros(N,1);
  73 
  74 for k = 1:N
  75     tokens = regexp(response{k}, ...
  76         '\(([-+0-9.eE]+)dB,([-+0-9.eE]+)°\)', ...
  77         'tokens');
  78 
  79     mag(k)   = str2double(tokens{1}{1});
  80     phase(k) = str2double(tokens{1}{2});
  81 end
  82 
  83 % exported ltspice data
  84 filename = "trans_comp_model_utf8.txt";
  85 
  86 fid = fopen(filename, "r");
  87 
  88 % Skip header
  89 fgetl(fid);
  90 
  91 % Read frequency and complex string
  92 data = textscan(fid, "%f %s", "Delimiter", "\t");
  93 
  94 fclose(fid);
  95 
  96 freq = data{1};
  97 response = data{2};
  98 
  99 N = numel(freq);
 100 
 101 mag_model = zeros(N,1);
 102 phase_model = zeros(N,1);
 103 
 104 for k = 1:N
 105     tokens = regexp(response{k}, ...
 106         '\(([-+0-9.eE]+)dB,([-+0-9.eE]+)°\)', ...
 107         'tokens');
 108 
 109     mag_model(k)   = str2double(tokens{1}{1});
 110     phase_model(k) = str2double(tokens{1}{2});
 111 end
 112 
 113 figure(1);
 114 grid on;
 115 set(gcf, "paperunits", "inches");
 116 set(gcf, "papersize", [10 5]);
 117 set(gcf, "paperposition", [0 0 10 5]);
 118 % theorical transfer function
 119 
 120 H1 = tf([1 250], [1 500])
 121 H2 = tf([4000^2], [1 4000^2/9000 4000^2])
 122 H = 12*H1*H2
 123 [mag_the, pha_the, freq] = bode(H, freq);
 124 
 125 % bode functions returns linear scale y-axis
 126 mag_the = squeeze(mag_the);
 127 pha_the = squeeze(pha_the);
 128 mag_the = 20*log10(mag_the);
 129 
 130 
 131 % theorical normalized transfer function
 132 
 133 H1_norm = tf([1 252.52], [1 505.05])
 134 H2_norm = tf([4014.80^2], [1 4014.80/2.264 4014.80^2])
 135 H_norm = 12*H1_norm*H2_norm
 136 [mag_the_norm, pha_the_norm, freq] = bode(H_norm, freq);
 137 
 138 % bode functions returns linear scale y-axis
 139 mag_the_norm = squeeze(mag_the_norm);
 140 pha_the_norm = squeeze(pha_the_norm);
 141 mag_the_norm = 20*log10(mag_the_norm);
 142 
 143 %% Magnitude
 144 ax1 = subplot(2, 1, 1)
 145 hold on;
 146 y1 = semilogx(freq, mag_the, "LineWidth", 3.75);
 147 y2 = semilogx(freq, mag_the_norm, "LineWidth", 3);
 148 y3 = semilogx(freq*2*pi, mag, "LineWidth", 2.25);
 149 y4 = semilogx(freq*2*pi, mag_model, "LineWidth", 1.5);
 150 ylim([-35 35])
 151 yticks([-20 0 20]);
 152 xlim([1 1e5])
 153 % xlabel("Frequency [Hz]");
 154 ylabel("MAGNITUD [dB]");
 155 % title("Bode Magnitude");
 156 grid on;
 157 
 158 % Remove x tick labels from upper plot
 159 set(ax1, "XTickLabel", []);
 160 
 161 %% Phase
 162 ax2 = subplot(2, 1, 2)
 163 hold on;
 164 semilogx(freq, pha_the, "LineWidth", 3.75);
 165 semilogx(freq, pha_the_norm, "LineWidth", 3);
 166 semilogx(freq*2*pi, phase, "LineWidth", 2.25);
 167 semilogx(freq*2*pi, phase_model, "LineWidth", 1.5);
 168 
 169 grid on;
 170 ylim([-225 45])
 171 yticks([-180 -90 0]);
 172 xlim([1 1e5])
 173 xlabel("FRECUENCIA  [rad/s]");
 174 ylabel("FASE  [grados]");
 175 % title("Bode Phase");
 176 % legend("Normalizado", "Teórica", "Location", "northeast");
 177 
 178 % Invisible axes covering the whole figure
 179 axL = axes("Position", [0 0 1 1], ...
 180            "Visible", "off", ...
 181            "Units", "normalized");
 182 
 183 % Create legend on top axes
 184 lgd = legend(axL, [y1 y2 y3 y4], ...
 185                 {"H", "H normalizada", ...
 186                  "Circuito ideal", ...
 187                  "Circuito con modelo TL081"});
 188 
 189 % Move legend between plots
 190 set(lgd, "position", [0.16 0.54 0.10 0.05]);
 191 set(lgd, "numcolumns", 1);
 192 
 193 % Reduce vertical spacing
 194 set(ax1, "Position", [0.13 0.58 0.80 0.38]);
 195 set(ax2, "Position", [0.13 0.13 0.80 0.38]);
 196 
 197 print("trans_comp_norm_model.png", "-dpng", "-r500");