% adc with B bits and +/-1 full scale
B = 10;
delta = 2/(2^B-1);
th = -1+delta/2:delta:1-delta/2;

% signal
fs = 1;
cycles = 2711;
N = 2^14;
fx = fs*cycles/N

t = linspace(0, (N-1)/fs, N);
x = sin(2*pi*fx*t);
y = adc(x, th) * delta - 1;
s = abs(fft(y)/N*2);
s = s(1:N/2);
f = (0:length(s)-1) / N;

bx = N * fx/fs + 1;
Asignal = 20*log10(s(bx))
sn = s;
sn(bx) = 0;
Anoise = 10*log10(sum(sn.^2))
SNR = Asignal-Anoise
SFDR = Asignal - max(20*log10(sn))

figure(1);
plot(f, 20*log10(s), '-');
xlabel('Frequency  [ f / f_s ]');
ylabel('Amplitude  [ dBFS ]');
title(sprintf('N = %d    A = %.1fdBFS   SNR = %.1fdB', N, Asignal, SNR));
axis([ min(f) max(f) -120 10 ]);
fixfig;

bx = N * fx/fs + 1;
Asignal = 20*log10(s(bx))
s(bx) = 0;
Anoise = 10*log10(sum(s.^2))
SNR = Asignal-Anoise
SFDR = Asignal - max(20*log10(s))