#%% Get acceleration, LF and GF time = glm_df.loc[:, 'time'].to_numpy() accX = glm_df.loc[:, 'LowAcc_X'].to_numpy() * (-9.81) accX = accX - np.nanmean(accX[baseline]) GF = glm_df.loc[:, 'GF'].to_numpy() GF = GF - np.nanmean(GF[baseline]) LFv = TFx_thumb + TFx_index LFh = TFz_thumb + TFz_index LF = np.hypot(LFv, LFh) # %%Filter data freqAcq = 800 #Frequence d'acquisition des donnees freqFiltAcc = 20 #Frequence de coupure de l'acceleration freqFiltForces = 20 #Frequence de coupure des forces accX = glm.filter_signal(accX, fs=freqAcq, fc=freqFiltAcc) GF = glm.filter_signal(GF, fs=freqAcq, fc=freqFiltForces) LF = glm.filter_signal(LF, fs=freqAcq, fc=freqFiltForces) LFv = glm.filter_signal(LFv, fs=freqAcq, fc=freqFiltForces) LFh = glm.filter_signal(LFh, fs=freqAcq, fc=freqFiltForces) #%% CUTTING THE TASK INTO SEGMENTS (your first task) pk = signal.find_peaks(accX, prominence=5, width=(100, 1000)) ipk = pk[0] cycle_starts = ipk[:-1] cycle_ends = ipk[1:] - 1 #%% Compute derivative of LF dGF = der.derive(GF, 800) dGF = glm.filter_signal(dGF, fs=freqAcq, fc=10)
def get_mu_points(y_COP, tf_h, tf_v, NF, fs=1000, y_thresh=0.5, tf_thresh=0.25, nf_thresh=0.25): if len(y_COP) != len(tf_h) or len(tf_h) != len(tf_v) or len(tf_v) != len( NF): raise NameError('Vectors must be the same length') # Filter signals fc = 40 y_COP = glm.filter_signal(y_COP - np.nanmean(y_COP), fs, fc, 4) dy = der.derive(y_COP, fs) tf_h = glm.filter_signal(tf_h, fs, fc) tf_v = glm.filter_signal(tf_v, fs, fc) NF = glm.filter_signal(NF, fs, fc) # Compute the norm of the normal and tangential forces and TF/NF NF = abs(NF) TF = np.sqrt(np.multiply(tf_h, tf_h) + np.multiply(tf_v, tf_v)) ratio = TF / NF # Find first index where NF>NF_thresh i0 = np.nonzero(NF > nf_thresh)[0][0] # Find first index where NF>NF_thresh starting from the end if NF[-1] > nf_thresh: iend = len(NF) - 1 else: reversed_NF = NF[::-1] iend = np.nonzero(reversed_NF > nf_thresh)[0][0] iend = len(NF) - iend # Find where tangential force changes sign. This marks the beginnings of the # useful zones iRoots = i0 + np.nonzero(np.diff(np.sign(tf_v[i0:iend])))[0] iStart = np.insert(iRoots, 0, i0) #Add i0 as the beginning of the first usefull zone # Find the end of the search zones by looking at the COP displacement # (end zone is reached when displacement is greater than y_thresh) iEnd = np.zeros(len(iStart), dtype=int) out = np.zeros(len(iStart), dtype=bool) out[0] = True out[-1] = True for i in range(0, len(iStart) - 1): y_loc = y_COP[iStart[i]:iStart[i + 1]] dy_loc = dy[iStart[i]:iStart[i + 1]] #range(0,np.floor((iStart[i+1]-iStart[i])/2)) #print('%d' %(np.floor((iStart[i+1]-iStart[i])/2))) slope = np.nanmean(dy_loc[0:np.floor((iStart[i + 1] - iStart[i]) / 2).astype(np.int)]) if slope < 0: extrem = min(y_COP[iStart[i]:iStart[i + 1]]) else: extrem = max(y_COP[iStart[i]:iStart[i + 1]]) displacement_thresh = y_thresh * abs(extrem - y_loc[0]) # avant 1 tf_loc = abs(np.mean(tf_v[iStart[i]:iStart[i + 1]])) nf_loc = np.mean(NF[iStart[i]:iStart[i + 1]]) if displacement_thresh > 0.002 and tf_loc > tf_thresh and nf_loc > nf_thresh: i_end_loc = np.nonzero( abs(y_loc - y_loc[0]) >= displacement_thresh)[0][0] if i_end_loc == 0: out[i] = True else: iEnd[i] = (iStart[i] + i_end_loc - 1).astype(np.int) else: out[i] = True iStart = iStart[np.logical_not(out)] iEnd = iEnd[np.logical_not(out)] check_indexes = iEnd - iStart if any(check_indexes <= 0): raise NameError( 'Some start indexes are equal to - or bigger than - their corresponding stop indexes' ) # Search for slip points as the points where TF/NF is maximal. TF/NF then # theoritically corresponds to the static coefficient of friction (mu) nz = len(iStart) mu = np.zeros(nz) slip_indexes = np.zeros(nz, dtype=int) directions = np.zeros(nz, dtype=int) for i in range(0, nz): mu_loc = ratio[iStart[i]:iEnd[i]] imax = np.argmax(mu_loc) dy_loc = dy[iStart[i]:iEnd[i]] slip_indexes[i] = iStart[i] + imax - 1 mu[i] = mu_loc[imax] directions[i] = np.sign(np.nanmean(dy_loc)) # Some things were removes from the Matlab version here # Remove slip points corresponding to abnormally low values of TF or NF discard = (abs(tf_v[slip_indexes]) < tf_thresh).astype(bool) | ( NF[slip_indexes] < nf_thresh).astype(bool) slip_indexes = slip_indexes[np.logical_not(discard)] mu = mu[np.logical_not(discard)] directions = directions[np.logical_not(discard)] # Remove outliers (code missing, can be added later if needed) # Return values return mu, slip_indexes, iStart, iEnd
# Initialisation des structures servant à stocker les vakeurs de TF/NF et NF # aux moments des glissements all_mu_points_thumb = [] all_NF_thumb = [] all_mu_points_index = [] all_NF_index = [] #%% Filtrage des donnees freqAcq = 800 # Frequence d'acquisition des donnees freqFiltForces = 20 # Frequence de coupure du filtrage des forces (peut etre modifié) for filepath in filepaths: glm_df = glm.import_data(filepath) for i in range(21, 43): glm_df.iloc[:, i] = glm.filter_signal(glm_df.iloc[:, i], freqAcq, freqFiltForces) #%% Extraction des signaux et mise a zero des forces en soustrayant la valeur # moyenne des 500 premieres ms baseline = range(0, 400) # Normal Force exerted by the thumb NF_thumb = glm_df.loc[:, 'Fygl'] - np.nanmean(glm_df.loc[baseline, 'Fygl']) # Vertical Tangential Force exerted by the thumb TFx_thumb = glm_df.loc[:, 'Fxgl'] - np.nanmean(glm_df.loc[baseline, 'Fxgl']) #Horizontal Tangential Force exerted by the thumb TFz_thumb = glm_df.loc[:, 'Fzgl'] - np.nanmean(glm_df.loc[baseline, 'Fzgl']) # Normal Force exerted by the index NF_index = -(glm_df.loc[:, 'Fygr'] -
for trial in range(1,ntrials+1): glm_path = "DataGroupe4/%s_00%d.glm" % (s,trial) glm_df = glm.import_data(glm_path) baseline = range(0,400) NF_thumb = glm_df.loc[:,'Fygl']-np.nanmean(glm_df.loc[baseline,'Fygl']) TFx_thumb = glm_df.loc[:,'Fxgl']-np.nanmean(glm_df.loc[baseline,'Fxgl']) TFz_thumb = glm_df.loc[:,'Fzgl']-np.nanmean(glm_df.loc[baseline,'Fzgl']) NF_index = -(glm_df.loc[:,'Fygr']-np.nanmean(glm_df.loc[baseline,'Fygr'])) TFx_index = glm_df.loc[:,'Fxgr']-np.nanmean(glm_df.loc[baseline,'Fxgr']) TFz_index = glm_df.loc[:,'Fzgr']-np.nanmean(glm_df.loc[baseline,'Fzgr']) time = glm_df.loc[:,'time'].to_numpy() GF = glm_df.loc[:,'GF'].to_numpy() freqAcq=800 #Frequence d'acquisition des donnees freqFiltAcc=20 #Frequence de coupure de l'acceleration freqFiltForces=20 #Frequence de coupure des forces GF = glm.filter_signal(GF, fs = freqAcq, fc = freqFiltForces) segmentations=np.array([]) mB0= glm_df['Metronome_b0'].to_numpy() mB1= glm_df['Metronome_b1'].to_numpy() mB2= glm_df['Metronome_b2'].to_numpy() start=4000 end=30959 mB0=mB0[start:end] mB1=mB1[start:end] mB2=mB2[start:end] bip=False for i in range( len (mB0)): if((mB0[i]+ mB1[i]+mB2[i])!=3 and bip==False ): segmentations=np.append(segmentations,int(i+start)) bip=True
'Fzgr']) #%% Get acceleration, LF and GF time = glm_df.loc[:, 'time'].to_numpy() GFSP = glm_df.loc[:, 'GF'].to_numpy() LFvSP = TFx_thumb + TFx_index LFhSP = TFz_thumb + TFz_index LFSP = np.hypot(LFvSP, LFhSP) # %%Filter data freqAcq = 800 #Frequence d'acquisition des donnees freqFiltAcc = 20 #Frequence de coupure de l'acceleration freqFiltForces = 20 #Frequence de coupure des forces GFSP = glm.filter_signal(GFSP, fs=freqAcq, fc=freqFiltForces) LFSP = glm.filter_signal(LFSP, fs=freqAcq, fc=freqFiltForces) LFvSP = glm.filter_signal(LFvSP, fs=freqAcq, fc=freqFiltForces) LFhSP = glm.filter_signal(LFhSP, fs=freqAcq, fc=freqFiltForces) #%% segmentations segmentations = np.array([]) mB0 = glm_df['Metronome_b0'].to_numpy() mB1 = glm_df['Metronome_b1'].to_numpy() mB2 = glm_df['Metronome_b2'].to_numpy() start = 4000 end = 30959 mB0 = mB0[start:end] mB1 = mB1[start:end] mB2 = mB2[start:end]
# Chemins d'acces aux fichiers (A MODIFIER) filepath_strong = '.\S10_CF_004.glm' filepath_medium = '.\S10_CF_005.glm' filepath_weak = '.\S10_CF_006.glm' # Importation des donnes glm_strong_df = glm.import_data(filepath_strong) glm_medium_df = glm.import_data(filepath_medium) glm_weak_df = glm.import_data(filepath_weak) #%% Filtrage des donnees freqAcq=800 # Frequence d'acquisition des donnees freqFiltForces=20; # Frequence de coupure du filtrage des forces (peut etre modifié) for i in range(21,43): glm_strong_df.iloc[:,i]=glm.filter_signal(glm_strong_df.iloc[:,i],freqAcq,freqFiltForces) glm_medium_df.iloc[:,i]=glm.filter_signal(glm_medium_df.iloc[:,i],freqAcq,freqFiltForces) glm_weak_df.iloc[:,i]=glm.filter_signal(glm_weak_df.iloc[:,i],freqAcq,freqFiltForces) # Frequence de coupure du filtrage du COP (peut etre modifié) # COP = Center Of Pressure. Il s'agit de la résultante des forces appliquées # par le doigt sur le capteur. Cela donne donc une idee de la position du doigt # sur celui-ci freqFiltCOP=40 glm_strong_df.loc[:,'OPxgr']=glm.filter_signal(glm_strong_df.loc[:,'OPxgr'],freqAcq,freqFiltCOP) glm_strong_df.loc[:,'OPxgl']=glm.filter_signal(glm_strong_df.loc[:,'OPxgl'],freqAcq,freqFiltCOP) glm_medium_df.loc[:,'OPxgr']=glm.filter_signal(glm_medium_df.loc[:,'OPxgr'],freqAcq,freqFiltCOP) glm_medium_df.loc[:,'OPxgl']=glm.filter_signal(glm_medium_df.loc[:,'OPxgl'],freqAcq,freqFiltCOP) glm_weak_df.loc[:,'OPxgr']=glm.filter_signal(glm_weak_df.loc[:,'OPxgr'],freqAcq,freqFiltCOP) glm_weak_df.loc[:,'OPxgl']=glm.filter_signal(glm_weak_df.loc[:,'OPxgl'],freqAcq,freqFiltCOP)