%% Extract and Concatenate Regions of Interest From Data Set
% Consider a set of temperature data collected by a thermometer inside an office building for four months. The device takes a reading every half hour. The sample rate is thus 48 measurements/day. Convert the temperature to degrees Celsius.

load officetemp

tempC = (temp-32)*5/9;

fs = 48;

% Create region-of-interest (ROI) limits that correspond to five random two-week periods separated by at least 24 hours. Temperature readings on these days are audited.

r = 5;
w = 14*fs;
s = 1*fs;

hq = histcounts(randi(r+1,1,length(tempC)-r*w-(r-1)*s),(1:r+2)-1/2);
t = (1 + (0:r-1)*(w+s) + cumsum(hq(1:r)))';

roilims = [t t+w-1];

% Extract the regions of interest. Compute the mean temperature of each audited region of interest and display the values.

sigroi = extractsigroi(tempC,roilims);

cellfun(@mean,sigroi)'

% Extract the regions of interest again but now concatenate the samples into a single vector. Compute the mean temperature across the audited regions.

sigroic = extractsigroi(tempC,roilims,true);

avgTFc = mean(sigroic)

% Convert the ROI limits to a binary sequence and create a mask. Express time in weeks.

m = sigroi2binmask(roilims,length(tempC));

msk = signalMask(m,'SampleRate',fs*7,'Categories',"Audit");

% Plot the data and visualize the regions of interest with rectangular patches.

plotsigroi(msk,tempC,true)
xlabel('Time (weeks)')
ylabel('Temperature ( {}^\circC )')

%
% Copyright 2020 The MathWorks, Inc.