tomwalters@0: /* tomwalters@0: Copyright (c) Applied Psychology Unit, Medical Research Council. 1993 tomwalters@0: =========================================================================== tomwalters@0: tomwalters@0: Permission to use, copy, modify, and distribute this software without fee tomwalters@0: is hereby granted for research purposes, provided that this copyright tomwalters@0: notice appears in all copies and in all supporting documentation, and that tomwalters@0: the software is not redistributed for any fee (except for a nominal tomwalters@0: shipping charge). Anyone wanting to incorporate all or part of this tomwalters@0: software in a commercial product must obtain a license from the Medical tomwalters@0: Research Council. tomwalters@0: tomwalters@0: The MRC makes no representations about the suitability of this tomwalters@0: software for any purpose. It is provided "as is" without express or tomwalters@0: implied warranty. tomwalters@0: tomwalters@0: THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING tomwalters@0: ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL tomwalters@0: THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES tomwalters@0: OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, tomwalters@0: WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, tomwalters@0: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS tomwalters@0: SOFTWARE. tomwalters@0: */ tomwalters@0: tomwalters@0: /* trigger.c tomwalters@0: * tomwalters@0: * Part of the temporal regularity programs tomwalters@0: * tomwalters@0: * M.Akeroyd. Summer 1993. Revised Winter 1994. tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include tomwalters@0: tomwalters@0: #include "tip.h" tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: extern struct Peak peak[MAX_PEAKS]; /* all channels */ tomwalters@0: tomwalters@0: extern int framewidth_samples; /* pwidth + nwidth * samplerate */ tomwalters@0: extern int pwidth; /* in msecs */ tomwalters@0: extern int nwidth; /* in msecs: NEGATIVE */ tomwalters@0: extern int samplerate; /* samples per sec */ tomwalters@0: tomwalters@0: tomwalters@0: extern char progname[MAX_STRING_LENGTH]; tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: int find_trigger(int frame, int channel, int total_peaks) tomwalters@0: { tomwalters@0: tomwalters@0: /* What this code does: tomwalters@0: * tomwalters@0: * it looks for trigger peaks, at or near ImageTime = 0. tomwalters@0: * First, it looks for a peak reasonably tomwalters@0: * close to where the trigger-peak SHOULD be. If there isn't one there, then it reports tomwalters@0: * a failure, and crashes. tomwalters@0: * (No it doesn't: in fact it takes the fisrt peak to the right of the pre-defined trigger point). tomwalters@0: * tomwalters@0: * The peaknumber of the trigger_peak is RETURNed. tomwalters@0: * tomwalters@0: * Version of 1st June 1993. tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: int n_1; tomwalters@0: int channel_1; tomwalters@0: int trigger_sample; /* where the trigger of channel_1 is */ tomwalters@0: double trigger_double; tomwalters@0: int trigger_found = OFF; tomwalters@0: int trigger_peak; /* THIS IS RETURNED */ tomwalters@0: tomwalters@0: tomwalters@0: /* hack ... */ tomwalters@0: if (total_peaks == 0) tomwalters@0: return 0; tomwalters@0: tomwalters@0: /* First, find out where the trigger-point is : tomwalters@0: * pwidth is in msecs, so divide that by 1000, multiply by sample, and you get pwidth in tomwalters@0: * samples. tomwalters@0: * So, trigger = that - 1 (-1 because the indexing is 0 to (framewidth_samples -1)) tomwalters@0: * Hope this integer arithemetic works */ tomwalters@0: tomwalters@0: trigger_double = (double) ((double) pwidth * (double) samplerate) / 1000.0 - 1; tomwalters@0: trigger_sample = (int) trigger_double; tomwalters@0: n_1 = total_peaks + 1; tomwalters@0: tomwalters@0: /* find first peak to RIGHT of trigger (reverse, remember) */ tomwalters@0: while ((peak[--n_1].sample < trigger_sample)) tomwalters@0: ; tomwalters@0: tomwalters@0: /* the next peak will be the trigger, at least on today's definition */ tomwalters@0: trigger_found = ON; tomwalters@0: trigger_sample = peak[n_1].sample; tomwalters@0: trigger_peak = n_1; tomwalters@0: tomwalters@0: if (trigger_found == OFF ) { tomwalters@0: fprintf(stderr, "%s: unable to find trigger point: frame %i channel %i\n", progname, frame, channel_1); tomwalters@0: exit(-1);} tomwalters@0: tomwalters@0: return trigger_peak; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /* The End .........................................................................*/ tomwalters@0: /*..................................................................................*/ tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: