function y = adc(x, thresholds) % ADC quantize input to integers % % Y = ADC(X, THRESHOLDS) % maps X to values 0:length(THRESHOLDS) such that % Y = 0 for X < THRESHOLDS(1) % Y = 1 for TH(1) <= X < TH(2) % ... % Y = length(X) for X >= TH(end) % N = length(x); y = zeros(N, 1); for i=1:N y(i) = sum(x(i) >= thresholds); end return N = length(thresholds); % make sure thresholds is a column vector thresholds = reshape(thresholds, N, 1); % sort by level ... in case ADC is non-monotonic xy = sortrows([thresholds (1:N)'], 1); th = xy(:, 1); cc = xy(:, 2); y = zeros(length(x), 1); for i = 1:N y(x >= th(i)) = cc(i); end