-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulate_logging.py
351 lines (290 loc) · 13.5 KB
/
simulate_logging.py
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# -*- coding: utf-8 -*-
"""
Experiments in Data Logging
Purpose: Python code to let you experiment with signals and noise to see the effect of various techniques used in data logging.
Originally published on Instructables, this is a minor update to Python 3.6 and a posting here to make the matarial easier to view and download.
Enviroment: Program should run on any OS supporting Python 3.6. This should include Windows, Mac, Linux,
and Raspberry Pi. So far only tested on Windows.
Program Status: Seems to be fine, done. Could be extended, let me know as I use this code in several other projects of mine.
Project Archive at: https://github.com/russ-hensel/AdvDataLogging
Instructables Link: Experiments in Advanced Data Logging ( Using Python ): 11 Steps
https://www.instructables.com/id/Experiments-in-Advanced-Data-Logging-Using-Python-/
"""
import matplotlib.pyplot as plt # plotting stuff
import moving_average
import sensor_simulator as ss
import data_trigger as dt
import line_style
"""
LoggingSim in simulate_logging.py
"""
# ========================== Begin Class ================================
class LoggingSim:
"""
Show graph for logging with simulated noisy and processed data.
"""
def __init__( self, ):
self.version = "2016 01 30.1"
self.plot_title = "plot_title"
self.lines_ix = 0 # lines in plot
self.lineStyle = line_style.LineStyle()
# ------------------------------------------------
def add_sensor_data(self, name, amplitude, noise_amp, delta_t, max_t, run_ave, trigger_value ):
"""
Simulate a sensor and collect data with various parameters
amplitude, noise_amp, delta_t, max_t, run_ave,
trigger_value sets the trigger level for time, data not saved untill trigger value is reached
"""
# make a simulated sensor
sensor = ss.SensorSim( amplitude, noise_amp, delta_t = delta_t )
data0 = []
data1 = []
x0 = 0.
if trigger_value > 0:
trigger = dt.DataTrigger( trigger_value )
if run_ave > 0:
averager = moving_average.MovingAverage( run_ave)
while x0 < max_t:
x0, x1 = sensor.get_next_sample()
if run_ave > 0:
x1 = averager.nextVal( x1 )
add_flag = True
if trigger_value > 0:
add_flag = trigger.need_save( x0 )
if add_flag:
data0.append( x0 )
data1.append( x1 )
self.add_line( name, data0, data1 )
# ------------------------------------------------
def start_plot( self, plot_title = "start_plot" ):
"""
start a plot process: initialize the graph
no data yet, do only once for each graph
return nothing but change object state
"""
self.lineStyle.reset()
self.plot_title = plot_title
self.lines_ix = 0
self.fig = plt.figure( figsize=(12, 12) )
self.axes = self.fig.add_subplot(111) # to have multiple lines in the plot
self.axes.set_xlabel( 'Time' )
self.axes.set_ylabel( 'Signal' )
self.axes.set_title( self.plot_title )
return
# ------------------------------------------------
def add_line( self, name, data0, data1 ):
"""
add a line of data in variables x y to the graph
label ( legend ) alabel
return nothing
"""
line_style = self.lineStyle.get_styles()
self.lines_ix += 1
self.axes.plot( data0, data1, label = name,
linestyle = line_style[0], marker = line_style[2], color = line_style[1] )
self.axes.legend( loc=2 ) # set axes location: 4 is lower left 2 is upper left
return
# ------------------------------------------------
def show_plot( self ): # calling show seems to destroy the plot
"""
show the plot
should supress blank plot
return nothing
"""
if self.lines_ix <= 0:
print( "show_plot() nothing to show" )
else:
plt.show( self.fig )
return
# ======================== begin experiments ==================
# ------------------------------------------------
def experiment_with_sample_rates( self ):
print( """
Experiment with Sample Rates
Looking at different sample rates by changing delta T
""" )
self.start_plot( plot_title = "Experiment with Sample Rates - Part 1/3: Delta T = 1.0" )
self.add_sensor_data( name = "dt = 1.",
amplitude = 1.,
noise_amp = .0,
delta_t = 1.,
max_t = 10.,
run_ave = 0,
trigger_value = 0 )
self.show_plot( )
# ------------------------------------------------
self.start_plot( plot_title = "Experiment with Sample Rates - Part 2/3: Delta T = 0.1" )
self.add_sensor_data( name = "dt = 1.",
amplitude = 1.,
noise_amp = .0,
delta_t = 0.1,
max_t = 10.,
run_ave = 0,
trigger_value = 0 )
self.show_plot( )
# ------------------------------------------------
self.start_plot( plot_title = "Experiment with Sample Rates - Part 3/3: Delta T = 0.01" )
self.add_sensor_data( name = "dt = 1.",
amplitude = 1.,
noise_amp = .0,
delta_t = 0.01,
max_t = 10.,
run_ave = 0,
trigger_value = 0 )
self.show_plot( )
# ------------------------------------------------
def experiment_showing_noise( self ):
print( """
Experiment showing noise
Looking at different amounts of noise by changing the noise amplitude.
""" )
self.start_plot( plot_title = "Experiment Showing Noise" )
self.add_sensor_data( name = "noise = 0.0",
amplitude = 1.,
noise_amp = .0,
delta_t = .1,
max_t = 10.,
run_ave = 0,
trigger_value = 0 )
self.add_sensor_data( name = "noise = 0.1",
amplitude = 1.,
noise_amp = .1,
delta_t = .1,
max_t = 10.,
run_ave = 0,
trigger_value = 0 )
self.add_sensor_data( name = "noise = 1.0",
amplitude = 1.,
noise_amp = 1.,
delta_t = .1,
max_t = 10.,
run_ave = 0,
trigger_value = 0 )
self.show_plot( )
# ------------------------------------------------
def experiment_with_moving_average( self ):
print( """
Experiment with MovingAverage
Looking at different MovingAverage by changing the length.
All have the same noise.
""" )
# ------------------------------------------------
self.start_plot( plot_title = "Experiment with MovingAverage - Part 1/2: No Moving Average" )
self.add_sensor_data( name = "ave len=0",
amplitude = 1.,
noise_amp = .1,
delta_t = .1,
max_t = 10.,
run_ave = 0,
trigger_value = 0 )
self.show_plot( )
self.start_plot( plot_title = "Experiment with MovingAverage - Part 2/2: Len 8 and 32 Moving Average" )
self.add_sensor_data( name = "ave len=8",
amplitude = 1.,
noise_amp = .1,
delta_t = .1,
max_t = 10.,
run_ave = 8,
trigger_value = 0 )
self.add_sensor_data( name = "ave len=32",
amplitude = 1.,
noise_amp = .1,
delta_t = .1,
max_t = 10.,
run_ave = 32,
trigger_value = 0 )
self.show_plot( )
# ------------------------------------------------
def experiment_with_moving_average_and_sample_rate( self ):
print( """
Experiment with Moving Average and Sample Rate,
dt,
run average being varied
""" )
# ------------------------------------------------
self.start_plot( plot_title = "Experiment with Moving Average and Sample Rate" )
self.add_sensor_data( name = "dt=.1 ra=0 trig=0",
amplitude = 1.,
noise_amp = .1,
delta_t = .1,
max_t = 10.,
run_ave = 0,
trigger_value = 0 )
self.add_sensor_data( name = "dt=.1 ra=10 trig=0",
amplitude = 1.,
noise_amp = .1,
delta_t = .1,
max_t = 10.,
run_ave = 10,
trigger_value = 0 )
self.add_sensor_data( name = "dt=.01 ra=100 trig=0",
amplitude = 1.,
noise_amp = .1,
delta_t = .01,
max_t = 10.,
run_ave = 100,
trigger_value = 0 )
self.show_plot( )
# ------------------------------------------------
def experiment_with_trigger( self ):
print( """
Experiment with Triggering,
dt,
run average
and trigger all being varied
""" )
# ------------------------------------------------
self.start_plot( plot_title = "An Experiment with Trigger 1/1 - Triggering On" )
self.add_sensor_data( name = "dt=.1 ra=10, trig =0",
amplitude = 1.,
noise_amp = .1,
delta_t = .1,
max_t = 10.,
run_ave = 10,
trigger_value = 0 )
self.add_sensor_data( name = "dt=.01 ra=100, trig =.1",
amplitude = 1.,
noise_amp = .1,
delta_t = .01,
max_t = 10.,
run_ave = 100,
trigger_value = .1 )
self.show_plot( )
# ------------------------------------------------
def experiment_with_trigger_louder_noise( self ):
print( """
Louder noise than prior experiment
""" )
self.start_plot( plot_title = "An Experiment with Trigger - Louder Noise" )
self.add_sensor_data( name = "...dt=.1 ra=10",
amplitude = 1.,
noise_amp = .5,
delta_t = .1,
max_t = 10.,
run_ave = 10,
trigger_value = 0 )
self.add_sensor_data( name = "..dt=.01 ra=100 tv =.1",
amplitude = 1.,
noise_amp = .5,
delta_t = .01,
max_t = 10.,
run_ave = 100,
trigger_value = .1 )
self.show_plot( )
# ------------------------------------------------
if __name__ == '__main__':
"""
run the app
comment, uncomment the experiments, or create your own.
"""
print( "=========Run LoggingSim===============" )
sim_logging = LoggingSim( )
# comment uncomment to run experiments
sim_logging.experiment_with_sample_rates()
#sim_logging.experiment_showing_noise()
#sim_logging.experiment_with_moving_average()
#sim_logging.experiment_with_moving_average_and_sample_rate( )
#sim_logging.experiment_with_trigger()
#sim_logging.experiment_with_trigger_louder_noise()
print( "---------End LoggingSim----------------" )
# ==================== eof =============================