%this is a bode plot from one of 4 tube audio power amplifiers
% in the quad amplifier chasis

%this data was acquired open-loop, with all old loop compensators removed
%and a 8 ohm 100 watt resistor wired to the output.

%for the first set of data phase lag and lead was not measured
%for all other data - = phase lag, + = phase lead
%where lag means the output lags the input in time on a scope
%where lead means the output leads the input in time on a scope

close all;
clear all;

%---- plot open loop data before corrective networks were applied ---%
%phase lag or lead was not measured here, it was an overisght on my part
%however, lag and lead was measured for all other plots

%test frequency
f = [3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 200 300 400 500 600 700 ...
    800 900 1E3 2E3 3E3 4E3 5E3 6E3 7E3 8E3 9E3 10E3 20E3 30E3 40E3 50E3 ...
    60E3 70E3 80E3 90E3 100E3];

%voltage output (peak-to-peak)
Vout = [0.574 1.08 1.69 2.36 3.12 3.96 4.72 5.55 11.7 15.2 17.1 18.2 18.35 ...
    18.9 19.25 19.4 19.4 20.2 20.2 20.2 20.2 20.2 20.2 20.2 20.2 20.15 20.15 ...
    20.15 20.7 20.7 20.7 20.7 20.7 21.35 21.75 20.95 14.75 11.6 14.25 12.5 ...
    9.95 8 6.85 5.85];

%voltage input
Vin = 100E-3; %(V) peak-to-peak

%phase (deg)
phi = [27 216 144 172 191.52 144 142.56 129.6 79.2 64.8 51.84 36 32.4 32.76 ...
    25.92 25.9 18 14.4 8.64 0 0 0 0 0 0 0 0 0 0 18 21.6 25.2 23.04 19.44 18 ...
    43.2 75.6 57.6 81 86.4 100.8 115.2 243 113.4];

mag = 20*log10(Vout/Vin);

figure(200);
subplot(2,1,1), semilogx(f, mag);
grid on;
xlabel('frequency (Hz)');
ylabel('Power Gain (dB)');
xlim([1 f(size(f,2))]);
title('Open-Loop Bode Plot Before Corrective Networks');

subplot(2,1,2), semilogx(f, -1*phi);
grid on;
xlabel('Frequency (Hz)');
ylabel('Phase (deg)');
ylim([-250 10]);
xlim([1 f(size(f,2))]);

%save the plot as a jpg
print(gcf, '-djpeg100', 'tube_audio_power_amplifier_bode_before.jpg');

%using this data estimate the high and low frequencies just before -180 deg
f_high = 85E3; %(Hz) estimated off bode plot
mag_high = 31.4;

f_low = 7.5;
mag_low = 25;

%------ start of corrective network analysis ---------%
%close all;
%clc;

disp('   ');
disp('---- compensation results -----');

%enter desired gain margin
gm = 3; %(dB)

disp('   ');
disp(sprintf('Expected gain margin = %0.5g (dB)', gm));
disp('   ');

%using schematic of tube power amp first gain stage (12AX7 tube)
%and the schematic of the corrective
%network in figure 7-59A of Radio Designer's Handbook 4th ed
%calculate the high frequency corrective network necessary to stabilize the
%PA
RL = 100E3; %(ohms) plate load resistor of 12AX7, first gain stage of PA
RG = 100E3; %(ohms) grid resistor to ground of the 12AU7 phse splitter, 2nd gain stage of PA

R1 = RL*RG/(RL + RG);
attn_high = mag_high + gm; %desired attenuation at that frequency plus 6 db of gain margin
R_high = R1/(10^(attn_high/20) - 1);
C_high = 1/(2*pi*R_high*f_high);
f_cutoff_high = 1/(2*pi*R1*C_high);
f_flat_high = 1/(2*pi*R_high*C_high);

disp(sprintf('High frequency cutoff = %0.5g (KHz)', f_cutoff_high/1E3));
disp(sprintf('High frequency flattening out = %0.5g (KHz)', f_flat_high/1E3));
disp(sprintf('high frequency attenuation = %0.5g (dB)', attn_high));
disp(sprintf('High frequency corrective R = %0.5g (k ohms)', R_high/1E3));
disp(sprintf('high frequency corrective C = %0.5g (pF)', C_high/1E-12));


%using schematic of tube power amp first gain stage (12AX7 tube)
%and the schematic of the corrective
%network in figure 7-59B of Radio Designer's Handbook 4th ed
%calculate the low frequency corrective network necessary to stabilize the
%PA
attn_low = mag_low + gm; %desired attenuation at that frequency plus 6 db of gain margin
R_low = RG*(10^(attn_low/20) - 1);
C_low = 1/(2*pi*R_low*f_low);
f_cutoff_low = 1/(2*pi*C_low*(RL + RG));
f_flat_low = 1/(2*pi*R_low*C_low);

disp('   ');
disp(sprintf('Low frequency cutoff = %0.5g (KHz)', f_cutoff_low/1E3));
disp(sprintf('Low frequency flattening out = %0.5g (Hz)', f_flat_low));
disp(sprintf('Low frequency attenuation = %0.5g (dB)', attn_low));
disp(sprintf('Low frequency corrective R = %0.5g (k ohms)', R_low/1E3));
disp(sprintf('Low frequency corrective C = %0.5g (pF)', C_low/1E-12));

%------ plot the open-loop bode plot of the PA after these corrective
%networks have been applied -------------%
%test frequency
clear f Vin Vout phi mag;

f = [4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 200 300 400 500 600 700 ...
    800 900 1E3 2E3 3E3 4E3 5E3 6E3 7E3 8E3 9E3 10E3 20E3 30E3 40E3 50E3 ...
    60E3 70E3 80E3 90E3 100E3 200E3 300E3 400E3 500E3 600E3 700E3 800E3 ...
    900E3 1E6];

%voltage output (peak-to-peak)
Vout = [0.365 0.272 0.375 0.494 0.624 0.760 0.932 2.67 4.55 6.28 7.78 8.9 10 ...
    10.92 11.68 12.12 14.68 15.24 15.34 15.34 14.98 14.84 14.56 14.26 13.82 ...
    10.72 8.24 6.66 5.42 4.63 4.01 3.53 3.145 2.845 1.465 0.858 0.533 0.613 ...
    0.538 0.433 0.356 0.300 0.249 62E-3 33.7E-3 23.1E-3 146E-3 171.5E-3 ...
    159E-3 146E-3 148E-3 165E-3];

%voltage input
Vin(1:size(f,2)) = 100E-3; %(V) peak-to-peak
Vin(1) = 200E-3; %(V) p-p
Vin(size(f,2)-6:size(f,2)) = 800E-3; %(V) p-p

%phase (deg)
phi = [-158 -162 -151.2 -138.6 -144 -145.8 -126 -117 -94.5 -86.4 -81 -70.2 -63 ...
    -57.6 -48.6 -45 -21.6 -10.8 0 4.5 10.8 15.12 21.6 24.3 27 46.8 54 72 72 75.6 ...
    75.6 79.2 81 81 90 108 72 90 97.2 107.1 115.2 113.4 117 122.4 108 108 72 75.6 ...
    75.6 72 81 72];

phi = abs(phi);

mag = 20*log10(Vout./Vin);

figure(201);
subplot(2,1,1), semilogx(f, mag);
grid on;
xlabel('frequency (Hz)');
ylabel('Power Gain (dB)');
xlim([1 f(size(f,2))]);
title('Open-Loop Bode Plot After Corrective Networks');

subplot(2,1,2), semilogx(f, -1*phi);
grid on;
xlabel('Frequency (Hz)');
ylabel('Phase (deg)');
ylim([-250 10]);
xlim([1 f(size(f,2))]);

%save the plot as a jpg
print(gcf, '-djpeg100', 'tube_audio_power_amplifier_bode_after.jpg');

%------ plot the closed-loop R_feedback = 10K bode plot -------------%
clear f Vin Vout phi mag;

f = [5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 200 300 400 500 600 700 ...
    800 900 1E3 2E3 3E3 4E3 5E3 6E3 7E3 8E3 9E3 10E3 20E3 30E3 40E3 50E3 ...
    60E3 70E3 80E3 90E3 100E3 200E3 300E3];

%voltage output (peak-to-peak)
Vout = [0.99 1.37 1.715 2.05 2.42 2.81 6.12 7.86 8.78 9.34 9.56 9.70 9.82 9.9 ...
    9.9 10.1 10.1 10.1 10.1 10.1 10.1 10.1 10.14 10.1 10.06 9.82 9.68 9.30 ...
    9.02 8.66 8.32 8 7.76 5.11 3.3 2.3 2.53 2.24 1.805 1.505 1.248 1.054 0.251 ...
    0.135];

%voltage input
Vin = 400E-3; %(V) peak-to-peak

%phase (deg)
phi = [-135 -129.6 -126 -115.2 -113.4 -108 -79.2 -54 -43.2 -36 -32.4 -25.2 -21.6 ...
    -19.44 -18 -7.2 -5.4 0 0 0 0 0 0 10.3 7.2 13.5 21.6 27 32.4 31.5 36 40.5 45 ...
    72 86.4 72 81 97.2 100.8 115.2 113.4 113.4 108 108];

phi = abs(phi);

mag = 20*log10(Vout./Vin);

figure(202);
subplot(2,1,1), semilogx(f, mag);
grid on;
xlabel('frequency (Hz)');
ylabel('Power Gain (dB)');
xlim([1 f(size(f,2))]);
title('Closed-Loop (10K feedback resistor) Bode Plot After Corrective Networks');

subplot(2,1,2), semilogx(f, -1*phi);
grid on;
xlabel('Frequency (Hz)');
ylabel('Phase (deg)');
ylim([-250 10]);
xlim([1 f(size(f,2))]);

%save the plot as a jpg
print(gcf, '-djpeg100', 'tube_audio_power_amplifier_closed_loop_R10k_bode.jpg');

%------ plot the closed-loop R_feedback = 4.7K bode plot -------------%
clear f Vin Vout phi mag;

f = [5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 200 300 400 500 600 700 ...
    800 900 1E3 2E3 3E3 4E3 5E3 6E3 7E3 8E3 9E3 10E3 20E3 30E3 40E3 50E3 ...
    60E3 70E3 80E3 90E3 100E3 200E3 300E3];

%voltage output (peak-to-peak)
Vout = [0.88 1.08 1.33 1.56 1.8 2.03 3.76 4.53 4.91 5.13 5.23 5.3 5.35 5.38 ...
    5.41 5.44 5.44 5.44 5.44 5.44 5.44 5.44 5.44 5.43 5.43 5.43 5.34 5.32 5.25 ...
    5.19 5.13 5.03 4.94 3.99 2.93 2.30 2.515 2.27 1.89 1.58 1.308 1.1 .253 .139];

%voltage input
Vin = 400E-3; %(V) peak-to-peak

%phase (deg)
phi = [-135 -108 -113.4 -100.8 -97.2 -90 -57.6 -43.2 -36 -27 -21.6 -20.16 -20.16 ...
    -16.2 -14.4 -7.2 -5.4 -2.88 0 0 0 0 0 0 7.2 10.8 14.4 16.2 19.44 22.68 25.92 ...
    29.16 28.8 50.4 70.2 50.4 63 86.4 100.8 100.8 110.16 117 115.2 108];

phi = abs(phi);

mag = 20*log10(Vout./Vin);

figure(203);
subplot(2,1,1), semilogx(f, mag);
grid on;
xlabel('frequency (Hz)');
ylabel('Power Gain (dB)');
xlim([1 f(size(f,2))]);
title('Closed-Loop (4.7K feedback resistor) Bode Plot After Corrective Networks');

subplot(2,1,2), semilogx(f, -1*phi);
grid on;
xlabel('Frequency (Hz)');
ylabel('Phase (deg)');
ylim([-250 10]);
xlim([1 f(size(f,2))]);

%save the plot as a jpg
print(gcf, '-djpeg100', 'tube_audio_power_amplifier_closed_loop_R5pt7k_bode.jpg');

