# Smoothing

Jump to: navigation, search

# 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.

# 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


## 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.

### 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).

## 2.7. Average/Min/Max

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

1. Write a program that create a new array with values that are the average of each block of 10 data values.
2. Write a program that create a new array with values that are the average of each block of 11 data values.
3. Write a program that create a new array with values that are the maximum of each block of 10 data values.
4. Write a program that create a new array with values that are the maximum of each block of 11 data values.
5. Write a program that create a new array with values that are the minimum of each block of 10 data values.
6. Write a program that create a new array with values that are the minimum of each block of 11 data values.