Exemplo n.º 1
0
def getFFT(motionVal):
    fft_len = 30
    fft = np.empty((0, fft_len * 2), dtype=np.float64)
    fourier = np.empty((0, fft_len), dtype=np.float64)
    if not len(motionVal) or len(motionVal[0]) < 41: return [], []
    check = [0] * len(motionVal)
    for i in range(len(motionVal)):
        check[i] = abs(0.98 - motionVal[i][0])
    ind = check.index(min(check))
    motionVal[ind] = motionVal[ind] - 0.98

    vals = np.empty((0, len(motionVal[0]) - 1), dtype=np.float64)
    for i in range(len(motionVal)):
        m = motionVal[i]
        m = savitzky_golay(m, 23, 3)
        m = integrate(m)
        m = savitzky_golay(m, 23, 3)
        vals = np.vstack([vals, m])

        m = np.fft.fft(m)
        m = m[1:fft_len + 1]
        # interlacing the real coefficients and the imaginary coefficients
        a = np.real(m)
        b = np.imag(m)
        m = np.empty((a.size + b.size, ), dtype=a.dtype)
        m[0::2] = a
        m[1::2] = b
        if (max(m) - min(m)) != 0: m = m / (max(m) - min(m))
        fft = np.append(fft, np.array([m]), axis=0)
        fourier = np.append(fourier, np.array([a]), axis=0)
    return fft, vals
def getOneMSR(file, both):

    motionVal = np.zeros((0, 0), dtype=np.float64)

    line = file.readline()
    line = file.readline().rstrip().split()
    while line:

        if line == ['40'] or line == ['80']:
            val = np.zeros((0, 0), dtype=np.float64)
            line = file.readline()
            line = line.rstrip().split()
            check = (len(line) > 1)
            while (len(line) > 1):
                line = line[:3]
                line = [float(x) for x in line]
                val = np.append(val, line)

                if not both: file.readline()

                line = file.readline().rstrip().split()
            if len(val) == 120: val = val[:60]
            val = np.reshape(val, (val.shape[0], 1))  #transpose

            if not len(motionVal): motionVal = val
            else: motionVal = np.hstack([motionVal, val])

    #printMSRMotion(motionVal)

    fft_len = 15
    fft = np.empty((0, fft_len * 2), dtype=np.float64)
    if not len(motionVal): return [], []

    diffs = np.empty((0, len(motionVal[0]) - 1), dtype=np.float64)
    for i in range(len(motionVal)):
        m = motionVal[i]
        m = savitzky_golay(m, 21, 3)
        m = np.diff(m)

        if i not in [24, 25, 36, 37]:
            m = savitzky_golay(m, 21, 3)
            if max(m) - min(m) > 0: m = m / (max(m) - min(m))
            diffs = np.append(diffs, np.array([m]), axis=0)
            m = np.fft.fft(m)
            m = m[:fft_len]
            # interlacing the real coefficients and the imaginary coefficients
            a = np.real(m)
            b = np.imag(m)
            m = np.empty((a.size + b.size, ), dtype=a.dtype)
            m[0::2] = a
            m[1::2] = b
            if (max(m) - min(m)) != 0:
                m = m / (max(m) - min(m))
            fft = np.append(fft, np.array([m]), axis=0)

    file.close()

    return fft, diff
