1

I have the following code in MATLAB for generation random trajectory:

N = 20;
scale = 40;
alpha = 0.8;

x = ones(N, 1);
y = ones(N, 1);
d = round(smoothdata(rand(N,2)*scale-(scale*alpha/2)));

for i = 2:N
    x(i) = x(i-1) + d(i, 1);
    y(i) = y(i-1) + d(i, 2);
end

This code generates a random trajectory, which I can plot as plot(x,y). Then, I apply some filtering on the obtained curve.

My question is, how can convert this MATLAB's code to Python, to obtain similar randomly generated trajectories? In Python, I want to write something like this:

import numpy as np

N = 20
scale = 40
alpha = 0.8

x = np.ones(N)
y = np.ones(N)
d = np.around(some_smoothing_function(np.random.rand(2, N) * scale - (scale * alpha / 2)))

for i in range(1, N):
    x[i] = x[i-1] + d[0][i]
    y[i] = y[i-1] + d[1][i]

What can I use as some_smoothing_function? It seems that Python does not have a function, that would do the same (or similar) as MATLAB's smoothdata function. And if I don't apply any function, then pairs of points (x[i],y[i]) are just too random and the trajectory does not look good.

Example of a good trajectory, which I want to create (this is what I got in MATLAB): enter image description here

Jan
  • 75
  • 6
  • 1
    There are so many options and blogs. Google for "smooth noisy data pyhon". – PaulusHub Apr 26 '21 at 11:27
  • Relevant: https://stackoverflow.com/q/20618804/1959808 – 0 _ Apr 26 '21 at 11:36
  • Relevant: https://pypi.org/project/smoothfit/ – 0 _ Apr 26 '21 at 11:36
  • I tried using the following 2 functions: ```from scipy.ndimage import gaussian_filter``` and ```from scipy.signal import savgol_filter```, but it looks like that I always get almost a straight line, which is not fine for applying my filtering on it, and I would prefer to obtain some more complex curve. – Jan Apr 26 '21 at 11:38
  • How does the result compare with smooth method contributed below (if you like that you can port it to python): https://www.mathworks.com/matlabcentral/fileexchange/17986-supersmoother – PaulusHub Apr 26 '21 at 12:00

0 Answers0