protected void Load(Waveform wav,float scale=1.0f) { dataPoints = new List<VitalDataPoint>(); VitalDataPoint newData = new VitalDataPoint(); length = wav.samples; dataMax = 0; dataMin=9999; // For each character in the file for (int i = 0; i < length; i++) { newData = new VitalDataPoint(); newData.time = i; newData.point = wav.data[i]*scale; if (newData.point>dataMax) dataMax=newData.point; if (newData.point<dataMin) dataMin=newData.point; dataPoints.Add(newData); } dataPoints.TrimExcess(); lastPoint = dataPoints[0]; nextPoint = dataPoints[0]; }
// legacy load from old integer text files protected void Load() { dataPoints = new List<VitalDataPoint>(); string text = dataFile[dataIndex].dataFile.text; string temp = ""; int textLength = text.Length; int step = 0; float tempValue = 0; VitalDataPoint newData = new VitalDataPoint(); bool ranges = true; float timePast = -1; int numSamples=1; float sum = 0; dataMax = 0; dataMin = 999999; // For each character in the file for (int i = 0; i < textLength; i++) { // Check if within normal character range if (text[i] >= 0x21 && text[i] <= 0x7e) { // Add to temp string temp = temp.Insert(temp.Length, new String(text[i], 1)); } // If special character found and temp has data, convert to float and assign to proper variable else if (temp.Length != 0) { tempValue = Convert.ToSingle(temp); temp = ""; switch (step) { case 0: { // Grab time newData = new VitalDataPoint(); newData.time = tempValue; //newData.timeText = tempValue.ToString(); step++; } break; case 1: { // Grab heartrate //newData.values.Add(tempValue); //newData.values.TrimExcess(); newData.point = tempValue; if (tempValue < dataMin) dataMin=tempValue; if (ranges) { length = newData.time; // dataMax = (int)newData.point; // dataMin=dataMax; ranges = false; } else { if (newData.point > dataMax) dataMax=newData.point; if (newData.point < dataMin) dataMin=newData.point; //These data contain multiple y values per x, reduce to single average value // if time value has changed, add the point, otherwise keep an average if (newData.time > timePast){ VitalDataPoint avgData = new VitalDataPoint(); avgData.time = timePast; avgData.point = sum/(float)numSamples; if (avgData.time >=0) dataPoints.Add(avgData); timePast = newData.time; sum=0; numSamples=0; } sum+=newData.point; numSamples++; } step = 0; } break; } } } dataPoints.TrimExcess(); }
void UpdatePoints() { // Modify timer based on Beats-per-minute and ratio of length of data to average beats-per-minute // timer += Time.deltaTime * bpmMod * (length * (normal / 60f)); // scan thru all the samples up to present time, and choose the max or min of the curve over the interval. // this should really only be done when a point is requested, so we determine from all the sampled points between updates // this algorithm is coded so we don't miss extremes of the curve. float sumValue = 0; int numSamples=0; int maxIndex = nextIndex; int minIndex = nextIndex; while (true) { if (nextIndex <= timer) { // Save last point //lastPoint = dataPoints[nextIndex]; //hitPoints.Add(dataPoints[nextIndex]); nextIndex++; if (nextIndex >= dataPoints.Count) { nextIndex = 0; timer = 0; graph.CheckMode(); } sumValue+=dataPoints[nextIndex].point; numSamples++; if (dataPoints[nextIndex].point > dataPoints[maxIndex].point) maxIndex = nextIndex; if (dataPoints[nextIndex].point < dataPoints[minIndex].point) minIndex = nextIndex; } else break; } if ( sumValue/numSamples > lastPoint.point ) nextPointIndex = maxIndex; else nextPointIndex = minIndex; nextPoint = dataPoints[nextPointIndex]; }
public VitalDataPoint GetNextPoint() { lastPoint = nextPoint; nextPointIndex=-1; // causes to recheck increasing/decreasing in update loop. return nextPoint; }
// we need to keep a historic time value, and apply the current bpmMod to the interval since the last sample public void Resample(float time) { float deltaTime = time-prev_sampleTime; prev_sampleTime = time; timer += deltaTime * bpmMod * (length * ((float)normal / 60.0f)); CheckMarker (); nextIndex = (int)timer;//(int)timer; while (nextIndex >= (int)length && length != 0){ nextIndex -= (int)length; timer=nextIndex; graph.CheckMode(); graph.DoCallback("EOF"); } if (dataPoints != null && nextIndex >=0 && nextIndex < dataPoints.Count) nextPoint = dataPoints[nextIndex]; //UpdatePoints (); }