Skip to content

Instantly share code, notes, and snippets.

View trevphil's full-sized avatar

Trevor Phillips trevphil

View GitHub Profile
@trevphil
trevphil / spectre.c
Created August 1, 2019 09:38
Spectre attack implemented in C
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <intrin.h> /* for rdtscp and clflush */
#pragma optimize("gt",on)
#else
#include <x86intrin.h> /* for rdtscp and clflush */
#endif
@trevphil
trevphil / CheckScenario.swift
Last active June 3, 2019 13:43
Check scenario
func checkScenario(_ scenario: Scenario) {
let filter = KalmanFilter()
// Add up the distance between each location in the true path
let trueDist = unfilteredDistance(scenario.truePath)
// Add up the distance between each location in the noisy path
let unfilteredDist = unfilteredDistance(scenario.noisyPath)
@trevphil
trevphil / KalmanFilter.swift
Created June 3, 2019 13:24
Kalman Filter for CLLocation
import Foundation
import CoreLocation
extension CLLocation {
// Alias for `horizontalAccuracy` (more readable)
var uncertainty: Double {
return horizontalAccuracy
}
}
@trevphil
trevphil / Parameters.swift
Last active June 3, 2019 12:50
Parameters
enum PathType: String, CaseIterable {
case outAndBack, loop
}
enum Distance: Double, CaseIterable {
case short = 1000 // in meters
case medium = 5000
case long = 15000
}
@trevphil
trevphil / Scenario.swift
Last active June 3, 2019 12:50
Scenario Struct
struct Scenario {
/// A sequence of CLLocations representing the "true path" of the GPS track
let truePath: [CLLocation]
/// A sequence of CLLocations based on the true path, but with noise added to various parameters
let noisyPath: [CLLocation]
/// A number between 0.0-1.0 representing the minimum allowable percentage difference between
@trevphil
trevphil / LocationConversion.swift
Last active June 3, 2019 12:50
True location to noisy location
func addNoise(to trueLocation: CLLocation) -> CLLocation {
let distanceWiggle = sampleNormalDist(withMean: 0, std: 0.952)
let xContribution = rng.nextUniform()
let xoff = distanceWiggle * Double(xContribution)
let yoff = distanceWiggle * sqrt(1 - pow(Double(xContribution), 2))
let newCoord = trueLocation.coordinate.offsetBy(xMeters: xoff, yMeters: yoff)
let newHacc = sampleNormalDist(withMean: 7.179, std: 1.682)
let newVacc = sampleNormalDist(withMean: 3.643, std: 0.300)
let timeWiggle = sampleNormalDist(withMean: 1.00, std: 0.131)
@trevphil
trevphil / SampleNormalDist.swift
Last active June 3, 2019 12:51
Sampling from normal distribution
private func sampleNormalDist(withMean mean: Double, std: Double) -> Double {
precondition(std > 0)
let first = Double(rng.nextUniform())
let second = Double(rng.nextUniform())
let zScore = sqrt(-2 * log(first)) * cos(2 * Double.pi * second)
return zScore * std + mean
}
@trevphil
trevphil / gpx_to_csv.py
Created June 3, 2019 09:33
Python helper to convert GPX data to CSV
"""
Helper to convert GPX files to CSV files for analysis.
Created by Trevor Phillips
"""
import os
import json
try: