-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
176 lines (144 loc) · 5.17 KB
/
main.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <fstream>
#include <thread>
#include <random>
#include <WaveFile.h>
#include <Buffer.h>
#include <Utils.h>
#include <CICFilter.h>
#include <Constellation.h>
#include <Constants.h>
/* This file is only for testing */
/* Init random generator */
std::mt19937 rng(std::random_device{}());
// Random bytes distribuition
std::uniform_int_distribution<U32> dist(0, UINT32_MAX);
std::normal_distribution<F32> ndist(0.0f,1.0f);
/*
class SoapySink: public SyncBlock<CF32>
{
public:
struct Settings{
double SampleRate;
double Frequency;
size_t BufferSize=131072;
};
SoapySink(std::string KwArgs, Settings config={.SampleRate=1e6,.Frequency=433e6}): SyncBlock(config.BufferSize, Sink) {
m_name = "SoapySink";
LOG_DEBUG("Created Soapy Sink.");
SoapySDR::Kwargs args = SoapySDR::KwargsFromString(KwArgs);
m_sdr = SoapySDR::Device::make(args);
if(m_sdr == NULL){
LOG_ERROR("SoapySDR::Device::make failed");
exit(EXIT_FAILURE);
}
// 3. apply settings
m_sdr->setSampleRate( SOAPY_SDR_TX, 0, config.SampleRate);
m_sdr->setFrequency( SOAPY_SDR_TX, 0, config.Frequency);
// 4. setup a stream (complex floats)
m_tx_stream = m_sdr->setupStream( SOAPY_SDR_TX, SOAPY_SDR_CF32);
if( m_tx_stream == NULL)
{
LOG_ERROR("Failed");
SoapySDR::Device::unmake( m_sdr );
exit(EXIT_FAILURE);
}
m_sdr->activateStream( m_tx_stream, 0, 0, 0);
}
size_t work(const size_t& n_inputItems, std::vector<CF32>& input, std::vector<CF32>& output){
void *buffs[] = {input.data()};
int flags;
long long time_ns;
int ret = m_sdr->writeStream(m_tx_stream, buffs, n_inputItems, flags, time_ns, 1e5);
LOG_DEBUG("ret = {}, flags = {}, time_ns = {}", ret, flags, time_ns);
return ret;
}
~SoapySink(){
m_sdr->deactivateStream( m_tx_stream, 0, 0); //stop streaming
m_sdr->closeStream( m_tx_stream );
// 8. cleanup device handle
SoapySDR::Device::unmake( m_sdr );
LOG_DEBUG("Destroyed Soapy Sink.");
}
private:
SoapySDR::Device *m_sdr;
SoapySDR::Stream *m_tx_stream;
};
*/
void _writeFloat(F32 data){
I16 val = (short)(0.5f * data * (float)INT16_MAX);
std::cout << (U8)(val & 0xFF) << (U8)((val >> 8) & 0xFF);
}
void WriteCout(F32 data){
_writeFloat(data);
}
void WriteCout(CF32 data){
_writeFloat(data.real());
_writeFloat(data.imag());
}
int main(){
//SoapySink sink("driver=plutosdr", {.SampleRate=1e6, .Frequency=433e6, .BufferSize=4096});
//sink.connect(stream);
//sink.start();
// CIC Converter
CICRate Rate = {24, 15};
CICFilter cic_interp(5, {Rate.Interpolation, 1}, 1024);
CICFilter cic_decim(5, {1, Rate.Decimation}, 1024);
CICFilter resampler(1, Rate, 1024);
// Samples
size_t Nsamples = 8192;
std::vector<CF32> input_data(Nsamples);
std::vector<CF32> temp_data(Nsamples * Rate.Interpolation);
std::vector<CF32> output_data(Nsamples * Rate.Interpolation);
// AWGN Noise
F32 npwr = 1.0f / sqrtf(2.0f);
for (size_t i = 0; i < input_data.size(); i++)
{
input_data[i] = CF32(npwr*ndist(rng),npwr*ndist(rng));
}
// Filter
cic_interp.work(input_data.size(), input_data, temp_data);
cic_decim.work(temp_data.size(), temp_data, output_data);
float spwr = Vec_AvgPwr(output_data);
LOG_TEST("Avg Power: {:.3f} V(rms)",spwr);
WriteWav("test_cic.wav", ComplexInterleave(output_data), 48000, 2, 500.0f);
std::vector <CF32> points;
for (size_t i = 0; i < 4; i++)
{
float angle = (float)i / 4.0f + 0.125f;
points.push_back(0.4f * 0.707f * std::exp(_1j * M_TWOPI_F * angle));
}
for (size_t i = 0; i < 12; i++)
{
float angle = (float)i / 12.0f;
points.push_back(0.67f * std::exp(_1j * M_TWOPI_F * angle));
}
for (size_t i = 0; i < 16; i++)
{
float angle = (float)i / 16.0f + 0.5f / 16.0f;
points.push_back(std::exp(_1j * M_TWOPI_F * angle));
}
ConstellationMapper qam_mapper(points, 131072);
ConstellationDemapper qam_demapper(points, 131072);
std::vector<U8> bitsin(256, 0);
std::vector<U8> bitsout(256, 0);
// Test the Map -> Demap
while(1){
for (size_t i = 0; i < 256; i++)
{
bitsin[i] = dist(rng) & 1;
}
size_t n = qam_mapper.work(bitsin.size(), bitsin, output_data);
size_t n2 = qam_demapper.work(n, output_data, bitsout);
for (size_t i = 0; i < std::min(bitsin.size(),n2); i++)
{
LOG_INFO("%d == %d ? %d", bitsin[i], bitsout[i], bitsin[i] == bitsout[i]);
//CF32 awgn = CF32(ndist(rng), ndist(rng)) / sqrtf(2.0f);
//WriteCout(awgn * 0.1f + output_data[i] * 1.4142f);
}
break;
//LOG_DEBUG("Wrote {} Samples", n);
std::this_thread::sleep_for(std::chrono::milliseconds(1000*n/48000));
}
return 0;
}