def main(session_id): ctx = drawing.Context(2000, 1000) for sensor in [2, 3, 4]: result = db.branches.find({'session': session_id, 'sensor': sensor}).sort([('t', ASCENDING)]) if not result.count(): continue result = list(result) ts = [r['t'] for r in result] xs = [r['sample'][0] for r in result] ys = [r['sample'][1] for r in result] zs = [r['sample'][2] for r in result] rms = [r['sample'][3] for r in result] duration = ts[-1] - ts[0] SAMPLING_RATE = 50 # hz x_signal = (sp.resample(ts, xs, duration * SAMPLING_RATE) - RANGE[0]) / (RANGE[1] - RANGE[0]) y_signal = (sp.resample(ts, ys, duration * SAMPLING_RATE) - RANGE[0]) / (RANGE[1] - RANGE[0]) z_signal = (sp.resample(ts, zs, duration * SAMPLING_RATE) - RANGE[0]) / (RANGE[1] - RANGE[0]) rms_signal = (sp.resample(ts, rms, duration * SAMPLING_RATE) - RANGE[0]) / (RANGE[1] - RANGE[0]) # ctx.plot(x_signal, stroke=(1.0, 0.0, 0.0, 1.0), thickness=2.0) # ctx.plot(y_signal, stroke=(0.0, 1.0, 0.0, 1.0), thickness=2.0) # ctx.plot(z_signal, stroke=(0.0, 0.0, 1.0, 1.0), thickness=2.0) rms_signal = sp.normalize(rms_signal) ctx.plot(rms_signal, stroke=colors[sensor], thickness=2.0) ctx.output("graphs")
def process_readings(kind, color, thickness=5): global t, db, ctx data = model.fetch_readings(kind, t - DURATION, t) if not len(data): return False data.sort(key=lambda d: d['t']) ts = [d['t'] for d in data] vs = [d['v'] for d in data] signal = sp.resample(ts, vs, DURATION) signal = sp.normalize(signal, RANGE[kind][0], RANGE[kind][1]) db[kind] = list(signal) if config['draw']: ctx.line([(float(i) / DURATION, sample) for (i, sample) in enumerate(signal)], thickness=thickness, stroke=color)
def main(session_id): ctx = drawing.Context(2000, 1000) for sensor in [2, 3, 4]: result = db.branches.find({ 'session': session_id, 'sensor': sensor }).sort([('t', ASCENDING)]) if not result.count(): continue result = list(result) ts = [r['t'] for r in result] xs = [r['sample'][0] for r in result] ys = [r['sample'][1] for r in result] zs = [r['sample'][2] for r in result] rms = [r['sample'][3] for r in result] duration = ts[-1] - ts[0] SAMPLING_RATE = 50 # hz x_signal = (sp.resample(ts, xs, duration * SAMPLING_RATE) - RANGE[0]) / (RANGE[1] - RANGE[0]) y_signal = (sp.resample(ts, ys, duration * SAMPLING_RATE) - RANGE[0]) / (RANGE[1] - RANGE[0]) z_signal = (sp.resample(ts, zs, duration * SAMPLING_RATE) - RANGE[0]) / (RANGE[1] - RANGE[0]) rms_signal = (sp.resample(ts, rms, duration * SAMPLING_RATE) - RANGE[0]) / (RANGE[1] - RANGE[0]) # ctx.plot(x_signal, stroke=(1.0, 0.0, 0.0, 1.0), thickness=2.0) # ctx.plot(y_signal, stroke=(0.0, 1.0, 0.0, 1.0), thickness=2.0) # ctx.plot(z_signal, stroke=(0.0, 0.0, 1.0, 1.0), thickness=2.0) rms_signal = sp.normalize(rms_signal) ctx.plot(rms_signal, stroke=colors[sensor], thickness=2.0) ctx.output("graphs")
t_max = timeutil.timestamp(timeutil.string_to_dt("2016-01-01", tz='America/Denver')) log.info("T_MIN %s" % t_min) log.info("T_MAX %s" % t_max) signals = [] labels = list(streams.keys()) log.info("LABELS %s" % labels) for label in labels: log.info(label) ts = tses[label] ts = [t_min] + ts + [t_max] values = [d[label] if label in d else None for d in streams[label]] values = [values[0]] + values + [values[-1]] values = sp.remove_shots(values, nones=True) # repair missing values signal = sp.resample(ts, values) num_samples = len(signal) sample_rate = num_samples / duration signal = sp.normalize(signal) signal = sp.smooth(signal, 15) signals.append(signal) log.info("Drawing...") ctx = drawing.Context(1200, 500, margin=20, hsv=True) for b in range(12): ctx.line(b / 12, 0, b / 12, 1, stroke=(0.5, 0.5, 0.5, 0.5), thickness=0.5) ctx.line(1, 0, 1, 1, stroke=(0.5, 0.5, 0.5, 0.5), thickness=0.5) for i, signal in enumerate(signals): color = i / (len(signals) + 4) + .1, 1., .8, 1. ctx.plot(signal, stroke=color, thickness=1.5) ctx.line(10 / ctx.width, 1 - ((10 + (i * 10)) / ctx.height), 30 / ctx.width, 1 - ((10 + (i * 10)) / ctx.height), stroke=color, thickness=2)
db.stream.find( { "t_utc": { "$gt": util.timestamp(util.parse_date(START, tz="America/New_York")), "$lt": util.timestamp(util.parse_date(END, tz="America/New_York")), } } ).sort([("t_utc", pymongo.ASCENDING)]) ) log.info("--> done") ## ts = [r["t_utc"] for r in results] xs = [r["x"] for r in results] duration = ts[-1] - ts[0] SAMPLING_RATE = 100 log.info("Resampling...") signal = sp.resample(ts, xs, duration * SAMPLING_RATE) signal += 1 # change -1,1 to 0,2 signal = sp.normalize(signal, 0, 2) log.info("--> done") log.info("Drawing...") ctx = drawing.Context(1000, 400) ctx.plot(signal, stroke=(0.0, 0.0, 0.0, 1.0), thickness=1.0) ctx.output("graphs") log.info("--> done")
def generate(): # load data into t and count arrays per species species = OrderedDict() start_t = util.timestamp(util.parse_date(str(config['start']))) end_t = util.timestamp(util.parse_date(str(config['end']))) max_count = 0 with open("data.csv") as f: data = csv.reader(f) for r, row in enumerate(data): if r == 0: continue plot = row[1] name = row[2] if len(config['species_list'] ) and name not in config['species_list']: continue dt = datetime.datetime(int(row[3]), 1, 1) + datetime.timedelta(int(row[4]) - 1) t = util.timestamp(dt) if t < start_t or t > end_t: continue count = 0 if row[5] == "NA" else int(row[5]) if count > max_count: max_count = count if name not in species: species[name] = {'ts': [start_t, t - 1], 'counts': [0, 0]} species[name]['ts'].append(t) species[name]['counts'].append(count) species = OrderedDict(sorted(species.items())) print("--> loaded") # add a zero count at the start and end of every year yts = [ util.timestamp(datetime.datetime(y, 1, 1)) for y in range(1974, 2017) ] for name in species: ts = species[name]['ts'] for yt in yts: i = 0 while i < len(ts) and ts[i] < yt: i += 1 if i > 0: end_season_t = ts[i - 1] if i < len(ts): start_season_t = ts[i] ts.insert(i, start_season_t - config['tail']) species[name]['counts'].insert(i, 0) ts.insert(i, end_season_t + config['tail']) species[name]['counts'].insert(i, 0) species[name]['ts'].append(end_t) species[name]['counts'].append(0) print("--> onsets added") # create and draw signals signals = [] names = [] i = 0 for name, data in species.items(): print("Processing %s..." % name) # create signal from bloom counts signal = sp.resample(data['ts'], data['counts']) if config['normalize']: signal = sp.normalize(signal) else: signal = sp.normalize(signal, 0, max_count) signal = sp.smooth(signal, size=8) signal = sp.limit( signal, max(signal)) # get rid of noise below 0 for onset detection # add spikes for peaks if config['peak_spikes']: peaks, valleys = sp.detect_peaks(signal, lookahead=50) peak_signal = np.zeros(len(signal)) for peak in peaks: peak_signal[peak[0]] = 1.0 signal += peak_signal # add spikes for onsets if config['onset_spikes']: onsets = sp.detect_onsets(signal) onset_signal = np.zeros(len(signal)) for onset in onsets: onset_signal[onset] = 0.5 onset_signal[onset + 1] = 0.4 onset_signal[onset + 2] = 0.25 signal += onset_signal # limit signal = sp.limit(signal, 1.0) signal *= 0.9 # hack, just controlling gain signals.append(signal) names.append(name) i += 1 return signals, names
# # print(t, value) # log.info("--> done") # util.save("data.pkl", {'ts': ts, 'values': values}) # exit() data = util.load("data.pkl") ts = data['ts'] values = data['values'] # http://www.usbr.gov/pn-bin/instant.pl?station=ABEI&year=2015&month=7&day=1&year=2016&month=6&day=1&pcode=OB log.info("Converting to 0-1 signal...") signal = sp.resample(ts, values, len(ts)) signal = sp.normalize(signal) log.info("--> done") # log.info("Drawing...") # ctx = drawing.Context(6000, 500) # ctx.plot(signal) # ctx.output("audification_signals/") # log.info("--> done") audio_signal = sp.make_audio(signal) # import subprocess # filename = "weather_test_44.wav"
def process_walk(walk_id, force=False): if not model.process_check(walk_id): log.error("Walk %s already processed" % walk_id) if force: log.info("--> forcing...") model.remove_sequence(walk_id) else: return log.info("Processing walk %s" % walk_id) # fetch data data = model.fetch_accels(walk_id) data = [(reading['t'], reading['x'], reading['y'], reading['z']) for reading in data] # let's sample every millisecond, so the time of the last reading is how many samples we need data = np.array(data) ts = data[:, 0] total_samples = int(ts[-1]) # need at least 10s of data # add 2000 for trimming at nd if total_samples < 10000 + 2000: log.info("No footsteps detected (too short)") model.hide(walk_id) return # resample the values xs = sp.resample(ts, data[:, 1], total_samples) ys = sp.resample(ts, data[:, 2], total_samples) zs = sp.resample(ts, data[:, 3], total_samples) # skip for accelerometer startup and for phone out of pocket at end skipin, skipout = 0, 2000 xs = xs[skipin:-skipout] ys = ys[skipin:-skipout] zs = zs[skipin:-skipout] total_samples -= (skipin + skipout) log.info("TOTAL SAMPLES %s (%fs)" % (total_samples, (total_samples / 1000.0))) # get 3d magnitude (not RMS) -- orientation shouldnt matter ds = np.sqrt(np.power(xs, 2) + np.power(ys, 2) + np.power(zs, 2)) # prep the raw values for display # normalize the values to a given range (this is Gs) MIN = -10.0 MAX = 10.0 xs = (xs - MIN) / (MAX - MIN) ys = (ys - MIN) / (MAX - MIN) zs = (zs - MIN) / (MAX - MIN) # smooth them xs = sp.smooth(xs, 300) ys = sp.smooth(ys, 300) zs = sp.smooth(zs, 300) # process the magnitude signal ds = sp.smooth(ds, 500) ds = np.clip(ds, -10.0, 10.0) # limit the signal to +-10 Gs ds = sp.normalize(ds) ds = 1 - ds ds = sp.compress(ds, 3.0) ds = sp.normalize(ds) # detect peaks peaks, valleys = sp.detect_peaks(ds, lookahead=50, delta=0.10) peaks = np.array(peaks) valleys = np.array(valleys) log.info("PEAKS %s" % len(peaks)) if not len(peaks): log.info("No footsteps detected") model.hide(walk_id) return # get foot separator line fxs = [int(peak[0]) for peak in peaks] fys = [peak[1] for peak in peaks] avs = np.average([peak[1] for peak in peaks]) fys[0] = avs # it's going to start with a peak, so we need to bring it up or down accordingly fxs.append(total_samples - 1) fys.append(avs) fs = sp.resample(fxs, fys, total_samples) fs = sp.smooth(fs, 3000) # print out log.info("Saving sequence (%s)..." % walk_id) sequence = [] for p, peak in enumerate(peaks): foot = 'right' if peak[1] > fs[int(peak[0])] else 'left' t = peak[0] t += 250 # turns out the peak hits just before the step sequence.append((t, foot)) # fix triples for i in range(len(sequence) - 2): if sequence[i][1] == sequence[i + 1][1] == sequence[i + 2][1]: sequence[i + 1] = (sequence[i + 1][0], 'right') if sequence[i + 1][1] == 'left' else ( sequence[i + 1][0], 'left') model.insert_sequence(walk_id, sequence) plot(walk_id, xs, ys, zs, ds, peaks, total_samples, fs)
def generate(): # load data into t and count arrays per species species = OrderedDict() start_t = util.timestamp(util.parse_date(str(config['start']))) end_t = util.timestamp(util.parse_date(str(config['end']))) max_count = 0 with open("data.csv") as f: data = csv.reader(f) for r, row in enumerate(data): if r == 0: continue plot = row[1] name = row[2] if len(config['species_list']) and name not in config['species_list']: continue dt = datetime.datetime(int(row[3]), 1, 1) + datetime.timedelta(int(row[4]) - 1) t = util.timestamp(dt) if t < start_t or t > end_t: continue count = 0 if row[5] == "NA" else int(row[5]) if count > max_count: max_count = count if name not in species: species[name] = {'ts': [start_t, t - 1], 'counts': [0, 0]} species[name]['ts'].append(t) species[name]['counts'].append(count) species = OrderedDict(sorted(species.items())) print("--> loaded") # add a zero count at the start and end of every year yts = [util.timestamp(datetime.datetime(y, 1, 1)) for y in range(1974, 2017)] for name in species: ts = species[name]['ts'] for yt in yts: i = 0 while i < len(ts) and ts[i] < yt: i += 1 if i > 0: end_season_t = ts[i-1] if i < len(ts): start_season_t = ts[i] ts.insert(i, start_season_t - config['tail']) species[name]['counts'].insert(i, 0) ts.insert(i, end_season_t + config['tail']) species[name]['counts'].insert(i, 0) species[name]['ts'].append(end_t) species[name]['counts'].append(0) print("--> onsets added") # create and draw signals signals = [] names = [] i = 0 for name, data in species.items(): print("Processing %s..." % name) # create signal from bloom counts signal = sp.resample(data['ts'], data['counts']) if config['normalize']: signal = sp.normalize(signal) else: signal = sp.normalize(signal, 0, max_count) signal = sp.smooth(signal, size=8) signal = sp.limit(signal, max(signal)) # get rid of noise below 0 for onset detection # add spikes for peaks if config['peak_spikes']: peaks, valleys = sp.detect_peaks(signal, lookahead=50) peak_signal = np.zeros(len(signal)) for peak in peaks: peak_signal[peak[0]] = 1.0 signal += peak_signal # add spikes for onsets if config['onset_spikes']: onsets = sp.detect_onsets(signal) onset_signal = np.zeros(len(signal)) for onset in onsets: onset_signal[onset] = 0.5 onset_signal[onset+1] = 0.4 onset_signal[onset+2] = 0.25 signal += onset_signal # limit signal = sp.limit(signal, 1.0) signal *= 0.9 # hack, just controlling gain signals.append(signal) names.append(name) i += 1 return signals, names
def sample(draw=False): log.info("START SAMPLE") # get the time # dt = timeutil.get_dt(tz=config['tz']) # dt -= datetime.timedelta(days=300) # time adjustment if necessary for testing # t_utc = timeutil.t_utc(dt) t_utc = timeutil.t_utc() dt = timeutil.get_dt(t_utc, tz=config['tz']) log.info("CURRENT TIME %s" % timeutil.get_string(t_utc, tz=config['tz'])) # pull the last 24 hours worth -- we're going to normalize over that to set our dynamic levels log.info(config['sites'][config['sample']]) # # this is the real-time last 24 hours # query = {'site': config['sample'], 't_utc': {'$gt': t_utc - 86400, '$lt': t_utc}} # log.info(query) # results = db.entries.find(query) # this is the last 24 hours we have # assume updating every 15 minutes, last 24 hours is the last 96 results results = db.entries.find({ 'site': config['sample'] }).sort([('t_utc', DESCENDING)]).limit(96) results = list(results) results.reverse() log.info("%s results" % len(results)) # should be 96 log.info(json.dumps(results[-1], indent=4, default=lambda d: str(d))) # show the last one # resample signals for each ts = [d['t_utc'] for d in results] duration = ts[-1] - ts[0] log.info("DURATION %s %s" % (duration, timeutil.format_seconds(duration))) signals = [] rates = [] labels = list(config['labels'].values()) labels.sort() for i, label in enumerate(labels): # log.debug(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) signals.append(signal) except KeyError as e: log.error(log.exc(e)) log.error(values) # draw if desired if draw: from housepy import drawing ctx = drawing.Context(1200, 500, margin=20, hsv=True) for i, label in enumerate(labels): color = i / len(labels), .8, .8, 1. signal = signals[i] ctx.plot(signal, stroke=color, thickness=2) ctx.output("charts/") # collapse into n-dimensional points points = [] for i in range(len(signals[0])): point = [signal[i] for signal in signals] points.append(point) # PCA to 4D -- this takes whatever data we've got and maximizes variation for our four panels points = np.array(points) # log.debug("INPUT: %s POINTS, %s DIMENSIONS" % points.shape) points = decomposition.PCA(n_components=4).fit_transform(points) # log.debug("OUTPUT: %s POINTS, %s DIMENSIONS" % points.shape) # normalize each dimension independently, again amplifying dynamics points = np.column_stack((sp.normalize(points[:, 0], np.min(points[:, 0]), np.max(points[:, 0])), sp.normalize(points[:, 1], np.min(points[:, 1]), np.max(points[:, 1])), sp.normalize(points[:, 1], np.min(points[:, 1]), np.max(points[:, 2])), sp.normalize(points[:, 2], np.min(points[:, 3]), np.max(points[:, 3])))) # now, for each time this is queried we want to return an interpolation between the last two points # this essentially implements a delay that closes in on the most recent query # ...hopefully to be refreshed with a new USGS reading when it gets there # if that reading doesnt come, it's ok, it just hovers there until we proceed # aaandd actually we want a couple of hours delay, because these come in at bulk every 1-4 hours # we know we have 96 points. four hours back is 16 points # interpolating between points -17 and -16 should give the most recent guaranteed smooth transitions # transduction takes time, pues point_a = points[-17] point_b = points[-16] # log.debug(point_a) # log.debug(point_b) # linear interpolation over 15 minutes position = (((dt.minute % 15) * 60) + dt.second) / (15 * 60) # log.debug(position) point = [(point_a[i] * (1.0 - position)) + (point_b[i] * position) for i in range(len(point_a))] log.info("RESULT: %s" % point) return point
def process_walk(walk_id, force=False): if not model.process_check(walk_id): log.error("Walk %s already processed" % walk_id) if force: log.info("--> forcing...") model.remove_sequence(walk_id) else: return log.info("Processing walk %s" % walk_id) # fetch data data = model.fetch_accels(walk_id) data = [(reading['t'], reading['x'], reading['y'], reading['z']) for reading in data] # let's sample every millisecond, so the time of the last reading is how many samples we need data = np.array(data) ts = data[:,0] total_samples = int(ts[-1]) # need at least 10s of data # add 2000 for trimming at nd if total_samples < 10000 + 2000: log.info("No footsteps detected (too short)") model.hide(walk_id) return # resample the values xs = sp.resample(ts, data[:,1], total_samples) ys = sp.resample(ts, data[:,2], total_samples) zs = sp.resample(ts, data[:,3], total_samples) # skip for accelerometer startup and for phone out of pocket at end skipin, skipout = 0, 2000 xs = xs[skipin:-skipout] ys = ys[skipin:-skipout] zs = zs[skipin:-skipout] total_samples -= (skipin + skipout) log.info("TOTAL SAMPLES %s (%fs)" % (total_samples, (total_samples / 1000.0))) # get 3d magnitude (not RMS) -- orientation shouldnt matter ds = np.sqrt(np.power(xs, 2) + np.power(ys, 2) + np.power(zs, 2)) # prep the raw values for display # normalize the values to a given range (this is Gs) MIN = -10.0 MAX = 10.0 xs = (xs - MIN) / (MAX - MIN) ys = (ys - MIN) / (MAX - MIN) zs = (zs - MIN) / (MAX - MIN) # smooth them xs = sp.smooth(xs, 300) ys = sp.smooth(ys, 300) zs = sp.smooth(zs, 300) # process the magnitude signal ds = sp.smooth(ds, 500) ds = np.clip(ds, -10.0, 10.0) # limit the signal to +-10 Gs ds = sp.normalize(ds) ds = 1 - ds ds = sp.compress(ds, 3.0) ds = sp.normalize(ds) # detect peaks peaks, valleys = sp.detect_peaks(ds, lookahead=50, delta=0.10) peaks = np.array(peaks) valleys = np.array(valleys) log.info("PEAKS %s" % len(peaks)) if not len(peaks): log.info("No footsteps detected") model.hide(walk_id) return # get foot separator line fxs = [int(peak[0]) for peak in peaks] fys = [peak[1] for peak in peaks] avs = np.average([peak[1] for peak in peaks]) fys[0] = avs # it's going to start with a peak, so we need to bring it up or down accordingly fxs.append(total_samples-1) fys.append(avs) fs = sp.resample(fxs, fys, total_samples) fs = sp.smooth(fs, 3000) # print out log.info("Saving sequence (%s)..." % walk_id) sequence = [] for p, peak in enumerate(peaks): foot = 'right' if peak[1] > fs[int(peak[0])] else 'left' t = peak[0] t += 250 # turns out the peak hits just before the step sequence.append((t, foot)) # fix triples for i in range(len(sequence) - 2): if sequence[i][1] == sequence[i+1][1] == sequence[i+2][1]: sequence[i+1] = (sequence[i+1][0], 'right') if sequence[i+1][1] == 'left' else (sequence[i+1][0], 'left') model.insert_sequence(walk_id, sequence) plot(walk_id, xs, ys, zs, ds, peaks, total_samples, fs)
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) signals.append(signal) # color = colors[i] color = i / len(labels), .8, .8, 1. ctx.plot(signal, stroke=color, thickness=2) ctx.line(10 / ctx.width, 1 - ((10 + (i * 10)) / ctx.height), 30 / ctx.width, 1 - ((10 + (i * 10)) / ctx.height), stroke=color, thickness=2) ctx.label(35 / ctx.width, 1 - ((13 + (i * 10)) / ctx.height), label.upper(), size=8) except KeyError as e: log.error(log.exc(e)) log.error(values)
def main(session_id): result = db.branches.find({'session': session_id}).sort([('t', ASCENDING)]) if not result.count(): print("NO DATA!") exit() log.info("Start processing...") result = list(result) ts = [r['t'] for r in result] rms = [r['sample'][3] for r in result] duration = ts[-1] - ts[0] SAMPLING_RATE = 60 # hz log.info("DURATION %fs" % duration) signal = sp.resample(ts, rms, duration * SAMPLING_RATE) signal = sp.remove_shots(signal) signal = sp.normalize(signal) signal = sp.smooth(signal, 15) # this number should match some lower frequency bound. ie, put this in hz. # the smaller the number, the more it will affect small motion # so this should be higher than the slowest motion we care about # ie, dont care about motion over 0.5hz, which is 120 samples trend = sp.smooth(signal, 120) signal -= trend signal += 0.5 atrend = sp.smooth(signal, 500) ## autocorrelation auto = sp.autocorrelate(signal) # this should be small -- if 60hz, fastest gesture would reasonably be half of that, so 30 peaks, valleys = sp.detect_peaks(auto, 10) peaks = [peak for peak in peaks[1:] if peak[1] > 0.5] partials = [] for peak in peaks: frequency = SAMPLING_RATE / peak[0] partial = frequency * 1000 partials.append([partial, float(peak[1])]) log.info("%d samps\t%fhz\t%f magnitude\t%f map" % (peak[0], frequency, peak[1], partial)) log.info(partials) ctx = drawing.Context(2000, 750) ctx.plot(auto, stroke=(0.0, 0.0, 0.0, 1.0), thickness=2.0) for peak in peaks: x = peak[0] / len(auto) ctx.line(x, 0.0, x, peak[1], stroke=(1.0, 0.0, 0.0, 1.0)) ctx.output("graphs") ## audio audio_signal = sp.make_audio(signal) spectrum(audio_signal, SAMPLING_RATE) AUDIO_RATE = 11025 filename = "%s.wav" % util.timestamp() sound.write_audio(audio_signal, filename, AUDIO_RATE) subprocess.call(["open", filename]) log.info("AUDIO DURATION %fs" % (duration / (AUDIO_RATE / SAMPLING_RATE))) ctx = drawing.Context(2000, 750) ctx.plot(signal, stroke=(0.0, 0.0, 0.0, 1.0), thickness=2.0) ctx.plot(trend, stroke=(1.0, 0.0, 0.0, 1.0), thickness=2.0) ctx.plot(atrend, stroke=(0.0, 0.0, 1.0, 1.0), thickness=2.0) ctx.output("graphs") log.info("--> done") # around 300ms
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(): from housepy import drawing log.info("--> done") log.info("Plotting...") ctx = drawing.Context() ctx.plot(signal)
def process_walk(walk_id, force=False): if not model.process_check(walk_id): log.error("Walk %s already processed" % walk_id) if force: log.info("--> forcing...") model.remove_sequence(walk_id) else: return log.info("Processing walk %s" % walk_id) data = model.fetch_accels(walk_id) data = [(reading['t'], reading['x'], reading['y'], reading['z']) for reading in data] # log.debug(data) # let's sample every millisecond, so the time of the last reading is how many samples we need data = np.array(data) # log.debug(data) ts = data[:,0] total_samples = ts[-1] log.info("TOTAL SAMPLES %s (%fs)" % (total_samples, (total_samples / 1000.0))) # resample the values xs = sp.resample(ts, data[:,1], total_samples) ys = sp.resample(ts, data[:,2], total_samples) zs = sp.resample(ts, data[:,3], total_samples) # skip 0.5s for intro skip = 500 xs = xs[skip:] ys = ys[skip:] zs = zs[skip:] total_samples -= skip # # for testing # log.debug(total_samples) # xs = xs[(30 * 1000):(50 * 1000)] # ys = ys[(30 * 1000):(50 * 1000)] # zs = zs[(30 * 1000):(50 * 1000)] # total_samples = len(xs) # log.debug(total_samples) # get 3d vector ds = np.sqrt(np.power(xs, 2) + np.power(ys, 2) + np.power(zs, 2)) # normalize the values to a given range (this is gs, I believe) MIN = -20.0 MAX = 20.0 xs = (xs - MIN) / (MAX - MIN) ys = (ys - MIN) / (MAX - MIN) zs = (zs - MIN) / (MAX - MIN) ds = (ds - MIN) / (MAX - MIN) # low-pass filter ds = sp.smooth(ds, 300) # ds = sp.normalize(ds) # av = np.average(ds) # detect peaks # lookahead should be the minimum time of a step, maybe .3s, 300ms peaks, valleys = sp.detect_peaks(ds, lookahead=150, delta=0.10) if len(peaks) and peaks[0][0] == 0: peaks = peaks[1:] peaks = np.array(peaks) valleys = np.array(valleys) log.info("PEAKS %s" % len(peaks)) log.info("VALLEYS %s" % len(valleys)) if not (len(peaks) and len(valleys)): log.info("No footsteps detected") return peaks = valleys # start = np.min((np.min(peaks[:,0]), np.min(valleys[:,0]))) start = np.min(peaks[:,0]) log.debug("START %s" % start) xs = xs[start:] ys = ys[start:] zs = zs[start:] ds = ds[start:] peaks = [(peak[0] - start, peak[1]) for peak in peaks] valleys = [(valley[0] - start, valley[1]) for valley in valleys] total_samples -= start # get foot separator line fxs = [peak[0] for peak in peaks] fys = [peak[1] for peak in peaks] avs = np.average([peak[1] for peak in peaks]) fxs.append(total_samples-1) fys.append(avs) fs = sp.resample(fxs, fys, total_samples) fs = sp.smooth(fs, 3000) # print out log.info("Saving sequence (%s)..." % walk_id) sequence = [] for p, peak in enumerate(peaks): foot = 'right' if peak[1] > fs[peak[0]] else 'left' sequence.append((peak[0], foot)) model.insert_sequence(walk_id, sequence) plot(walk_id, xs, ys, zs, ds, peaks, valleys, total_samples, fs)