Skip to content

Commit

Permalink
Move plotting to a separate file. Create a reference data learning fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
Alexander James Wallar committed Jan 29, 2014
1 parent 84945c3 commit 0e3229f
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 185 deletions.
70 changes: 70 additions & 0 deletions app/Locabean/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. -->
<project basedir="." default="build" name="Locabean">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<path id="Android 4.2.2.libraryclasspath">
<pathelement location="../../../../Software/adt-bundle-mac-x86_64-20130514/sdk/platforms/android-17/android.jar"/>
</path>
<path id="Android Private Libraries.libraryclasspath">
<pathelement location="libs/android-support-v4.jar"/>
<pathelement location="libs/gson-2.2.4.jar"/>
<pathelement location="libs/musicg-1.4.2.0.jar"/>
</path>
<path id="Android Dependencies.libraryclasspath"/>
<path id="Locabean.classpath">
<pathelement location="bin/classes"/>
<path refid="Android 4.2.2.libraryclasspath"/>
<path refid="Android Private Libraries.libraryclasspath"/>
<path refid="Android Dependencies.libraryclasspath"/>
<pathelement location="libs/musicg-1.4.2.0.jar"/>
<pathelement location="libs/gson-2.2.4.jar"/>
</path>
<target name="init">
<mkdir dir="bin/classes"/>
<copy includeemptydirs="false" todir="bin/classes">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy includeemptydirs="false" todir="bin/classes">
<fileset dir="gen">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin/classes"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin/classes" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<src path="gen"/>
<classpath refid="Locabean.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
</project>
47 changes: 0 additions & 47 deletions app/Locabean/res/layout/setup_dialog.xml

This file was deleted.

16 changes: 8 additions & 8 deletions app/Locabean/src/com/locaudio/functional/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ public R call(T input) {
return body(input);
}

public <IN_TYPE, OUT_TYPE> List<OUT_TYPE> map(
Function<IN_TYPE, OUT_TYPE> f, IN_TYPE[] inArray) {
public <A, B> List<B> map(
Function<A, B> f, A[] inArray) {

List<OUT_TYPE> retList = new ArrayList<OUT_TYPE>();
for (IN_TYPE inVal : inArray) {
List<B> retList = new ArrayList<B>();
for (A inVal : inArray) {
retList.add(f.call(inVal));
}

return retList;
}

public <IN_TYPE, OUT_TYPE> List<OUT_TYPE> map(
Function<IN_TYPE, OUT_TYPE> f, List<IN_TYPE> inArray) {
public <A, B> List<B> map(
Function<A, B> f, List<A> inArray) {

List<OUT_TYPE> retList = new ArrayList<OUT_TYPE>();
for (IN_TYPE inVal : inArray) {
List<B> retList = new ArrayList<B>();
for (A inVal : inArray) {
retList.add(f.call(inVal));
}

Expand Down
25 changes: 14 additions & 11 deletions app/Locabean/src/com/locaudio/locabean/NodeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class NodeActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_node);

setupTextViews();
setupLocaudio();
}
Expand All @@ -45,18 +45,21 @@ private void setupLocaudio() {
locaudio = new Locaudio(IP_ADDRESS, PORT);

acquisitionThread = locaudio.getAcquisitionThread(
getApplicationContext(), new UIFunction<NotifyResponse>(this) {
getApplicationContext(), acquisitionCallback);

acquisitionThread.start();
}

@Override
public Void body(NotifyResponse nr) {
nameTextView.setText(nr.name);
confidenceTextView.setText("" + nr.confidence);
private UIFunction<NotifyResponse> acquisitionCallback = new UIFunction<NotifyResponse>(
this) {

return null;
}
@Override
public Void body(NotifyResponse nr) {
nameTextView.setText(nr.name);
confidenceTextView.setText("" + nr.confidence);

});
return null;
}

acquisitionThread.start();
}
};
}
7 changes: 4 additions & 3 deletions locaudio/detectionserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import json
import triangulation as tri
import fingerprint
import plot
import db
import os

Expand Down Expand Up @@ -94,7 +95,7 @@ def get_sound_positions(sound_name):

radius, spl, _ = db.get_reference_data(sound_name)

