# Confusion regarding formula of SQNR? Finding SQNR using MATLAB

I am learning about SQNR and when i google i get its formula that it is equal to

6.02 db(per bit)+1.76 db

But in our Lab/class, we saw different formula ,which is following formula when using MATLAB:

SQNR=10*log10(sum(x.^2)/sum(E.^2))


Where x is original signal and E is error/noise signal

I am confused which formula is correct/proper?

Becuase if we use former formula we get different results and if we use latter formula we get different results

Based upon answer of MR Dan, i have updated my question and also included the MATLAB code we saw in our Lab

clc %clears all the text from the Command Window
clear all %clear/delete the variables created in
close all %delete all the figures whose handles are not
f=100%signal frequecny
Fs=200; %sampling frequency
Ts=1/Fs
n=4; %number of bits
L=2.^n; %quantization levels
t=0:Ts:0.1; %time interval
x=cos(2.*pi.*f.*t); %Signal x(t)
D=(max(x)-min(x))/(L-1);
xq=quant(x,D); %quantization
E=x-xq; %error calculation
SQNR=10*log10(sum(x.^2)/sum(E.^2))