Created
July 12, 2020 17:27
-
-
Save bodokaiser/c675303ce76bddc047cda37f014fea2a to your computer and use it in GitHub Desktop.
ZeroMQ subscriber to GNU Radio ZeroMQ source for digital OOK PWM signal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const zmq = require('zeromq') | |
const socket = zmq.socket('sub') | |
socket.connect('tcp://127.0.0.1:3000') | |
socket.subscribe('') | |
const shortSamples = 9 | |
const longSamples = 34 | |
const resetSamples = 348 | |
let bit = 0 | |
let code = 0 | |
let codes = [] | |
let samples = 0 | |
let prevLevel = 0 | |
socket.on('message', (buffer) => { | |
for (let offset = 0; offset < buffer.length; offset += 4) { | |
const currLevel = buffer.readFloatLE(offset) | |
// logic level change detected | |
if (prevLevel !== currLevel) { | |
// logic level changed from high to low | |
if (currLevel === 0 ) { | |
if (samples >= longSamples) { | |
code = code | (1 << bit) | |
bit++ | |
} else { | |
if (samples >= shortSamples) bit++ | |
} | |
} else { | |
if (samples >= resetSamples) { | |
if (codes.push(code) >= 10) { | |
console.log(mode(codes)) | |
codes = [] | |
} | |
code = 0 | |
bit = 0 | |
} | |
} | |
samples = 0 | |
prevLevel = currLevel | |
} | |
samples++ | |
} | |
}) | |
function histogram(values) { | |
let hist = {} | |
values.forEach(value => { | |
hist[value] = (hist[value] || 0) + 1 | |
}) | |
return hist | |
} | |
function mode(values) { | |
let maxFreq = 0 | |
let modeValue | |
for (const [value, freq] of Object.entries(histogram(values))) { | |
if (freq > maxFreq) { | |
maxFreq = freq | |
modeValue = value | |
} | |
} | |
return +modeValue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment