-
Notifications
You must be signed in to change notification settings - Fork 2
/
iq_u8.go
123 lines (110 loc) · 3.92 KB
/
iq_u8.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2020-2021
//
// 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. }}}
package sdr
import (
"unsafe"
)
// SamplesU8 indicates that the samples are being sent as a vector
// of interleaved uint8 numbers, where 0 is -1, and 1 is 0xFF.
//
// This type is very hard to process, since the 0 value is 127.5, which
// is not representable, but it's *very* effective to send data over
// a connection, since it's the most compact representation.
//
// This is the native format of the rtl-sdr.
type SamplesU8 [][2]uint8
// Format returns the type of this vector, as exported by the SampleFormat
// enum.
func (s SamplesU8) Format() SampleFormat {
return SampleFormatU8
}
// Size will return the size of this sdr.Samples in *bytes*. This is used
// when your code needs to be aware of the underlying storage size. This
// should usually only be used at i/o boundaries.
func (s SamplesU8) Size() int {
return int(unsafe.Sizeof([2]uint8{})) * len(s)
}
// Length will return the number of IQ samples in this vector of Samples.
//
// This is the count of real and imaginary pairs, so in the case
// of the U8 type, this will be half the size of the vector.
//
// This function is usually the correct one to use when processing
// sample information.
func (s SamplesU8) Length() int {
return len(s)
}
// Slice will return a slice of the sample buffer from the provided
// starting position until the ending position. The returned value is
// assumed to be a slice, which is to say, mutations of the returned
// Samples will modify the slice from whence it came.
//
// samples.Slice(0, 10) is assumed to be the same as samples[:10], except
// it does not require the typecast to the concrete type implementing
// this interface.
func (s SamplesU8) Slice(start, end int) Samples {
return s[start:end]
}
// ToI16 will convert the uint8 data to a vector of interleaved int16
// values.
func (s SamplesU8) ToI16(out SamplesI16) (int, error) {
if s.Length() > out.Length() {
return 0, ErrDstTooSmall
}
for i := range s {
out[i] = [2]int16{
int16((int32(s[i][0]) << 8) - 32768),
int16((int32(s[i][1]) << 8) - 32768),
}
}
return s.Length(), nil
}
// ToI8 will convert the uint8 data to a vector of int8 values.
func (s SamplesU8) ToI8(out SamplesI8) (int, error) {
if s.Length() > out.Length() {
return 0, ErrDstTooSmall
}
for i := range s {
out[i] = [2]int8{
int8(int16(s[i][0]) - 128),
int8(int16(s[i][1]) - 128),
}
}
return s.Length(), nil
}
// ToC64 will convert the uint8 data to a vector of complex64 numbers.
func (s SamplesU8) ToC64(out SamplesC64) (int, error) {
if s.Length() > out.Length() {
return 0, ErrDstTooSmall
}
convU8ToC64(s, out)
return s.Length(), nil
}
func convU8ToC64Native(s1 SamplesU8, s2 SamplesC64) {
for i := range s1 {
rel := s1[i][0]
img := s1[i][1]
s2[i] = complex(
(float32(rel)-127.5)/127.5,
(float32(img)-127.5)/127.5,
)
}
}
// vim: foldmethod=marker