function histogram = getHistogram(x, n, lowerBound, upperBound) %#codegen
%GETHISTOGRAM create a bounded histogram of values 
%
% This function creates a histogram whose bins span a specified lower and
% upper bound (inclusive).  Values outside this boundary are ignored.
%
% This function is for internal use only.

%   Copyright 2011-2021 The MathWorks, Inc.

  %define boundaries and number of bins
  lower = lowerBound;
  upper = upperBound;
  
  denom = upper-lower;
  if denom==0
    denom = eps;
  end
   histogram = zeros(n, 1);
  if coder.target('MATLAB')
      idx = n * (x-lower)/denom;
      idx = ceil(idx) + (idx==0);
      idx = idx(idx>=1 & idx<=n);
      for i=1:numel(idx)
          histogram(idx(i)) = histogram(idx(i)) + 1;
      end
  else
      for i = 1:coder.internal.indexInt(length(x))
          idx = n*(x(i)-lower)/denom;
          hIdx = cast(ceil(idx) + double(idx == 0),coder.internal.indexIntClass);
          if (hIdx >=1 && hIdx <= n)
              histogram(hIdx) = histogram(hIdx) + 1;
          end
      end
  end
end
