Пример #1
0
def cov(X, Y, biased=False):
    assert (X.shape == Y.shape
            and len(X.shape) == 1), "Expected data vectors of equal length"
    assert len(X) > 1, "At least 2 data points are required"

    X = X - np.mean(X)
    Y = Y - np.mean(Y)
    denom = len(X) if biased else len(X) - 1

    return (np.sum(X * Y)) / denom
Пример #2
0
             dtype=np.uint8)
b = np.array(
    [range(2**56 - 3, 2**56),
     range(2**16 - 3, 2**16),
     range(2**8 - 3, 2**8)],
    dtype=np.float)
print(np.sort(a, axis=None))
print(np.sort(b, axis=None))
print(np.sort(a, axis=0))
print(np.sort(b, axis=0))
print(np.sort(a, axis=1))
print(np.sort(b, axis=1))

print("Testing np.sum:")
a = np.array([253, 254, 255], dtype=np.uint8)
print(np.sum(a))
print(np.sum(a, axis=0))
a = np.array([range(255 - 3, 255),
              range(240 - 3, 240),
              range(250 - 3, 250)],
             dtype=np.float)
print(np.sum(a))
print(np.sum(a, axis=0))
print(np.sum(a, axis=1))

print("Testing np.mean:")
a = np.array([253, 254, 255], dtype=np.uint8)
print(np.mean(a))
print(np.mean(a, axis=0))
a = np.array([range(255 - 3, 255),
              range(240 - 3, 240),
Пример #3
0
def main():
    sensor.enable_proximity = True
    while True:
        # Wait for user to put finger over sensor
        while sensor.proximity < PROXIMITY_THRESHOLD_HI:
            time.sleep(.01)

        # After the finger is sensed, set up the color sensor
        sensor.enable_color = True
        # This sensor integration time is just a little bit shorter than 125ms,
        # so we should always have a fresh value when we ask for it, without
        # checking if a value is available.
        sensor.integration_time = 220
        # In my testing, 64X gain saturated the sensor, so this is the biggest
        # gain value that works properly.
        sensor.color_gain = APDS9660_AGAIN_4X
        white_leds.value = True

        # And our data structures
        # The most recent data samples, equal in number to the filter taps
        data = np.zeros(len(taps))
        # The filtered value on the previous iteration
        old_value = 1
        # The times of the most recent pulses registered.  Increasing this number
        # makes the estimation more accurate, but at the expense of taking longer
        # before a pulse number can be computed
        pulse_times = []
        # The estimated heart rate based on the recent pulse times
        rate = None
        # the number of samples taken
        n = 0

        # Rather than sleeping for a fixed duration, we compute a deadline
        # in nanoseconds and wait for the new deadline time to arrive.  This
        # helps the long term frequency of measurements better match the desired
        # frequency.
        t0 = deadline = time.monotonic_ns()
        # As long as their finger is over the sensor, capture data
        while sensor.proximity >= PROXIMITY_THRESHOLD_LO:
            deadline += dt
            sleep_deadline(deadline)
            value = sum(sensor.color_data)  # Combination of all channels
            data = np.roll(data, 1)
            data[-1] = value
            # Compute the new filtered variable by applying the filter to the
            # recent data samples
            filtered = np.sum(data * taps)

            # We gathered enough data to fill the filters, and
            # the light value crossed the zero line in the positive direction
            # Therefore we need to record a pulse
            if n > len(taps) and old_value < 0 <= filtered:
                # This crossing time is estimated, but it increases the pulse
                # estimate resolution quite a bit.  If only the nearest 1/8s
                # was used for pulse estimation, the smallest pulse increment
                # that can be measured is 7.5bpm.
                cross = estimated_cross_time(old_value, filtered, deadline)
                # store this pulse time (in seconds since sensor-touch)
                pulse_times.append((cross - t0) * 1e-9)
                # and maybe delete an old pulse time
                del pulse_times[:-10]
                # And compute a rate based on the last recorded pulse times
                if len(pulse_times) > 1:
                    rate = 60 / (pulse_times[-1] -
                                 pulse_times[0]) * (len(pulse_times) - 1)
            old_value = filtered

            # We gathered enough data to fill the filters, so report the light
            # value and possibly the estimated pulse rate
            if n > len(taps):
                print((filtered, rate))
            n += 1

        # Turn off the sensor and the LED and go back to the top for another run
        sensor.enable_color = False
        white_leds.value = False
        print()
data = np.zeros(len(taps))
t0 = deadline = time.monotonic_ns()
n = 0
# Take an initial reading to subtract off later, so that the graph in mu
# accentuates the small short term changes in pressure rather than the large
# DC offset of around 980
offset = sensor.pressure

while True:
    deadline += dt
    sleep_deadline(deadline)
    # Move the trace near the origin so small differences can be seen in the mu
    # plot window .. you wouldn't do this subtraction step if you are really
    # interested in absolute barometric pressure.
    value = sensor.pressure - offset
    if n == 0:
        # The first time, fill the filter with the initial value
        data = data + value
    else:
        # Otherwise, add it as the next sample
        data = np.roll(data, 1)
        data[-1] = value
    filtered = np.sum(data * taps)
    # Actually print every 10th value.  This prints about 1.6 values per
    # second.  You can print values more quickly by removing the 'if' and
    # making the print unconditional, or change the frequency of prints up
    # or down by changing the number '10'.
    if n % 10 == 0:
        print((filtered, value))
    n += 1
Пример #5
0
def normalized_rms_ulab(values):
    # this function works with ndarrays only
    minbuf = np.mean(values)
    values = values - minbuf
    samples_sum = np.sum(values * values)
    return math.sqrt(samples_sum / len(values))