Here is one possible implementation of the wideband spectrum generator class:
#include <vector>
#include <cmath>
#include <cstdlib>
struct Signal {
double power; // dbm
double frequency; // Hz
double bandwidth; // Hz
};
class SpectrumGenerator {
public:
SpectrumGenerator(double startFreq, double endFreq, double sampleRate, double noiseFloorDbm, double noiseStdDev,
const std::vector<Signal>& signals)
: m_startFreq(startFreq), m_endFreq(endFreq), m_sampleRate(sampleRate),
m_noiseFloorDbm(noiseFloorDbm), m_noiseStdDev(noiseStdDev), m_signals(signals) {}
std::vector<double> generate() const {
std::vector<double> spectrum(m_endFreq - m_startFreq + 1, m_noiseFloorDbm);
// generate noise floor
for (double freq = m_startFreq; freq <= m_endFreq; freq += m_sampleRate) {
int index = static_cast<int>(freq - m_startFreq);
spectrum[index] += m_noiseStdDev * randNormal();
}
// generate signals
for (const Signal& signal : m_signals) {
double powerDbm = signal.power;
double powerW = dbmToWatt(powerDbm);
double freq = signal.frequency;
double bandwidth = signal.bandwidth;
double sigma = std::sqrt(bandwidth / (2 * M_PI));
// calculate complex spectrum
std::vector<std::complex<double>> complexSpectrum(m_endFreq - m_startFreq + 1);
for (double f = m_startFreq; f <= m_endFreq; f += m_sampleRate) {
std::complex<double> value(powerW * std::exp(-std::pow(f - freq, 2) / (2 * std::pow(sigma, 2))), 0);
complexSpectrum[static_cast<int>(f - m_startFreq)] = value;
}
// calculate power spectrum
std::vector<double> powerSpectrum(m_endFreq - m_startFreq + 1);
for (int i = 0; i < complexSpectrum.size(); ++i) {
double mag = std::abs(complexSpectrum[i]);
powerSpectrum[i] = wattToDbm(mag * mag);
}
// add signal spectrum to total spectrum
for (int i = 0; i < spectrum.size(); ++i) {
double freq = m_startFreq + i * m_sampleRate;
double deltaFreq = std::abs(freq - signal.frequency);
if (deltaFreq < signal.bandwidth / 2) {
spectrum[i] += powerSpectrum[i] - m_noiseFloorDbm;
}
}
}
return spectrum;
}
private:
double m_startFreq;
double m_endFreq;
double m_sampleRate;
double m_noiseFloorDbm;
double m_noiseStdDev;
std::vector<Signal> m_signals;
double randNormal() const {
// generate normal random number with mean 0 and std deviation 1
double u1 = static_cast<double>(rand()) / RAND_MAX;
double u2 = static_cast<double>(rand()) / RAND_MAX;
double z = std::sqrt(-2 * std::log(u1)) * std::cos(2 * M_PI * u2);
return z;
}
double dbmToWatt(double dbm) const {
return std::pow(10, dbm / 10);
}
double wattToDbm(double watt) const {
return 10 * std::log10(watt);
}
};
In this implementation, the SpectrumGenerator class takes the frequency range, sample rate, noise floor, noise standard deviation, and a list of signals as constructor arguments. The generate() method generates the total spectrum and returns it as a vector with one element per frequency bin.
The noise floor is generated by adding a normal (Gaussian) random number to each frequency bin with the given standard deviation. The signals are generated by calculating their complex spectrum (which has a Gaussian shape centered at the signal frequency) and then converting it to a power spectrum (which is the magnitude squared of the complex spectrum). The power spectrum is added to the total spectrum for all frequency bins that fall within the signal bandwidth.
Note that this implementation assumes that the signals are narrowband, i.e., their bandwidth is much smaller than the sample rate. If the signals are wideband, the Gaussian shape of the complex spectrum will overlap with neighboring frequency bins, which may cause artifacts in the total spectrum. To handle wideband signals, a more sophisticated method such as FFT-based filtering could be used.