% avg_velocity.m
clc
clear

% load variables for Monday, 24 March
load 2014-03-24/monday_24_03.mat;

% define time slots for velocity values
speed_val = 0;
speed_num = 0;
speeds = [];

% 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 counters
j = 0;
p = 0;
indx = 0;

% 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)
        
        % count the number of records left
        j = j + 1;
        
        % filter out velocities above 150 km/hod
        if m_speed(i) <= 150
            
            % count the number of records left
            p = p + 1;
            
            % get hours
            hours = str2num(substring(m_time{i},11,13));
            % get minutes
            mins = str2num(substring(m_time{i},14,16));
            % get seconds
            secs = str2num(substring(m_time{i},17,19));
            
            time = (hours*60*60) + (mins*60) + secs;
            
            speed_val = speed_val + m_speed(i);
            speed_num = speed_num + 1;
            
            % calculate mean speed for each 30-minute slot
            if mod(time,450) == 0 && i > (indx+100)
                
                % calculate mean speed for given time slot
                speeds(end+1) = speed_val / speed_num;
                
                % reset values
                speed_val = 0;
                speed_num = 0;
                indx = i;
                
            end
            
        end
        
    end
    
end