Exemplo n.º 3
0
def getKITFFT(file):
    motionDict = {}

    motionVal = np.zeros((0, 0), dtype=np.float64)

    # ############## READ IN DATA ##############
    for line in file:

        if 'MotionFrame' in line:
            while '/MotionFrame' not in line:

                if 'JointPosition' in line and '</JointPosition>' in line:
                    line = line.lstrip('<JointPosition>|\t').rstrip(
                        '</JointPosition>|\n')
                    line = [float(x) for x in line.split()]
                    if len(line) == 44:
                        line = np.delete(line, [0, 1, 2, 26, 27, 42, 43])
                    val = np.array(line, dtype=np.float64)
                    if len(val) == 39: print(val)

                line = file.readline()

            if len(val) > 3:
                if not len(motionVal): motionVal = np.append(motionVal, val)
                else: motionVal = np.vstack([motionVal, val])

    motionVal = np.transpose(motionVal)

    fft_len = 31
    fftArrays = np.empty((0, fft_len * 2), dtype=np.float64)
    diffs = np.empty((0, motionVal.shape[1] - 1), dtype=np.float64)
    #first_list, last_list = [],[]

    for i in range(motionVal.shape[0]):
        m = motionVal[i]
        m = savitzky_golay(m, 95, 3)  # 71
        m = np.diff(m)
        m = savitzky_golay(m, 95, 3)

        diffs = np.append(diffs, np.array([m]), axis=0)
        #first = np.mean(first_list)
        #last = np.mean(last_list)

        if sum(abs(m)) > 0:
            m = np.fft.fft(m)[:fft_len]
            # interlacing the real coefficients and the imaginary coefficients
            a, b = np.real(m), np.imag(m)
            tmp = np.empty((a.size + b.size, ), dtype=a.dtype)
            tmp[0::2], tmp[1::2] = a, b
            m = tmp
            #m = m / (max(m) - min(m))
            fftArrays = np.append(fftArrays, np.array([m]), axis=0)
        else:
            fftArrays = np.append(fftArrays,
                                  np.array([[0] * (fft_len * 2)]),
                                  axis=0)

    return fftArrays, diffs
Exemplo n.º 4
0
def getOneSMS(file):
    line = file.readline().rstrip()
    while line != 'MOTION':
        line = file.readline().rstrip()
    line = file.readline()
    line = file.readline()

    motionVal = np.zeros((0, 0), dtype=np.float64)
    line = file.readline()
    while line:
        line = line.rstrip().split()
        line = [float(x) for x in line]
        val = np.array(line)
        if not len(motionVal): motionVal = val
        else:
            if len(val): motionVal = np.vstack([motionVal, val])
            else: break
        line = skipLines(file)

    motionVal = np.transpose(motionVal)
    fft_len = 31
    fft = np.empty((0, fft_len * 2), dtype=np.float64)
    if not len(motionVal): return [], []

    diffs = np.empty((0, len(motionVal[0]) - 2), dtype=np.float64)
    motionVal1 = np.empty((0, len(motionVal[0])), dtype=np.float64)

    for i in range(len(motionVal)):
        m = motionVal[i][1:]
        #m = savitzky_golay(m,13,3)
        m = savitzky_golay(m, 45, 3)
        m = np.diff(m)
        #if i not in [27,28,29,33,34,45,46,47,51,52,61,66,67,78,79,84,85]:
        #m = savitzky_golay(m,13,3)
        m = savitzky_golay(m, 45, 3)

        diffs = np.append(diffs, np.array([m]), axis=0)
        motionVal1 = np.append(motionVal1,
                               motionVal[i].reshape(1, len(motionVal[i])),
                               axis=0)
        m = np.fft.fft(m)
        m = m[:fft_len]
        # interlacing the real coefficients and the imaginary coefficients
        a = np.real(m)
        b = np.imag(m)
        m = np.empty((a.size + b.size, ), dtype=a.dtype)
        m[0::2], m[1::2] = a, b
        fft = np.append(fft, np.array([m]), axis=0)

    file.close()
    #print('len(fft)',len(fft))
    #fft = np.delete(fft,[31,32,33,44,45,46,50,59,61,73,74,75],axis=0)
    #diffs = np.delete(diffs,[31,32,33,44,45,46,50,59,61,73,74,75],axis=0)
    #motionVal1 = np.delete(motionVal1,[31,32,33,44,45,46,50,59,61,73,74,75],axis=0)
    #printSMSMotion(motionVal1)
    return fft, diffs
