1

So basically I have some data and I need to find a way to smoothen it out (so that the line produced from it is smooth and not jittery). When plotted out the data right now looks like this: placeholder text for image

and what I want it to look is like this: placeholder text for image

I tried using this numpy method to get the equation of the line, but it did not work for me as the graph repeats (there are multiple readings so the graph rises, saturates, then falls then repeats that multiple times) so there isn't really an equation that can represent that.

I also tried this but it did not work for the same reason as above.

The graph is defined as such:

gx = [] #x is already taken so gx -> graphx
gy = [] #same as above

#Put in data

#Get nice data #[this is what I need help with]

#Plot nice data and original data

plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

The method I think would be most applicable to my solution is getting the average of every 2 points and setting that to the value of both points, but this idea doesn't sit right with me - potential values may be lost.

Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
Evgeny
  • 153
  • 16
  • Why don't you want a polynomial? (1)polynomial interpolation, (2) cubic splining, and (3) using Bézier curves, are three of the standard ways to do what you want, and they all use polynomials. – Toothpick Anemone Nov 07 '19 at 12:46

2 Answers2

1

You could use a infinite horizon filter

import numpy as np
import matplotlib.pyplot as plt

x = 0.85 # adjust x to use more or less of the previous value
k = np.sin(np.linspace(0.5,1.5,100))+np.random.normal(0,0.05,100)
filtered = np.zeros_like(k)
#filtered = newvalue*x+oldvalue*(1-x)
filtered[0]=k[0]
for i in range(1,len(k)):
# uses x% of the previous filtered value and 1-x % of the new value
    filtered[i] = filtered[i-1]*x+k[i]*(1-x) 

plt.plot(k)
plt.plot(filtered)
plt.show()
en_lorithai
  • 1,120
  • 7
  • 13
0

I figured it out, by averaging 4 results I was able to significantly smooth out the graph. Here is a demonstration:

demo

Hope this helps whoever needs it

Evgeny
  • 153
  • 16