-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
225 lines (171 loc) · 6.55 KB
/
main.cpp
File metadata and controls
225 lines (171 loc) · 6.55 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdint>
#include <complex>
#include <list>
#include <bitset>
#include <cmath>
#include <concepts>
const float pi = std::acos(-1);
template <typename T>
concept Container = requires(T a, typename T::value_type val) {
{ a.push_back(val) };
{ a.begin() };
{ a.end() };
};
std::vector<int> toBinary(const int& number, const int& bits) {
std::vector<int> binary(bits);
for (int i = 0; i < bits; ++i) {
binary[bits - i - 1] = (number >> i) & 1;
}
return binary;
}
int binaryToGray(int num) {
return num ^ (num >> 1);
}
int graydecode(unsigned int gray) {
unsigned int bin;
for (bin = 0; gray; gray >>= 1) {
bin ^= gray;
}
return bin;
}
std::list<double> cumsum(const std::list<double>& data){
std::list<double> cumsum;
double sum=0;
for (auto val : data){
sum += val;
cumsum.emplace_back(sum);
}
return cumsum;
}
double sum_abs(const std::vector<std::complex<double>>& data) {
double sum = 0;
for (const auto& val : data) {
sum += std::abs(val);
}
return sum;
}
std::list<double> angle(const std::list<std::complex<double>>& data) {
std::list<double> angles;
for (const auto& val : data) {
angles.emplace_back(std::arg(val));
}
return angles;
}
std::list<std::complex<double>> pskmod(const std::vector<int>& data, double M, double phaseOffset = 0.0, bool useGrayCoding = true) {
std::list<std::complex<double>> modulatedData;
int numSymbols = data.size();
double phaseStep = 2.0 * pi / M;
for (int i = 0; i < numSymbols; ++i) {
int symbol = data[i];
if (symbol >= M) {
throw std::invalid_argument("Symbol index out of range");
}
if (useGrayCoding) {
symbol = binaryToGray(symbol);
}
double phase = phaseOffset + symbol * phaseStep;
modulatedData.emplace_back(std::cos(phase), std::sin(phase));
}
return modulatedData;
}
std::list<int> pskDemodGray(const std::list<std::complex<double>>& iq_cleared, int M, double phaseOffset) {
std::list<int> demodulatedSymbols;
double angleStep = 2 * pi / M;
for (const auto& iq : iq_cleared) {
double phase = std::arg(iq) - phaseOffset;
if (phase < 0) phase += 2 * pi;
int symbolIndex = static_cast<int>(std::round(phase / angleStep)) % M;
symbolIndex = graydecode(symbolIndex);
demodulatedSymbols.emplace_back(symbolIndex);
}
return demodulatedSymbols;
}
template <Container ContainerType>
ContainerType Demodulate(const double M, const std::vector<int>& preamb, const std::vector<std::complex<double>>& complexData) {
unsigned int n = complexData.size();
unsigned int pLen = preamb.size();
std::list<std::complex<double>> iq_preamb = pskmod(preamb, M, pi / M, true);
std::list<std::complex<double>> ph_deltas;
auto dataIt = complexData.begin();
auto nextIt = std::next(dataIt);
for (; nextIt != complexData.end(); ++dataIt, ++nextIt) {
ph_deltas.emplace_back(std::pow(std::pow(*nextIt, M) * std::conj(std::pow(*dataIt, M)), 1.0 / M));
}
std::list<double> ph_acc{0};
std::list<double> angles = angle(ph_deltas);
std::list<double> csum = cumsum(angles);
ph_acc.insert(ph_acc.end(), csum.begin(), csum.end());
std::list<std::complex<double>> iq_dedop;
auto ph_accIt = ph_acc.begin();
for (const auto& data : complexData) {
iq_dedop.emplace_back(data * std::exp(std::complex<double>(0.0, -(*ph_accIt))));
++ph_accIt;
}
std::complex<double> sum_prod = 0;
auto iq_dedopIt = iq_dedop.begin();
auto iq_preambIt = iq_preamb.begin();
for (size_t i = 0; i < pLen; ++i, ++iq_dedopIt, ++iq_preambIt) {
sum_prod += (*iq_dedopIt) * std::conj(*iq_preambIt);
}
double sum_abs_val = sum_abs(std::vector<std::complex<double>>(iq_dedop.begin(), std::next(iq_dedop.begin(), pLen)));
std::complex<double> initial_phase = sum_prod / static_cast<std::complex<double>>(sum_abs_val);
std::cout << "Initial phase: " << initial_phase << std::endl;
// reuse iq_dedop to store iq_cleared for minimize memory
for (auto& val : iq_dedop) {
val *= std::conj(initial_phase);
}
std::list<int> demodulatedSymbols = pskDemodGray(iq_dedop, M, pi / M);
ContainerType output_data;
for (auto i = std::next(demodulatedSymbols.begin(), pLen); i != demodulatedSymbols.end(); ++i) {
auto bits = toBinary(*i, std::log2(M));
output_data.insert(output_data.end(), bits.begin(), bits.end());
}
return output_data;
}
std::vector<int> binaryToVector(const std::string& binaryStr) {
std::vector<int> result;
for (size_t i = 0; i < binaryStr.size(); i += 2) {
std::string byte = binaryStr.substr(i, 2);
result.push_back(std::stoi(byte, nullptr, 2));
}
return result;
}
int main(int argc, char* argv[]) {
setlocale(LC_ALL, "Russian");
if (argc != 4) {
std::cerr << "Usage: " << argv[0] << " <mod_order> <preamble_binary> <filename>" << std::endl;
return 1;
}
double mod_order = std::stod(argv[1]);
std::string preamble_binary = argv[2];
std::vector<int> preamb = binaryToVector(preamble_binary);
std::ifstream file(argv[3], std::ios::binary);
if (!file.is_open()) {
std::cerr << "Can't open the file: " << argv[3] << std::endl;
return 1;
}
file.seekg(0, std::ios::end);
unsigned long int file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<int16_t> data(file_size / sizeof(int16_t));
if (file.read(reinterpret_cast<char*>(data.data()), file_size)) {
std::cout << "File is read: " << data.size() << std::endl;
} else {
std::cerr << "Error reading the file" << std::endl;
}
file.close();
std::vector<std::complex<double>> complexData(data.size() / 2);
for (unsigned long int i = 0; i < complexData.size(); ++i) {
complexData[i] = std::complex<double>(data[2 * i], data[2 * i + 1]);
}
std::list<double> result = Demodulate<std::list<double>>(mod_order, preamb, complexData); //u can change return container and the tyoe of data in it
std::cout<<"Demodulated data: ";
for (auto val:result){
std::cout<<val;
}
std::cout<<"\n";
return 0;
}