-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensorproject.html
More file actions
235 lines (215 loc) · 8.2 KB
/
Copy pathsensorproject.html
File metadata and controls
235 lines (215 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Improving Sensor Accuracy with Real-Time Digital Filtering</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Improving Sensor Accuracy with Real-Time Digital Filtering</h1>
<nav>
<a href="index.html">Home</a> |
<a href="about.html">About</a>
</nav>
</header>
<main>
<section>
<h2>Objectives</h2>
<p>
Low-cost sensors like the HC-SR04 ultrasonic sensor are accessible and easy to use,
but often their raw readings are prone to noise and spikes/outliers.
Taking raw, unfiltered signals from these devices can have negative consequences
when used in larger systems.
</p>
<p>
The purpose of this project was to create a modular set of classes for a few digital filters
that can be reused in future projects.
These were a simple moving average (SMA), exponential moving average (EMA), and median filter.
To demonstrate these filters and check the classes are working as intended, I aimed to improve
the accuracy of an HC-SR04 ultrasound sensor.
</p>
</section>
<section>
<h2>Implementation</h2>
<p>
The hardware used was minimal, just an HC-SR04 ultrasound sensor and an Arduino Nano.
Separate classes were created for each filter.
</p>
<p>
To show the filters in action, I created a Python script to plot the filtered and unfiltered
data in real time and to calculate metrics such as SNR and standard deviation. An object was
placed at a set distance, and the raw unfiltered signal was analysed. Then I added each of
the filters in turn.
</p>
</section>
<section>
<h2>Filters</h2>
<h3>Simple Moving Average (SMA)</h3>
<p>
This filter smooths data by averaging a set number of previous measurements within a defined
window size. This helps with noise reduction and is simple to implement. However, large
window sizes can introduce significant delay to the signal.
</p>
<img src="images/sensorproject/SMAk15.png" alt="SMA filter results" class="result-img">
<table class="results-table">
<thead>
<tr>
<th>Metric</th>
<th>Unfiltered</th>
<th>k = 5</th>
<th>k = 10</th>
<th>k = 15</th>
</tr>
</thead>
<tbody>
<tr>
<td>Standard Deviation (cm)</td>
<td>0.05</td>
<td>0.01</td>
<td>0.01</td>
<td>0.01</td>
</tr>
<tr>
<td>Peak-to-Peak (cm)</td>
<td>0.12</td>
<td>0.05</td>
<td>0.03</td>
<td>0.03</td>
</tr>
<tr>
<td>SNR</td>
<td>139</td>
<td>881</td>
<td>1054</td>
<td>1078</td>
</tr>
</tbody>
</table>
<p>
The SMA filter clearly was effective in noise reduction, the standard deviation of the signal fell
from 0.05 cm to 0.01 cm, and the signal-to-noise ratio improved nearly 8x.
The SMA seems optimal for this task, despite its simple implementation.
</p>
<h3>Exponential Moving Average (EMA)</h3>
<p>
This filter smooths data by applying a weighted average where recent measurements are given
more weighting than older ones. This weighting is controlled by a smoothing parameter alpha.
Small alpha produces stronger smoothing but more lag, large alpha means less smoothing but
less lag.
</p>
<img src="images/sensorproject/EMAa0.2.png" alt="EMA filter results" class="result-img">
<table class="results-table">
<thead>
<tr>
<th>Metric</th>
<th>Unfiltered</th>
<th>alpha = 0.2</th>
<th>alpha = 0.5</th>
<th>alpha = 0.8</th>
</tr>
</thead>
<tbody>
<tr>
<td>Standard Deviation (cm)</td>
<td>0.05</td>
<td>0.01</td>
<td>0.02</td>
<td>0.04</td>
</tr>
<tr>
<td>Peak-to-Peak (cm)</td>
<td>0.12</td>
<td>0.04</td>
<td>0.08</td>
<td>0.11</td>
</tr>
<tr>
<td>SNR</td>
<td>139</td>
<td>814</td>
<td>345</td>
<td>195</td>
</tr>
</tbody>
</table>
<p>
The EMA filter was also effective in noise reduction, and the difference in effect for different alpha values is clear.
For example with alpha = 0.8, the SNR was only improved by 40%, but with a much lower alpha of 0.2 there is more smoothing
and the SNR improves nearly 6x.
Tuning is needed to find the best value of alpha that doesnt introduce too much lag to the system.
</p>
<h3>Median Filter</h3>
<p>
This filter smooths data by sorting a window of n recent measurements and taking the median
value as the output. It is highly effective at rejecting outliers, but large window sizes (n) can cause delay in the response.
</p>
<img src="images/sensorproject/median13.png" alt="Median filter results" class="result-img">
<p>
The median filter is best for outlier rejection, making it suitable for sensors prone to sudden spikes.
However it didn't improve the SNR as much as the SMA or EMA.
Maybe a different demonstration/experiment would be better suited to show this filter.
</p>
<table class="results-table">
<thead>
<tr>
<th>Metric</th>
<th>Unfiltered</th>
<th>n = 5</th>
<th>n = 9</th>
<th>n = 13</th>
</tr>
</thead>
<tbody>
<tr>
<td>Standard Deviation (cm)</td>
<td>0.05</td>
<td>0.03</td>
<td>0.04</td>
<td>0.02</td>
</tr>
<tr>
<td>Peak-to-Peak (cm)</td>
<td>0.12</td>
<td>0.12</td>
<td>0.12</td>
<td>0.11</td>
</tr>
<tr>
<td>SNR</td>
<td>139</td>
<td>274</td>
<td>330</td>
<td>399</td>
</tr>
</tbody>
</table>
</section>
<section>
<h2>Conclusion and Future Work</h2>
<p>
All three filters significantly improved measurement stability.
Overall I think the best way to effectively process these signals would be to pass them through the median filter first,
in order to reject outliers/sudden disturbances, and then apply a Simple Moving Average, to smooth out noise. As these filters
are lightweight and not computationally intensive, this would work well in embedded systems applications.
Future work on this project will involve creating more advanced filters, such as a Kalman filter,
testing these filters with different sensors, and applying them in a closed-loop control system.
</p>
<p>
</p>
</section>
<section>
<h2>Source Code</h2>
<p>
The full implementation, including Arduino classes and Python script,
is on the GitHub repository below:
</p>
<a href="https://github.com/jamieashton22/ultrasonic-filtering" target="_blank">View on GitHub</a>
</section>
</main>
<footer>
<p>September 2025 Jamie Ashton</p>
</footer>
</body>
</html>