<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Basic linear interpolation in time in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Is-there-a-command-that-does-Basic-linear-interpolation-in-time/m-p/404667#M99298</link>
    <description>&lt;P&gt;Here's what I've come up with so far:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| mstats 
    span=1s
    avg(value) as value
    WHERE index=my_metrics
| sort - _time 
| streamstats window=2 first(value) as next_value, first(_time) as next_time 
| eval inc = (next_value - value) / (next_time - _time) 
| makecontinuous span=1s 
| filldown inc
| streamstats sum(eval(coalesce(value, inc))) as value_interpolated reset_before="ISNOTNULL(value)"
| table _time value value_interpolated
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;CODE&gt;|mstats&lt;/CODE&gt; pulls as many points as possible with a min resolution of 1s. It ends up being an item every 20 seconds or so&lt;/LI&gt;
&lt;LI&gt;I do a descending sort on time, so that when I do the streamstats to calculate the deltas (&lt;CODE&gt;inc&lt;/CODE&gt;), it'll be the delta needed between the current and next data point, as opposed to getting the delta between the current and previous data point.&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;|eval inc =&lt;/CODE&gt; just finds the amount it should increment each by each row, aka the slope&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;|makecontinuous&lt;/CODE&gt; just fills in the _time field on a 1 second interval but leaves everything else null&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;|filldown&lt;/CODE&gt; fills the null values with the prev ones&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;|streamstats...&lt;/CODE&gt; takes a running sum of the increments and resets every time it hits an actual data point&lt;/LI&gt;
&lt;/OL&gt;

&lt;HR /&gt;

&lt;P&gt;As a macro, I called it &lt;CODE&gt;linear_interpolate(1)&lt;/CODE&gt;, defined as:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sort - _time 
| streamstats window=2 first($field$) as next_value, first(_time) as next_time 
| eval delta = (next_value - $field$) / (next_time - _time) 
| makecontinuous span=1s 
| filldown delta
| streamstats sum(eval(coalesce($field$, delta))) as $field$_interpolated reset_before="ISNOTNULL($field$)"
| fields - delta next_value next_time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;with a single argument &lt;CODE&gt;field&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Then to use it:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| mstats 
    span=1s
    ...
| `linear_interpolate(field=value)`
| table _time value value_interpolated
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It works but it really irks me that there doesn't seem to be a simple linear interpolation command, unless I'm completely missing something.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;It ends up looking something like this:&lt;/P&gt;

&lt;P&gt;&lt;IMG src="https://i.imgur.com/KFlKYjY.png" alt="screenshot of data fit" /&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 23 Jul 2019 18:13:38 GMT</pubDate>
    <dc:creator>khevans</dc:creator>
    <dc:date>2019-07-23T18:13:38Z</dc:date>
    <item>
      <title>Is there a command that does Basic linear interpolation in time?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Is-there-a-command-that-does-Basic-linear-interpolation-in-time/m-p/404666#M99297</link>
      <description>&lt;P&gt;Is there a command that just does linear interpolation? I have data that is logging every 20 seconds or so. I would like to interpolate it to a second-resolution on a table. Is there a single command that does this?&lt;/P&gt;
&lt;P&gt;For example, if I do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;| mstats
    span=20s
    latest(MyCounter)
    WHERE index=my_metrics
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It might return something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;_time MyCounter
0:00:00 100
0:00:20 120
0:00:40 160
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I would like to do is have it transform to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;_time MyCounter
0:00:00 100
0:00:01 101
0:00:02 102
...
0:00:19 119
0:00:20 120
0:00:21 122
0:00:22 124
...
0:00:39 159
0:00:40 160
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've tried using &lt;CODE&gt;| predict&lt;/CODE&gt; with a couple of algorithms, but it seems to vary wildly. I just would like a simple NN/linear interpolation between two data points. I was thinking it might be doable using &lt;CODE&gt;| makecontinuous _time span=1s&lt;/CODE&gt; and a &lt;CODE&gt;| streamstats&lt;/CODE&gt; but I haven't quite figured it out yet.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 14:13:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Is-there-a-command-that-does-Basic-linear-interpolation-in-time/m-p/404666#M99297</guid>
      <dc:creator>khevans</dc:creator>
      <dc:date>2022-08-17T14:13:45Z</dc:date>
    </item>
    <item>
      <title>Re: Basic linear interpolation in time</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Is-there-a-command-that-does-Basic-linear-interpolation-in-time/m-p/404667#M99298</link>
      <description>&lt;P&gt;Here's what I've come up with so far:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| mstats 
    span=1s
    avg(value) as value
    WHERE index=my_metrics
| sort - _time 
| streamstats window=2 first(value) as next_value, first(_time) as next_time 
| eval inc = (next_value - value) / (next_time - _time) 
| makecontinuous span=1s 
| filldown inc
| streamstats sum(eval(coalesce(value, inc))) as value_interpolated reset_before="ISNOTNULL(value)"
| table _time value value_interpolated
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;CODE&gt;|mstats&lt;/CODE&gt; pulls as many points as possible with a min resolution of 1s. It ends up being an item every 20 seconds or so&lt;/LI&gt;
&lt;LI&gt;I do a descending sort on time, so that when I do the streamstats to calculate the deltas (&lt;CODE&gt;inc&lt;/CODE&gt;), it'll be the delta needed between the current and next data point, as opposed to getting the delta between the current and previous data point.&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;|eval inc =&lt;/CODE&gt; just finds the amount it should increment each by each row, aka the slope&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;|makecontinuous&lt;/CODE&gt; just fills in the _time field on a 1 second interval but leaves everything else null&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;|filldown&lt;/CODE&gt; fills the null values with the prev ones&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;|streamstats...&lt;/CODE&gt; takes a running sum of the increments and resets every time it hits an actual data point&lt;/LI&gt;
&lt;/OL&gt;

&lt;HR /&gt;

&lt;P&gt;As a macro, I called it &lt;CODE&gt;linear_interpolate(1)&lt;/CODE&gt;, defined as:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sort - _time 
| streamstats window=2 first($field$) as next_value, first(_time) as next_time 
| eval delta = (next_value - $field$) / (next_time - _time) 
| makecontinuous span=1s 
| filldown delta
| streamstats sum(eval(coalesce($field$, delta))) as $field$_interpolated reset_before="ISNOTNULL($field$)"
| fields - delta next_value next_time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;with a single argument &lt;CODE&gt;field&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Then to use it:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| mstats 
    span=1s
    ...
| `linear_interpolate(field=value)`
| table _time value value_interpolated
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It works but it really irks me that there doesn't seem to be a simple linear interpolation command, unless I'm completely missing something.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;It ends up looking something like this:&lt;/P&gt;

&lt;P&gt;&lt;IMG src="https://i.imgur.com/KFlKYjY.png" alt="screenshot of data fit" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jul 2019 18:13:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Is-there-a-command-that-does-Basic-linear-interpolation-in-time/m-p/404667#M99298</guid>
      <dc:creator>khevans</dc:creator>
      <dc:date>2019-07-23T18:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: Basic linear interpolation in time</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Is-there-a-command-that-does-Basic-linear-interpolation-in-time/m-p/609746#M105696</link>
      <description>&lt;P&gt;Thanks for posting this. Solved the problem I was having perfectly.&lt;BR /&gt;&lt;BR /&gt;Only difference was I was trying to convert 30 min data to 5 min data which I did with a modified version of your macro&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Macro Def - linear_interpolate_minutes($field$,$minutes$)

sort - _time 
| streamstats window=2 first($field$) as next_value, first(_time) as next_time 
| eval delta = ((next_value - $field$) / (next_time - _time)) * ($minutes$ * 60) 
| makecontinuous span=$minutes$m 
| filldown delta 
| streamstats sum(eval(coalesce($field$, delta))) as $field$_interpolated reset_before="ISNOTNULL($field$)" 
| fields - delta next_value next_time&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 17 Aug 2022 03:38:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Is-there-a-command-that-does-Basic-linear-interpolation-in-time/m-p/609746#M105696</guid>
      <dc:creator>cameronjust</dc:creator>
      <dc:date>2022-08-17T03:38:22Z</dc:date>
    </item>
  </channel>
</rss>

