-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathECGExample.java
More file actions
211 lines (178 loc) · 7.16 KB
/
ECGExample.java
File metadata and controls
211 lines (178 loc) · 7.16 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
/*
* Copyright 2016 AndroidPlot.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.androidplot.demos;
import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import com.androidplot.Plot;
import com.androidplot.util.Redrawer;
import com.androidplot.xy.*;
import java.lang.ref.*;
/**
* An example of a real-time plot displaying an asynchronously updated model of ECG data. There are three
* key items to pay attention to here:
* 1 - The model data is updated independently of all other data via a background thread. This is typical
* of most signal inputs.
*
* 2 - The main render loop is controlled by a separate thread governed by an instance of {@link Redrawer}.
* The alternative is to try synchronously invoking {@link Plot#redraw()} within whatever system is updating
* the model, which would severely degrade performance.
*
* 3 - The plot is set to render using a background thread via config attr in R.layout.ecg_example.xml.
* This ensures that the rest of the app will remain responsive during rendering.
*/
public class ECGExample extends Activity {
private XYPlot plot;
/**
* Uses a separate thread to modulate redraw frequency.
*/
private Redrawer redrawer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ecg_example);
// initialize our XYPlot reference:
plot = (XYPlot) findViewById(R.id.plot);
ECGModel ecgSeries = new ECGModel(2000, 200);
// add a new series' to the xyplot:
MyFadeFormatter formatter =new MyFadeFormatter(2000);
formatter.setLegendIconEnabled(false);
plot.addSeries(ecgSeries, formatter);
plot.setRangeBoundaries(0, 10, BoundaryMode.FIXED);
plot.setDomainBoundaries(0, 2000, BoundaryMode.FIXED);
// reduce the number of range labels
plot.setLinesPerRangeLabel(3);
// start generating ecg data in the background:
ecgSeries.start(new WeakReference<>(plot.getRenderer(AdvancedLineAndPointRenderer.class)));
// set a redraw rate of 30hz and start immediately:
redrawer = new Redrawer(plot, 30, true);
}
/**
* Special {@link AdvancedLineAndPointRenderer.Formatter} that draws a line
* that fades over time. Designed to be used in conjunction with a circular buffer model.
*/
public static class MyFadeFormatter extends AdvancedLineAndPointRenderer.Formatter {
private int trailSize;
MyFadeFormatter(int trailSize) {
this.trailSize = trailSize;
}
@Override
public Paint getLinePaint(int thisIndex, int latestIndex, int seriesSize) {
// offset from the latest index:
int offset;
if(thisIndex > latestIndex) {
offset = latestIndex + (seriesSize - thisIndex);
} else {
offset = latestIndex - thisIndex;
}
float scale = 255f / trailSize;
int alpha = (int) (255 - (offset * scale));
getLinePaint().setAlpha(alpha > 0 ? alpha : 0);
return getLinePaint();
}
}
/**
* Primitive simulation of some kind of signal. For this example,
* we'll pretend its an ecg. This class represents the data as a circular buffer;
* data is added sequentially from left to right. When the end of the buffer is reached,
* i is reset back to 0 and simulated sampling continues.
*/
public static class ECGModel implements XYSeries {
private final Number[] data;
private final long delayMs;
private final int blipInteral;
private final Thread thread;
private boolean keepRunning;
private int latestIndex;
private WeakReference<AdvancedLineAndPointRenderer> rendererRef;
/**
*
* @param size Sample size contained within this model
* @param updateFreqHz Frequency at which new samples are added to the model
*/
ECGModel(int size, int updateFreqHz) {
data = new Number[size];
for(int i = 0; i < data.length; i++) {
data[i] = 0;
}
// translate hz into delay (ms):
delayMs = 1000 / updateFreqHz;
// add 7 "blips" into the signal:
blipInteral = size / 7;
thread = new Thread(new Runnable() {
@Override
public void run() {
try {
while (keepRunning) {
if (latestIndex >= data.length) {
latestIndex = 0;
}
// generate some random data:
if (latestIndex % blipInteral == 0) {
// insert a "blip" to simulate a heartbeat:
data[latestIndex] = (Math.random() * 10) + 3;
} else {
// insert a random sample:
data[latestIndex] = Math.random() * 2;
}
if(latestIndex < data.length - 1) {
// null out the point immediately following i, to disable
// connecting i and i+1 with a line:
data[latestIndex +1] = null;
}
if(rendererRef.get() != null) {
rendererRef.get().setLatestIndex(latestIndex);
Thread.sleep(delayMs);
} else {
keepRunning = false;
}
latestIndex++;
}
} catch (InterruptedException e) {
keepRunning = false;
}
}
});
}
void start(final WeakReference<AdvancedLineAndPointRenderer> rendererRef) {
this.rendererRef = rendererRef;
keepRunning = true;
thread.start();
}
@Override
public int size() {
return data.length;
}
@Override
public Number getX(int index) {
return index;
}
@Override
public Number getY(int index) {
return data[index];
}
@Override
public String getTitle() {
return "Signal";
}
}
@Override
public void onStop() {
super.onStop();
redrawer.finish();
}
}