Thread: Linear interpolation with boundary issue problem...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    84

    Thumbs down Linear interpolation with boundary issue problem...

    Hello,

    I am trying to write some code to do a linear interpolation scheme along an 360 element array (so 0 - 359 elements with each element representing a degree, aka like on a compass). I have been working and cannot find a way to interpolate from the end back to the beginning of the array (i.e., from 340th element to element 0 and so on).

    What I want to do is to first fill the array with an initialized value and then copy data in that replaces only X-odd elements of the array. I then want to linear interpolate between X elements if they reside within 90 elements (aka 90 degrees) of that value or, if not, just linear interpolate over 90 elements to that initialized value that they array was initialized to.

    Any ideas...

    My current strategy doesn't do the job:

    At this point, I have initialized the array to the same value and have read in data that only replaces X elements out of the 360. What I do is create a temporary 360 array and copy the last 91 elements to the beginning and append the first 91 elements to the end -- coming up with a 542 element array. After the linear interpolation, I then copy elements 91 through 451, to then obtain the original 360 degree array with values now being linear interpolated. I don't think this is efficient or the best one...

    Code:
    /*    Loop through the temp array and find consecutive azimuths that
          have a different initialize value. */
          for(j = 0; j < 542; ++j){
    
    /*       Find an azimuth that has a different value where a flag is set such that it flag is greater than 0. */
             if(j >= 90 && t360flag[j] > 0){
                flag360last = j;
    
    /*          Go backwards and find either an azimuth that had 
                data replaced or at most a 90 degree
                span away from the first point then linear interpolate 
                from one or the other. */
                for(k = (flag360last - 1); k >= flag360last - 90; --k){
    
    /*             Found an azimuth with a value replace and 
                   LI from this azimuth to the first one. */
                   if(t360flag[k] > 0){
    
                      flag360first = k;
    
                      printf("** LI has been triggered at %d. **\n",
                             k);
    
    /*                Start linear interpolation. */
    /*                Compute delta value for linear interpolation. */
                      delta360 = (t360array[flag360last] - t360array[flag360first]) / (flag360last - flag360first);
    
                      printf("** Delta value for LI is %f. **\n", delta360);
                      printf("** Where y2 = %f, y1 = %f, x2 = %d, and x1 = %d. **\n",
                             t360array[flag360last], t360array[flag360first],
                             flag360last, flag360first);
    
    /*                Fill in values between the azimuths/bins. */
                      for(k = (flag360first + 1); k < flag360last; ++k){
                          t360array[k] = delta360 + t360array[k - 1];
                      }
    
                      break;
    
    /*            Did not find an azimuth with an obs and have span
                   90 degrees - so LI from here using initialized value. */
                   } else if( k == (flag360last - 90)){
    
                      flag360first = (flag360last - 90);
    
                      printf("** LI has been triggered at %d %s. **\n",
                             k, "(90 degree span scenario)");
    
    /*                Start linear interpolation. */
    /*                Compute delta value for linear interpolation. */
                      delta360 = (t360array[flag360last] - t360array[flag360first]) / (flag360last - flag360first);
    
                      printf("** Delta value for LI is %f. **\n", delta360);
                      printf("** Where y2 = %f, y1 = %f, x2 = %d, and x1 = %d. **\n",
                             t360array[flag360last], t360array[flag360first],
                             flag360last, flag360first);
    
    /*                Fill in values between the azimuths/bins. */
                      for(k = (flag360first + 1); k < flag360last; ++k){
                          t360array[k] = delta360 + t360array[k - 1];
                      }
    
                      break;
                   }
                }
             } else if( (j - flag360last) == 90){
    
                flag360first = (flag360last);
    
                printf("** LI has been triggered at %d. **\n",
                       k);
    
    /*          Start linear interpolation. */
    /*          Compute delta value for linear interpolation. */
                delta360 = (t360array[j] - t360array[flag360first]) / (j - flag360first);
    
                printf("** Delta value for LI is %f. **\n", delta360);
                printf("** Where y2 = %f, y1 = %f, x2 = %d, and x1 = %d. **\n",
                       t360array[j], t360array[flag360first],
                       j, flag360first);
    
    /*          Fill in values between the azimuths/bins. */
                for(k = (flag360first + 1); k < j; ++k){
                    t360array[k] = delta360 + t360array[k - 1];
                }
             }
          }
    Then I would just copy the 360 degree array back out.


    Thoughts!?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm a little bit confused by some of your azimuth and flag stuff, but I'm not sure it matters. I think you just need to look into the modulus (remainder) operator, %. If you mod by 360, you will always end up with a value between 0 and 359. 360 will become 0, 361 will become 1, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linear Search Problem
    By Taka in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 04:11 AM
  2. Interpolation in C - Help
    By finnpark in forum C Programming
    Replies: 3
    Last Post: 11-01-2005, 12:44 PM
  3. problem with plot a linear graph on the command line
    By dionys in forum C Programming
    Replies: 5
    Last Post: 04-17-2004, 07:01 AM
  4. what's interpolation?
    By jverkoey in forum Game Programming
    Replies: 9
    Last Post: 02-12-2003, 03:55 PM
  5. Problem with Hash Table Linear Probing
    By kirby1024 in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 06:03 AM