clc; clear; close all;

fileID = fopen("FDR_two_10M_Ref_10M_DUT.dat");  
A1 = int32(fread(fileID,'int32','b'));
fclose(fileID);

B1 = swapbytes(reshape(A1,1,[]));
%B1 = (reshape(A1,1,[]));

CC = reshape(B1,4,[]);

CCC1 = CC(4,1:1024*1000);
CCC2 = CC(3,1:1024*1000);
CCC3 = CC(2,1:1024*1000);
CCC4 = CC(1,1:1024*1000);

avg_LEN = 200000;
avg = ones(1, avg_LEN)/avg_LEN;

CCC1_avg = filter(avg, 1, CCC1);
CCC2_avg = filter(avg, 1, CCC2);
CCC3_avg = filter(avg, 1, CCC3);
CCC4_avg = filter(avg, 1, CCC4);


CIC_R = 128;
FS = 133000000;
Fs_base = FS/CIC_R;
f_REF1 = 10000000;

LEN = length(CCC1_avg);
T = LEN/Fs_base;

time = linspace(0,T,length(CCC1_avg));

avg_LEN = avg_LEN*2;
tt = time(avg_LEN:end);
CH1 = CCC1_avg(avg_LEN:end)/(2^32)*Fs_base;
CH2 = CCC2_avg(avg_LEN:end)/(2^32)*Fs_base;
CH3 = CCC3_avg(avg_LEN:end)/(2^32)*Fs_base;
CH4 = CCC4_avg(avg_LEN:end)/(2^32)*Fs_base;



figure(1);
plot(tt,CH1,'-g');
hold on;
plot(tt, CH2,'-b');

hold on;
plot(tt, CH3,'-m');
hold on;
plot(tt, CH4,'-k');
hold off;
grid on;
xlabel('time /S');
ylabel('Hz');
legend('CH1','CH2','CH3','CH4')
axis tight
title('Raw Frequecies')


figure(2);
plot(tt,CH1-CH3 + f_REF1,'-k');
hold on;
plot(tt,CH2-CH3+ f_REF1,'-b');
hold off;
grid on;
xlabel('time /S');
ylabel('Hz');
title('Frequecies /Referenced to REF1')

legend('CH1-CH3: DUT','CH2-CH3: REF2');




