finished the transformation from MATLAB code to Python code