OutputPort1         = InputPort1;	

%% Interleaving

Channel1 = InputPort1.Sampled.Signal;
Channel2 = InputPort2.Sampled.Signal;
Channel3 = InputPort3.Sampled.Signal;
Channel4 = InputPort4.Sampled.Signal;

UpChannel1 = upsample(Channel1,4);
UpChannel2 = upsample(Channel2,4);
UpChannel3 = upsample(Channel3,4);
UpChannel4 = upsample(Channel4,4);


interleaved = UpChannel1 + UpChannel2 + UpChannel3 + UpChannel4 ;
OutputPort1.Sampled.Signal = interleaved ;
len = length (OutputPort1.Sampled.Signal);
OutputPort1.Sampled.Signal = OutputPort1.Sampled.Signal(1:len/4);

%%

time = InputPort1.Sampled.Time;                          

Sample_size = size(interleaved)

Sample_length = Sample_size(1,2);                        % Number of samples of input 

Sample_rate = Sample_length/max(time);

band = (Sample_length/Sample_rate)*(4e9) +3
%%---------------------------- Calculating FFT ----------------------------%

ADC_out = interleaved'.* hann(Sample_length)                        % windowing
fft_ADC_out = abs(fft(ADC_out));                         
                                                                                          

fft_ADC_out = fft_ADC_out(1:band);                        % taking half spectrum
                                                         

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;
logfft = 10*log10(fft_ADC_out);
fft_ADC_out(1)=0;
fft_ADC_out(2)=0;
[MSig1 MInd1] = max(fft_ADC_out);   
[second ind] = max(fft_ADC_out(fft_ADC_out<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;
%fft_ADC_out(ind)=0;

%fft_ADC_out(fft_ADC_out>-69) = 0;
ADC_har = find(logfft> -68);
fft_ADC_out(ADC_har) = 0;


MNoise1 = sum(fft_ADC_out)                        

MSNR1 = 10 *log10(second/MNoise1)

disp(MSNR1)





