Better?? PSK Modulator??

This commit is contained in:
2024-10-09 00:30:31 -04:00
parent d4cbd9daad
commit 445d9a6076
3 changed files with 52 additions and 68 deletions
+48 -64
View File
@@ -1,74 +1,58 @@
#ifndef PSK_MODULATOR_H
#define PSK_MODULATOR_H
#include <array>
#include <vector>
#include <cmath>
#include <cstdint>
#include <vector>
#include <stdexcept>
class PSKModulatorConfig {
class PSKModulator {
public:
int carrier_frequency;
int sample_rate;
int baud_rate;
int bits_per_symbol;
PSKModulator(double sample_rate) : sample_rate(sample_rate), carrier_freq(1800), phase(0.0) {
initializeSymbolMap();
}
PSKModulatorConfig(int cf, int sr, int br) : carrier_frequency(cf), sample_rate(sr), baud_rate(br) {}
std::vector<int16_t> modulate(const std::vector<uint8_t>& symbols) {
std::vector<int16_t> modulated_signal;
const double phase_increment = 2 * M_PI * carrier_freq / sample_rate;
for (auto symbol : symbols) {
if (symbol >= symbolMap.size()) {
throw std::out_of_range("Invalid symbol value for 8-PSK modulation");
}
double target_phase = symbolMap[symbol];
for (size_t i = 0; i < samples_per_symbol; ++i) {
double value = std::sin(phase + target_phase);
modulated_signal.push_back(static_cast<int16_t>(value * std::numeric_limits<int16_t>::max()));
phase += phase_increment;
if (phase >= 2 * M_PI) {
phase -= 2 * M_PI;
}
}
}
return modulated_signal;
}
private:
double sample_rate; ///< The sample rate of the system.
double carrier_freq; ///< The frequency of the carrier, set to 1800 Hz as per standard.
double phase; ///< Current phase of the carrier waveform.
size_t samples_per_symbol = 40; ///< Number of samples per symbol, calculated to match symbol duration with cycle.
std::vector<double> symbolMap; ///< The mapping of tribit symbols to phase shifts.
void initializeSymbolMap() {
symbolMap = {
0.0, // 0 (000) corresponds to 0 degrees
M_PI / 4, // 1 (001) corresponds to 45 degrees
M_PI / 2, // 2 (010) corresponds to 90 degrees
3 * M_PI / 4, // 3 (011) corresponds to 135 degrees
M_PI, // 4 (100) corresponds to 180 degrees
5 * M_PI / 4, // 5 (101) corresponds to 225 degrees
3 * M_PI / 2, // 6 (110) corresponds to 270 degrees
7 * M_PI / 4 // 7 (111) corresponds to 315 degrees
};
}
};
namespace milstd {
class PSKModulator {
public:
PSKModulator(const PSKModulatorConfig& config) : carrier_frequency(config.carrier_frequency), sample_rate(config.sample_rate), baud_rate(config.baud_rate) {
omega = 2.0 * M_PI * carrier_frequency / sample_rate;
baud_incr = static_cast<double>(baud_rate) / sample_rate;
phase = 0.0;
baud_frac = 0.0;
initializeSymbolMap();
}
std::vector<int16_t> modulate(const std::vector<uint8_t>& symbol_stream) {
std::vector<int16_t> modulated_signal;
modulated_signal.reserve(static_cast<size_t>(symbol_stream.size() / baud_incr));
for (uint8_t symbol : symbol_stream) {
double symbol_phase = symbolMap[symbol];
while (baud_frac < 1.0) {
double sample = std::sin(omega * phase + symbol_phase);
int16_t output_sample = static_cast<int16_t>(sample * 32767);
modulated_signal.push_back(output_sample);
phase = std::fmod(phase + omega, 2.0 * M_PI);
baud_frac += baud_incr;
}
baud_frac -= 1.0;
}
return modulated_signal;
}
private:
int carrier_frequency;
int sample_rate;
int baud_rate;
double phase;
double baud_frac;
double baud_incr;
double omega;
std::array<double, 8> symbolMap;
void initializeSymbolMap() {
double phase_increment = 2.0 * M_PI / 8.0;
for (int symbol = 0; symbol < 8; ++symbol) {
symbolMap[symbol] = symbol * phase_increment;
}
}
};
}
#endif /* PSK_MODULATOR_H */
#endif