clear all;
%fid = fopen('adc_data_5m_complex1x.bin','r');
fid = fopen('adc_data_5m_5cm_complex1x.bin','r');
color = 'g';
figureID = 3;
ratio = 1;
NUM_ADC_SAMPLES = 256*ratio; NUM_CHIRPS = 64; NUM_VIRTUAL_ANTENNAS = 4; NUM_FRAMES = 1;
adcRate = 2.8*ratio;

samples = NUM_ADC_SAMPLES * NUM_CHIRPS * NUM_VIRTUAL_ANTENNAS;
dataChunk  = fread(fid,samples*2*NUM_FRAMES,'uint16','l');
fclose(fid);
dataChunk = dataChunk - (dataChunk >= 2^15) * 2^16;

    % through iwr6843 + DCA1000,  radar_data has data in the following format 
    % Rx0I0, Rx0I1, Rx0Q0, Rx0Q1, Rx0I2, Rx0I3, Rx0Q2, Rx0Q3, ...
    % The following script reshapes it into a 4-dimensional array.    
    len = length(dataChunk) / 2;
    adcOut(1:2:len) = dataChunk(1:4:end) + 1j*dataChunk(3:4:end);
    adcOut(2:2:len) = dataChunk(2:4:end) + 1j*dataChunk(4:4:end);
    adcOut = reshape(adcOut, NUM_ADC_SAMPLES, NUM_VIRTUAL_ANTENNAS, NUM_CHIRPS, NUM_FRAMES);
    adcOut = permute(adcOut, [1, 3, 2, 4]);
    
fftSize1D = size(adcOut,1);
radar_data_1dFFT = fft(adcOut,[], 1);%.*window_1D 

% 2D vel-FFT across the 3nd dimensions (chirps)
fftSize2D = size(adcOut, 2);
radar_data_2dFFT = fft(radar_data_1dFFT,[], 2); %.*window_2D
radar_data_2dFFTMag = sum(abs(radar_data_2dFFT).^2, 3); 
figure(figureID); hold on; plot(adcRate*(0:NUM_ADC_SAMPLES-1)/NUM_ADC_SAMPLES, 10*log10((radar_data_2dFFTMag(:,:,1,1))),color);
grid on;
% figure(2); hold on; plot(adcRate*(0:NUM_ADC_SAMPLES-1)/NUM_ADC_SAMPLES, squeeze(imag(radar_data_2dFFT(:,1,1,:))),color);
% figure(2); hold on; plot(adcRate*(0:NUM_ADC_SAMPLES-1)/NUM_ADC_SAMPLES, squeeze(real(radar_data_2dFFT(:,1,1,:))),color);
    