forked from vsergeev/luaradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slicer.lua
44 lines (35 loc) · 1.08 KB
/
slicer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
---
-- Slice a real-valued signal on a threshold into a bit stream.
--
-- $$ y[n] = \begin{cases} 1 & \text{if } x[n] > \text{threshold} \\ 0 & \text{if } x[n] < \text{threshold} \end{cases} $$
--
-- @category Digital
-- @block SlicerBlock
-- @tparam[opt=0.0] number threshold Threshold
--
-- @signature in:Float32 > out:Bit
--
-- @usage
-- -- Slice at default threshold 0.0
-- local slicer = radio.SlicerBlock()
--
-- -- Slice at threshold 0.5
-- local slicer = radio.SlicerBlock(0.5)
local block = require('radio.core.block')
local types = require('radio.types')
local SlicerBlock = block.factory("SlicerBlock")
function SlicerBlock:instantiate(threshold)
self.threshold = threshold or 0.0
self:add_type_signature({block.Input("in", types.Float32)}, {block.Output("out", types.Bit)})
end
function SlicerBlock:initialize()
self.out = types.Bit.vector()
end
function SlicerBlock:process(x)
local out = self.out:resize(x.length)
for i = 0, x.length-1 do
out.data[i].value = (x.data[i].value > self.threshold) and 1 or 0
end
return out
end
return SlicerBlock