%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));

%change for mono-block.... done %
%------ 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];

%voltage output (peak-to-peak)
Vout = [0.7 0.910 1.145 1.365 1.6 1.820 2.04 4.14 5.90 7.64 8.94 10.08 10.82 ...
    11.56 12.08 12.46 14.34 14.34 14.34 14.0 14.0 14.0 13.68 13.50 13.06 ...
    10.22 7.86 6.16 5.16 4.36 3.82 3.31 2.96 2.66 1.3 0.82 0.517 0.368 ...
    0.294 0.253 0.242 0.191 0.151 0.052 0.023];

%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 = [-86.4 -90 -86.4 -88.2 -86.4 -97.2 -72 -72 -64.8 -57 -54 -54 -44.1 ...
    -43.2 -38.88 -36 -14.4 -5.4 0 3.6 10.8 12.6 17.28 25.92 28.8 50.4 64.8 ...
    72 72 75.6 75.6 86.4 81 90 97.2 108 115.2 117 108 113.4 115.2 121.5 ...
    126 126 108];

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_PA_mono_bode_after.jpg');

%change for mono-block%
%------ plot the closed-loop R_feedback = 4.7K bode plot -------------%
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];

%voltage output (peak-to-peak)
Vout = [4.86 5.36 5.46 5.33 5.36 5.46 5.49 5.73 5.73 5.73 5.73 5.73 5.73 ..
    5.73 5.73 5.73 5.73 5.73 5.73 5.73 5.73 5.73 5.73 5.73 5.75 5.75 5.75 ...
    5.75 5.75 5.75 5.75 5.75 5.66 5.66 5.28 4.68 3.58 2.53 1.955 1.705 1.60 ...
    1.27 0.988 0.335 0.177];

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

%phase (deg)
phi = [-72 -54 -43.2 -37.8 -28.8 -25.92 -27 -14.4 -10.8 -7.2 -9 -10.8 ...
    -6.3 -7.2 -3.24 -3.6 0 0 0 0 0 0 0 0 0 0 2.16 7.2 7.2 9.72 12.6 ...
    14.4 16.2 18 39.6 64.8 79.2 90 91.8 94.5 100.8 113.4 115.2 129.6 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_PA_mono_closed_loop_R5pt7k_bode.jpg');

