SNR Code
SNR Code
**********************************************************************
%-------------------------------------------------%
%%matlab code to calculate SNR,SNDR,THD SFDR
%%date : 11 July 2005
%%rev : 1
%-------------------------------------------------%
fclk = 80e+6;
numpt=32;
numbit=3;
load test.dat; %load data from disk
a = test';
N=length(a);
%[M,N]=size(a); %number of data
for i=1:1:N;
c=int2str(a(i)); %change integer type data to string type
temp=0;
Nlength=length(c); %length of the string;
for j=1:1:Nlength;
d=str2num(c(j))*2^(Nlength-j); %binary to dec
temp=temp+d;
end;
code(i)=temp;%/numpt*2.5;
end;
N=length(code);
%plot results in time domain
figure;
plot([1:N],code);
title('TIME DOMAIN')
xlabel('SAMPLES');
ylabel('DIGITAL OUTPUT CODE');
zoom xon;
%performing FFT
Dout_spect=fft(Doutw,numpt);
%recalculate to dB
Dout_dB=20*log10(abs(Dout_spect));
%plot([1:N/2],Dout_dB(1:N/2));
%display the results in the frequency domain with FFT plot
figure;
maxdB=max(Dout_dB(2:numpt/2));
%-----------------------------------------------%
%calculate SNR,SINAD,ENOB,THD and SFDR values
%-----------------------------------------------%
%find the signal bin number, DC=bin 1
fin=find(Dout_dB(1:numpt/2)==maxdB);
%Span of the input freq on each side
%span=5;
span=max(round(numpt/200),5);
%approximate search span for harmonics on each side
spanh=2;
%determine power spectrum
spectP=(abs(Dout_spect)).*(abs(Dout_spect));
%find DC offset power
Pdc=sum(spectP(1:span));
%extract overall signal power
Ps=sum(spectP(span-fin:span+fin));
%vector/matric to store both freq and power of signals and harmonics
Fh=[];
%the 1st element in the vector/matrix represents the signal,
%the next element reps the 2nd harmonic,etc..
Ph=[];
%find harmonic freq and power components in the FFT spectrum
for har_num=1:10
%input tones greater than fSAMPLE are aliased back into the spectrum
tone=rem((har_num*(fin-1)+1)/numpt,1);
if tone>0.5
%input tones greater than 0.5*fSAMPLE(after aliasing) are reflected
tone=1-tone;
end
Fh=[Fh tone];
%for this procedure to work,ensure the folded back high order harmonics
%do not overlap
%with DC or signal or lower order harmonics
har_peak=max(spectP(round(tone*numpt)-spanh:round(tone*numpt)+spanh));
har_bin=find(spectP(round(tone*numpt)-spanh:round(tone*numpt)+spanh)==har_peak);
har_bin=har_bin+round(tone*numpt)-spanh-1;
Ph=[Ph sum(spectP(har_bin-1:har_bin+1))];
end
SINAD=10*log10(Ps/(Pn+Pd));
SNR=10*log10(Ps/Pn);
disp('THD is calculated from 2nd through 5th order harmonics');
THD=10*log10(Pd/Ph(1));
SFDR=10*log10(Ph(1)/max(Ph(2:10)));
disp('Signal & Harmonic power components:');
HD=10*log10(Ph(1:10)/Ph(1));
ENOB =(SNR-1.7)/6.0206;
%distinguish all harmonics locations within the FFT plot
hold on;
plot(Fh(2)*fclk,0,'mo',Fh(3)*fclk,0,'cx',Fh(4)*fclk,0,'r+',Fh(5)*fclk,0,'g*',Fh(6)*fclk,0,'bs',Fh(7)*fc
legend('1st','2nd','3rd','4th','5th','6th','7th','8th','9th');
fprintf('SINAD=%gdB \n',SINAD);
fprintf('SNR=%gdB \n',SNR);
fprintf('THD=%gdB \n',THD);
fprintf('SFDR=%gdB \n',SFDR);
fprintf('ENOB=%g \n',ENOB);