I have implemented an ideal 10 bit midrise type quantizer in MATLAB. The signal-to-quantization-noise value from the simulation
and theory differ by approximately 6 dB. I have checked this difference with multiple bit numbers such as 10 bits, 12bits, 16bits and
the result is the same, SQNR from the simulation is always 6 dB below the ideal value. Also, fft of the quantized signal is strange, it
does have two peaks at signal frequency as expected, however all other values are zero. I cannot understand these two results.
Here is my code:
y1 = sin(2*pi*37/512*1e6*t);
plot(f,20*log10(y1_dft));
title("fft of the original signal y1");
delta = 2*max(abs(y1))/(2^nbits);
partition_right = delta:delta:(n_quants/2)*delta;
partition_left = -flip(partition_right,2);
partition = [partition_left 0 partition_right];
codebook_left = [min(partition_left) partition_left];
codebook_right = [partition_right max(partition_right)];
codebook = [codebook_left codebook_right];
[index,y1_quantized] = quantiz(y1,partition,codebook);
legend('Original signal y1','Quantized y1');
quants_dft = abs(fft(y1_quantized))/N;
title("fft of the quantized signal");
quantization_noise = y1 - y1_quantized;
plot(t,quantization_noise);
ylabel('quantization noise')
title('quantization noise in time domain')
quantization_noise_power = sum(abs(quantization_noise).^2)/N;
quantization_noise_power_theory = (delta^2)/12;
SQNR_sim = 10*log10(signal_power/quantization_noise_power);
SQNR_theory = 6.02*nbits + 1.76