title: Find key or value in Dictionary<double, double> with linear interpolation
body: <p>I am developing application with .NET 4.5 / C# 6.0 which gets measurement data from a hardware device, which is also configured by my application. E.g. I configure my device with the following:</p>
  
  <ul>
  <li>start frequency: 10 kHz</li>
  <li>stop frequency:  20 kHz</li>
  <li>measurement points: 11</li>
  </ul>
  
  <p>and at the end of the processing pipeline of my raw data I get a dictionary like the following, where the key is the frequency and the value e.g. the magnitude in dB:</p>
  
  <blockquote>
    <p>key => value<br>
    10k => -3<br>
    11k => -3<br>
    12k => -3<br>
    13k => -3.5<br>
    14k => -4<br>
    15k => -5<br>
    16k => -6<br>
    17k => -7<br>
    18k => -8<br>
    19k => -10<br>
    20k => -12  </p>
  </blockquote>
  
  <p>This dictionary is updated "on the fly" as the device is continuously sweeping these 11 points in a loop.<br>
  These values are for once displayed in a chart, which is no problem as I simply update the chart data whenever a new point is ready (I get an event for each new point), but I also have a data grid where the user can display the values for manually entered points ([value] means manually entered), e.g.:</p>
  
  <pre><code>        | A: f       | B: mag(db)  
  Point 1 | [11 kHz]   | -3 dB
  Point 2 | [16.5 kHz] | -6.5 dB
  Point 3 | 18.5 kHz   | [-9.5 dB] 
  </code></pre>
  
  <p><code>Point 1</code> is easy as it hits exactly on one measured point, but for e.g. <code>Point 2</code> the user manually enters <code>16.5 kHz</code> in column A, which means I need to interpolate the value of column B for the two measurement points next to it.<br>
  About the same, but the other way around for <code>Point 3</code>: The user manually entered <code>-9.5 dB</code> in column B and so I need to find the interpolated frequency where this value would be the interpolated result.  </p>
  
  <p>Note - the following constraints apply for <code>Point 3</code>:</p>
  
  <ul>
  <li>If the value would be possible twice or more, the first one is used, e.g. for <code>-3 dB</code> it should return <code>10 kHz</code></li>
  <li>The frequency for the entered value is only searched once and then it behaves the same as <code>Point 2</code></li>
  <li>If the given value is not found the closest one is returned (also valid for <code>Point 2</code>), e.g. values > <code>-3 dB</code> return <code>10 kHz</code> and values &lt; <code>-12 dB</code> return <code>20 kHz</code></li>
  </ul>
  
  <p><strong>Is there some fast/optimized way to get to these values for <code>Point 2</code> and <code>Point 3</code>?</strong></p>
  
  <p>I could only think of iterating over all point every time a value is manually entered and then interpolate between every two points until the given value is found. Then update column B when one of the neighboring frequencies is updated.<br>
  Note: The device delivers up to several hundred points per second.</p>