forked from vsergeev/luaradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplyconstant.lua
77 lines (65 loc) · 2.55 KB
/
multiplyconstant.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
-- Multiply a complex or real valued signal by a real-valued constant, or
-- multiply a complex-valued signal by a complex-valued constant.
--
-- $$ y[n] = C \; x[n] $$
--
-- @category Math Operations
-- @block MultiplyConstantBlock
-- @tparam number|Float32|ComplexFloat32 constant Constant
--
-- @signature in:Float32 > out:Float32
-- @signature in:ComplexFloat32 > out:ComplexFloat32
--
-- @usage
-- -- Multiply by number (Float32) constant
-- local gain = radio.MultiplyConstantBlock(5.0)
--
-- -- Multiply by Float32 constant
-- local gain = radio.MultiplyConstantBlock(radio.types.Float32(5.0))
--
-- -- Complex rotation by 45 degrees
-- local rotator = radio.MultiplyConstantBlock(radio.types.ComplexFloat32(math.cos(math.pi/4),
-- math.sin(math.pi/4)))
local block = require('radio.core.block')
local class = require('radio.core.class')
local types = require('radio.types')
local MultiplyConstantBlock = block.factory("MultiplyConstantBlock")
function MultiplyConstantBlock:instantiate(constant)
assert(constant, "Missing argument #1 (constant)")
-- Convert constant to Float32 or ComplexFloat32
if class.isinstanceof(constant, "number") then
self.constant = types.Float32(constant)
elseif class.isinstanceof(constant, types.Float32) then
self.constant = constant
elseif class.isinstanceof(constant, types.ComplexFloat32) then
self.constant = constant
else
error("Unsupported constant type")
end
if class.isinstanceof(constant, types.ComplexFloat32) then
-- Only allow complex inputs for a complex constant
self:add_type_signature({block.Input("in", types.ComplexFloat32)}, {block.Output("out", types.ComplexFloat32)})
else
self:add_type_signature({block.Input("in", types.Float32)}, {block.Output("out", types.Float32)})
self:add_type_signature({block.Input("in", types.ComplexFloat32)}, {block.Output("out", types.ComplexFloat32)}, self.process_complex_by_real)
end
end
function MultiplyConstantBlock:initialize()
self.out = self:get_output_type().vector()
end
function MultiplyConstantBlock:process(x)
local out = self.out:resize(x.length)
for i = 0, x.length - 1 do
out.data[i] = x.data[i] * self.constant
end
return out
end
function MultiplyConstantBlock:process_complex_by_real(x)
local out = self.out:resize(x.length)
for i = 0, x.length - 1 do
out.data[i] = x.data[i]:scalar_mul(self.constant.value)
end
return out
end
return MultiplyConstantBlock