lundi 26 avril 2021

MATLAB's smoothdata function in Python

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




Aucun commentaire:

Enregistrer un commentaire