OutputPort1         = InputPort1;	

ADC_out = InputPort1.Sampled.Signal;

time = InputPort1.Sampled.Time;

Sample_size = size(ADC_out)

Sample_length = size(1,2);

%---------------------------- Calculating FFT ----------------------------%
ADC_out = ADC_out.* hann(Sample_length)       
fft_ADC_out = abs(fft(ADC_out));               % sample length is 65536 and so 65536 point FFT
                                               % it gives a 1024 GHz frequency spectrum

%--- Taking Half band
fft_ADC_out = fft_ADC_out(1:258);            % the whole spectrum is 1024 GHz, i am finding only the 4GHz band over here 

%--- Normalizing
fft_ADC_out = fft_ADC_out/max(fft_ADC_out);


%%------------------------------- Plots -----------------------------------%
fs = 8000000000;
freq = fs/2*(0:length(fft_ADC_out)-1)/length(fft_ADC_out);

figure(1);
hold on;
plot(freq,20*log10(fft_ADC_out));
set(gca,'FontSize',12,'FontName','TimesNewRoman');
xlabel('Frequency (Hz)', 'FontSize', 12, 'FontWeight','bold','FontName','TimesNewRoman');
ylabel('Magnitude (dB)', 'FontSize', 12, 'FontWeight','bold','FontName','TimesNewRoman');
title('ADC Output','FontSize',16);
xlim([0 max(freq)]);
ylim([-150 0]);
hold off;

%%-------------------------- SNR Calculation ------------------------------%

fft_ADC_out = fft_ADC_out.^2;
fft_ADC_out(1)=0;
[MSig1 MInd1] = max(fft_ADC_out);
MSig1 = MSig1  + fft_ADC_out(MInd1-1) + fft_ADC_out(MInd1+1);
fft_ADC_out(MInd1)=0;
fft_ADC_out(MInd1-1)=0;
fft_ADC_out(MInd1+1)=0;

[sorted_fft,sorted_fft_Ind] = sort(fft_ADC_out,'descend');

max1 = sorted_fft_Ind(1);  % 1st harmonic
max2 = sorted_fft_Ind(2);  % 2nd harmonic
max3 = sorted_fft_Ind(3);  % 3rd harmonic
max4 = sorted_fft_Ind(4);  % 4th harmonic
max5 = sorted_fft_Ind(5);  % 5th harmonic
fft_ADC_out(max1) = 0;
fft_ADC_out(max2) = 0;
fft_ADC_out(max3) = 0;
fft_ADC_out(max4) = 0;
fft_ADC_out(max5) = 0;

MNoise1 = sum(fft_ADC_out)
MSNR1 = 10 *log10(MSig1/MNoise1)


