# Threshold and Blank Algorithm

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-15-2010
Threshold and Blank Algorithm
Hey all,

I have an assignment for my biomedical programming class that requires us to calculate the instantaneous heart rate of a given input text file. My professor suggested using a technique called the "Threshold and Blank Algorithm." Could someone point me in the right direction to begin this project. I know how to begin (scanning the file with a while loop, etc.), it's just getting to the guts of the program I'm having trouble with.

Where can I begin?
• 11-15-2010
claudiu
No idea what you are referring to. You need to be specific about what the input file contains and how you need to manipulate that data.
• 11-15-2010
Sorry! Alright. Basically, you are given a set of data points (blood pressure readings as a function of time = beats/minute) and are asked to find the instantaneous heart rate at each beat. This value is given as 60/T where T is the period of the heart beat in seconds and HR is in beats per minute.

So, using some version of the Threshold and Blank algorithm, we are supposed to calculate these instantaneous heart rates. I know how to scan the file for its data points using a while loop/fscanf commands, but I am totally lost on where to begin the analysis using this Threshold and Blank algorithm.
• 11-15-2010
Welcome to the forum, Illiad!

Can you, instead of describing it, show us an example - preferably shows an actual equation or algorithm? Because the T&B algorithm is one I've never heard of, and sounds quite specialized.

Maybe Google or Wikipedia would have something to get you started?

The C stuff we can help you with, but investigating the T&B algo, is up to you, I'm afraid.
• 11-15-2010
KBriggs
From your description of the problem I can hazard a guess as to the algorithm you are talking about. Note that I am guessing you are stuck more on the theory than the code, so here is a breakdown of what the theory would look like if I am interpreting your problem correctly:

Basically, blood pressure will be highest right after a heart beat, and lowest just before the beat. Thus, your pressure data will look like some sort of sine wave + a constant offset (the threashold). You can locate the position in time of the beats by looking at the position in time of the maxima of the pressure values, and the period of the beats will be simply the distance between the maxima.

locate the threshold - that is, the largest local minimum in between beats. That way, you can say that if the pressure is ever above this threshold, there must be a beat happening, and you can therfore find the position of the local maximum.

Your algorithm must search through the file for a point where the pressure surpasses the threshold, and the point where it drops below the threshold again. In between these two locations is a maximum in pressure that indicates a beat, which you have to find.

Once you find that, you need to move to the next pair of locations where your pressure surpasses the threshold and search between them for a maximum. The the distance between the two maxima you just found gives the instantaneous hearbeat period.

NOTE: I don't know what the Threshold and Blank algorithm is, but this is how I would approach your problem. For the problem if finding local maxima between two points, there are lots of algorithms, the most efficient of which involve a clever bisection of your range.

Your classmate (?) Describes something along these lines here: http://cboard.cprogramming.com/c-pro...r-r-value.html

here: File:Cardiac Cycle Left Ventricle.PNG - Wikipedia, the free encyclopedia

Is a rather busy example of the sort of data you are going to be looking at (the blue line is the one you are interested in. Your threshold value will be slightly above the value at the constant portion of the graph, and you are going to be looking for the position of the maxima.
• 11-16-2010
That's exactly what the theory sounds like it should be. Very nice.

However, my I'm still a bit confused with the code; how can I, using probably a while loop, continually read, store, and analyze all of these changing time points? I get your explanation of recording this data from the crossing of the threshold, but I just don't know how to format the code to do so.
• 11-16-2010
To avoid repeating things, check out this thread, from time to time:
http://cboard.cprogramming.com/c-pro...tml#post983365

Usually, a while loop is good:

Code:

```int ok = 0; //use fopen to open the file, with your FILE* pointer, then while(1) {      //an endless loop   ok = fscanf(filePointerName, "%f %d", time1, bp);   if(ok< 2)      break;  //no more data or error, break out of the loop } fclose(filePointerName);```
• 11-16-2010
rags_to_riches
Quote:

how can I, using probably a while loop, continually read, store, and analyze all of these changing time points? I get your explanation of recording this data from the crossing of the threshold, but I just don't know how to format the code to do so.
Can I ask an honest question? How does one get into a biomedical programming class without knowing how to program using simple loops and variables? It would seem to me that would be a prerequisite to the course.
• 11-16-2010
I really don't see what the purpose is in those kinds of responses. You are either willing to help or you are not, and for your information I know how those basic operators work.

ANYWAY, I believe I have the majority of the program on lock, but I keep getting segmentation errors (core dump) when I try to run the program. Take a look. What am I missing that is leading to this problem?

Here's the .c file:
• 11-16-2010
rags_to_riches
I asked out of genuine curiosity. I have a background in electrocardiography (not programming), so I am moderately interested in the subject. I wouldn't have thought that biomedical programming would be an intro course...I would have figured it would have prereqs up to and including algorithms and data structures.

Anyway, here's your code. You really should post the code in your post rather than as an attachment.

Code:

```/* Instantaneous Heart Rate Analysis Date:            11.15.10 This program calculates the instantaneous heart rate at each heart beat. */ #include <stdio.h> #include <string.h> FILE *fp; char fn[100000]; int main(int argc, char *argv[]) {         float t1, IHR;         int i, n, blank, threshold;         float t[300000];         float b[300000];         n = 3601;         strcpy(fn,argv[1]);         fp = fopen(fn,"r");         if(fp==NULL) {                 printf("Error: File Does Not Exist\n");         }         else {                 printf("File Opened Successfully\n");                 i = 0;                 threshold = 100;                 t1 = -1;                 while(fscanf(fp,"%f %f", &t[i], &b[i])!=EOF) {                         while (b[i] < threshold) {                                 i++;                         }                         IHR = 60. / (t[i] - t1);                         if ( t1 > -1) {                                                                printf("The instantaneous heart rate is %f",IHR);                                                                }                         t1 = t[i];                         i = i + blank;                 }                 fclose(fp);         }                 return 0; }```
Now, here:
Code:

`i = i + blank;`
blank is an uninitialized value. You have no idea what it contains, so it could very well making i some crazy number that exceeds the bounds of your array.

Depending on your environment, the declaration of two 300000-element arrays of floats may also be blowing the stack.

Without the data it's hard to say much more than that.
• 11-16-2010
Right. I just tried making the array sizes large enough to allow all the data points being used (which are 2 columns of 3601 points).
• 11-16-2010
anduril462
You declared your arrays (300,000 / 3601) = 83.3 times bigger than they need to be. If you're only ever going to have 3601 points (or some fixed upper limit), then #define that somewhere and declare your arrays that size. If the number of points might change significantly on any given run of the program, you may want to consider dynamic memory allocation.
• 11-16-2010
CommonTater
Quote:

Originally Posted by ILLiad
ANYWAY, I believe I have the majority of the program on lock, but I keep getting segmentation errors (core dump) when I try to run the program. Take a look. What am I missing that is leading to this problem?
Here's the .c file:

Two arrays of floats at 300,000 items each... 600,000 items 8 bytes each... 4.8 megabytes, declared on the stack in your main() function... yep, tha'll do it... stack overflow.

You need to either declare your arrays dynamically ( with malloc() ) or globally outside main().
• 11-16-2010
How can I declare the arrays globally without having them lose reference to the later part of the code? In the while loop, from the code above, the &t[i] and &b[i] won't have reference if they are formatted with #define ...?
• 11-16-2010
CommonTater
Quote:

Originally Posted by ILLiad
How can I declare the arrays globally without having them lose reference to the later part of the code? In the while loop, from the code above, the &t[i] and &b[i] won't have reference if they are formatted with #define ...?

Not with #define... simply not in any function...
Code:

```#include <stdio.h> float a[300000]; float b[300000]; int main(void) { ... return 0; }```
A better bet is to use malloc() and free() to create the arrays dynamically, on the system's memory heap.

I also seriously doubt you need 100000 characters (roughly the size of a small novel) for a filename... 256 would probably do it.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last