## ENGG1811 Lab 11: Linear Equations and data processing

### Summary

This lab consists of two parts. Part A is on linear equations. In Part B, you will process some real-life data coming from accelerometers.

### Assessment

Part A is worth 0.5 marks. Part B is worth 1.5 marks. The online assessment is on curve fitting, and can be done at any time.

### Part A: Solving Linear Equations

A separation process accepts a mixture of water, ethanol and methanol in certain proportions, and produces two output streams, each with particular proportions of the component liquids. The input flow of the combined liquids is 100 L/min, and the output flows are the unknowns b and c. The proportions in each stream are shown in the yellow boxes.The output proportions are known, as is the input proportion of water. The proportion of ethanol in the input stream is the third unknown x.

Since the input and output flow of each material must match, we can devise three equations in the three unknowns: 1. Re-arrange these equations so that each is of the form
z1 x + z2 b + z3 c = k
where the z coefficients and k are constants.
2. Express the solution in matrix form A x = b, where
A is the matrix of coefficients,
x is the column vector of unknowns and
b is the column vector of right-hand-side constants.
3. Write a Matlab script (so you can recall how you solved the problem later) that assigns the matrix A and vector b, using multiple lines continued with ... to align the columns of A neatly. Remember that b is a column vector.

Then solve the equations using matrix left division:

