From: Kris on
I am trying to simulate and understand the Signal to Quantization Noise Ratio (SQNR) for a linear ADC. This file will later be expanded to include noise shaping etc and also to choose the converter of choice.

Here is my question-

1) I have calculated the Noise power using the variance of a uniform pdf which is Q^2/12, where Q is the quantization level. This noise power when used with Signal power to compute SQNR turns out ok according to the standard formula 6.02 N + 1.76 dB. This is called AvgNoisePowerIdeal in the sim below


2) When i used the statistics of the quantization error to compute quantization noise power the SQNR turns out higher. This noise power is called AvgNoisePower below in the sim.

I am unable to reconcile difference between the two. any help would be helpful. thanks!

The full .m file is below

close all
clear all
clc

A = 0.7; % Peak amplitude in Volts
f = 1000; %Fundamental Frequency in Hz
Fs = 10000 ; %Sampling frequency in Hz
Ts = 1/Fs;
n = 5; % Simulation length in Seconds
n = n/Ts;% Simulation length in number of sampling instants
NFFT = 2^nextpow2(n);%vector length for FFT
DC = 0.0;

TimeVector = (0:n-1)*Ts;
%AWGN = randn(size(TimeVector));
AWGN = zeros(size(TimeVector));
Signal = DC + A*sin(2*pi*f*TimeVector) + AWGN;
FreqVector = linspace(-Fs/2,Fs/2,NFFT);

%Scaling of FFT so the result is independent of n
a = fft(Signal,NFFT)./n;

%TWO SIDED MAGNITUDE SPECTRUM - log scale
figure(2)
plot(FreqVector,20*log10(fftshift(abs(a))));
title('Two sided Magnitude Spectrum');
xlabel('Frequency in Hz');
ylabel('Magnitude in dBV');

%POWER SPECTRAL DENSITY
BW = Fs;
Pxx = abs(a).*conj(abs(a))/BW;
%power spectral density
figure(3)
hpsd = dspdata.psd(Pxx,'Fs',Fs,'SpectrumType','twosided');
plot(hpsd);
% NO DIFFERENCE BETWEEN THIS PLOT AND THE ONE ABOVE.
% figure(6)
% plot(FreqVector/1000,10*log10(Pxx));
% title('two sided power spectral density');
% xlabel('frequency in hz');
% ylabel('dB/hz');

%Uniform Quantization
NumBits = 14;
FSV = 2*A; % Full scale Voltage assumed to be 1.4 V
%Quantization step in V
Q = FSV/(2^NumBits);
%DC offset in signal corresponds to input common mode voltage
%Input signal is divided by FSV/2 so quantized output signal is in range of
% -1 to 1
ScaledInput = (Signal-DC)./(FSV/2);
ScaledQ = Q/(FSV/2);
%Quantized code value in decimal
QuantizedOutput = round(ScaledInput/ScaledQ);
error = ScaledInput-(QuantizedOutput*ScaledQ);
%Average Noise power will always be 0.5 for this simulation because of
%input signal scaling
AvgSignalPower = sum(ScaledInput.^2)./n;
AvgNoisePower = sum(error.^2)./n;
%Average Noise power of uniform pdf
AvgNoisePowerIdeal = ScaledQ^2/12;

SQNR = 10*log10(AvgSignalPower/AvgNoisePower)
%2's complement output
%AdcOutput = dec2bin(mod((QuantizedOutput),2^NumBits),NumBits);
AdcOutput = QuantizedOutput;

% figure(4)whos
% plot(ScaledInput,error);
% title('Quantization Error vs Scaled input voltage (scaled by FSV/2');
% xlabel('Scaled Input- Unitless');
% ylabel('error in V');

%y = fft(bin2dec(AdcOutput),NFFT)/n;
y = fft((AdcOutput),NFFT)/n;
figure(5)
plot(FreqVector,20*log10(fftshift(abs(y))*ScaledQ));
title('Magnitude Spectrum of a practical A/D');
xlabel('Frequency in Hz');
ylabel('dB');