# Smoothing

### From ComputingForScientists

## Contents |

# 1. Algorithms

## 1.1. Average over a time interval

Given a series of measurements, we are often only interested in visualizing the average, maximum, and minimum over a given time range. For example, suppose that you recorded the wind speed every six hours over a period of 10 days, but were only interested in looking at the daily average, daily maximum, and daily minimum speed.

To compute the daily mean, we need to iterate over an array until we have 40 values and then compute the mean. The following program will compute the mean over the first day:

clear; A = [1:40]; % Create an test array with 10 days of values. As = 0; % A sum variable. for i = [1:4] % Iterate over first four points As = A(i) + As; % Add to previous sum new value. end Am = As/4 % Convert the sum to an average.

To compute the next sum, we could write

As = 0; % A sum variable. for i = [5:8] % Iterate over first 1440 points. As = A(i) + As; % Add to previous sum new value. end Am = As/4 % Convert the sum to an average.

We could repeat the above eight more times to get all 10 daily values. Here is a method:

clear; A = [1:40]; % Create an test array with 10 days of values. As = 0; k = 1; As = 0; % Will be the running sum of values. for i = [1:40] As = A(i) + As; if (mod(i,4) == 0) % If i/4 has a remainder of 0, mod(i,4) == 0 is true. Am(k) = As/4; % Convert sum into an array. Place result in new array Am. As = 0; % Re-set the running sum to 0 k = k+1; % Increment the index for the mean array Am end end

The above is a basic template for computing the average over a time interval in the case that the time intervals are equally spaced. There are easier ways to do it in MATLAB:

clear; A = [1:40]; Ar = reshape(A,4,10); % Reshape A into a 4 row and 10 column matrix. Am = mean(Ar,1) % Average the columns of Ar.

In this course, I'll emphasize the `for`

loop method, as not all programming languages that you encounter will have a `reshape`

function or a `mean`

function that can take the average of columns of a matrix.

## 1.2. Smoothing

# 2. Problems

## 2.1. Averaging

Write a program using a `for`

loop that computes the average of the array `A=[1,3,4,9,10,22]`

## 2.2. Averaging

Write a program (without using the function `mean`

) that computes the average of the **rows** of a matrix and places the values in an array. For example, if the starting matrix is

1 2 3 4 1 2 3 4 1 2 3 4

the average array would be

2.5 2.5 2.5 2.5

Answer |
---|

A = [1 2 3 4; 1 2 3 4;1 2 3 4]; % Solution for averaging first row. s = 0; for z = 1:4 s = s + A(1,z) end Am(1) = s/4; % Need to repeat above once for each or the % 3 rows for i = 1:3 s = 0; for z = 1:4 s = s + A(i,z) end Am(i) = s/4; end % Note that the above could have been done % using Am = mean(A,2) |

## 2.3. Averaging

Write a program that computes the average of the columns of a matrix and places the values in an array. For example, if the starting matrix is

1 2 3 4 1 2 3 4 1 2 3 4

the average array would be

1 2 3 4

## 2.4. Averaging

Write a MATLAB program using a `for`

loop that compute the average of an array of arbitrary size and excludes values of `99999`

from the computation of the average. For example, the average of `[0,99999,1]`

will be `(0+1)/2 = 0.5`

.

## 2.5. Smoothing

### 2.5.1. Part I

Write a program that computes the 3-point "box-car" average of an array. For example, if the original array is

Ao = [1,2,3, 4,5,6]

the 3-point box-car average is

Af = [ 2 , 5]

where the first element in `Af`

is the average of the first three points in `Ao`

and the second element of `Af`

is the average of the next three elements of `Ao`

. Your program should compute the correct result for `Ao`

of any length that is a multiple of `3`

.

Answer |
---|

"Partial credit" answer using two clear; Ao = [1,2,3, 4,5,6]; s = 0; for i = 1:3 s = s+Ao(i); end Af(1) = s/3; s = 0; for i = 4:6 s = s+Ao(i); end Af(2) = s/3; Af Using one clear; Ao = [1,2,3, 4,5,6] s = 0; c = 1; for i = 1:length(Ao) s = s+Ao(i); if (mod(i,3) == 0) Af(c) = s/3; s = 0; c = c+1; end end Af Without using a clear; Ao = [1,2,3, 4,5,6] Af = reshape(Ao,3,length(Ao)/3); % Create a matrix with three rows and length(Ao)/3 columns. Af = mean(Af) |

### 2.5.2. Part II

Write a program that computes the 3-point "box-car" maximum of an array. For example, if the original array is

Ao = [1,3,3, 4,6,2]

the 3-point box-car maximum is

Af = [ 3 , 6]

where the first element in `Af`

is the maximum of the first three points in `Ao`

and the second element of `Af`

is the maximum of the next three elements of `Ao`

. Your program should compute the correct result for `Ao`

of any length that is a multiple of `3`

.

## 2.6. Smoothing

To smooth values in an array, a "running average" can be used; each value is replaced by the average of itself and a certain number of points before and after it. For example, given the array

A = [1,2,3,4,5,6,7]

the 3-point running average of A is

Aave(1) = NaN Aave(2) = (A(1) + A(2) + A(3))/3 Aave(3) = (A(2) + A(3) + A(4))/3 Aave(4) = (A(3) + A(4) + A(5))/3 Aave(5) = (A(4) + A(5) + A(6))/3 Aave(6) = (A(5) + A(6) + A(7))/3 Aave(7) = NaN

where NaN values are used for positions that do not have enough points.

Write a program that computes the 5-point running average of an array. Your code will first be tested first with the array

A = [1:10]

and then arrays of arbitrary length (greater than 5).

Answer |
---|

%Aave(1) = NaN %Aave(2) = (A(1) + A(2) + A(3))/3 %Aave(3) = (A(2) + A(3) + A(4))/3 %Aave(4) = (A(3) + A(4) + A(5))/3 %Aave(5) = (A(4) + A(5) + A(6))/3 %Aave(6) = (A(5) + A(6) + A(7))/3 %Aave(7) = NaN clear A = [1:7]; w = 3; A(1) = NaN; A(length(A)) = NaN; for i = 2:length(A)-1 Aave(i) = (A(i-1) + A(i) + A(i+1))/w; end A Aave %w = 5; %Aave(1) = NaN %Aave(2) = NaN %Aave(3) = (A(1) + A(2) + A(3) + A(4) + A(5))/w %Aave(4) = (A(2) + A(3) + A(4) + A(5) + A(6))/w %Aave(5) = (A(3) + A(4) + A(5) + A(6) + A(7))/w %Aave(6) = NaN %Aave(7) = NaN clear A = [1:7]; w = 5; Aave(1) = NaN; Aave(2) = NaN; Aave(length(A)-1) = NaN; Aave(length(A)) = NaN; % Better approach: replace above four lines with % Aave = NaN*ones(size(A)); for i = 3:length(A)-2 Aave(i) = (A(i-2) + A(i-1) + A(i) + A(i+1) + A(i+2))/w; end A Aave |

## 2.7. Average/Min/Max

Create an array with 100 values using `A = [1:100]`

.

- Write a program that create a new array with values that are the average of each block of 10 data values.
- Write a program that create a new array with values that are the average of each block of 11 data values.
- Write a program that create a new array with values that are the maximum of each block of 10 data values.
- Write a program that create a new array with values that are the maximum of each block of 11 data values.
- Write a program that create a new array with values that are the minimum of each block of 10 data values.
- Write a program that create a new array with values that are the minimum of each block of 11 data values.