1 Replies - 393 Views - Last Post: 11 August 2011 - 05:40 PM

#1 Sami_codes_AllDay   User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 09-August 11

Nearest-Neighbor-Search with variable-sized search window that wraps

Posted 11 August 2011 - 05:18 PM

The algorithm assumes a static list (array []) and the sliding window can wrap around in the positive or negative direction.

// This re-definition of the % function will always return a positive number, which enables us to wrap the search window around in the negative direction
#define MODULUS(number,modulus) ((number) % (modulus) < (0) ? (number) % (modulus) + (modulus): (number) % (modulus))



// Variable-sized-window circular NNS - This search algorithm assumes that it's reading a static table with non-unique values in pseudo-ascending order. An arbitrary starting point is input
// into the search algorithm. The search window slides forward, but can wrap around and overlap in the positive or negative direction.  
int smallRange_NN_search(float array[], float desired_val, int size, int beg_index)   // to be tested:
{
// Local variables:
 int found								= FALSE;                    // A boolean value which indicates whether we have an exact match or not
 int i									= 0;					    // Indexes through the entire array
 int j									= 0;					    // Holds the index of the closet obtained value
 int pivot_leg_size						= 3;					    // Holds the size of how many adjacent values we'll search, above and below, from the beg_index
 int srch_size							= (pivot_leg_size * 2 + 1); // Holds the total finite search size that we'll be traversing. This will always be an odd number.  
 float min_diff							= 1000.0;		            // Ficticious initial value
 float curr_diff;


 
 
      while((i < srch_size) && (found == FALSE))
      {
             
		   curr_diff = fabs(desired_val - array[MODULUS(i + beg_index - pivot_leg_size, size)]); // Subtract the desired value by the current table value to obtain the current delta. The current table value is offset by beg_index

						  if(curr_diff == 0)		                                             // An exact match has been found - this is highly unlikely
						  {
							  found = TRUE;					
							  j = MODULUS(i + beg_index - pivot_leg_size, size);
							   for( int k = 0; k < 10000; k++)
							   puts("Equal!!");
						  }
			
						  else if (curr_diff < min_diff)									     // We're now moving towards a closer value to the desired value
						  {
						      min_diff = curr_diff;												 // Update to a new minimum difference 
							  j = MODULUS(i + beg_index - pivot_leg_size, size);				 // Update this value as the newest index since it's the newest low value found
							  i++;															     // Keep iterating through the table until the end						   
						  }

						  else																     // We're now moving away from the closest value to the desired value
						  {
							  i++;															     // Keep iterating through the table until the end												      
						  }
				


       }


		return j;																			     // This returns the actual index of the closet value (in degrees) from the look-up table
      
}






Is This A Good Question/Topic? 0
  • +

Replies To: Nearest-Neighbor-Search with variable-sized search window that wraps

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Nearest-Neighbor-Search with variable-sized search window that wraps

Posted 11 August 2011 - 05:40 PM

Thanks for your efforts to share your solutions. Please feel free to submit your code snippets to the appropriate section in the DIC Snippets section. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1