Better?? PSK Modulator??

This commit is contained in:
RecklessAndFeckless 2024-10-09 00:30:31 -04:00
parent d4cbd9daad
commit 445d9a6076
3 changed files with 52 additions and 68 deletions

View File

@ -51,7 +51,7 @@ public:
interleaver(baud_rate, interleave_setting, is_frequency_hopping),
input_data(std::move(_data)),
mgd_decoder(baud_rate, is_frequency_hopping),
modulator(PSKModulatorConfig(1500, 48000, baud_rate)) {}
modulator(48000) {}
/**
* @brief Transmits the input data by processing it through different phases like FEC encoding, interleaving, symbol formation, scrambling, and modulation.
@ -101,7 +101,7 @@ private:
Scrambler scrambler; ///< Scrambler instance for scrambling the data.
FECEncoder fec_encoder; ///< FEC encoder instance for encoding the data.
Interleaver interleaver; ///< Interleaver instance for interleaving the data.
milstd::PSKModulator modulator; ///< PSK modulator instance for modulating the data.
PSKModulator modulator; ///< PSK modulator instance for modulating the data.
MGDDecoder mgd_decoder; ///< MGD decoder
/**

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;
PSKModulatorConfig(int cf, int sr, int br) : carrier_frequency(cf), sample_rate(sr), baud_rate(br) {}
};
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;
PSKModulator(double sample_rate) : sample_rate(sample_rate), carrier_freq(1800), phase(0.0) {
initializeSymbolMap();
}
std::vector<int16_t> modulate(const std::vector<uint8_t>& symbol_stream) {
std::vector<int16_t> modulate(const std::vector<uint8_t>& symbols) {
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;
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];
baud_frac -= 1.0;
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:
int carrier_frequency;
int sample_rate;
int baud_rate;
double phase;
double baud_frac;
double baud_incr;
double omega;
std::array<double, 8> symbolMap;
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() {
double phase_increment = 2.0 * M_PI / 8.0;
for (int symbol = 0; symbol < 8; ++symbol) {
symbolMap[symbol] = symbol * phase_increment;
}
}
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
};
}
}
};
#endif /* PSK_MODULATOR_H */
#endif

View File

@ -19,12 +19,12 @@ int main() {
size_t baud_rate = 150;
bool is_voice = false; // False indicates data mode
bool is_frequency_hopping = false; // Fixed frequency operation
size_t interleave_setting = 1; // Short interleave
size_t interleave_setting = 2; // Short interleave
// Create ModemController instance
ModemController modem(baud_rate, is_voice, is_frequency_hopping, interleave_setting, bitstream);
const char* file_name = "modulated_signal_150bps_shortinterleave.wav";
const char* file_name = "modulated_signal_150bps_longinterleave.wav";
// Perform transmit operation to generate modulated signal
std::vector<int16_t> modulated_signal = modem.transmit();