0

I am using the following code in order to smooth my data

a = get_data()
y, x = a.T
t = np.linspace(0, 1, len(x))
t2 = np.linspace(0, 1, len(x))

x2 = np.interp(t2, t, x)
y2 = np.interp(t2, t, y)
sigma = 50
x3 = gaussian_filter1d(x2, sigma)
y3 = gaussian_filter1d(y2, sigma)

x4 = np.interp(t, t2, x3)
y4 = np.interp(t, t2, y3)


plt.plot(x, y, "o-", lw=2)
plt.plot(x3, y3, "r", lw=2)
plt.plot(x4, y4, "o", lw=2)
plt.show()

I found this code here:
line smoothing algorithm in python?

My problem is that I need to get points from the new fit which are exactly with the same x values as my original x values (the points that I have smoothed). The fit works well but the x values of the new points are different. How can I get points from the new fit which has the same x value but with the new fit y values. The x values for the points starts from 0 and the space between each one should be 1800.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • I genuinely do not understand what your strategy here is. What is the purpose of your operations and what is the connection to the linked SO question? Why do you have t and t2, when they are the same? Where exactly is your fit function and how do you know it works well, if you were not able to map it back to the original x data? You should also add a toy data set to make it a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Mr. T Jul 15 '18 at 08:43
  • The link is just a link to where the code was taken from. I smoothed the data using the following code. As you can see in the link attached after smoothing the data new points are created. The x values of the new values - of the smoothed data do not have the same values as the data that I smoothed. For example lets say I had the following set of points (1,1) (2,2), (3,8) and now after the smooth the points I get are (0.5, 0.5), (1.5, 1.5), (2.5, 5). I want to get the smoothed points so their x values will be the same as the Input data so with the same example I want the smoothed points to be – Amit shabtay Jul 15 '18 at 08:49
  • (1,1),(2,2.2),(3,6) – Amit shabtay Jul 15 '18 at 08:52

1 Answers1

0

I think what is particular to your case is that the data to smooth are like a free line in the plane (x, y) = f(t) rather than a function y = f(x)

Maybe the trick is that the points have to be sorted before the interpolation (see numpy.interp):

# Generate random data:
t = np.linspace(0, 3, 20)
x = np.cos(t) + 0.1*np.random.randn(np.size(t))
y = np.sin(t) + 0.1*np.random.randn(np.size(t))

# Smooth the 2D data:
sigma = 2
x_smooth = gaussian_filter1d(x, sigma)
y_smooth = gaussian_filter1d(y, sigma)

# Sort (see: https://stackoverflow.com/a/1903579/8069403) 
permutation = x_smooth.argsort()
x_smooth = x_smooth[permutation]
y_smooth = y_smooth[permutation]

x_new = np.sort(x)  # not mandatory

# Interpolation on the original x points:
y_smooth_new = np.interp(x_new, x_smooth, y_smooth)

# Plot:
plt.plot(x, y, label='x, y');
plt.plot(x_smooth, y_smooth, label='x_smooth, y_smooth');
plt.plot(x_new, y_smooth_new, '-ro', label='x_new, Y_smooth_new', alpha=0.7);
plt.legend(); plt.xlabel('x');

enter image description here

xdze2
  • 3,986
  • 2
  • 12
  • 29