function [SNR,SNDR,ENOB] = snr_plot(filename,data_var,query_plot);

%clear all
%clc
 
%filename = 'test_fft.tr0';
%data_var = 'sd_out';
x1 = loadsig_cppsim(filename);
Dout = evalsig(x1,data_var);
clear x1;

F = 10;
numpt = 2000*F;
fclk = 200e6;
fsig = 500e3;
finbw = 2e6;
span = 1;

%determine index of signal
n_sig = (fsig/fclk)*numpt;

%determine index of highest inband harmonic
n_dist_max = floor(finbw/fsig);

%determine index of max. inband frequency
n_bw = (finbw/fclk)*numpt;

%DC frequency
f_dc = [1:span+1];

%Input signal frequency 
f_in = [(n_sig+1-span):1:(n_sig+1+span)];

%Inband frequencies
f_bw = [1:1:(n_bw+1+span)];

%Window Data
Doutw=Dout.*hann(numpt);

%Performing the Fast Fourier Transform
Dout_spect=fft(Doutw);
clear Doutw;

%Signal Power
S_Dout_spect = Dout_spect.*conj(Dout_spect);

%Recalculate to dB
S_Dout_spect_dB=10*log10(S_Dout_spect);

%generate frequency bins for FFT plot
f = [0:numpt-1].*fclk/numpt;

%DC power
Pdc = sum(S_Dout_spect(f_dc));

%Signal power
Ps = sum(S_Dout_spect(f_in));

%Total inband power
Pntot = sum(S_Dout_spect(f_bw));

%reset accumulated distortion power to zero
Pdisttot = 0;

%Harmonic frequencies (inband):2nd to n-th harmonic
for n = 2:n_dist_max
    Pdisttot = Pdisttot+sum(S_Dout_spect([(n*n_sig+1-span):1:(n*n_sig+1+span)]));
end

%Scale relative to signal power
maxdB = 10*log10(Ps);

if(query_plot)
    figure; 
    semilogx(f(1:numpt/2),S_Dout_spect_dB(1:numpt/2)-maxdB,'r');  
    grid on; 
    title('FFT PLOT'); 
    xlabel('ANALOG INPUT FREQUENCY (MHz)');
    ylabel('AMPLITUDE (dB)'); 
    xlima([1e4 fclk/2])
    ylima([-120 20])
    format;
end

SNDR= 10*log10(Ps/(Pntot-Ps-Pdc));
SNR = 10*log10(Ps/(Pntot-Ps-Pdc-Pdisttot));
ENOB=(SNDR-1.763)/6.02;

if(query_plot)
    text(2e4,-40,strcat('\bf{SNDR = ',num2str(SNDR),'}'))
    text(2e4,-50,strcat('\bf{SNR = ',num2str(SNR),'}'))
    text(2e4,-60,strcat('\bf{ENOB = ',num2str(ENOB),'}'))
end