Skip to content

Instantly share code, notes, and snippets.

@mrrezaie
mrrezaie / osimN101.py
Created June 3, 2023 22:12
Apply filter and time-normalize STO/MOT files
# Written by Mohammadreza Rezaie [kernalnt@gmail.com]
import org.opensim.utils as utils
import os
folder = utils.FileUtils.getInstance().browseForFolder('Choose a dirctory where STO/MOT files are located',0)
if folder==None:
print('Nothing was selected')
@mrrezaie
mrrezaie / LyE.py
Created May 26, 2022 10:04
Maximum lyapunov exponent according to Rosenstein algorithm.
from matplotlib import pyplot as plt
import numpy as np
from sklearn.neighbors import KDTree
def LyE(vec, dim, lag, fs, scale=None, theiler='meanPeriod', ws=10, neighbors=1, plot=False):
'''max lyapunov exponent (local dynamic stability) according to Rosenstein (1993) algorithm.
https://github.com/SjoerdBruijn/LocalDynamicStability
vec: 1D vector
dim: embedding dimention
lag: time delay
@mrrezaie
mrrezaie / RQA.py
Created May 26, 2022 09:56
(cross) Recurrence Quantification Analysis
import numpy as np
from matplotlib import pyplot as plt
from sklearn.metrics import pairwise_distances
from scipy import stats
def RQA(vec1, dim, lag, cross=None, scale=None, norm='euclidean', rescale=None, threshold=['rec',0.05], theiler=None, minLine=2, epoch=None, shift=None, get=False, save=True, show=False, plot=False, output='RQAepoch.txt'):
'''https://en.wikipedia.org/wiki/Recurrence_quantification_analysis#RQA_measures
https://juliadynamics.github.io/DynamicalSystems.jl/latest/rqa/quantification/
diagonal vertical
@mrrezaie
mrrezaie / normalize.py
Created May 26, 2022 09:53
scale data
import numpy as np
def normalize(vec, typ=None):
'''
normalize or scale data
typ: description:
==== ============
'None' do nothing
'range' ** [a,b] ranged between a and b (e.g. [-1,1])
@mrrezaie
mrrezaie / PSD.py
Created May 26, 2022 09:51
Power Spectral Density
import numpy as np
from matplotlib import pyplot as plt
from scipy import signal
def PSD(data, fs, first=None, last=None, plot=False, statistics=False):
'''Power Spectral Density
fs: frequency sampled
The period is the time duration of one cycle.
MNF is an average frequency which is calculated as the sum of product of the EMG power spectrum and the frequency divided by the total sum of the power spectrum.
@mrrezaie
mrrezaie / SSR.py
Created May 26, 2022 09:50
State Space Reconstruction
import numpy as np
def SSR(data, dim, lag, plot=False):
'''State Space Reconstruction
represents all possible states of a system
'''
assert isinstance(data, np.ndarray), 'file type is not ndarray'
if data.ndim == 1:
data = data.reshape(-1,1)
@mrrezaie
mrrezaie / readExp.py
Created May 26, 2022 09:48
Read OpenSim STO and MOT files.
import numpy as np
def readExp(file, sep='\t'):
''' Read OpenSim STO and MOT files
or any other format that the headers are separated from labels and data by 'endheader' line.
can return data and labels, dataFrame, or dict'''
with open(file, mode='r') as f:
while True:
line = f.readline()
if 'endheader' in line:
@mrrezaie
mrrezaie / readExp.m
Created May 26, 2022 09:47
Read OpenSim STO and MOT files
% function struct = readExp(file)
function [data, label] = readExp(file)
%Read OpenSim STO and MOT files Or any other format that
%the headers are separated from labels and data by 'endheader' line.
%can also return struct'''
i = 0
while true
try
data = dlmread(file, '\t', i, 0);
@mrrezaie
mrrezaie / sampEnt.py
Created May 26, 2022 09:43
(cross) Smaple Entropy
from sklearn.neighbors import KDTree
import numpy as np
def sampEnt(vec1, cross=None, scale=None, dim=3, radius=0.25, lag=1, norm='chebyshev'):
'''
scale: normalize input vector
[None]: no normalization
['range', [0,1]] : amplitude normalization [0,1]
['range', [-1,1]] : amplitude normalization [-1,1]
@mrrezaie
mrrezaie / FreeMoment.m
Created May 26, 2022 09:39
Calculate free moment from Kistler (type 3) force plate
% Calculate free moment from Kistler (type 3) force plate
% Input is a text file contains analog signals (8 channels for each force plate)
% REF: http://isbweb.org/software/movanal/vaughan/kistler.pdf
clear all; clc; close all
i = 0;
while true
try
data = dlmread('atena_zarei_GR1.txt', '\t', i, 1);
% 'atena_zarei_GR1.txt'