From: Russ Handorf Date: Sat, 13 Feb 2016 21:14:55 +0000 (-0500) Subject: Added files, directories and comments. X-Git-Url: http://russells-world.com/code/?p=sdr-websocket.git;a=commitdiff_plain Added files, directories and comments. --- diff --git a/README b/README index e69de29..ebfa13b 100644 --- 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 index 0000000..97489d7 --- /dev/null +++ b/sdrninja-client/sdr.html @@ -0,0 +1,2 @@ + + diff --git a/sdrninja-client/sdr.js b/sdrninja-client/sdr.js new file mode 100644 index 0000000..236b4e9 --- /dev/null +++ b/sdrninja-client/sdr.js @@ -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; ycanvasWidth; 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 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 index 0000000..88bf935 --- /dev/null +++ b/sdrninja-client/sdrninja/start.sh @@ -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 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 index 0000000..beda7a0 --- /dev/null +++ b/sdrninja-server/client.py @@ -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 index 0000000..00612f1 --- /dev/null +++ b/sdrninja-server/radio_math.py @@ -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)