function [xxmax, xxmin, xmax, xmin] = ws_srs(nspec, th, a1, a2, b1, b2, b3, f)
%
% ws_srs_fast  (high-speed vectorized Smallwood SRS)
% Uses cached feed-forward and recursive matrices for acceleration.
%
% Drop-in replacement for ws_srs.m
%

% -------- shape control ----------
th = th(:);
a1 = a1(:); a2 = a2(:);
b1 = b1(:); b2 = b2(:); b3 = b3(:);

Nsig = length(th);
Nres = round(0.75 / f(1));
N = Nsig + Nres;

yy = zeros(N,1);
yy(1:Nsig) = th;

% -------- precompute coefficient matrices ----------
Mfb = [b1 b2 b3].';     % 3 × nspec   feed-forward
Mab = [a1 a2].';        % 2 × nspec   recursion

% -------- outputs ----------
xmax = -inf(nspec,1);
xmin =  inf(nspec,1);

y1 = zeros(nspec,1);
y2 = zeros(nspec,1);

% -------- main loop ----------
for n = 3:N

    a0  = yy(n);
    a1s = yy(n-1);
    a2s = yy(n-2);

    % feed-forward term (optimized)
    u = Mfb * [a0; a1s; a2s];

    % recursive term
    v = Mab * [y1.'; y2.'];   % note transpose → 1×nspec row

    y = u + v;

    xmax = max(xmax, y);
    xmin = min(xmin, y);

    y2 = y1;
    y1 = y;
end

% -------- final packaging ----------
xxmax = max(abs(xmax), abs(xmin));
xxmin = xxmax;

xmax = abs(xmax);
xmin = abs(xmin);

end
