% sample_and_distance_rate_histogram.m
clc
clear

% load variables for given day
load 2014-03-24/monday_24_03.mat;

% gps coordinates of Vienna north-east corner
ne_x = 48.292079905275806;
ne_y = 16.56538598632801;
% gps coordinates of Vienna south-west corner
sw_x = 48.09216946074534;
sw_y = 16.195977050781266;

% define needed variables
max_d = 0;
max_t = 0;
distance = zeros(1,1500);
times = zeros(1,300);

% loop through the whole data set for the given day
for i = 1:length(m_id)
    
    % check whether the vehicle's gps coordinates are within Vienna region
    if (m_x(i) >= sw_y && m_x(i) <= ne_y) && (m_y(i) >= sw_x && m_y(i) <= ne_x)
        
        % find the maximum value of the distance rate
        if m_meter(i) > max_d
            max_d = m_meter(i);
        end
        
        % find the maximum value of the sample rate
        if m_int(i) > max_t
            max_t = m_meter(i);
        end

        % count the number of records for the given distance
        if m_meter(i) > 1499
            distance(1500) = distance(1200) + 1;
        else
            distance(m_meter(i)+1) = distance(m_meter(i)+1) + 1;
        end
        
        % count the number of intervals
        if m_int(i) > 299
            times(300) = times(300) + 1;
        else
            times(m_int(i)) = times(m_int(i)) + 1;
        end
        
    end  
    
end

% display distance histogram
figure
subplot(1,2,1)
bar(1:1500,distance)
xlabel('Distance (meters)');
ylabel('Frequency');
set(gca,'XTick',[0:100:1500]);
set(gca,'YTickMode','manual');
set(gca,'YTickLabel',num2str(get(gca,'YTick')'));
set(gca,'XTickLabel',{'0', '100', '200', '300', '400', '500', '600', '700', '800', '900', '1000', '1100', '1200', '1300', '1400', '>1500'});
set(gca,'XLim',[0 1550]);

% display time interval histogram
hold on
subplot(1,2,2)
bar(1:300,times)
xlabel('Time interval (seconds)');
ylabel('Frequency');
set(gca,'XTick',[0:30:300]);
set(gca,'YTickMode','manual');
set(gca,'YTickLabel',num2str(get(gca,'YTick')'));
set(gca,'XTickLabel',{'0', '30', '60', '90', '120', '150', '180', '210', '240', '270', '>300'});
set(gca,'XLim',[0 310]);
