Added files, directories and comments. master
authorRuss Handorf <rhandorf@handorf.org>
Sat, 13 Feb 2016 21:14:55 +0000 (16:14 -0500)
committerRuss Handorf <rhandorf@handorf.org>
Sat, 13 Feb 2016 21:14:55 +0000 (16:14 -0500)
12 files changed:
README
sdrninja-client/sdr.html [new file with mode: 0644]
sdrninja-client/sdr.js [new file with mode: 0644]
sdrninja-client/sdrninja.init [new file with mode: 0755]
sdrninja-client/sdrninja/server.py [new file with mode: 0644]
sdrninja-client/sdrninja/start.sh [new file with mode: 0755]
sdrninja-server/.waterfall.py.swp [new file with mode: 0644]
sdrninja-server/client.py [new file with mode: 0644]
sdrninja-server/radio_math.py [new file with mode: 0644]
sdrninja-server/radio_math.pyc [new file with mode: 0644]
sdrninja-server/sdrninja.initd [new file with mode: 0755]
sdrninja-server/start.sh [new file with mode: 0755]

diff --git a/README b/README
index e69de29..ebfa13b 100644 (file)
--- a/README
+++ b/README
@@ -0,0 +1,47 @@
+Howdy do neighbor!
+
+If you're reading this, then pour yourself a tasty beverage. The history of this was so that I could learn the basics of websockets, html js canvasas, integration with wordpress, and some python coding for rtlsdr bits. All the code contained within is basically what you would need to do in order to mimic what is live on http://sdr.ninja.
+
+Firstly, lets get the licensing out of the way...
+
+###############################################################################
+# The MIT License (MIT)
+# Copyright (c) Russell Handorf
+# Other copyrights noted where code modification is located
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+#
+###############################################################################
+
+In all cases for scripts, replace all instances of 'YOURSERVER' with your server's domainname, ip, or some other identifier. To make your life easier, just grep for this.
+
+sdrninja-client
+-sdr.html : This is the HTML code for your web page to include the canvas
+-sdr.js : This is the JS that is called to connect to the websocket and render the canvas. This has a YOURSERVER config requirement.
+-sdrninja.init : This is an init.d script for making it an automagical process if you'd like. Copy the sdrninja dir into /etc/ to make that happen more easily
+-sdrninja/start.sh : This is the script that the init script calls
+-sdrninja/server.py : This is what creates the python websocket. You'll have some dependencies to install laddy!
+-sdrninja/logs : If you want logs, dump 'em here
+
+sdrninja-server
+-client.py : A fairly modified script origionally written by Tavendo GmbH. I've added web socket handling, frequency range mapping, and a few other minor fixes to make this work. This is a great script to learn from for doing basif FFT in python. Tip of the hat to Tavendo GmbH!
+-radio_math.py/radio_math.pyc : Math dependencies for client.py. Tau for the win.
+-sdrninja.init : This is an init.d script for making it an automagical process if you'd like. Copy the sdrninja dir into /etc/ to make that happen more easily
+-logs : If you want logs, dump 'em here
+
diff --git a/sdrninja-client/sdr.html b/sdrninja-client/sdr.html
new file mode 100644 (file)
index 0000000..97489d7
--- /dev/null
@@ -0,0 +1,2 @@
+<canvas id="sdr" height="48" width="1260" style="vertical-align: bottom" alt="SDR Ninja"></canvas>
+<script src="/js/sdr.js"></script>
diff --git a/sdrninja-client/sdr.js b/sdrninja-client/sdr.js
new file mode 100644 (file)
index 0000000..236b4e9
--- /dev/null
@@ -0,0 +1,144 @@
+var canvas = document.getElementById("sdr");
+var canvasWidth = canvas.width;
+var canvasHeight = canvas.height;
+var ctx = canvas.getContext("2d");
+
+ctx.imageSmoothingEnabled = false;
+ctx.mozImageSmoothingEnabled = false;
+var canvasData = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
+
+//test
+var buffer = document.createElement('canvas');
+buffer.width = canvas.width;
+buffer.height = canvas.height;
+
+
+//init pixels
+var pixels = new Array(canvasHeight*canvasWidth);
+
+//generate static
+var pixelCounter=0;
+
+for (y=0; y<canvasHeight; y++) {
+  for (x=0; x<canvasWidth; x++) {
+    //console.log("y:" + y + " x: " + x);
+    r=Math.floor((Math.random() * 255) + 1);
+    g=Math.floor((Math.random() * 255) + 1);
+    b=Math.floor((Math.random() * 255) + 1);
+    pixels[pixelCounter]={x:x, y:y, r:r, g:g, b:b};
+    pixelCounter++;
+  }
+}
+
+//convert static to json
+var pixelJSON = JSON.stringify(pixels);
+var obj = JSON.parse(pixelJSON);
+var arr = new Array();
+
+for(var x in obj){
+  arr.push(obj[x]);
+}
+
+
+var socket = null;
+var isopen = false;
+
+
+socket = new WebSocket("ws://YOURSERVER:9000");
+//socket.binaryType = "arraybuffer";
+
+socket.onopen = function() {
+   console.log("Connected!");
+   isopen = true;
+   sendText();
+}
+
+socket.onmessage = function(e) {
+   //console.log(e.data);
+   if (typeof e.data == "string") {
+     animate(e.data);
+   } else {
+      var arr = new Uint8Array(e.data);
+      var hex = '';
+      for (var i = 0; i < arr.length; i++) {
+         hex += ('00' + arr[i].toString(16)).substr(-2);
+      }
+      //console.log("Binary message received: " + hex);
+   }
+}
+
+socket.onclose = function(e) {
+   console.log("Connection closed.");
+   socket = null;
+   isopen = false;
+}
+
+function sendText() {
+  if (isopen) {
+     //socket.send(canvasWidth);
+     //console.log("Text message sent.");
+  } else {
+     console.log("Connection not opened.")
+  }
+};
+
+startStatic();
+
+function startStatic() {
+  //draw existing array
+  for (var x=0; x<((arr.length)); x++) {
+    drawPixel(arr[x].x, arr[x].y, arr[x].r, arr[x].g, arr[x].b, 255);
+  }
+  updateCanvas();
+}
+
+function animate(newrowJSON) {
+  //draw existing array
+  for (var x=0; x<((arr.length)); x++) {
+    drawPixel(arr[x].x, arr[x].y, arr[x].r, arr[x].g, arr[x].b, 255);
+  }
+  updateCanvas();
+  //shift everything down
+  for (var x=(arr.length-1); x>canvasWidth; x--) {
+    var offset = x-canvasWidth;
+    arr[x].r = arr[offset].r;
+    arr[x].g = arr[offset].g;
+    arr[x].b = arr[offset].b;
+  }
+  //insert the new row
+  var newrow = JSON.parse(newrowJSON);
+  padding=(canvasWidth-newrow.length)/2;
+  for (var x=0; x<padding; x++) {
+    arr[x].r = 0;
+    arr[x].g = 0;
+    arr[x].b = 100;
+  }
+  for (var x=padding; x<(newrow.length+padding); x++) {
+    //console.log(padding);
+    arr[x].r = newrow[x-padding].r;
+    arr[x].g = newrow[x-padding].g;
+    arr[x].b = newrow[x-padding].b;
+  }
+  for (var x=(newrow.length+padding); x<canvasWidth; x++) {
+    arr[x].r = 0;
+    arr[x].g = 0;
+    arr[x].b = 100;
+  }
+}
+
+function updateCanvas() {
+  ctx.save();
+  ctx.putImageData(canvasData, 0, 0);
+  ctx.restore();
+}
+
+//define the value of a pixel
+function drawPixel (x, y, r, g, b, a) {
+  var index = (x + y * canvasWidth) * 4;
+
+  canvasData.data[index + 0] = r;
+  canvasData.data[index + 1] = g;
+  canvasData.data[index + 2] = b;
+  canvasData.data[index + 3] = a;
+}
+
diff --git a/sdrninja-client/sdrninja.init b/sdrninja-client/sdrninja.init
new file mode 100755 (executable)
index 0000000..0c21e7f
--- /dev/null
@@ -0,0 +1,47 @@
+#! /bin/sh
+
+### BEGIN INIT INFO
+# Provides:          sdrninja
+# Required-Start:      $remote_fs $syslog
+# Required-Stop:       $remote_fs $syslog
+# Default-Start:       2 3 4 5
+# Default-Stop:                
+# Short-Description: sdrninja
+# Description: SDRNINJA
+### END INIT INFO
+
+#N=/etc/init.d/sdrninja
+
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+DAEMON=/etc/sdrninja/start.sh
+PIDFILE=/var/run/sdrninja.pid
+
+test -x $DAEMON || exit 0
+
+. /lib/lsb/init-functions
+
+case "$1" in
+  start)
+     log_daemon_msg "Starting sdrninja"
+     start_daemon -p $PIDFILE $DAEMON
+     log_end_msg $?
+   ;;
+  stop)
+     log_daemon_msg "Stopping sdrninja"
+     killproc -p $PIDFILE $DAEMON
+     PID=`ps x |grep feed | head -1 | awk '{print $1}'`
+     kill -9 $PID       
+     log_end_msg $?
+   ;;
+  force-reload|restart)
+     $0 stop
+     $0 start
+   ;;
+ *)
+   echo "Usage: /etc/init.d/sdrninja {start|stop|restart|force-reload}"
+   exit 1
+  ;;
+esac
+
+exit 0
+
diff --git a/sdrninja-client/sdrninja/server.py b/sdrninja-client/sdrninja/server.py
new file mode 100644 (file)
index 0000000..2b434f5
--- /dev/null
@@ -0,0 +1,110 @@
+from random import randint
+import json, time
+
+import sys
+
+from twisted.internet import reactor
+from twisted.python import log
+from twisted.web.server import Site
+from twisted.web.static import File
+
+from autobahn.twisted.websocket import WebSocketServerFactory, \
+    WebSocketServerProtocol, \
+    listenWS
+
+
+class BroadcastServerProtocol(WebSocketServerProtocol):
+
+    def onOpen(self):
+        self.factory.register(self)
+
+    def onMessage(self, payload, isBinary):
+        if not isBinary:
+            #msg = "{} from {}".format(payload.decode('utf8'), self.peer)
+           msg=format(payload.decode('utf8'))
+            #print self.peer
+            if (format(self.peer).find("192.168.5.84")):
+                self.factory.broadcast(msg)
+
+    def connectionLost(self, reason):
+        WebSocketServerProtocol.connectionLost(self, reason)
+        self.factory.unregister(self)
+
+
+class BroadcastServerFactory(WebSocketServerFactory):
+
+    """
+    Simple broadcast server broadcasting any message it receives to all
+    currently connected clients.
+    """
+
+    def __init__(self, url, debug=False, debugCodePaths=False):
+        WebSocketServerFactory.__init__(self, url, debug=debug, debugCodePaths=debugCodePaths)
+        self.clients = []
+       self.canvasWidth = 1260
+        #self.randomrow()
+
+    #def randomrow(self):
+        #row=[None]*self.canvasWidth
+        #for x in range (0,self.canvasWidth):
+        #  r=randint(0,255)
+        #  g=randint(0,255)
+        #  b=randint(0,255)
+        #  row[x]={"r": r, "g": g, "b": b}
+        #self.broadcast(json.dumps(row))
+        #reactor.callLater(0.1, self.randomrow)
+        ##return(json.dumps(row))
+
+    def register(self, client):
+        if client not in self.clients:
+            print("registered client {}".format(client.peer))
+            self.clients.append(client)
+
+    def unregister(self, client):
+        if client in self.clients:
+            print("unregistered client {}".format(client.peer))
+            self.clients.remove(client)
+
+    def broadcast(self, msg):
+        #print("broadcasting message '{}' ..".format(msg))
+        for c in self.clients:
+            c.sendMessage(msg.encode('utf8'))
+            #print("message sent to {}".format(c.peer))
+
+
+class BroadcastPreparedServerFactory(BroadcastServerFactory):
+
+    """
+    Functionally same as above, but optimized broadcast using
+    prepareMessage and sendPreparedMessage.
+    """
+
+    def broadcast(self, msg):
+        print("broadcasting prepared message '{}' ..".format(msg))
+        preparedMsg = self.prepareMessage(msg)
+        for c in self.clients:
+            c.sendPreparedMessage(preparedMsg)
+            print("prepared message sent to {}".format(c.peer))
+
+
+if __name__ == '__main__':
+
+    if len(sys.argv) > 1 and sys.argv[1] == 'debug':
+        log.startLogging(sys.stdout)
+        debug = True
+    else:
+        debug = False
+
+    ServerFactory = BroadcastServerFactory
+    # ServerFactory = BroadcastPreparedServerFactory
+
+    factory = ServerFactory("ws://localhost:9000",
+                            debug=debug,
+                            debugCodePaths=debug)
+
+    factory.protocol = BroadcastServerProtocol
+    factory.setProtocolOptions(allowHixie76=True)
+    listenWS(factory)
+
+    reactor.run()
+
diff --git a/sdrninja-client/sdrninja/start.sh b/sdrninja-client/sdrninja/start.sh
new file mode 100755 (executable)
index 0000000..88bf935
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+PWD=`pwd`
+script=$PWD'/server.py'
+/usr/bin/python $script &
diff --git a/sdrninja-server/.waterfall.py.swp b/sdrninja-server/.waterfall.py.swp
new file mode 100644 (file)
index 0000000..b1af671
Binary files /dev/null and b/sdrninja-server/.waterfall.py.swp differ
diff --git a/sdrninja-server/client.py b/sdrninja-server/client.py
new file mode 100644 (file)
index 0000000..beda7a0
--- /dev/null
@@ -0,0 +1,267 @@
+###############################################################################
+#
+# The MIT License (MIT)
+#
+# Copyright (c) Tavendo GmbH
+# Modified by Russell Handorf
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+#
+###############################################################################
+
+import json, time
+import sys, math, ctypes, numpy
+
+from rtlsdr import *
+from itertools import *
+from radio_math import *
+import operator
+
+from numpy import mean
+from random import randint
+from twisted.internet import reactor
+from autobahn.twisted.websocket import WebSocketClientFactory, \
+    WebSocketClientProtocol, \
+    connectWS
+
+class SdrWrap(object):
+    "wrap sdr and try to manage tuning"
+    def __init__(self):
+        self.sdr = RtlSdr()
+        self.read_samples = self.sdr.read_samples
+        self.prev_fc = None
+        self.prev_fs = None
+        self.prev_g  = 19
+        self.sdr.gain = 19
+    def tune(self, fc, fs, g):
+        if fc == self.prev_fc and fs == self.prev_fs and g == self.prev_g:
+            return
+        if fc != self.prev_fc:
+            self.sdr.center_freq = fc
+        if fs != self.prev_fs:
+            self.sdr.sample_rate = fs
+        if g != self.prev_g:
+            self.sdr.gain = g
+        self.prev_fc = fc
+        self.prev_fs = fs
+        self.prev_g  = g
+        time.sleep(0.04)  # wait for settle
+        self.sdr.read_samples(2**11)  # clear buffer
+        configure_highlight()
+    def gain_change(self, x):
+        # the whole 10x gain number is annoying
+        real_g = int(self.prev_g * 10)
+        i = self.sdr.GAIN_VALUES.index(real_g)
+        i += x
+        i = min(len(self.sdr.GAIN_VALUES) -1, i)
+        i = max(0, i)
+        new_g = self.sdr.GAIN_VALUES[i]
+        self.sdr.gain = new_g / 10.0
+        self.prev_g   = new_g / 10.0
+
+sdr = SdrWrap()
+
+class Stateful(object):
+    "bucket of globals"
+    def __init__(self):
+        self.freq_lower = None
+        self.freq_upper = None
+        self.vertexes   = []   # (timestamp, vertex_list)
+        self.batches    = []
+        self.time_start = None
+        self.viewport   = None
+        self.history    = 60   # seconds
+        self.fps        = 10
+        self.focus      = False
+        self.hover      = 0
+        self.highlight  = False
+        self.hl_mode    = None  # set this to a function!
+        self.hl_lo      = None
+        self.hl_hi      = None
+        self.hl_filter  = None
+        self.hl_pixels  = None
+       self.width      = 1260
+       self.shiftfreq  = time.time() +10
+
+state = Stateful()
+
+#state.freq_lower = float(929e6)
+#state.freq_upper = float(930e6)
+state.freq_lower = float(100e6)
+state.freq_upper = float(101e6)
+state.time_start = time.time()
+state.viewport = (0,0,1,1)
+
+def x_to_freq(x):
+    vp = state.viewport
+    delta = state.freq_upper - state.freq_lower
+    return delta * x / state.width + state.freq_lower
+
+def log2(x):
+    return math.log(x)/math.log(2)
+
+def acquire_sample(center, bw, detail, samples=8, relay=None):
+    "collect a single frequency"
+    assert bw <= 2.8e6
+    if detail < 8:
+        detail = 8
+    sdr.tune(center, bw, sdr.prev_g)
+    detail = 2**int(math.ceil(log2(detail)))
+    sample_count = samples * detail
+    data = sdr.read_samples(sample_count)
+    ys,xs = psd(data, NFFT=detail, Fs=bw/1e6, Fc=center/1e6)
+    ys = 10 * numpy.log10(ys)
+    if relay:
+        relay(data)
+    return xs, ys
+
+def mapping(x):
+    "assumes -50 to 0 range, returns color"
+    r = int((x+50) * 255 // 50)
+    r = max(0, r)
+    r = min(255, r)
+    return r,r,100
+
+def render_sample(now, dt, freqs, powers):
+    #quads = []
+    #colors = []
+    #row = [None]*4096
+    #interval = int(round(4096/state.width)+1)
+    interval = int(round(4096/state.width))
+    row = [None]*(1024)
+    temp=0
+    counter = 0
+    avgrgb=tuple([0,0,0])
+
+    #server side centering
+    #pad = (((4096-state.width)/interval)/interval)/interval
+    #for temp in range(0,pad):
+    #    row[temp]={"r": 0, "g": 0, "b": 100}
+
+    #temp=pad
+
+    for i,f in enumerate(freqs):
+        rgb = mapping(powers[i])
+        if (counter < interval):
+            avgrgb=tuple(map(operator.add, avgrgb, rgb))
+            #print "added {0}".format(rgb)
+            #print "sum {0}".format(avgrgb)
+            counter+=1
+        else:
+            #print "sum {0}".format(avgrgb)
+            #avgrgb=tuple(map(mean, zip(avgrgb)))
+            avgrgb=tuple(x/interval for x in avgrgb)
+            avgrgb=tuple(map(int, avgrgb))
+            #print "average {0}".format(avgrgb)
+            if (temp<(state.width+1)): 
+                row[temp]={"r": avgrgb[0], "g": avgrgb[1], "b": avgrgb[2]}
+            temp+=1
+            counter=0
+            avgrgb=tuple([0,0,0])
+            #print temp
+            #print "reset {0}".format(avgrgb)
+        #row[i]={"r": rgb[0], "g": rgb[1], "b": rgb[2]}
+
+    #server side centering
+    #for tmp in range(0,(state.width-temp)):
+    #    row[temp+tmp]={"r": 0, "g": 0, "b": 100}
+
+    return(json.dumps(row).encode('utf8'))
+    #self.sendMessage(json.dumps(row).encode('utf8'))
+    #return(json.dumps(row))
+
+def acquire_range(lower, upper):
+    "automatically juggles frequencies"
+    delta  = upper - lower
+    center = (upper+lower)/2
+    #if delta < 1.4e6:
+    if delta < 2.8e6:
+        # single sample
+        return acquire_sample(center, 2.8e6,
+            detail=state.width*2.8e6/delta,
+            relay=state.hl_mode)
+    xs2 = numpy.array([])
+    ys2 = numpy.array([])
+    detail = state.width // ((delta)/(2.8e6))
+    for f in range(int(lower), int(upper), int(2.8e6)):
+        xs,ys = acquire_sample(f+1.4e6, 2.8e6, detail=detail)
+        xs2 = numpy.append(xs2, xs)
+        ys2 = numpy.append(ys2, ys)
+    return xs2, ys2
+
+def configure_highlight():
+    if not state.highlight:
+        return
+    pass_fc = (state.hl_lo + state.hl_hi) / 2
+    pass_bw = state.hl_hi - state.hl_lo
+    if pass_bw == 0:
+        return
+    state.hl_filter = Bandpass(sdr.prev_fc, sdr.prev_fs,
+                               pass_fc, pass_bw)
+
+class BroadcastClientProtocol(WebSocketClientProtocol):
+
+    """
+    Simple client that connects to a WebSocket server, send a HELLO
+    message every 2 seconds and print everything it receives.
+    """
+    canvasWidth = 1260
+
+    def randomrow(self):
+        row=[None]*self.canvasWidth
+        for x in range (0,self.canvasWidth):
+          r=randint(0,255)
+          g=randint(0,255)
+          b=randint(0,255)
+          row[x]={"r": r, "g": g, "b": b}
+        #print(json.dumps(row).encode('utf8'))
+        self.sendMessage(json.dumps(row).encode('utf8'))
+        reactor.callLater(1, self.randomrow)
+        #return(json.dumps(row))
+
+    def update(self):
+        now = time.time() - state.time_start
+       if (state.shiftfreq < time.time()):
+            state.shiftfreq = time.time()+20
+            #state.freq_lower = randint(24e6,1766e6)
+            state.freq_lower = randint(80e6,105e6)
+            state.freq_upper = state.freq_lower + 1e6
+            #print "jumping frequency to {0}".format(state.freq_lower)
+       dt = 1.0/state.fps
+        freqs,power = acquire_range(state.freq_lower, state.freq_upper)
+        self.sendMessage(render_sample(now, dt, freqs, power))
+        reactor.callLater(0.1, self.update)
+
+    def onOpen(self):
+       #self.randomrow()
+       self.update()
+
+
+if __name__ == '__main__':
+
+    if len(sys.argv) < 2:
+        print("Need the WebSocket server address, i.e. ws://localhost:9000")
+        sys.exit(1)
+
+    factory = WebSocketClientFactory(sys.argv[1]) 
+    #factory = WebSocketClientFactory("ws://localhost:9000")
+    factory.protocol = BroadcastClientProtocol
+    connectWS(factory)
+
+    reactor.run()
diff --git a/sdrninja-server/radio_math.py b/sdrninja-server/radio_math.py
new file mode 100644 (file)
index 0000000..00612f1
--- /dev/null
@@ -0,0 +1,204 @@
+#! /usr/bin/env python2
+
+from itertools import *
+import math, numpy
+
+tau = 2 * math.pi
+
+class Translate(object):
+    "set up once, consumes an I/Q stream, returns an I/Q stream"
+    def __init__(self, num, den):
+        angles = [(a*tau*num/den) % tau for a in range(den)]
+        fir = [complex(math.cos(a), math.sin(a)) for a in angles]
+        self.looping_fir = cycle(fir)
+    def __call__(self, stream):
+        return numpy.array([s1*s2 for s1,s2 in izip(self.looping_fir, stream)])
+
+class Downsample(object):
+    "set up once, consumes an I/Q stream, returns an I/Q stream"
+    # aka lowpass
+    def __init__(self, scale):
+        self.scale = scale
+        self.offset = 0
+        self.window = numpy.hanning(scale * 2)
+        self.window = self.window / sum(self.window)
+    def __call__(self, stream):
+        prev_off = self.offset
+        self.offset = self.scale - ((len(stream) + self.offset) % self.scale)
+        # bad edges, does 60x more math than needed
+        stream2 = numpy.convolve(stream, self.window)
+        return stream2[prev_off::self.scale]
+
+class DownsampleFloat(object):
+    # poor quality, but good temporal accuracy
+    # uses triangle window
+    def __init__(self, scale):
+        self.scale = scale
+        self.offset = 0
+    def __call__(self, stream):
+        # bad edges
+        # should be using more numpy magic
+        scale = self.scale
+        stream2 = []
+        for x in numpy.arange(self.offset, len(stream), scale):
+            frac = x % 1.0
+            window = numpy.concatenate((numpy.arange(1-frac, scale),
+                     numpy.arange(int(scale)+frac-1, 0, -1)))
+            window = window / sum(window)
+            start = x - len(window)//2
+            start = max(start, 0)
+            start = min(start, len(stream)-len(window))
+            c = sum(stream[start : start+len(window)] * window)
+            stream2.append(c)
+        return numpy.array(stream2)
+
+class Upsample(object):
+    # use minimal power interpolation?
+    def __init__(self, scale):
+        self.scale = scale
+        self.offset = 0
+    def __call__(self, stream):
+        xp = range(len(stream))
+        x2 = numpy.arange(self.offset, len(stream), 1.0/self.scale)
+        self.offset = (len(stream) + self.offset) % self.scale
+        reals = numpy.interp(x2, xp, stream.real)
+        imags = numpy.interp(x2, xp, stream.imag)
+        return numpy.array([complex(*ri) for ri in zip(reals, imags)])
+
+class Bandpass(object):
+    "set up once, consumes an I/Q stream, returns an I/Q stream"
+    def __init__(self, center_fc, center_bw, pass_fc, pass_bw):
+        # some errors from dropping the fractional parts of the ratio
+        # either optimize tuning, scale up, or use floating point
+        ratio = (center_fc - pass_fc) / center_bw
+        self.translate = Translate(ratio * 1024, 1024)
+        self.downsample = Downsample(int(center_bw/pass_bw))
+    def __call__(self, stream):
+        return self.downsample(self.translate(stream))
+
+# check license on matplotlib code
+# chop out as much slow junk as possible
+
+# http://mail.scipy.org/pipermail/numpy-discussion/2003-January/014298.html
+# http://cleaver.cnx.rice.edu/eggs_directory/obspy.signal/obspy.signal/obspy/signal/freqattributes.py
+# /usr/lib/python2.7/site-packages/matplotlib/mlab.py
+
+def detrend_none(x):
+    "Return x: no detrending"
+    return x
+
+def window_hanning(x):
+    "return x times the hanning window of len(x)"
+    return numpy.hanning(len(x))*x
+
+def psd(x, NFFT=256, Fs=2, Fc=0, detrend=detrend_none, window=window_hanning,
+        noverlap=0, pad_to=None, sides='default', scale_by_freq=True):
+    Pxx,freqs = csd(x, x, NFFT, Fs, detrend, window, noverlap, pad_to, sides,
+        scale_by_freq)
+    return Pxx.real, freqs + Fc
+
+def csd(x, y, NFFT=256, Fs=2, detrend=detrend_none, window=window_hanning,
+        noverlap=0, pad_to=None, sides='default', scale_by_freq=True):
+    Pxy, freqs, t = _spectral_helper(x, y, NFFT, Fs, detrend, window,
+        noverlap, pad_to, sides, scale_by_freq)
+
+    if len(Pxy.shape) == 2 and Pxy.shape[1]>1:
+        Pxy = Pxy.mean(axis=1)
+    return Pxy, freqs
+
+
+#This is a helper function that implements the commonality between the
+#psd, csd, and spectrogram.  It is *NOT* meant to be used outside of mlab
+def _spectral_helper(x, y, NFFT=256, Fs=2, detrend=detrend_none,
+        window=window_hanning, noverlap=0, pad_to=None, sides='default',
+        scale_by_freq=True):
+    #The checks for if y is x are so that we can use the same function to
+    #implement the core of psd(), csd(), and spectrogram() without doing
+    #extra calculations.  We return the unaveraged Pxy, freqs, and t.
+    same_data = y is x
+
+    #Make sure we're dealing with a numpy array. If y and x were the same
+    #object to start with, keep them that way
+    x = numpy.asarray(x)
+    if not same_data:
+        y = numpy.asarray(y)
+    else:
+        y = x
+
+    # zero pad x and y up to NFFT if they are shorter than NFFT
+    if len(x)<NFFT:
+        n = len(x)
+        x = numpy.resize(x, (NFFT,))
+        x[n:] = 0
+
+    if not same_data and len(y)<NFFT:
+        n = len(y)
+        y = numpy.resize(y, (NFFT,))
+        y[n:] = 0
+
+    if pad_to is None:
+        pad_to = NFFT
+
+    # For real x, ignore the negative frequencies unless told otherwise
+    if (sides == 'default' and numpy.iscomplexobj(x)) or sides == 'twosided':
+        numFreqs = pad_to
+        scaling_factor = 1.
+    elif sides in ('default', 'onesided'):
+        numFreqs = pad_to//2 + 1
+        scaling_factor = 2.
+    else:
+        raise ValueError("sides must be one of: 'default', 'onesided', or "
+            "'twosided'")
+
+    #if cbook.iterable(window):
+    if type(window) != type(lambda:0):
+        assert(len(window) == NFFT)
+        windowVals = window
+    else:
+        windowVals = window(numpy.ones((NFFT,), x.dtype))
+
+    step = NFFT - noverlap
+    ind = numpy.arange(0, len(x) - NFFT + 1, step)
+    n = len(ind)
+    Pxy = numpy.zeros((numFreqs, n), numpy.complex_)
+
+    # do the ffts of the slices
+    for i in range(n):
+        thisX = x[ind[i]:ind[i]+NFFT]
+        thisX = windowVals * detrend(thisX)
+        fx = numpy.fft.fft(thisX, n=pad_to)
+
+        if same_data:
+            fy = fx
+        else:
+            thisY = y[ind[i]:ind[i]+NFFT]
+            thisY = windowVals * detrend(thisY)
+            fy = numpy.fft.fft(thisY, n=pad_to)
+        Pxy[:,i] = numpy.conjugate(fx[:numFreqs]) * fy[:numFreqs]
+
+    # Scale the spectrum by the norm of the window to compensate for
+    # windowing loss; see Bendat & Piersol Sec 11.5.2.
+    Pxy /= (numpy.abs(windowVals)**2).sum()
+
+    # Also include scaling factors for one-sided densities and dividing by the
+    # sampling frequency, if desired. Scale everything, except the DC component
+    # and the NFFT/2 component:
+    Pxy[1:-1] *= scaling_factor
+
+    # MATLAB divides by the sampling frequency so that density function
+    # has units of dB/Hz and can be integrated by the plotted frequency
+    # values. Perform the same scaling here.
+    if scale_by_freq:
+        Pxy /= Fs
+
+    t = 1./Fs * (ind + NFFT / 2.)
+    freqs = float(Fs) / pad_to * numpy.arange(numFreqs)
+
+    if (numpy.iscomplexobj(x) and sides == 'default') or sides == 'twosided':
+        # center the frequency range at zero
+        freqs = numpy.concatenate((freqs[numFreqs//2:] - Fs, freqs[:numFreqs//2]))
+        Pxy = numpy.concatenate((Pxy[numFreqs//2:, :], Pxy[:numFreqs//2, :]), 0)
+
+    return Pxy, freqs, t
+
+
diff --git a/sdrninja-server/radio_math.pyc b/sdrninja-server/radio_math.pyc
new file mode 100644 (file)
index 0000000..4fbe981
Binary files /dev/null and b/sdrninja-server/radio_math.pyc differ
diff --git a/sdrninja-server/sdrninja.initd b/sdrninja-server/sdrninja.initd
new file mode 100755 (executable)
index 0000000..8626e46
--- /dev/null
@@ -0,0 +1,47 @@
+#! /bin/sh
+
+### BEGIN INIT INFO
+# Provides:          sdrninja
+# Required-Start:    $local_fs $remote_fs
+# Required-Stop:
+# X-Start-Before:    rmnologin
+# Default-Start:     2 3 4 5
+# Default-Stop:
+# Short-Description: sdrninja
+# Description: SDRNINJA
+### END INIT INFO
+
+#N=/etc/init.d/sdrninja
+
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+DAEMON=/etc/sdrninja/start.sh
+PIDFILE=/var/run/sdrninja.pid
+
+test -x $DAEMON || exit 0
+
+. /lib/lsb/init-functions
+
+case "$1" in
+  start)
+     log_daemon_msg "Starting sdrninja"
+     start_daemon -p $PIDFILE $DAEMON
+     log_end_msg $?
+   ;;
+  stop)
+     log_daemon_msg "Stopping sdrninja"
+     killproc -p $PIDFILE $DAEMON
+     PID=`ps x |grep feed | head -1 | awk '{print $1}'`
+     kill -9 $PID       
+     log_end_msg $?
+   ;;
+  force-reload|restart)
+     $0 stop
+     $0 start
+   ;;
+ *)
+   echo "Usage: /etc/init.d/sdrninja {start|stop|restart|force-reload}"
+   exit 1
+  ;;
+esac
+
+exit 0
diff --git a/sdrninja-server/start.sh b/sdrninja-server/start.sh
new file mode 100755 (executable)
index 0000000..b2cabef
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+PWD=`pwd`
+script=$PWD'/client.py ws://YOURSERVER:9000'
+/usr/bin/python $script &