dkumar
Member level 3

HI All,
I am trying to find SQNR using a matlab code i wrote. But i am not sure where am i mistaking as the result is not matching to the standard equation of SQNR = 6.02N + 1.76.
attached is the matlab code.
%% Define the input signal
clc; clear;
p=10;
Nfft = 2^p;
n = 0:1:Nfft-1; % taking Nfft number of samples.
X = sin(2*pi*(3/Nfft)*n); %half volt amplitude and 3rd bin fundamental
%% Defining the quantization levels for N bit quantizer
N = 4; %number of bits of quantizer
Nlvl = 2^N+1; %number of levels in quantizer.
LSB = 2/2^N; %defining LSB size based on 1 volt amplitude of sine wave for full scale sinewave
P = -1:LSB:1; %quantization levels b/w -1 to +1 i.e 17 lvlsL = 17
L = length(X);
%%Quantizing
for i = 1:L
for j = 1:length(P)-1
if ((P(j)<= X(i))&&(X(i)<=P(j+1)))
Xq(i)=(P(j)+P(j+1))/2;
end
end
end
%% Quantization error and distribution.
Qe = Xq-X; %calculating the quantization error.
%% Plotting
figure(1)
plot(X) %plot signal
hold on
plot(Xq) %plot quantized value.
hold on
plot(Qe) %plottind the quantization error.
grid on
%% Plotting the FFT of quantized output
Qf = abs(fft(Xq,Nfft)); % computing FFt
figure(2)
Qf_shift = Qf(1:Nfft/2); %Taking only the half of the symmetric output
plot([0:Nfft/2-1],20*log10(Qf_shift)); %plotting the spectrum
grid on
%% Finding peak signal to quantization ratio
Sig_bin = find(Qf_shift==max(Qf_shift)); %Finding the signal bin
Sig_pwr = (Qf_shift(Sig_bin))^2; %Sig power.
Noise_pwr = (sum(Qf_shift)-Qf_shift(Sig_bin))^2;
SQNR = Sig_pwr/Noise_pwr;
I am worried if i am mistaking on how to quantize the signal.
I am trying to find SQNR using a matlab code i wrote. But i am not sure where am i mistaking as the result is not matching to the standard equation of SQNR = 6.02N + 1.76.
attached is the matlab code.
%% Define the input signal
clc; clear;
p=10;
Nfft = 2^p;
n = 0:1:Nfft-1; % taking Nfft number of samples.
X = sin(2*pi*(3/Nfft)*n); %half volt amplitude and 3rd bin fundamental
%% Defining the quantization levels for N bit quantizer
N = 4; %number of bits of quantizer
Nlvl = 2^N+1; %number of levels in quantizer.
LSB = 2/2^N; %defining LSB size based on 1 volt amplitude of sine wave for full scale sinewave
P = -1:LSB:1; %quantization levels b/w -1 to +1 i.e 17 lvlsL = 17
L = length(X);
%%Quantizing
for i = 1:L
for j = 1:length(P)-1
if ((P(j)<= X(i))&&(X(i)<=P(j+1)))
Xq(i)=(P(j)+P(j+1))/2;
end
end
end
%% Quantization error and distribution.
Qe = Xq-X; %calculating the quantization error.
%% Plotting
figure(1)
plot(X) %plot signal
hold on
plot(Xq) %plot quantized value.
hold on
plot(Qe) %plottind the quantization error.
grid on
%% Plotting the FFT of quantized output
Qf = abs(fft(Xq,Nfft)); % computing FFt
figure(2)
Qf_shift = Qf(1:Nfft/2); %Taking only the half of the symmetric output
plot([0:Nfft/2-1],20*log10(Qf_shift)); %plotting the spectrum
grid on
%% Finding peak signal to quantization ratio
Sig_bin = find(Qf_shift==max(Qf_shift)); %Finding the signal bin
Sig_pwr = (Qf_shift(Sig_bin))^2; %Sig power.
Noise_pwr = (sum(Qf_shift)-Qf_shift(Sig_bin))^2;
SQNR = Sig_pwr/Noise_pwr;
I am worried if i am mistaking on how to quantize the signal.