Пример #1
0
    def ema_test(self):
        """ema_test: this test is checks the calculation of exponential moving average"""

        sourcelist = [
            0.9676834571, 0.9517338109, 0.9469666224, 0.9455741929,
            0.937271954, 0.9756308369, 0.963667954, 0.9783003172, 0.9791772432,
            0.9630688213, 0.9636879293, 0.9581131469, 0.9749188851, 0.96132287,
            0.9594692316, 0.9517417142, 0.9580682981, 0.9694806605,
            0.9705071034, 0.9741733322, 0.9656285119, 0.9797829703,
            0.9476657225, 0.9505700439, 0.9769535092, 0.9628920433,
            0.9886532632, 0.9749905082, 0.9622008273, 0.9588126694,
            0.9585201885, 0.9656285119, 0.9635884074, 0.9637201077,
            0.9486464305, 0.9448569633, 0.9548531757, 0.9413523349,
            0.9437067169, 0.9295829852, 0.9443785543, 0.9266972306,
            0.9272177018, 0.9396456813, 0.948808991, 0.9573977701, 0.970075925,
            0.9793470801, 0.9840866322, 0.9765331821, 0.9656885071,
            0.9629277499, 0.9614462269, 0.9738112064, 0.949794186,
            0.9964832677, 0.9958061173, 0.9737453088, 0.9641612591,
            0.9680686611, 0.9796823214, 0.9715538306, 0.9917106859,
            0.961369319, 0.960533384, 0.9630009426, 0.9597456627, 0.9426550241,
            0.9571523382, 0.9441424433, 0.9533811307, 0.9547935049,
            0.9595538268, 0.9522523389, 0.9563979183, 0.9743059924,
            0.963151262, 0.9664897915, 0.9625850612, 0.9878803841, 0.973709362,
            0.9593169424, 0.9501199574, 0.9492373943, 0.9438450032,
            0.9455317748, 0.967039832, 0.9616603865, 0.9824073673,
            0.9699467778, 0.9750983573, 0.9927066538, 0.9729307196,
            0.9851261483, 0.9534832401, 0.9562380523, 0.9606531787,
            0.967532279, 0.9584946534, 0.9740792079
        ]
        target = 0.60700186991

        result = emafit.ema(sourcelist)

        self.assertAlmostEqual(result, target)
Пример #2
0
    def ema_test(self):

        sourcelist = [0.9676834571,0.9517338109,0.9469666224,0.9455741929,0.937271954,0.9756308369,0.963667954,0.9783003172,
                      0.9791772432,0.9630688213,0.9636879293, 0.9581131469,0.9749188851,0.96132287,0.9594692316,0.9517417142,
                      0.9580682981,0.9694806605,0.9705071034,0.9741733322,0.9656285119,0.9797829703,0.9476657225,0.9505700439,
                      0.9769535092,0.9628920433,0.9886532632,0.9749905082,0.9622008273,0.9588126694,0.9585201885,0.9656285119,
                      0.9635884074,0.9637201077,0.9486464305,0.9448569633,0.9548531757,0.9413523349,0.9437067169,0.9295829852,
                      0.9443785543,0.9266972306,0.9272177018,0.9396456813,0.948808991,0.9573977701,0.970075925,0.9793470801,
                      0.9840866322,0.9765331821,0.9656885071,0.9629277499,0.9614462269,0.9738112064,0.949794186,0.9964832677,
                      0.9958061173,0.9737453088,0.9641612591,0.9680686611,0.9796823214,0.9715538306,0.9917106859,0.961369319,
                      0.960533384,0.9630009426,0.9597456627,0.9426550241,0.9571523382,0.9441424433,0.9533811307,0.9547935049,
                      0.9595538268,0.9522523389,0.9563979183,0.9743059924,0.963151262,0.9664897915,0.9625850612,0.9878803841,0.973709362,
                      0.9593169424,0.9501199574,0.9492373943,0.9438450032,0.9455317748,0.967039832,0.9616603865,0.9824073673,
                      0.9699467778,0.9750983573,0.9927066538,0.9729307196,0.9851261483,0.9534832401,0.9562380523,0.9606531787,
                      0.967532279,0.9584946534,0.9740792079]
        target = 0.60700186991


        result = emafit.ema(sourcelist)

        self.assertAlmostEqual(result,target)
Пример #3
0
def calc_features(fullarray, micannot, samp_rate):

    #vect_c not used

    pre_win = [0, 100]  #pre-window 1s

    impact_max = [50, 150]  #1s
    imp_win = [50, 650]  # impact window 6s
    post_win = [300, 1200]  #9 s, based on the paper it should be 250-1200

    datAr = []  #for vector magnitude
    datXa = []  #for X values
    datYa = []  #for Y values
    datZa = []  #for Z values

    datXg = []
    datYg = []
    datZg = []

    for tempdata in fullarray:
        datAr.append(float(tempdata[6]))
        datXa.append(float(tempdata[0]))
        datYa.append(float(tempdata[1]))
        datZa.append(float(tempdata[2]))

        datXg.append(float(tempdata[3]))
        datYg.append(float(tempdata[4]))
        datZg.append(float(tempdata[5]))

    #if micannot == 2:
#        print "fall"
#else:
#    print "non-fall"
#plt.plot(datAr)
#plt.show()

#******************************************************************************************************************************
#minimum pre impact
    min_pre = min(datAr[pre_win[0]:pre_win[1]])

    #minimum value impact
    #min_imp = min(datAr[imp_win[0]:imp_win[1]])

    #minimum post imp
    #min_post = min(datAr[post_win[0]:post_win[1]])

    #******************************************************************************************************************************
    #maximum value pre-impact
    #max_pre = max(datAr[pre_win[0]:pre_win[1]])

    #maximum value impact
    max_imp = max(datAr[impact_max[0]:impact_max[1]])

    #maximum value post
    #max_post = max(datAr[post_win[0]:post_win[1]])
    #******************************************************************************************************************************
    #mean for pre-impact event
    meanList1 = datAr[pre_win[0]:pre_win[1]]
    meanVal1 = numpy.mean(meanList1)

    #******************************************************************************************************************************
    #mean for impact
    meanList2 = datAr[imp_win[0]:imp_win[1]]
    meanVal2 = numpy.mean(meanList2)

    #******************************************************************************************************************************
    #mean for post-impact
    meanList3 = datAr[post_win[0]:post_win[1]]
    meanVal3 = numpy.mean(meanList3)

    #******************************************************************************************************************************
    #Root mean square for pre-impact
    rmsList1 = datAr[pre_win[0]:pre_win[1]]
    rms1 = sqrt(mean(numpy.array(rmsList1)**2))

    #Root mean square for impact
    rmsList2 = datAr[imp_win[0]:imp_win[1]]
    rms2 = sqrt(mean(numpy.array(rmsList2)**2))

    #Root mean square for post-impact
    rmsList3 = datAr[post_win[0]:post_win[1]]
    rms3 = sqrt(mean(numpy.array(rmsList3)**2))

    #******************************************************************************************************************************
    #variance for pre-impact
    variance1 = numpy.var(datAr[pre_win[0]:pre_win[1]], ddof=1)

    #variance for impact
    variance2 = numpy.var(datAr[imp_win[0]:imp_win[1]], ddof=1)

    #variance for post-impact
    variance3 = numpy.var(datAr[post_win[0]:post_win[1]], ddof=1)

    #******************************************************************************************************************************
    #velocity in pre-impact
    velo1 = integrator.integrate(datAr[pre_win[0]:pre_win[1]])

    #velocity in impact
    velo2 = integrator.integrate(datAr[imp_win[0]:imp_win[1]])

    #velocity in post-impact
    velo3 = integrator.integrate(datAr[post_win[0]:post_win[1]])

    #******************************************************************************************************************************
    #energy in pre-impact
    win1x = datXa[pre_win[0]:pre_win[1]]
    win1y = datYa[pre_win[0]:pre_win[1]]
    win1z = datZa[pre_win[0]:pre_win[1]]
    energy1 = energyFeat.energyCalc(win1x, win1y, win1z)

    #energy in impact
    win2x = datXa[imp_win[0]:imp_win[1]]
    win2y = datYa[imp_win[0]:imp_win[1]]
    win2z = datZa[imp_win[0]:imp_win[1]]
    energy2 = energyFeat.energyCalc(win2x, win2y, win2z)

    #energy in post-impact
    win3x = datXa[post_win[0]:post_win[1]]
    win3y = datYa[post_win[0]:post_win[1]]
    win3z = datZa[post_win[0]:post_win[1]]
    energy3 = energyFeat.energyCalc(win3x, win3y, win3z)

    #******************************************************************************************************************************
    #signal magnitude are in pre-impact
    sma1 = smaFeat.smafeat(datXa[pre_win[0]:pre_win[1]],
                           datYa[pre_win[0]:pre_win[1]],
                           datZa[pre_win[0]:pre_win[1]])

    #signal magnitude are in impact
    sma2 = smaFeat.smafeat(datXa[imp_win[0]:imp_win[1]],
                           datYa[imp_win[0]:imp_win[1]],
                           datZa[imp_win[0]:imp_win[1]])

    #signal magnitude are in pre-impact
    sma3 = smaFeat.smafeat(datXa[post_win[0]:post_win[1]],
                           datYa[post_win[0]:post_win[1]],
                           datZa[post_win[0]:post_win[1]])
    #******************************************************************************************************************************
    #exponential moving average in pre-impact
    ewma1 = emafit.ema(datAr[pre_win[0]:pre_win[1]])

    #exponential moving average in impact
    ewma2 = emafit.ema(datAr[imp_win[0]:imp_win[1]])

    #exponential moving average in post-impact
    ewma3 = emafit.ema(datAr[post_win[0]:post_win[1]])

    #******************************************************************************************************************************

    datfeat = [
        min_pre, max_imp, meanVal1, meanVal2, meanVal3, rms1, rms2, rms3,
        variance1, variance2, variance3, velo1, velo2, velo3, energy1, energy2,
        energy3, sma1, sma2, sma3, ewma1, ewma2, ewma3, micannot
    ]

    return datfeat
Пример #4
0
def featurescom (fullarray, micannot,act_state):

    samp_rate = source_var.sampling_rate()
    pre_win = [0, samp_rate] #pre-window
    imp_win = [(samp_rate/2)-1, int(6.5 * samp_rate)] # impact window
    post_win = [int((2.5 * samp_rate))-1, 12 * samp_rate] #post impact window

    max_win = [(samp_rate/2)-1, int(1.5 * samp_rate)] # window for search max value
    tilt_sample_impact = [(2*samp_rate)-1, 3*samp_rate]
    tilt_sample_post = [(3 * samp_rate) - 1, 12 * samp_rate] # window for tilt-angle

    datAr = [] #for vector magnitude
    datXa= [] #for X values
    datYa= [] #for Y values
    datZa= [] #for Z values

    datXg=[]
    datYg=[]
    datZg=[]

    for tempdata in fullarray:
        datAr.append(float(tempdata[6]))
        datXa.append(float(tempdata[0]))
        datYa.append(float(tempdata[1]))
        datZa.append(float(tempdata[2]))

        datXg.append(float(tempdata[3]))
        datYg.append(float(tempdata[4]))
        datZg.append(float(tempdata[5]))

    #******************************************************************************************************************************
    #minimum value feature
    minVal = min(datAr[pre_win[0]:pre_win[1]])

    #******************************************************************************************************************************
    #maximum value feature
    maxVal = max(datAr[max_win[0]:max_win[1]])

    #******************************************************************************************************************************
    #mean for pre-impact event
    meanList1 = datAr[pre_win[0]:pre_win[1]]
    meanVal1 = numpy.mean(meanList1)

    #******************************************************************************************************************************
    #mean for impact
    meanList2 = datAr[imp_win[0]:imp_win[1]]
    meanVal2 = numpy.mean(meanList2)

    #******************************************************************************************************************************
    #mean for post-impact
    meanList3 = datAr[post_win[0]:post_win[1]]
    meanVal3 = numpy.mean(meanList3)

    #******************************************************************************************************************************
    #Root mean square for pre-impact
    rmsList1 = datAr[pre_win[0]:pre_win[1]]
    rms1 = sqrt(mean(numpy.array(rmsList1)**2))

    #Root mean square for impact
    rmsList2 = datAr[imp_win[0]:imp_win[1]]
    rms2 = sqrt(mean(numpy.array(rmsList2)**2))

    #Root mean square for post-impact
    rmsList3 = datAr[post_win[0]:post_win[1]]
    rms3 = sqrt(mean(numpy.array(rmsList3)**2))

    #******************************************************************************************************************************
    #variance for pre-impact
    variance1 = numpy.var(datAr[pre_win[0]:pre_win[1]],ddof=1)

    #variance for impact
    variance2 = numpy.var(datAr[imp_win[0]:imp_win[1]],ddof=1)

    #variance for post-impact
    variance3 = numpy.var(datAr[post_win[0]:post_win[1]],ddof=1)

    #******************************************************************************************************************************
    #velocity in pre-impact
    velo1 = integrator.integrate(datAr[pre_win[0]:pre_win[1]])

    #velocity in impact
    velo2 = integrator.integrate(datAr[imp_win[0]:imp_win[1]])

    #velocity in post-impact
    velo3 = integrator.integrate(datAr[post_win[0]:post_win[1]])

    #******************************************************************************************************************************
    #energy in pre-impact
    win1x = datXa[pre_win[0]:pre_win[1]]
    win1y = datYa[pre_win[0]:pre_win[1]]
    win1z = datZa[pre_win[0]:pre_win[1]]
    energy1 = energyFeat.energyCalc(win1x,win1y,win1z)

    #energy in impact
    win2x = datXa[imp_win[0]:imp_win[1]]
    win2y = datYa[imp_win[0]:imp_win[1]]
    win2z = datZa[imp_win[0]:imp_win[1]]
    energy2 = energyFeat.energyCalc(win2x,win2y,win2z)

     #energy in post-impact
    win3x = datXa[post_win[0]:post_win[1]]
    win3y = datYa[post_win[0]:post_win[1]]
    win3z = datZa[post_win[0]:post_win[1]]
    energy3 = energyFeat.energyCalc(win3x,win3y,win3z)

    #******************************************************************************************************************************
    #signal magnitude are in pre-impact
    sma1 = smaFeat.smafeat(datXa[pre_win[0]:pre_win[1]],datYa[pre_win[0]:pre_win[1]],datZa[pre_win[0]:pre_win[1]])

    #signal magnitude are in impact
    sma2 = smaFeat.smafeat(datXa[imp_win[0]:imp_win[1]],datYa[imp_win[0]:imp_win[1]],datZa[imp_win[0]:imp_win[1]])

    #signal magnitude are in pre-impact
    sma3 = smaFeat.smafeat(datXa[post_win[0]:post_win[1]],datYa[post_win[0]:post_win[1]],datZa[post_win[0]:post_win[1]])
    #******************************************************************************************************************************
    #exponential moving average in pre-impact
    ewma1 = emafit.ema(datAr[pre_win[0]:pre_win[1]])

    #exponential moving average in impact
    ewma2 = emafit.ema(datAr[imp_win[0]:imp_win[1]])

    #exponential moving average in post-impact
    ewma3 = emafit.ema(datAr[post_win[0]:post_win[1]])

    #******************************************************************************************************************************
    #Tilt Angle in pre-impact
    all_angle_1 = mytilt.mytilt(datXa[pre_win[0]:pre_win[1]],datYa[pre_win[0]:pre_win[1]],datZa[pre_win[0]:pre_win[1]],
                                datXg[pre_win[0]:pre_win[1]],datYg[pre_win[0]:pre_win[1]],datZg[pre_win[0]:pre_win[1]])
    angle_y_1 = numpy.max(numpy.abs(numpy.array(all_angle_1[0])))
    angle_z_1 = numpy.max(numpy.abs(numpy.array(all_angle_1[1])))

    #Tilt Angle in impact
    all_angle_2 = mytilt.mytilt(datXa[tilt_sample_impact[0]:tilt_sample_impact[1]],datYa[tilt_sample_impact[0]:tilt_sample_impact[1]],
                               datZa[tilt_sample_impact[0]:tilt_sample_impact[1]],datXg[tilt_sample_impact[0]:tilt_sample_impact[1]],
                               datYg[tilt_sample_impact[0]:tilt_sample_impact[1]],datZg[tilt_sample_impact[0]:tilt_sample_impact[1]])
    angle_y_2 = numpy.max(numpy.abs(numpy.array(all_angle_2[0])))
    angle_z_2 = numpy.max(numpy.abs(numpy.array(all_angle_2[1])))

    #Tilt Angle in post-impact
    all_angle_3 = mytilt.mytilt(datXa[tilt_sample_post[0]:tilt_sample_post[1]],datYa[tilt_sample_post[0]:tilt_sample_post[1]],
                               datZa[tilt_sample_post[0]:tilt_sample_post[1]],datXg[tilt_sample_post[0]:tilt_sample_post[1]],
                              datYg[tilt_sample_post[0]:tilt_sample_post[1]],datZg[tilt_sample_post[0]:tilt_sample_post[1]])
    angle_y_3 = numpy.max(numpy.abs(numpy.array(all_angle_3[0])))
    angle_z_3 = numpy.max(numpy.abs(numpy.array(all_angle_3[1])))


    #******************************************************************************************************************************

    annot = micannot

    #datfeat = [minVal, maxVal, meanVal1, meanVal2, meanVal3, rms1, rms2, rms3, variance1, variance2, variance3, velo1,
    #          velo2, velo3, energy1, energy2, energy3, sma1, sma2, sma3, ewma1, ewma2, ewma3, act_state, annot]
    datfeat = [minVal, maxVal, meanVal1, meanVal2, meanVal3, rms1, rms2, rms3, variance1, variance2, variance3, velo1,
               velo2, velo3, energy1, energy2, energy3, sma1, sma2, sma3, ewma1, ewma2, ewma3, angle_y_1, angle_z_1,
               angle_y_2, angle_z_2,angle_y_3,angle_z_3,act_state, annot]

    return datfeat