I am working on some kind of project which involves simulating an ECG (electrocardiogram). I do it by generating some random numbers between an interval and sending that data to another program analyzing and graphing it. But, the problem is the random number generated between an interval say [a,b] by java code have a lot of variance, meaning the next generated random value differs greatly from the previous number. What I want is a flow of random numbers varying slightly so that graph looks smooth. At this point, the graph generated is very spiky, but I want it to be smooth and varying like a real ECG graph.
Please help me do that.
Here is the code
//setup ECG graph
StdDraw.setXscale(0.0, 100.0);
StdDraw.setYscale(0.0,200.0);
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.setPenRadius(0.0009);
//generate random points between interval of range
int t = 0;
int prevVal = 0;
int nextVal;
while(true){
nextVal = range.getRandomValue();
System.out.println(nextVal);
StdDraw.point(prevVal, nextVal);
StdDraw.line(t-1, prevVal, t, nextVal);
StdDraw.show(100);
prevVal = nextVal;
t = (t+1) % 100;
if(t == 0){
StdDraw.clear();
}
}
Thank You
Why don´t you try modulating your random signal with a sin(t)
function:
long n = 0;
double randomWeight = 0.5;
while(true) {
nextVal = range.getRandomValue();
double temp = AMPLITUDE*(randomWeight*((double)nextVal)+(1.0-randomWeight)*Math.sin(2.0*Math.PI*((double)n)*WIDTH_FACTOR));
nextVal = (long)temp;
n++;
System.out.println(nextVal);
StdDraw.point(prevVal, nextVal);
StdDraw.line(t-1, prevVal, t, nextVal);
StdDraw.show(100);
prevVal = nextVal;
t = (t+1) % 100;
if(t == 0){
StdDraw.clear();
}
The idea is to generate a sinusoidal wave function and add some random noise to it. You may want to add this noise to a square wave instead. I don´t really know very much about how an electrocardigram looks like but I suppose it must be governed by the heartbeat.
EDIT:
I have just checked how these signals look like:
It seems to me, a more accurate model would be given by a relatively flat random signal interrupted by periodic spikes:
long n = 0;
while(true) {
nextVal = range.getRandomValue();
if(n % SPIKE_PERIOD == 0) nextVal = SPIKE_APLITUDE*nextVal;
n++;
System.out.println(nextVal);
StdDraw.point(prevVal, nextVal);
StdDraw.line(t-1, prevVal, t, nextVal);
StdDraw.show(100);
prevVal = nextVal;
t = (t+1) % 100;
if(t == 0){
StdDraw.clear();
}