|
| 1 | +/* |
| 2 | + * Finds the first bigger sample in the given array |
| 3 | + * |
| 4 | + * INPUTS: |
| 5 | + * array : [double/single column vector] a sorted array |
| 6 | + * value : [double/single] |
| 7 | + * |
| 8 | + * OUTPUT: |
| 9 | + * index : [double] the |
| 10 | + * |
| 11 | + * Sagi Perel, 10/2009 |
| 12 | + */ |
| 13 | + |
| 14 | +#include "mex.h" |
| 15 | +#include "matrix.h" |
| 16 | + |
| 17 | +void |
| 18 | +mexFunction( |
| 19 | + int num_output_args, // Number of left hand side (output) arguments |
| 20 | + mxArray *output_arg[], // Array of left hand side arguments |
| 21 | + int num_input_args, // Number of right hand side (input) arguments |
| 22 | + const mxArray *input_arg[]) // Array of right hand side arguments |
| 23 | +{ |
| 24 | + double *array, value, *ptr; |
| 25 | + float *array_f; |
| 26 | + int array_length, this_array_length; |
| 27 | + mxClassID array_type; |
| 28 | + int debug=0; |
| 29 | + int i; |
| 30 | + int start_idx, end_idx, middle_idx; |
| 31 | + double epsilon = 0.00001; |
| 32 | + |
| 33 | + //sanity check for input arguments |
| 34 | + if( num_input_args != 2) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: wrong syntax: Cfind_first_bigger_sample_in_array(array, value)\n"); |
| 35 | + if( mxGetN(input_arg[0]) != 1 && mxGetM(input_arg[0]) != 1 ) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: the array must be a vector\n"); |
| 36 | + array_type = mxGetClassID(input_arg[0]); |
| 37 | + if( array_type != mxDOUBLE_CLASS && array_type != mxSINGLE_CLASS ) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: the array must be a column vector of type double or float\n"); |
| 38 | + |
| 39 | + if( mxGetN(input_arg[1]) != 1) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: the value must be a double\n"); |
| 40 | + if( mxGetM(input_arg[1]) != 1) mexErrMsgTxt( "Cfind_first_bigger_sample_in_array.c: the value must be a double\n"); |
| 41 | + |
| 42 | + array_length = (mxGetM(input_arg[0]) > mxGetN(input_arg[0])) ? mxGetM(input_arg[0]) : mxGetN(input_arg[0]); |
| 43 | + |
| 44 | + //read input arguments: |
| 45 | + ptr = (double*) mxGetData( input_arg[1]); |
| 46 | + value = *ptr; |
| 47 | + if(debug) |
| 48 | + printf("array_length=%d,value=%f\n",array_length,value); |
| 49 | + |
| 50 | + |
| 51 | + //-read the signal and find the first |
| 52 | + switch(array_type) |
| 53 | + { |
| 54 | + case mxDOUBLE_CLASS: |
| 55 | + array = (double *) mxGetData( input_arg[0]); |
| 56 | + |
| 57 | + //check for edge condition- if the first element is bigger than the value |
| 58 | + if((array[0] - value)>0) |
| 59 | + { |
| 60 | + output_arg[0] = mxCreateDoubleMatrix(1, 1, mxREAL); |
| 61 | + *mxGetPr(output_arg[0]) = 1; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + //shrink the array size by half every time |
| 66 | + start_idx = 0; |
| 67 | + end_idx = array_length-1; |
| 68 | + while(1) |
| 69 | + { |
| 70 | + |
| 71 | + this_array_length = end_idx - start_idx + 1; |
| 72 | + if(debug) |
| 73 | + printf("start_idx=%d end_idx=%d (%d)",start_idx, end_idx, this_array_length); |
| 74 | + |
| 75 | + if(this_array_length <= 20) |
| 76 | + { |
| 77 | + middle_idx = search_array_serial(array, value, start_idx, this_array_length); |
| 78 | + if(debug) |
| 79 | + printf("\nthis_array_length<=20: middle_idx=%d\n",middle_idx); |
| 80 | + if(middle_idx < 0) |
| 81 | + { |
| 82 | + output_arg[0] = mxCreateDoubleMatrix(0, 0, mxREAL); |
| 83 | + return; |
| 84 | + }else{ |
| 85 | + |
| 86 | + output_arg[0] = mxCreateDoubleMatrix(1, 1, mxREAL); |
| 87 | + *mxGetPr(output_arg[0]) = middle_idx; |
| 88 | + return; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + middle_idx = (this_array_length % 2 == 0 )? (start_idx+(this_array_length/2)) : (start_idx+((this_array_length-1)/2)); |
| 93 | + if(debug) |
| 94 | + printf(" middle_idx=%d,val=%f\n",middle_idx,array[middle_idx]); |
| 95 | + //shrink the array size by half |
| 96 | + if(mxIsNaN(array[middle_idx])) |
| 97 | + { |
| 98 | + if(middle_idx == start_idx) |
| 99 | + { |
| 100 | + output_arg[0] = mxCreateDoubleMatrix(0, 0, mxREAL); |
| 101 | + return; |
| 102 | + }else{ |
| 103 | + middle_idx = middle_idx -1; |
| 104 | + } |
| 105 | + }else{ |
| 106 | + /* |
| 107 | + //if the middle_idx is exactly the value we are looking for- numerical issues might cause to miss it |
| 108 | + if(array[middle_idx]-epsilon <= value && array[middle_idx]+epsilon >= value) |
| 109 | + { |
| 110 | + output_arg[0] = mxCreateDoubleMatrix(1, 1, mxREAL); |
| 111 | + *mxGetPr(output_arg[0]) = middle_idx+1; |
| 112 | + return; |
| 113 | + } |
| 114 | + */ |
| 115 | + |
| 116 | + //else continue as usual |
| 117 | + if((array[middle_idx] - value) > 0) |
| 118 | + { |
| 119 | + end_idx = middle_idx; |
| 120 | + }else{ |
| 121 | + start_idx = middle_idx; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + break; |
| 127 | + case mxSINGLE_CLASS: |
| 128 | + array_f = (float *) mxGetData( input_arg[0]); |
| 129 | + for(i=0; i<array_length; i++) |
| 130 | + { |
| 131 | + if((array_f[i] - value) > 0) |
| 132 | + { |
| 133 | + output_arg[0] = mxCreateDoubleMatrix(1, 1, mxREAL); |
| 134 | + *mxGetPr(output_arg[0]) = (i+1); |
| 135 | + return; |
| 136 | + } |
| 137 | + } |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + //create an empty output, to be returned if no such item exists |
| 142 | + output_arg[0] = mxCreateDoubleMatrix(0, 0, mxREAL); |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +int search_array_serial(double* array, double value, int start_idx, int array_length) |
| 147 | +{ |
| 148 | + int i; |
| 149 | + for(i=start_idx; i<start_idx+array_length; i++) |
| 150 | + { |
| 151 | + if((array[i] - value) >= 0) |
| 152 | + { |
| 153 | + return (i+1); |
| 154 | + } |
| 155 | + } |
| 156 | + return -1; |
| 157 | +} |
| 158 | + |
0 commit comments