location_list = tri.determine_sound_positions(
location_list = tri.determine_sound_locations(
radius, spl,
config.detection_events[sound_name],
disp=0
Expand All @@ -121,7 +122,7 @@ def get_position_viewer(sound_name):

radius, spl, _ = db.get_reference_data(sound_name)

location_list = tri.determine_sound_positions(
location_list = tri.determine_sound_locations(
radius, spl,
config.detection_events[sound_name],
disp=0
Expand All @@ -131,7 +132,7 @@ def get_position_viewer(sound_name):
img_web_path = "/" + img_path

if config.new_data[sound_name]:
tri.plot_detection_events(
plot.plot_detection_events(
location_list,
radius, spl,
config.detection_events[sound_name],
Expand Down
116 changes: 116 additions & 0 deletions locaudio/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

import triangulation as tri
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D


def determine_limits(*args):

x_min = args[0][0].x
x_max = args[0][0].x

y_min = args[0][0].y
y_max = args[0][0].y

for coord_list in args:
for coord in coord_list:
if coord.x < x_min:
x_min = coord.x
elif coord.x > x_max:
x_max = coord.x

if coord.y < y_min:
y_min = coord.y
elif coord.y > y_max:
y_max = coord.y

if x_max - x_min > y_max - y_min:
y_max = (x_max - x_min) + y_min
else:
x_max = (y_max - y_min) + x_min

x_step = (x_max - x_min) / float(30)
y_step = (y_max - y_min) / float(30)

return (
x_min - 100 * x_step, x_max + 100 * x_step,
y_min - 100 * y_step, y_max + 100 * y_step,
x_step, y_step
)


def plot_detection_events(locations, r_ref, l_ref, d_events, filename):
"""
Plots the detection events and saves the figure in the given path.
@param res The list of locations. Locations are named tuples with fields
which are position which is a point and confidence which is a float
@param rRef The reference distance at which the reference sound
pressure level was recorded
@param lRef The reference sound pressure level used to determine the
distance from the newly measured sound pressure level
@param nodeEvents The list ofassociated data when a node detects with some
confidence that the sound has been identified
@return The plt object of the saved figure
"""

fig = plt.figure("Locaudio")
ax = fig.add_subplot(111)
ax.set_xlabel("X Location")
ax.set_ylabel("Y Location")

# x_min = 56.3399
# x_max = 56.3400
# y_min = -2.80834
# y_max = -2.80824

(
x_min, x_max,
y_min, y_max,
x_step, y_step
) = determine_limits(locations, d_events)

x = np.arange(x_min, x_max, x_step)
y = np.arange(y_min, y_max, y_step)
X, Y = np.meshgrid(x, y)

zs = np.array(
[
tri.position_probability(x, y, r_ref, l_ref, d_events)
for x, y in zip(np.ravel(X), np.ravel(Y))
]
)

Z = zs.reshape(X.shape)
ax.pcolormesh(X, Y, Z, cmap=cm.jet)
ax.scatter(
[p.position.x for p in locations],
[p.position.y for p in locations],
marker="+",
linewidths=15,
c="white"
)
ax.scatter(
[d_event.x for d_event in d_events],
[d_event.y for d_event in d_events],
marker="o",
linewidths=5,
c="white",
s=300
)

ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)

plt.savefig(filename)
return plt


26 changes: 26 additions & 0 deletions locaudio/point.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

import math


EARTH_RADIUS = 1000 * 6371


class Point(object):

def __init__(self, x, y):
Expand All @@ -25,13 +29,35 @@ def set_y(self, y):
self.y = y
return self


def dist_to(self, other_point):
return math.sqrt(
pow(self.x - other_point.x, 2) +
pow(self.y - other_point.y, 2)
)


def dist_to_lat_long(self, other_point):
lat1 = math.radians(self.x)
lon1 = math.radians(self.y)
lat2 = math.radians(other_point.x)
lon2 = math.radians(other_point.y)

dlon = lon2 - lon1
dlat = lat2 - lat1

a = (
(math.sin(dlat / 2)) ** 2 +
math.cos(lat1) * math.cos(lat2) * (math.sin(dlon / 2)) ** 2
)

c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))

distance = EARTH_RADIUS * c

return distance


def to_list(self):
return [self.x, self.y]

Expand Down
Loading

0 comments on commit 0e3229f

Please sign in to comment.