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.1725 0.6275 0.1725;  # green
  10         0.5804 0.4039 0.7412;  # purple
  11         0.5490 0.3373 0.2941;  # brown
  12         0.8902 0.4667 0.7608;  # pink
  13         0.4980 0.4980 0.4980;  # gray
  14 ]);
  15 
  16 set(0, "DefaultFigureColor", [1 1 1]);
  17 set(0, "DefaultAxesColor", [1 1 1]);
  18 set(0, "DefaultAxesXColor", [0 0 0]);
  19 set(0, "DefaultAxesYColor", [0 0 0]);
  20 set(0, "DefaultTextColor", [0 0 0]);
  21 
  22 set(0, "DefaultAxesGridColor", [0 0 0]);
  23 set(0, 'DefaultAxesGridAlpha', 1.00);
  24 
  25 set(0, "DefaultAxesMinorGridColor", [0 0 0]);
  26 set(0, 'DefaultAxesMinorGridAlpha', 0.20);
  27 
  28 set(0, "DefaultLineLinewidth", 3.00);
  29 set(0, "DefaultAxesFontSize", 16);
  30 set(0, "DefaultTextFontSize", 16);
  31 set(0, "DefaultAxesLineWidth", 1.00);
  32 
  33 set(0, "DefaultAxesXGrid", "on");
  34 set(0, "DefaultAxesYGrid", "on");
  35 set(0, "DefaultAxesZGrid", "on");
  36 set(0, "DefaultAxesXMinorGrid", "on");
  37 set(0, "DefaultAxesYMinorGrid", "on");
  38 set(0, "DefaultAxesXMinorTick", "on");
  39 set(0, "DefaultAxesYMinorTick", "on");
  40 
  41 set(0, 'DefaultAxesGridAlpha', 0.50);
  42 
  43 set(0, "defaultAxesFontName", "Nimbus Sans");
  44 
  45 % exported ltspice data
  46 filename = "passive_highpass_normalized_utf8.txt";
  47 
  48 fid = fopen(filename, "r");
  49 
  50 % Skip header
  51 fgetl(fid);
  52 
  53 % Read frequency and complex string
  54 data = textscan(fid, "%f %s", "Delimiter", "\t");
  55 
  56 fclose(fid);
  57 
  58 freq = data{1};
  59 response = data{2};
  60 
  61 N = numel(freq);
  62 
  63 mag = zeros(N,1);
  64 phase = zeros(N,1);
  65 
  66 for k = 1:N
  67     tokens = regexp(response{k}, ...
  68         '\(([-+0-9.eE]+)dB,([-+0-9.eE]+)°\)', ...
  69         'tokens');
  70 
  71     mag(k)   = str2double(tokens{1}{1});
  72     phase(k) = str2double(tokens{1}{2});
  73 end
  74 
  75 figure(1);
  76 grid on;
  77 set(gcf, "paperunits", "inches");
  78 set(gcf, "papersize", [10 5]);
  79 set(gcf, "paperposition", [0 0 10 5]);
  80 
  81 % theorical transfer function
  82 
  83 H1=tf([1 250], [1 500])
  84 bode(H1);
  85 [mag_the, pha_the, freq] = bode(H1, freq);
  86 max(freq)
  87 
  88 % bode functions returns linear scale y-axis
  89 mag_the = squeeze(mag_the);
  90 pha_the = squeeze(pha_the);
  91 mag_the = 20*log10(mag_the);
  92 
  93 % theorical normalized transfer function
  94 
  95 H1_norm = tf([1 252.52], [1 505.05])
  96 [mag_the_norm, pha_the_norm, freq] = bode(H1_norm, freq);
  97 
  98 % bode functions returns linear scale y-axis
  99 mag_the_norm = squeeze(mag_the_norm);
 100 pha_the_norm = squeeze(pha_the_norm);
 101 mag_the_norm = 20*log10(mag_the_norm);
 102 
 103 %% Magnitude
 104 ax1 = subplot(2, 1, 1)
 105 y1 = semilogx(freq*2*pi, mag, "LineWidth", 3.5);
 106 grid on;
 107 hold on;
 108 y2 = semilogx(freq, mag_the, "LineWidth", 2.75);
 109 y3 = semilogx(freq, mag_the_norm, "LineWidth", 2);
 110 
 111 ylim([-6.5 0.5])
 112 yticks([-6 -3 0]);
 113 xlim([1 1e5])
 114 % xlabel("Frequency [Hz]");
 115 ylabel("MAGNITUD [dB]");
 116 % title("Bode Magnitude");
 117 % legend("Normalizado", "Teórica", "Location", "southeast");
 118 
 119 % Remove x tick labels from upper plot
 120 set(ax1, "XTickLabel", []);
 121 
 122 %% Phase
 123 ax2 = subplot(2, 1, 2)
 124 semilogx(freq*2*pi, phase, "LineWidth", 3.5);
 125 grid on;
 126 hold on;
 127 semilogx(freq, pha_the, "LineWidth", 2.75);
 128 semilogx(freq, pha_the_norm, "LineWidth", 2);
 129 
 130 xlim([1 1e5])
 131 xlabel('FRECUENCIA  [rad/s]');
 132 ylabel("FASE  [grados]");
 133 % title("Bode Phase");
 134 % legend("Normalizado", "Teórica", "Location", "northeast");
 135 
 136 % Create legend on top axes
 137 % lgd = legend(ax1, [y1 y2], ...
 138 %              "Respuesta en frecuencia circuito", ...
 139 %              'Transferencia H_{1}(j\omega)')
 140 
 141 % Invisible axes covering the whole figure
 142 axL = axes("Position", [0 0 1 1], ...
 143            "Visible", "off", ...
 144            "Units", "normalized");
 145 
 146 lgd = legend(axL, [y1 y2 y3], ...
 147              {"Pasa altos de primer orden",...
 148                  "H_{1}", ...
 149                  "H_{1} normalizada"});
 150 
 151 % Move legend between plots
 152 set(lgd, "position", [0.555 0.54 -0.50 -0.50]);
 153 set(lgd, "numcolumns", 1);
 154 
 155 % Reduce vertical spacing
 156 set(ax1, "Position", [0.13 0.58 0.80 0.38]);
 157 set(ax2, "Position", [0.13 0.13 0.80 0.38]);
 158 
 159 print("bode_passive_highpass.png", "-dpng", "-r500");
 160