function [dnl,inl] = dnl_inl_sin(y);
%DNL_INL_SIN
% dnl and inl plots from sine wave data
% input y contains the ADC output vector obtained from quantizing a
% sinusoid

% Boris Murmann, Aug 2002
% Bernhard Boser, Sept 2002

% histogram boundaries
minbin=min(y);
maxbin=max(y);

% histogram
h= hist(y, minbin:maxbin);

% cumulative histogram
ch= cumsum(h);

% transition levels
T = -cos(pi*ch/sum(h));

% linearized histogram
hlin = T(2:end) - T(1:end-1);

% truncate at least first and last bin, more if input did not clip ADC
trunc=2;
hlin_trunc = hlin(1+trunc:end-trunc);

% calculate lsb size and dnl
lsb = sum(hlin_trunc) / (length(hlin_trunc));
dnl = [0 hlin_trunc/lsb-1];
misscodes = length(find(dnl<-0.9));

% calculate inl
inl= cumsum(dnl);

% plot
if nargout == 0
    figure(1);  clf;
    subplot(2,1,1);
    plot(linspace(minbin+trunc, maxbin-trunc, length(dnl)), dnl, '.');
    xlabel('code');
    ylabel('DNL [LSB]');
    title(sprintf('DNL = +%.2f / %.2f LSB,  std.dev=%.2f,  %d missing codes (DNL <-0.9)', max(dnl), min(dnl), std(dnl), misscodes));
    subplot(2,1,2);
    plot(linspace(minbin+trunc, maxbin-trunc, length(dnl)), inl, '.');
    xlabel('code');
    ylabel('INL [LSB]');
    title(sprintf('INL = +%.2f / %.2f,  std.dev=%.2f', max(inl), min(inl), std(inl)));
end






