Thread: Threshold and Blank Algorithm

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    7

    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?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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.

    So, your task is to:

    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: Heart Rate R-to R value

    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.
    Last edited by KBriggs; 11-15-2010 at 09:00 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To avoid repeating things, check out this thread, from time to time:
    Heart Rate R-to R value

    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);

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    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:
    Last edited by ILLiad; 11-16-2010 at 04:35 PM.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    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).

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ILLiad View Post
    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().

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    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 ...?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ILLiad View Post
    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.
    Last edited by CommonTater; 11-16-2010 at 06:28 PM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread