komodovaran_
komodovaran_

Reputation: 2012

Combining similar signals in timeseries

Let's say I have a signal that goes like this. I've already put it through the rounds of fitting, and I can more easily manage the signal now.

But for whatever reason, there are some tiiiiny differences that I can't seem to eliminate.

enter image description here

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


a = [0.5]*20
b = [0.4]*20
c = [0.503]*20
d = [0.4]*20

signal = pd.Series(np.concatenate([a,b,c,d]))

plt.figure(figsize = (7,3))
plt.plot(signal, color = "firebrick")
plt.axhline(0.5, linestyle = ":")
plt.show()

# Identify the four different intensities, so they can be grouped
id = signal.transform(lambda x: (abs(x.diff()) > 0).cumsum())

I'm thinking a solution could be to line up all the signals from low to high [0.4, 0.4, 0.5, 0.503]

And then go through them, while ignoring tiny differences e.g. 0.010, so

id = signal.transform(lambda x: (abs(x.diff()) > 0.010).cumsum())

Then I would correctly identify only 2 different intensities. For the tiny discrepancy I could just take the mean or the median. It doesn't really make a difference. What matters is that I don't count more than 2 different intensities.

How would I do this?

Upvotes: 1

Views: 1224

Answers (1)

pault
pault

Reputation: 43544

Here is an elaboration of the idea I put in my comment.

Use the mean of the signal as a threshold value. Then compute the median value for the parts of the signal that are above the threshold, and for those below. Use these to transform your data.

med_high = signal[signal > signal.mean()].median()
med_low = signal[signal < signal.mean()].median()
print (med_low, med_high)

new_signal = signal.transform(lambda x: med_low if x < signal.mean() else med_high)
plt.figure(figsize = (7,3))
plt.plot(new_signal, color = "firebrick")
plt.axhline(0.5, linestyle = ":")
plt.show()

The result:

enter image description here

Upvotes: 1

Related Questions