# Resampling

In many features of Marple Insight, multiple signals must be combined: in functions, scatter plots, tables, filters, ... When these signals have different time bases, it becomes hard to know which datapoints from signal A correspond to which datapoints from signal B.&#x20;

Some tools don't allow you to combine signals with different timebases. In Marple Insight, this is solved by **resampling** the signals.

<figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2FQGe9AE4bEl4WKePVulYG%2Fimage.png?alt=media&#x26;token=16caa18a-5d60-4336-bf86-b6c4a7225aa5" alt=""><figcaption><p>Settings component to specify how to resample</p></figcaption></figure>

When multiple signals must be combined, you have the option to select how it's done:

* [`off`](#resampling-off) don't resample, only use the original timestamps.
* [`auto`](#resampling-auto)  resample using a dynamic frequency that depends on the underlying signals & database connection type
  * Marple DB, Postgres & TImescale: use the maximum frequency of the selected signals, upsample the signals with a lower frequency.
  * Other database connections: use the minimal frequency of the selected signals.
* [`on`](#resampling-on) resample using a selected frequency.

### Resampling off

When using  `off`, all signals are joined on their exact timestamp. If all signals have datapoints with **exactly matching timestamps** (up to the nanosecond)**,** the resulting signal will also have a datapoint at these timestamps. Datapoints with a timestamp that does not occur in all original signals are ignored in the resulting signal. &#x20;

<div><figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2FkFRksVDBULm8HMLU72RW%2Fresampling_off.svg?alt=media&#x26;token=f3405743-20e7-4c4a-a315-7880cc7e39cd" alt=""><figcaption><p>Resampling <code>off</code> with signals that have timestamps that match exactly</p></figcaption></figure> <figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2FMoBjkDelFDmHYFStCxW9%2Fresampling_off_offsetted.svg?alt=media&#x26;token=fd4df9af-011a-4ab6-838f-36a0dcb65c5f" alt=""><figcaption><p>Resampling <code>off</code> with signals that have timestamps that slightly differ</p></figcaption></figure></div>

Benefits:

* Exact timestamps are preserved
* Faster than `on`  or `auto`

{% hint style="danger" %}
Only use `off`  when you're sure that all signals have the exact same timebase. Otherwise you risk losing data.
{% endhint %}

### Resampling auto

`auto` is essentially the same as `on` but with a dynamic selection of the frequency. See [Resampling on](https://app.gitbook.com/o/3xj3gOaZBVq2qdke8xhC/s/wEBNWlmdcxXBXd7oyqyR/~/changes/199/marple-insight/user-manuals/visualisation/resampling#resampling-on) for more details.

Which frequency is used depends on whether the database connection implements upsampling.

* Marple DB, PostgreSQL and Timescale support upsampling. The maximum of the underlying frequencies is used.
* Other database connections don't support upsampling (yet). The minimum of the underlying frequencies is used.

### Resampling on

When using `on` (or `auto`), the input signals are sampled at the selected frequency to build the output signal.

To do this, the time range is divided in **buckets** (bins) of width `1/frequency` seconds. Within each bucket, the **first datapoint** is selected and gets a new timestamp assigned: the start time of the corresponding bucket. This ensures all signals have matching timestamps and we know which datapoints to combine in the resulting signal.

{% hint style="info" %}
Because data is aggregated per bucket, timestamps can slightly shift (at most 1 period).
{% endhint %}

The exact algorithm works slightly different depending on your connection.

#### Upsampling implemented

For the most used database connection types: Marple DB, PostgreSQL and Timescale, the algorithm first upsamples the signals to a common time axis by **forward filling** missing data to a common time axis with the specified frequency. The upsampled signals with the common times axis are then combined into the resulting signal as explained above. The **highest frequency** is selected when using `auto` .

{% hint style="info" %}
Using forward fill ensures no data is invented and integer signals remain integers.
{% endhint %}

The figures below explain what happens depending on the selected frequency

<div><figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2FaQmP7wshX856LsZsIKNx%2Fresampling_on_forward_fill_1hz.svg?alt=media&#x26;token=3c935bde-06a1-4ea4-8405-c5a14b811a7c" alt=""><figcaption><p>Upsampling at frequency of slowest signal</p></figcaption></figure> <figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2FcJdIRwmsXlTCnfCye62T%2Fresampling_on_forward_fill_3hz.svg?alt=media&#x26;token=c28f0e99-5a03-482f-bcc8-118e737c99a9" alt=""><figcaption><p>Upsampling at intermediate frequency</p></figcaption></figure></div>

<div><figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2FJMiXQn8rsm52k8y1yuSz%2Fresampling_on_forward_fill_5hz.svg?alt=media&#x26;token=93747cde-95aa-43c4-b972-d293f65abac9" alt=""><figcaption><p>Upsampling at frequency of fastest signal</p></figcaption></figure> <figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2Fam9ZBEwaCHrJdqg9C1xQ%2Fresampling_on_forward_fill_10hz.svg?alt=media&#x26;token=2feb47e0-e788-4465-ae66-7a5350fac031" alt=""><figcaption><p>Upsampling at frequency higher than fastest signal </p></figcaption></figure></div>

#### Upsampling not implemented

For some lesser used database connections, upsampling is not yet implemented and the resampling as described above uses the raw signal data. For these connections `auto` will automatically use the **lowest frequency.** Setting a higher frequency will not result in more data.

<div><figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2FAf7cqLLns1RDLGWeh4SK%2Fresampling_on_1hz.svg?alt=media&#x26;token=0aa0aace-c876-47ff-aecd-0793acd13750" alt=""><figcaption><p>Resampling at frequency of slowest signal</p></figcaption></figure> <figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2FArLmTSTaT8ufDndkz84z%2Fresampling_on_5hz.svg?alt=media&#x26;token=ed71a7f5-6863-4709-8e40-caaac31d1b2c" alt=""><figcaption><p>Resampling at frequency of fastest signal</p></figcaption></figure></div>

### Selecting the right frequency

In most cases it's best to let the **`auto`** setting determine the **optimal frequency.** In the following cases it makes sense to deviate from this setting:

* All your data has the **exact same timestamp** (e.g. it comes from csv files or from the same group within an MDF file) -> It's safe to use `off`&#x20;
* You have a very complex plot with many functions and/or filters which takes a while to load and you're interested in a **quick insight** rather than the exact data -> decrease the suggested frequency, you might some data but the plot will load faster
