Skip to content

Instantly share code, notes, and snippets.

@tobin
Created March 27, 2012 13:55
Show Gist options
  • Select an option

  • Save tobin/2216090 to your computer and use it in GitHub Desktop.

Select an option

Save tobin/2216090 to your computer and use it in GitHub Desktop.
Quantization noise demo
Show the effect of ADC/DAC quantization noise.
% To show the effect of quantization noise, we take a signal and quantize
% it (by rounding to the nearest integer). Taking the difference gives
% the quantization error. We then display the amplitude spectra of all
% three signals (original, quantized, and error). Finally, we compare to
% the theoretically expected quantization noise spectral density of
% 1/sqrt(6*fs) (valid with some assumptions; i.e. all quantization levels
% uniformly visited?).
% Tobin Fricke 2012-01-06
fs = 1024; % sample rate [Hz]
t = 0:(1/fs):16; % time vector [s]
% make a sinusoid
%A = 32768; % amplitude in quantization levels
%f = 20*exp(1)/3;
%x = A*sin(2*pi*f*t); % original signal
% make random noise
x = 4*randn(size(t)); % white noise
[B,A] = ellip(4, 3, 100, [10 200]/(fs/2),'stop');
%[B,A] = butter(4, 10/(fs/2), 'low');
x = filter(B, A, x);
y = round(x); % quantized signal
n = y - x; % quantization error (noise)
%plot(t, x, t, y, t, n);
bw = 0.5; % spectrum resolution (Hz)
nfft = fs/bw; % length of fft (samples)
[Pxx, f] = pwelch(x, hanning(nfft), nfft/2, nfft, fs);
[Pyy, f] = pwelch(y, hanning(nfft), nfft/2, nfft, fs);
[Pnn, f] = pwelch(n, hanning(nfft), nfft/2, nfft, fs);
loglog(f, sqrt(Pxx), f, sqrt(Pyy), f, sqrt(Pnn));
LSBvoltage = 1;
quant_noise_pred = LSBvoltage / sqrt(6*fs);
line(get(gca,'xlim'), quant_noise_pred*[1 1], 'color', [0 0 0], 'linestyle', '--');
grid on
xlim([bw (fs/2)])
legend('signal', 'signal (quantized)', 'quant. error', 'expected quant noise', ...
'Location', 'Best');
xlabel('frequency');
ylabel('counts per sqrt Hz');
title('quantization noise - amplitude spectral density');
Display the source blob
Display the rendered blob
Raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment