def twosigma(data, window, w):
    sp = data[:, 1]
    time = data[:, 0]
    sp = abs(sp)
    index = []

    for i in range(len(sp)):
        if sp[i] == 50:
            index.append(i)
    time = np.delete(time, index)
    sp = np.delete(sp, index)

    speed = sp

    for i in range(len(speed)):
        if i > window or len(speed) - i > window:
            mean = np.mean(speed[i - window:i + window])
        elif i < window:
            mean = np.mean(speed[0:i + window])
        elif len(speed) - i < window:
            mean = np.mean(speed[i - window:len(speed)])

        if abs((speed[i] - mean) / mean) > 0.1:
            speed[i] = mean
    speed = signal.medfilt(speed, w)
    return (time, speed)
def trace_filter(data, window, w):
    sp = data[:, 1]
    time = data[:, 0]
    sp = abs(sp)
    index = []

    for i in range(len(sp)):
        if sp[i] > 49 and sp[i] < 51 or sp[i] < 10:
            index.append(i)
    time = np.delete(time, index)
    sp = np.delete(sp, index)

    speed = sp

    for i in range(len(speed)):
        if i > window or len(speed) - i > window:
            thresh = np.mean(speed[i - window:i + window])
        elif i < window:
            thresh = np.mean(speed[0:i + window])
        elif len(speed) - i < window:
            thresh = np.mean(speed[i - window:len(speed)])

        if speed[i] < thresh:
            speed[i] = thresh
    speed = signal.medfilt(speed, w)

    return (time, speed)
def medianfilter2(data, w):
    sp = data[:, 1]
    time = data[:, 0]
    index = []

    for i in range(len(sp)):
        if sp[i] > -55 and sp[i] < 51:
            index.append(i)
    time = np.delete(time, index)
    sp = np.delete(sp, index)

    speed = sp
    #speed=signal.medfilt(speed,w)

    return (time, speed)
def median(data, w):
    sp = data[:, 1]
    time = data[:, 0]
    sp = abs(sp)
    index = []

    for i in range(len(sp)):
        if sp[i] > 49 and sp[i] < 51 or sp[i] < 10:
            index.append(i)
    time = np.delete(time, index)
    sp = np.delete(sp, index)

    speed = sp

    speed = signal.medfilt(speed, w)

    return (time, speed)