Ejemplo n.º 1
0
#!/usr/bin/env python3

import time
from mongo import db
from housepy import util, strings

for session in db.sessions.find():
    print("%s\t%s\t%s" % (session['_id'], util.dt(session['t'], tz="America/New_York"), strings.format_time(session['duration']) if 'duration' in session else ""))
Ejemplo n.º 2
0
import time, json, threading, subprocess, queue, platform, os, sys
import numpy as np
from housepy import log, config, strings, net, s3, util, process, drawing
from scipy.io import wavfile

DURATION = 10
AUDIO_TMP = os.path.abspath(os.path.join(os.path.dirname(__file__), "audio_tmp"))

t = sys.argv[1]

filename = "%s/%s.wav" % (AUDIO_TMP, t)
sample_rate, signal = wavfile.read(filename)
log.debug("samples %s" % len(signal))
log.debug("sample_rate %s" % sample_rate)
duration = float(len(signal)) / sample_rate
log.debug("duration %ss" % strings.format_time(duration))
signal = (np.array(signal).astype('float') / (2**16 * 0.5))   # assuming 16-bit PCM, -1 - 1
signal = abs(signal)    # magnitude

ctx = drawing.Context()
ctx.plot(signal)
ctx.line(0, config['noise_threshold'], 1, config['noise_threshold'], stroke=(255, 0, 0))
ctx.output("screenshots")

log.debug("noise threshold is %s" % config['noise_threshold'])
log.debug("found magnitude")
content_samples = 0
for sample in signal:
    if sample > config['noise_threshold']:
        content_samples += 1
total_content_time = float(content_samples) / sample_rate
Ejemplo n.º 3
0
#!/usr/bin/env python3

import time
from mongo import db
from housepy import util, strings

for session in db.sessions.find():
    print("%s\t%s\t%s" %
          (session['_id'], util.dt(session['t'], tz="America/New_York"),
           strings.format_time(session['duration'])
           if 'duration' in session else ""))
Ejemplo n.º 4
0
query = {
    'site': site,
    't_utc': {
        '$gt': timeutil.timestamp(timeutil.string_to_dt(start,
                                                        tz=config['tz'])),
        '$lt': timeutil.timestamp(timeutil.string_to_dt(stop, tz=config['tz']))
    }
}
log.info(query)
results = db.entries.find(query)
results = list(results)
print(json.dumps(results[0], indent=4, default=lambda d: str(d)))

ts = [d['t_utc'] for d in results]
duration = ts[-1] - ts[0]
print("DURATION %s %s" % (duration, strings.format_time(duration)))
signals = []
rates = []
labels = list(config['labels'].values())
labels.sort()
for i, label in enumerate(labels):
    log.info(label)
    try:
        values = [d[label] if label in d else None for d in results]
        values = sp.remove_shots(values, nones=True)  # repair missing values
        signal = sp.resample(ts, values)
        num_samples = len(signal)
        sample_rate = num_samples / duration
        rates.append(sample_rate)
        signal = sp.normalize(signal)
        signal = sp.smooth(signal, 15)
Ejemplo n.º 5
0
# start = "2016-08-01"
# stop = "2016-08-07"

start = "2017-02-27"
stop = "2017-03-01"

print(config['sites'][site])
query = {'site': site, 't_utc': {'$gt': timeutil.timestamp(timeutil.string_to_dt(start, tz=config['tz'])), '$lt': timeutil.timestamp(timeutil.string_to_dt(stop, tz=config['tz']))}}
log.info(query)
results = db.entries.find(query)
results = list(results)
print(json.dumps(results[0], indent=4, default=lambda d: str(d)))

ts = [d['t_utc'] for d in results]
duration = ts[-1] - ts[0]
print("DURATION %s %s" % (duration, strings.format_time(duration)))
signals = []
rates = []
labels = list(config['labels'].values())
labels.sort()
for i, label in enumerate(labels):
    log.info(label)
    try:
        values = [d[label] if label in d else None for d in results]
        values = sp.remove_shots(values, nones=True)  # repair missing values
        signal = sp.resample(ts, values)
        num_samples = len(signal)
        sample_rate = num_samples / duration
        rates.append(sample_rate)
        signal = sp.normalize(signal)
        signal = sp.smooth(signal, 15)
Ejemplo n.º 6
0
        except Exception as e:
            # log.warning(e)
            continue
        hrs.append(hr)
        ts.append(t)

    log.info("--> done")


    log.info("Processing...")
    total_time = ts[-1] - ts[0]
    total_samples = int(total_time / 60)     # once per minute

    log.debug("last_t %s" % ts[-1])
    log.debug("total_time %s" % total_time)
    log.debug("total_time_f %s" % strings.format_time(total_time))
    log.debug("total_samples %s" % total_samples)
    sample_length = total_time / total_samples
    log.debug("sample_length %s" % sample_length)

    signal = sp.resample(ts, hrs, total_samples)
    signal = sp.normalize(signal)
    signal = signal - sp.smooth(signal, size=100)   # flatten it out a bit
    threshold = np.average(signal) + (2 * np.std(signal))   # threshold is average plus 2 std deviation
    smoothed_signal = sp.smooth(signal, size=10)
    peaks, valleys = sp.detect_peaks(smoothed_signal, lookahead=10, delta=.001)
    max_peak = max(peaks, key=lambda p: p[1])
    log.info("max_peak %s" % max_peak)
    peaks = [peak for peak in peaks if peak[1] > threshold]

    def draw():