```x = A \ b
```
4. Interpret the results. Confirm that x is no greater than 0.5 (otherwise the methanol input proportion would be negative). What do you expect the sum b + c to be? (Think about overall flows if this isn't clear.)

### Part B: A Data Processing Exercise

You might have heard of the recommendation that we should walk at least 10,000 steps per day to stay healthy. Some people use Fitbit, pedometers or smartphone apps to keep track of the number of steps they walk per day. Have you ever wondered how these devices and applications work? In order to count the number of steps, you need both hardware and software. In Fitbit, pedometers and smartphones, there is a common piece of hardware called the accelerometer, which as its name suggests, measures acceleration. The acceleration can tell you a lot. You may recall the following slide from Week 2's lecture: The slide shows acceleration in the vertical axis and time (in minutes) is in the horizontal axis. Note that acceleration has 3 directions (in x-, y- and z-directions) and they are plotted in three different colours in the slide. The acceleration data in this graph was collected by an ENGG1811 lecturer (CTC, if you wish to know) using his smartphone during his commute to work. By processing this data, you can count how many steps he walked during this particular commute.

Note that these acceleration data were collected in a trip consisting of walking, train and bus rides. So the first part of data processing is to identify which sections of data correspond to walking. Once the walking sections have been identified, the second part of the data processing is to count the number of steps in those sections. The process of counting the number of steps is fairly similar to that of counting the number heart beats, which you have done in Week 1. So, in this lab, we won't be counting steps but we will focus on determining which sections of the data correspond to walking.

Fitbit and smartphone apps use fairly sophisticated algorithms for data processing. In this lab, you will use a basic algorithm to decide whether a person is walking in a certain time interval or not. The basic idea is to look at the acceleration over a short time interval. The figure below, which consists of two sub-plots (produced by using the matlab command subplot), shows the acceleration when the person was walking and not walking over a time interval of about 6 seconds. Note that the y-axes of the two sub-plots have the same scale. If the person was not walking (the sub-plot on the right), the acceleration varied around the value of 9. However, when the person was walking, the acceleration varied between 4 and 20. Hence, a way to distinguish between walking and non-walking is to say: walking corresponds to large variation in acceleration and non-walking corresponds to smaller variation of acceleration. There are many ways to measure variation. In this lab, we will use variance. If we calculate of the variance of the walking and non-walking data in the sub-plots below, we get 20.85 and 0.04 respectively. So, one key point that you need to use later on is this: large variation in acceleration means walking and vice versa. You are provided with a matlab mat file named accdata.mat. The file contains a variable matData which is a matrix with 49910 rows and 4 columns.

Let us inspect the contents of the first 8 rows of the matrix matData

```>> matData(1:8,:)

ans =

0    0.7219    3.6775   12.2583
0.0990    1.1169    2.7922    9.5751
0.1990    0.1090    3.3778    7.3958
0.2960    0.1090    3.2144    6.9736
0.3990   -3.9499    3.5277   10.0382
0.5090   -9.6160    3.1463    6.7829
0.6090  -10.1472    0.2724    5.1349
0.7090   -8.8124    0.8853    3.9090```

The first column is time (in seconds); the second, third and fourth columns are respectively the accelerations (in m/s^2) in the x-, y- and z-directions.

The first row of data is

`         0    0.7219    3.6775   12.2583`
which means that at time 0, the accelerations in the x-, y- and z-directions were 0.7219, 3.6775 and 12.2583.

The third row of data is

`   0.1990    0.1090    3.3778    7.3958`

which means that at time 0.1990, the accelerations in the x-, y- and z-directions were 0.1090, 3.3778 and 7.3958.

You can see that the numbers in the first column are 0, 0.0990, 0.1990, 0.2960 and so on, this means that the accelerometer was trying to measure the accelerations in the three directions roughly every 0.1 seconds, or about 10 samples per seconds.

You are asked to do your work in a script so that you can show your tutor later on. We have divided the work into four sub-parts B1, B2, B3 and B4.

### Part B1: Preliminary processing

The first part of your script is shown below. A couple of steps have been done for you and the comments are there to help you to understand what the steps are. The places that say "Your task" contain the work that you have to do. Some further explanation is below the yellow box.

```

% ENGG1811 Lab 11
% Part B: Processing acceleration data
% Constants
THERSHOLD = 0.5; 	  % Threshold to separate walking from non-walking

% Load the data file

% The file contains one variable called matData
% The matrix matData has 49910 rows and 4 columns.           % You should check the size of the matrix to confirm
%
% Column 1: The time at which the acceleration is measured
% Column 2: Acceleration in x-axis
% Column 3: Acceleration in y-axis
% Column 4: Acceleration in z-axis

% Your task: Create a vector vecTime which contains the first column of the
% the matrix matData          % Note that you will only be using the vector vecTime for plotting,           % you won't be using this vector for calculations
vecTime =  % You to complete

% Your task: Create a matrix called matAcc3Axes which contains columns 2-4 of
% the matrix matData
matAcc3Axes =  % You to complete

% Your task: Plot the accelerations in the three axes against time 	  % The plot should use xlabel, ylabel and caption 	  % You can use the plot in the lecture slide shown above as a sample	  % If you do not know how to use xlabel, ylabel, caption, look at the help page           %          % From the beginning of the course, we have stressed that it is always          % good to plot the data to have a look, so that's why we ask you to plot            % Your task: Calculate the total acceleration (See explanation below)          % You can store the result in a vector called vecAccTotal           % Note: You shouldn't use any loops.           % Your task: Plot the total acceleration against time   	  % The plot should use xlabel, ylabel

```

Here we explain the meaning of total acceleration. We assume you have the matrix matAcc3Axes which is a matrix with 3 columns. The three elements at each row of the matrix is the acceleration in the x-, y- and z-directions at a particular time instance. The matrix matAcc3Axes has the form then the total acceleration is the following vector You should use array operations and vectorisation to calculate the total acceleration. No loops should be used.

### Part B2: Determining which segments contain walking data using a (for or while) loop

Let us assume that you store the total acceleration in a vector called vevAccTotal. This vector has 49910 rows. (You may wish to check the size of the vector to confirm.) We will partition the elements in this vector into segments where each segment contains 60 elements. With this partition, we have:

• Segment 1 contains elements 1 to 60
• Segment 2 contains elements 61 to 120
• Segment 3 contains elements 121 to 180, and so on
Given that the vector vecAccTotal has 49910 elements and each segment has 60 elements, it means that there is going to be an incomplete segment at the end. We will simply discard that incomplete segment. We will use numSegments to denote the number of complete segments.

Since each segment contains 60 elements (or data points), and acceleration data were collected once per 0.1 seconds, each segment contains roughly 6 seconds of acceleration data. For each segment, you should compute the variance of the total acceleration in the segment. If the variance is greater than or equal to a threshold (i.e. if the variance is big) then the person was walking during this time segment; otherwise, the person was not walking. The threshold you should use is 0.5 and it is specified in the beginning of the script shown in Part B1.

You should output your answer in a vector with numSegments elements. The k-th element of this vector is 1 if Segment k corresponds to walking; otherwise the k-th element should be 0.

We will use a small example to illustrate the calculations. Let us assume that the transpose of  vevAccTotal is a vector with 14 elements shown below and each segment contains 3 elements. (Note you will use 60 elements per segment for you calculation, but for illustration, we need to use a much smaller number.) The total accelerations in Segment 1 are 2, 20 and 2. The variance of 2, 20 and 2 is 108, which is at or above the threshold of 0.5, so this is walking. Similarly:

• Segment 2: Variance of 10, 10 and 10 is 0. Below threshold 0.5. Not walking.
• Segment 3: Variance of 10, 9 and 10 is 0.333. Below threshold 0.5. Not walking.
• Segment 4: Variance of 3, 18 and 3 is 75. At or above threshold 0.5. Walking.

The output should be in a vector with 4 elements because there are 4 complete segments. The output in this case should be the vector

`[1 0 0 1] `

because Segments 1 and 4 correspond to walking. Note that the elements 6 and 7 are not used because they do not form a complete segment.

For Part B2, you should use an implementation that uses loops.

You will find that, out of a total of 831 complete intervals, 265 of them are walking intervals.

### Part B3: Determining which segments contain walking data WITHOUT using any loops

For Part B3, you should do the calculations stated in Part B2 but your implementation should NOT use any loops.

You will need to use the var function in Matlab to calculate variance. You can look at the documentation on how to use this function.

### Part B4: Comparing the outputs from Parts B2 and B3

The results from Parts B2 and B3 should be identical, and your task is to verify this. The vectors from Parts B2 and B3 are quite long, so visual comparison is tedious. However, in Matlab, you can do this verification with one line of code. We impose the following requirements in how you verify whether the two vectors are identical.
• Your method can use only one line of code
• You cannot use any if-statements
• You cannot use any for- or while-loops
• The line of code should return 1 if the vectors are identical, otherwise it should return 0

You can use any methods you like as long as they meet the above requirements. For this part, you need to come out with two different methods.

Some functions or operators that you may find useful: any, all, sum, ~, ==, ~=, -.