%% WIRELESS MODEM DATA
% A short script to load and process signal.txt files generated by
% the wireless modem web scraper python script.

%% LOAD DATA

%% Initialize variables.
filename = './ejm/signal.txt';

%% Format string for each line of text:
%   column1: datetimes (%{yyyy-MM-dd HH:mm:ss}D)
%	column2: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%19{yyyy-MM-dd HH:mm:ss}D%f%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', ',', 'WhiteSpace', '', 'EmptyValue' ,NaN, 'ReturnOnError', false);

%% Close the text file.
fclose(fileID);

%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.

%% Allocate imported array to column variable names
DateTime = dataArray{:, 1};
SignalStrength = dataArray{:, 2};

% For code requiring serial dates (datenum) instead of datetime, uncomment
% the following line(s) below to return the imported dates as datenum(s).

% DateTime=datenum(DateTime);


%% Clear temporary variables
clearvars filename formatSpec fileID dataArray ans;

time = datenum(DateTime);
time = time - time(1);
time = time * 24 * 3600; %in seconds
close all;
subplot(311);
plot(DateTime,SignalStrength,'.')
title('Wireless Modem Signal Data From 3/4/2016 to 3/7/2016');
xlabel('Time');
ylabel('Signal Strength (dB)');

%% Determine the byte gaps

% First assume that we sample at 1 hz, so a gap of 1 or less is good. 
byteD = diff(time);
idx = find(byteD == 1);
byteD(idx) = 0;
timeBD = DateTime(1:end-1);

%determine only the downtime
idx = find(byteD ~= 0);
downtime = byteD(idx);
timeDT = timeBD(idx);

%plot discontinuity record.
subplot(312);
plot(timeBD,byteD,'.-');
xlabel('Time');
ylabel('Byte Discontinuity (s)');

%plot dicontinuity distribution
subplot(313);
histogram(downtime, 20);
xlim([0 20])
xlabel('Downtime Amount (s)');
ylabel('Occurrences');