def getMSRMotionFeature(file, ind):

    motionVal = np.zeros((0, 0), dtype=np.float64)

    line = file.readline()
    line = file.readline().rstrip().split()
    while line:
        if line == ['40'] or line == ['80']:
            val = np.zeros((0, 0), dtype=np.float64)
            line = file.readline()
            line = line.rstrip().split()
            check = (len(line) > 1)
            while (len(line) > 1):
                line = line[:3]
                line = [float(x) for x in line]
                val = np.append(val, line)
                if not 0: file.readline()
                line = file.readline().rstrip().split()
            if len(val) == 120: val = val[:60]
            val = np.reshape(val, (val.shape[0], 1))  #transpose
            if not len(motionVal): motionVal = val
            else: motionVal = np.hstack([motionVal, val])
    val = motionVal[ind]
    val = savitzky_golay(val, 91, 3)
    return val
def getMSRAction(file):
    motionVal = np.zeros((0, 0), dtype=np.float64)

    line = file.readline()
    while line:
        val = np.zeros((0, 0), dtype=np.float64)

        for i in range(20):
            line = line.rstrip().split()
            line = [float(x) for x in line[:3]]
            val = np.append(val, line)
            line = file.readline()

        if not len(motionVal): motionVal = val
        else:
            if len(val): motionVal = np.vstack([motionVal, val])
            else: break
    motionVal = np.transpose(motionVal)

    #printMSRMotion(motionVal)

    fft_len = 15
    fft = np.empty((0, fft_len * 2), dtype=np.float64)
    if not len(motionVal): return [], []
    diffs = np.empty((0, len(motionVal[1]) - 1), dtype=np.float64)
    for i in range(len(motionVal)):
        m = motionVal[i]
        m = savitzky_golay(m, 11, 3)  # 21
        m = np.diff(m)
        m = savitzky_golay(m, 11, 3)

        diffs = np.append(diffs, np.array([m]), axis=0)
        m = np.fft.fft(m)
        m = m[:fft_len]
        # interlacing the real coefficients and the imaginary coefficients
        a = np.real(m)
        b = np.imag(m)
        m = np.empty((a.size + b.size, ), dtype=a.dtype)
        m[0::2], m[1::2] = a, b
        fft = np.append(fft, np.array([m]), axis=0)

    file.close()

    return fft, diffs
def printMocapDataMotion(motion):
    w = round(np.sqrt(len(motion)),0)
    h = int(len(motion)/w+1)
    pt.figure()
    for i in range(len(motion)):
        sp = pt.subplot(w,h,i+1)
        m = savitzky_golay(motion[i],131,3)
        pt.plot(m)
        sp.set_title(f_names[i])
    pt.show()
    return None
def printMSRMotion(motion):
    w = round(np.sqrt(len(motion)), 0)
    h = int(len(motion) / w + 1)
    pt.figure()
    for i in range(len(motion)):
        pt.subplot(w, h, i + 1)
        m = savitzky_golay(motion[i], 51, 3)

        pt.plot(m)
    pt.show()

    return None
Exemplo n.º 9
0
def getOneBKL(file):

    motionVal = np.zeros((0, 0), dtype=np.float64)
    for line in file:
        line = list(float(x) for x in line.split()[:3])
        if not len(motionVal): motionVal = np.array(line)
        else: motionVal = np.vstack([motionVal, np.array(line)])

    motionVal = np.transpose(motionVal)
    fft_len = 15
    fft = np.empty((0, fft_len * 2), dtype=np.float64)
    if not len(motionVal): return [], []

    diffs = np.empty((0, len(motionVal[0]) - 1), dtype=np.float64)
    motionVal1 = np.empty((0, len(motionVal[0])), dtype=np.float64)

    for i in range(len(motionVal)):
        m = motionVal[i]
        m = savitzky_golay(m, 121, 3)
        m = np.diff(m)
        m = savitzky_golay(m, 121, 3)
        if max(m) - min(m) > 0: m = m / (max(m) - min(m))
        diffs = np.append(diffs, np.array([m]), axis=0)
        motionVal1 = np.append(motionVal1,
                               motionVal[i].reshape(1, len(motionVal[i])),
                               axis=0)
        m = np.fft.fft(m)
        m = m[:fft_len]
        # interlacing the real coefficients and the imaginary coefficients
        a = np.real(m)
        b = np.imag(m)
        m = np.empty((a.size + b.size, ), dtype=a.dtype)
        m[0::2] = a
        m[1::2] = b
        if (max(m) - min(m)) != 0:
            m = m / (max(m) - min(m))
        fft = np.append(fft, np.array([m]), axis=0)
    return fft, diffs
Exemplo n.º 10
0
def getFFTArrays(file):
    motionVal = np.zeros((0, 0), dtype=np.float64)
    line = file.readline().rstrip().split()
    while line:
        if len(line) == 1 and line[0].isdigit():
            val = np.zeros((0, 0), dtype=np.float64)
            while True:
                line = file.readline().rstrip().split()
                tmp = list(float(x) for x in line[1:])
                val = np.append(val, tmp)
                if line[0] == 'ltoes':
                    if not len(motionVal): motionVal = val
                    else: motionVal = np.vstack([motionVal, val])
                    break
        line = file.readline().rstrip().split()
    motionVal = np.transpose(motionVal)
    fft_len = 31
    fft = np.empty((0, fft_len * 2), dtype=np.float64)
    if not len(motionVal): return [], []

    diffs = np.empty((0, len(motionVal[0]) - 1), dtype=np.float64)
    #diffs = np.empty((0,len(motionVal[0])),dtype=np.float64)
    for i in range(len(motionVal)):
        m = motionVal[i]
        m = savitzky_golay(m, 65, 3)  # originally 125
        m = np.diff(m)
        if i not in [24, 25, 36, 37]:  # delete left and right clavicles
            m = savitzky_golay(m, 65, 3)
            diffs = np.append(diffs, np.array([m]), axis=0)
            m = np.fft.fft(m)[:fft_len]
            # interlacing the real coefficients and the imaginary coefficients
            #m = np.abs(m)
            a, b = np.real(m), np.imag(m)
            m = np.empty((a.size + b.size, ), dtype=a.dtype)
            m[0::2], m[1::2] = a, b
            fft = np.append(fft, np.array([m]), axis=0)
    file.close()
    return fft, diffs
def getOneMocapData(file):
    line = file.readline().rstrip()
    while line != 'MOTION':
        line = file.readline().rstrip()
    line = file.readline()
    line = file.readline()
        
    motionVal = np.zeros((0,0),dtype=np.float64)
    line = file.readline()
    while line:
        line = line.rstrip().split()
        line = [float(x) for x in line]  
        if rotation: 
            line = list([line[i*3],line[i*3+1],line[i*3+2]] for i in range(19))
        else: 
            line = list([line[i*3+3],line[i*3+4],line[i*3+5]] for i in range(19))
        line = list(itertools.chain(*line)) 
        
        #print('len(line)',len(line))
        val = np.array(line)
        if not len(motionVal): motionVal = val
        else:
            if len(val): motionVal = np.vstack([motionVal,val])
            else: break
        line = file.readline()
        line = file.readline()
        line = file.readline()
        line = file.readline()
        
        
    motionVal = np.transpose(motionVal)
    fft_len = 15
    fft = np.empty((0,fft_len*2),dtype=np.float64)
    if not len(motionVal): return [],[]
    
    diffs = np.empty((0,len(motionVal[0])-1),dtype=np.float64)
    motionVal1 = np.empty((0,len(motionVal[0])),dtype=np.float64)
    #print('len(motionVal)',len(motionVal))

    for i in range(len(motionVal)):
        m = motionVal[i]
        m = savitzky_golay(m,13,3)
        m = np.diff(m)
        m = savitzky_golay(m,13,3)
        if max(m)-min(m) > 0: m = m / (max(m)-min(m))
        diffs = np.append(diffs,np.array([m]),axis=0)
        motionVal1 = np.append(motionVal1,motionVal[i].reshape(1,len(motionVal[i])),axis=0)
        m = np.fft.fft(m)
        m = m[:fft_len]
        # interlacing the real coefficients and the imaginary coefficients
        a = np.real(m)
        b = np.imag(m)
        m = np.empty((a.size + b.size,), dtype = a.dtype)
        m[0::2] = a
        m[1::2] = b
        #m = a
        #if (max(m)-min(m)) != 0:
        #    m = m / (max(m) - min(m))
        fft = np.append(fft, np.array([m]),axis=0)
            
    file.close()
    #print('len(fft)',len(fft))
    #fft = np.delete(fft,[31,32,33,44,45,46,50,59,61,73,74,75],axis=0)
    #diffs = np.delete(diffs,[31,32,33,44,45,46,50,59,61,73,74,75],axis=0)
    #motionVal1 = np.delete(motionVal1,[31,32,33,44,45,46,50,59,61,73,74,75],axis=0)
    #printMocapDataMotion(motionVal1)
    return fft, diffs
