function [] = Straight_Forward_Processing_with_chirp_configs_16xx(adc_file_name)
% Constants
lightSpeed = 3e8; 
is_real = 0; 

% Basic Chirp parameters. (In the comment, the parametrs in radar studio
% are shown)
Samples_Per_ChirpConfig = 128; % ADC Samples (in SensorConfig->Profile tab)
ChirpConfigs_Per_Chirp = 1;    % Number of chirps enabled (in SensorConfig->Chirp tab).
Chirps_Per_Frame = 20;         % No. of chirp loops (in SensorConfig->Frame tab).
Number_of_Frames = 500;          % No. of Frames (in SensorConfig->Frame tab).
n_adcBits = 16;                % ADC bitwidth (StaticConfig-> ADC Config tab)

if is_real == 1
    Number_of_real_channels = 4; % 8 real channels, 4 complex channels.
else
    Number_of_real_channels = 8; % 8 real channels, 4 complex channels.
end
Number_of_cmplx_channels = 4;

frame_periodicity_sec = 50e-3;

Sample_Rate_Hz = 2e6; % Sample Rate (ksps) (in SensorConfig->Profile tab)
Slope_Hz = 15.015e12;     % Frequency slope (Mhz/us)  (in SensorConfig->Profile tab)
Chirp_repetition_period_sec = 300e-6; % (Idle Time (us) + RampEnd Time (us)) (in SensorConfig->Profile tab)
Start_Frequency_Hz = 77e9; % Start Freq (Ghz) (in SensorConfig->Profile tab)
lambda = lightSpeed/(Start_Frequency_Hz);

% Some derived parameters. 
range_resolution = ((Sample_Rate_Hz/Samples_Per_ChirpConfig)/Slope_Hz)*(lightSpeed/2); 
vel_resolution = ((1/Chirp_repetition_period_sec)/Chirps_Per_Frame)*(lambda/2);
range_axis = (0:Samples_Per_ChirpConfig-1)*range_resolution; 
vel_axis = (-Chirps_Per_Frame/2:Chirps_Per_Frame/2-1)*vel_resolution;

% Windows
window_1D = repmat(hann(Samples_Per_ChirpConfig)',Number_of_cmplx_channels,1,Chirps_Per_Frame)/sum(hann(Samples_Per_ChirpConfig));
window_2D = permute(repmat(hann(Chirps_Per_Frame),1,Samples_Per_ChirpConfig,Number_of_cmplx_channels),[3,2,1])/sum(hann(Chirps_Per_Frame));

% Compute the total number of samples
total_samples = Number_of_real_channels*Samples_Per_ChirpConfig*ChirpConfigs_Per_Chirp*Chirps_Per_Frame*Number_of_Frames;    

% Open the file and read the data. 
fileID = fopen(adc_file_name,'r');
% TSW1400 converts ADC data to offset binary. Convert it back to 2's
% complement.
radar_data = fread(fileID,total_samples,'uint16')-2^15;
fclose(fileID);

% radar_data has data in the following format.
% Rx0I0, Rx0Q0, Rx0I1, Rx0Q1, Rx0I2, Rx0Q2,  Rx0I3, Rx0Q3, ... 
% The following script reshapes it into a 4-dimensional array. 
if is_real == 0

    radar_data = reshape(radar_data, 4, []);
    radar_data = radar_data([1 3 2 4],:); 

    radar_data = reshape(radar_data, 2*Samples_Per_ChirpConfig, Number_of_real_channels/2, ChirpConfigs_Per_Chirp, Chirps_Per_Frame, Number_of_Frames);

    % Convert the data to complex
    radar_data_real = radar_data(1:2:end, :, :, :, :); 
    radar_data_complex = radar_data(2:2:end, :, :, :, :); 
    radar_data = radar_data_real + 1i*radar_data_complex; 

    % Permute the dimensions of the array are so that they are of the format
    % [Channels, Samples, Chirps, ChirpTypes, Frames]

    radar_data = permute(radar_data, [2,1,3,4,5]);

else
    radar_data = reshape(radar_data, Samples_Per_ChirpConfig, Number_of_real_channels, ChirpConfigs_Per_Chirp, Chirps_Per_Frame, Number_of_Frames);
    
    % Permute the dimensions of the array are so that they are of the format
    % [Channels, Samples, Chirps, ChirpTypes, Frames]

    radar_data = permute(radar_data, [2,1,3,4,5]);
   
end    

radar_data = radar_data*(sqrt(2)/2^(n_adcBits-1));
% % Compute and remove DC
% dc_per_channel = mean(mean(mean(radar_data,2),3),4); 
% for ik = 1:4
%     radar_data(ik,:,:,:,:) = radar_data(ik,:,:,:,:) - dc_per_channel(ik); 
% end

frames_data = radar_data(1,:,:,:,:);
slow_fast_frames  = squeeze(mean(frames_data,4));
slow_fast_frames  = slow_fast_frames -  mean(mean(slow_fast_frames));

%% plot spectrogram
% nfft =  Samples_Per_ChirpConfig * 4;
% fft_windw = hamming(Samples_Per_ChirpConfig);
% slow_time =  (1: Number_of_Frames)  * frame_periodicity_sec;
% spectr = fftshift ( ((fft(slow_fast_frames .*  repmat(fft_windw,1,Number_of_Frames)    ,nfft,1))) );
% df = Sample_Rate_Hz/nfft;
% f =  df* (-nfft/2:1:nfft/2 - 1); 
% d_dist  = df/Slope_Hz* lightSpeed/2;
% dist = f/Slope_Hz * lightSpeed/2;
% figure, imagesc('XData',slow_time,'YData',dist, 'CData', db( abs (spectr).^2)); colormap(jet); colorbar ; title('base band spectrogram')
% caxis([-70 +  max(db( abs (spectr(:)).^2))        max(db( abs (spectr(:)).^2)) ]  );
% xlabel('slow time [sec]')
% ylabel('Dist[m] ')
% xlim([slow_time(1)  slow_time(end) ]);
% ylim([dist(1)  dist(end) ]);

h = figure; 
set(0,'Units','pixels') 
scnsize = get(0,'ScreenSize');
set(h,'units','characters','outerposition',[0 0 scnsize(3) scnsize(4)]);

for frameIdx = 1:Number_of_Frames
    for chirpConfigIdx = 1:ChirpConfigs_Per_Chirp
        % 1D range-FFT across the 2nd dimensions (samples)
        radar_data_curr = squeeze(radar_data(:,:,chirpConfigIdx,:,frameIdx));
        radar_data_1dFFT = fft(radar_data_curr.*window_1D,[], 2); 

        % 2D vel-FFT across the 3nd dimensions (chirps)
        radar_data_2dFFT = fft(radar_data_1dFFT.*window_2D,[], 3); 

        % radar_data_range_vel_chan_1 has the dimensions (range x vel x frames)
        radar_data_range_vel_chan_1 = squeeze(abs(radar_data_2dFFT(1,:,:).^2)); 

        % A simple plot 2D range - vel plot of the first channel
        surf(vel_axis, range_axis, 10*log10(squeeze(fftshift(radar_data_range_vel_chan_1,2))));
        ylabel('range (meters)'); 
        xlabel('velocity (meters/sec)'); 
        zlabel('FFT Output (dB)');
        view([25, 35]); 
        
        pause(0.1);
    end
end
end