% compressor.m
%
% This module compresses the values [-64:63] (which are the same values
% representable by a 7-bit 2's complement number) into an output range
% of [-31:+31] which almost fills the range of a 6-bit 2's complement
% number.
%
% Outputs are compressed according to a piecewise function comprised of
% partial sin waveforms and saturated regions.  They are not quantized or
% rounded in any other way.
%
% 2015/03/05  Cleaned up and documented better


PrintOn = 1;              % set to 1 to print tiff and png of figure(1)
XVals   = [-64:63];       % range of a 7-bit 2's complement number
XOffset = (1 - XVals(1)); % offset needed for array index to begin at 1
Xsat    = 48;             % +/- x value where full saturation begins
SatVal  = 31;             % both neg and pos. max +/- range of 6-bit 2's compl

for l = XVals,
  if     l < -Xsat
    b(l+XOffset) = -SatVal;
  elseif l < 0
    b(l+XOffset) = SatVal * sin((l/Xsat)*(pi/2));
  elseif l < Xsat
    b(l+XOffset) = SatVal * sin((l/Xsat)*(pi/2));
  else
    b(l+XOffset) = +SatVal;
  end
end

figure(1);clf;
plot(XVals, b);
hold on;
grid on;
%plot([-24:24], [-24:24]*(pi/2), 'r');   % not slope=+1 line
xlabel('Input value');
ylabel('Output value');
title('Compressor function');
%if (PrintOn)  print -dtiff compressor.tiff;  end
if (PrintOn)  print -dpng compressor.png;    end