def get_motion_representation(features, diffs, op):  #3rep
    print('Features are:\n', features)
    n = len(features)  # number of body parts
    tot_rep = []
    if op:
        names = ['Wave two hands', 'Wave one hand','Punching',\
                 'Jumping jacks','Throwing','Bending','Sitdown Standup']
    else:
        names = ['Walking', 'Running', 'Jumping']

    for motion_type in diffs:  # for each motion type
        rep_list = []
        for motion in motion_type:  # for each motion sample
            body_part_acce_list = []
            body_part_nf_list = []
            body_part_sig_f = []
            body_part_fft_list = []
            body_part_pca_list = []
            body_part_ind_list = []
            # get most important frequency of each body part (# of oscillations)
            for i in range(n):  # for each body part/joint
                # get one feature to represent a body part
                feat, ind, _ = choose_joint(i, features, motion,
                                            diffs.index(motion_type))

                body_part_ind_list.append(ind)
                body_part = get_PCA(feat, motion)
                body_part_pca = get_PCA2(feat, motion)
                body_part = savitzky_golay(body_part, 21, 3)

                body_part_acce_list.append(np.diff(body_part))
                body_part_nf_list.append(body_part)
                f = get_significant_frequency(body_part)
                axes_num = len(feat)
                fft_list = []
                for j in range(axes_num):
                    ft = np.fft.fft(motion[feat[j]])[1:31]
                    fft_list.append(np.hstack([np.real(ft), np.imag(ft)]))
                body_part_fft_list.append(fft_list)
                body_part_pca_list.append(body_part_pca)
                body_part_sig_f.append(f)
            body_part_sig_f = np.array(body_part_sig_f)

            # features' relative min magnitudes for each body part/joint
            body_part_mag2 = list(
                min(list(np.std(motion[x]) for x in feat)) for i in range(n))

            # body parts freqs' relative magnitude
            body_part_freq = list(
                np.abs(np.fft.fft(v))[1:31] for v in body_part_nf_list)

            body_part_f_mag = list(max(x) for x in body_part_freq)
            body_part_f_mag = np.array(body_part_f_mag) / max(body_part_f_mag)

            body_part_rgs = list(
                np.std(body_part_pca[0])
                for body_part_pca in body_part_pca_list)

            body_part_arm_dc = get_arm_relations(features, motion)
            e = np.e

            if max(body_part_mag2) != 0:
                body_part_mag2 = np.array(body_part_mag2) / max(body_part_mag2)

            body_part_direc = get_directional_correlations(body_part_fft_list)

            rep = np.hstack(
                [
                    body_part_direc, body_part_sig_f, body_part_mag2,
                    body_part_f_mag
                ]
            )  #, body_part_acce_tmp]) # body_part_f_mag, body_part_mag2, body_part_sig_f

            if np.isnan(rep).any():
                print(rep)
                inds = np.where(np.isnan(rep))[0]
                rep[inds] = 0

            print('rep', ' '.join(str(round(x, 2)) for x in rep))

            rep_list.append(rep)
        tot_rep.append(rep_list)

    return tot_rep