Lesson 46: Spike filter
このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
One bad reading
- Real sensors are not perfect. A loose wire or a flash of light can give one reading that is far too big: a spike.
- The average of five readings is pulled up by one spike.
- The median, the middle value after sorting, does not care about one spike.
Words for this lesson
| Word | Meaning |
|---|---|
| array | a list of values with one name: r[0] to r[4] |
| sort | put in order, smallest first |
| median | the middle value of a sorted list |
| spike | one reading that is far too high or low |
English
Average or median?
| Readings | Average | Median |
|---|---|---|
| 204 225 215 194 205 | 208 | 205 |
| 204 225 1003 194 215 | 368 | 215 |
- One spike moved the average by 160, but the median by only 10.
English
An array in C++
void setup() {
Serial.begin(9600);
int r[5] = {30, 10, 50, 20, 40};
int temp = r[0];
r[0] = r[1];
r[1] = temp;
Serial.println(r[0]);
}
void loop() {
}
- The Monitor shows 10: the first two values were swapped. In C++ the first element is
r[0].
English
In the exam
- 0478/22/F/M/24 Q3(b): write an algorithm to sort an array into ascending order. The mark scheme's points are all in your sort: an outer and an inner loop, comparing neighbours, swapping with a temporary variable, and the passes getting shorter. In Cambridge pseudocode the array would start at index 1, not 0.
難易度 4/5 · 280 コイン + 今日初回達成時の小ボーナス
A sensor on A0 sometimes gives a wrong reading, a spike. The program reads A0 five times into the array r. Sort the array with a bubble sort, smallest first, and print Sorted: and the five values. Watch where the spike ends up.
実行ボタンをクリックして出力を確認してください。
難易度 4/5 · 280 コイン + 今日初回達成時の小ボーナス
The program prints the average of the five readings: the spike pulls it far up. Sort the array, then print Median: and the middle value, r[2]. The median is not moved by one wrong reading.
実行ボタンをクリックして出力を確認してください。