function [cwns,cgwns,grho,ROHS]=table_gen(FREQ,ZS,dep,svp,dbpwl,rho,N)
% Compute an piecewise linear representation of the complex wave
% number squared (intercepts and gradients). Also find the logarithmic
% gradients in the density and the density at the source.
%
ROHS=1.0;
cwns=zeros(N,1)+1i*zeros(N,1);
cgwns=zeros(N,1)+1i*zeros(N,1);
grho=zeros(N,1);
% Define the factor that converts from dbpwl to the imaginary part of
% the complex wave number
eta=1.0/(40.0*pi*log10(exp(1.0)));
% Water column
ck=(2.0*pi*FREQ/svp(1))*(1.0+eta*dbpwl(1)*1i);
cksq=ck^2;
%
for n=1:N-1
    ckp1=(2.0*pi*FREQ/svp(n+1))*(1.0+eta*dbpwl(n+1)*1i);
    cksqp1=ckp1^2;
    % Compute gradient and intercept
    cgwns(n)=(cksqp1-cksq)/(dep(n+1)-dep(n));
    cwns(n)=cksq-cgwns(n)*dep(n);
    %
    grho(n)=log(rho(n+1)/rho(n))/(dep(n+1)-dep(n));
    if (dep(n) < ZS) && (ZS <= dep(n+1))
    ROHS=exp(grho(n)*(ZS-dep(n)))*rho(n); % Define density at ZS
    end
    %
    cksq=cksqp1;
end
%
cgwns(N)=cgwns(N-1);
cwns(N)=cksq-cgwns(N)*dep(N);
grho(N)=grho(N-1);