Skip to content

Commit

Permalink
Added the whole project
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxi committed May 14, 2015
1 parent bb25720 commit c6361a1
Show file tree
Hide file tree
Showing 9 changed files with 8,203 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python3.4 generator/generator.py
python3.4 main.py
./graphs/graph.sh source.csv source.png Not downsampled
./graphs/graph.sh sampled.csv sampled.png Downsampled
32 changes: 32 additions & 0 deletions generator/generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import csv
import random


def signal_generator():
seed = 1
max_y = 250
min_y = 100
start_x = 0
end_x = 7500

random.seed(seed)
prev_y = random.random()*(max_y-min_y)+min_y

with open('source.csv', 'w') as f:
csvf = csv.writer(f, delimiter=',')
cur = start_x
while cur <= end_x:
if random.random() > 0.01:
sign = random.choice([-1,1])
amp = random.random()*5
gen = random.random()*amp*sign+prev_y
else:
gen = random.random()*(max_y-min_y)+min_y
prev_y = gen
csvf.writerow([cur, gen])
cur += 1


if __name__ == '__main__':
signal_generator()
exit()
61 changes: 61 additions & 0 deletions graphs/graph.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash

if [ -z $1 ] || ! [ -f $1 ]; then
echo "Missing or incorrect input file"
exit
fi

if [ -z $2 ]; then
echo "Missing output file"
exit
fi

if [ -z $3 ]; then
echo "Missing title"
exit
fi

gnuplot << EOF
# reset config
reset
# define data delimiter
set datafile separator ","
# define terminal
set terminal pngcairo size 720,480 enhanced font 'Verdana,10'
# define axis
# remove border on top and right and set color to gray
set style line 11 lc rgb '#808080' lt 1
set border 3 back ls 11
set tics nomirror
# define grid
set style line 12 lc rgb '#808080' lt 0 lw 1
set grid back ls 12
# define legend
set key off
# define line styles
set style line 1 lt 1 lc rgb '#483D8B' lw 2 # Color: DarkSlateBlue
set style line 2 lt 1 lc rgb '#3CB371' lw 2 # Color: MediumSeaGreen
set style line 3 lt 1 lc rgb '#CD853F' lw 2 # Color: Peru
set style line 4 lt 1 lc rgb '#663399' lw 2 # Color: RebeccaPurple
set style line 5 lt 1 lc rgb '#6B8E23' lw 2 # Color: OliveDrab
set style line 6 lt 1 lc rgb '#DC143C' lw 2 # Color: Crimson
set style line 7 lt 1 lc rgb '#FF4500' lw 2 # Color: OrangeRed
# define ranges
set yrange [0:500]
# define output
set output "$2"
# define labels
set title "${@:3}"
set xlabel "Time (s)"
set ylabel "Y axis"
# plotting
plot "$1" using 1:2 ls 6 with lines
EOF
80 changes: 80 additions & 0 deletions lttb/lttb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import math


def largest_triangle_three_buckets(data, threshold):
"""
Return a downsampled version of data.
Parameters
----------
data: list of lists
data must be formated this way: [[x,y], [x,y], [x,y], ...]
threshold: int
threshold must be >= 2 and <= to the len of data
Returns
-------
data, but downsampled using threshold
"""

# Check if data and threshold are valid
if not isinstance(data, list):
raise Exception("incorrect data provided")
if not isinstance(threshold, int) or threshold <= 2 or threshold >= len(data):
raise Exception("threshold not well defined")
for i in data:
if not isinstance(i, list) or len(i) != 2:
raise Exception("incorrect data provided")

# Bucket size. Leave room for start and end data points
every = (len(data) - 2)/(threshold - 2)

a = 0 # Initially a is the first point in the triangle
next_a = 0
max_area_point = (0, 0)

sampled = [data[0]] # Always add the first point

for i in range(0, threshold-2):
# Calculate point average for next bucket (containing c)
avg_x = 0
avg_y = 0
avg_range_start = math.floor((i+1)*every) + 1
avg_range_end = math.floor((i+2)*every) + 1
avg_rang_end = avg_range_end if avg_range_end < len(data) else len(data)

avg_range_length = avg_rang_end - avg_range_start

while avg_range_start < avg_rang_end:
avg_x += data[avg_range_start][0]
avg_y += data[avg_range_start][1]
avg_range_start += 1

avg_x /= avg_range_length
avg_y /= avg_range_length

# Get the range for this bucket
range_offs = math.floor((i+0)*every) + 1
range_to = math.floor((i+1)*every) + 1

# Point a
point_ax = data[a][0]
point_ay = data[a][1]

max_area = -1

while range_offs < range_to:
# Calculate triangle area over three buckets
area = math.fabs((point_ax - avg_x)*(data[range_offs][1] - point_ay) - (point_ax - data[range_offs][0])*(avg_y-point_ay))*0.5
if area > max_area:
max_area = area
max_area_point = data[range_offs]
next_a = range_offs # Next a is this b
range_offs += 1

sampled.append(max_area_point) # Pick this point from the bucket
a = next_a # This a is the next a (chosen b)

sampled.append(data[len(data)-1]) # Always add last

return sampled
25 changes: 25 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from lttb.lttb import largest_triangle_three_buckets
import csv


def main():
with open('source.csv', 'r') as f:
c = 0
data = []
csvf = csv.reader(f, delimiter=',')
for row in csvf:
if c == 0:
c += 1
continue
data.append([int(row[0]), float(row[1])])
c += 1
sampled = largest_triangle_three_buckets(data, 500)
with open('sampled.csv', 'w') as f2:
csvf2 = csv.writer(f2, delimiter=',')
for row in sampled:
csvf2.writerow(row)


if __name__ == '__main__':
main()
exit()
Loading

0 comments on commit c6361a1

Please sign in to comment.