function yout = ampdist(xin,xtab,ytab)
%  This function will input a set of samples as the input to an amplifier
%  with transfer characteristics described by xtab and ytab it will output
%  signal with the specified amp distortion:
%
%     yout = ampdist(xin,xtab,ytab)
%
%  "xin" -the input signal vector
%  "xtab" -the values for amp input amplitudes (x-axis of TC curve)
%  "ytab" -the corresponding values for amp output amplitudes (y-axis of TC curve)
%  "yout" is the output signal
%
%   Written by Kevin D. Donohue Updated 3/3/2006 (donohue@engr.uky.edu)

%  Find input values less than lowest interpolation table value
lowlimx = min(xtab);
oorlow = find(xin < lowlimx);
if ~isempty(oorlow)   %  if any out of range low points exist ...
    xin(oorlow) = lowlimx(1);  %  clip the to be just in range
end

%  Find input values greater than highest interpolation table value
highlimx = max(xtab);
oorhigh = find(xin > highlimx);
if ~isempty(oorhigh)   %  if any out of range high points exist ...
    xin(oorhigh) = highlimx(1);  %  clip the to be just in range
end

%  Now apply interplotion to map input amplitudes to output amplitudes
yout = interp1(xtab,ytab,xin);  %  Use linear interpolation to map point from input to output using table values
yout = yout - mean(yout);    % Get rid of DC 
