-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSDRUtils.js
37 lines (30 loc) · 1.15 KB
/
SDRUtils.js
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
import ndarray from "ndarray";
const ONE_HALF_MINUS_EPSILON = 0.5-Number.MIN_VALUE;
class SDRUtils {
static buildLinearEncoderDecoder(minValue, maxValue, sdrSize, sdrWidth) {
return [
SDRUtils.buildLinearEncoder(minValue, maxValue, sdrSize, sdrWidth),
SDRUtils.buildLinearDecoder(minValue, maxValue, sdrSize, sdrWidth)
];
}
static buildLinearEncoder(minValue, maxValue, sdrSize, sdrWidth) {
const bitRange = sdrSize-sdrWidth;
const valueRange = maxValue - minValue;
return (value) => {
const firstBit = Math.floor(bitRange*(value-minValue)/valueRange + ONE_HALF_MINUS_EPSILON);
return SDRUtils.range(firstBit, firstBit+sdrWidth);
};
}
static buildLinearDecoder(minValue, maxValue, sdrSize, sdrWidth) {
const bitRange = sdrSize-sdrWidth;
const valueRange = maxValue - minValue;
return (sdr, rounded=true) => {
const decoded = (sdr[0]-ONE_HALF_MINUS_EPSILON)*valueRange/bitRange + minValue;
return rounded ? Math.round(decoded) : decoded;
}
}
static range(start, end, step=1) {
return Array.from(new Array(end-start), (_,i) => i*step + start);
}
}
export default SDRUtils;