function [T,fn,Sa] = Sa_twoPeriod(SDS, SD1, TL, siteClass)
% Sa_twoPeriod  Plot the ASCE 7-22 Two-Period DESIGN response spectrum
%   using SDS and SD1 directly (no Fa/Fv recomputation).
%
%   Inputs:
%     SDS       = design short-period spectral acceleration (g)
%     SD1       = design 1-sec spectral acceleration (g)
%     TL        = long-period transition period (s)
%     siteClass = letter A–E (for labeling only)
%
%   Outputs:
%     T   = period vector (sec)
%     fn  = frequency vector (Hz)
%     Sa  = spectral acceleration (g)

    % --- Validate inputs ---
    validateattributes(SDS, {'numeric'}, {'scalar','positive'});
    validateattributes(SD1, {'numeric'}, {'scalar','positive'});
    validateattributes(TL,  {'numeric'}, {'scalar','positive'});

    if ~(ischar(siteClass) || isstring(siteClass))
        error('siteClass must be a letter A–E');
    end
    siteClass = upper(string(siteClass));


    % --- 1/12-octave frequency grid ---
    f_min = 0.1; f_max = 100; ratio = 2^(1/12);
    frequencies = f_min;
    f = f_min;
    while f * ratio <= f_max
        f = f * ratio;
        frequencies(end+1,1) = f; %#ok<SAGROW>
    end
    if frequencies(end) < f_max
        frequencies(end+1,1) = f_max;
    end
    T = sortrows(1 ./ frequencies);


    % --- Characteristic periods ---
    T0 = 0.2 * SD1 / SDS;
    TS = SD1 / SDS;

    % --- Piecewise spectrum (ASCE 7-22 §11.4.5.2) ---
    Sa = zeros(size(T));

    idx1 = (T < T0);
    Sa(idx1) = SDS .* (0.4 + 0.6 .* (T(idx1) ./ T0));

    idx2 = (T >= T0) & (T <= TS);
    Sa(idx2) = SDS;

    idx3 = (T > TS) & (T <= TL);
    Sa(idx3) = SD1 ./ T(idx3);

    idx4 = (T > TL);
    Sa(idx4) = (SD1 / TL) .* (TL ./ T(idx4)).^2;

    % --- Plotting ---
    fig_num = 1;
    y_label = 'Spectral acceleration, S_a(T) (g)';
    t_string = sprintf('ASCE 7-22 Two-Period DESIGN Spectrum (5%% Damping)\nSite Class %s', siteClass);

    ppp = [T Sa];
    x_label = 'Period T (sec)';
    [fig_num,~] = plot_loglog_function_h2(fig_num, x_label, y_label, t_string, ppp, min(T), max(T));
    [fig_num,~] = plot_loglin_function_h2(fig_num, x_label, y_label, t_string, ppp, min(T), max(T));

    ppp = flipud(ppp);
    ppp(:,1) = frequencies;
    x_label = 'Natural Frequency fn (Hz)';
    [fig_num,~] = plot_loglog_function_h2(fig_num, x_label, y_label, t_string, ppp, min(frequencies), max(frequencies));
    [fig_num,~] = plot_loglin_function_h2(fig_num, x_label, y_label, t_string, ppp, min(frequencies), max(frequencies));

    % --- Display summary ---
    fprintf('\nTwo-Period DESIGN Spectrum\n');
    fprintf('  SDS = %.3f g\n  SD1 = %.3f g\n  TL  = %.2f s\n\n', SDS, SD1, TL);

    fn = frequencies;

end