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 |
#include <iostream> #include <random> // Function to generate a random noise value double generateNoise(double mean, double stddev) { static std::default_random_engine generator; std::normal_distribution<double> distribution(mean, stddev); return distribution(generator); } // Function to simulate signal transmission and reception double simulateCommunication(double signalStrength, double noiseMean, double noiseStddev) { // Generate noise double noise = generateNoise(noiseMean, noiseStddev); // Simulate received signal (signal + noise) double receivedSignal = signalStrength + noise; return receivedSignal; } int main() { double signalStrength = 100.0; // Signal strength in dB double noiseMean = 0.0; // Mean of the noise distribution double noiseStddev = 10.0; // Standard deviation of the noise distribution int numSimulations = 10; // Number of simulations to run std::cout << "Simulating wireless communication:\n"; for (int i = 0; i < numSimulations; ++i) { double receivedSignal = simulateCommunication(signalStrength, noiseMean, noiseStddev); std::cout << "Simulation " << (i + 1) << ": Received Signal = " << receivedSignal << " dB\n"; } return 0; } |
Explanation
- generateNoise Function:
- Generates a random noise value using a normal distribution.
- The
std::normal_distribution
class from the C++ Standard Library is used to generate noise values with a specified mean and standard deviation.
- simulateCommunication Function:
- Simulates the wireless communication by adding noise to the transmitted signal.
- The received signal is calculated by adding the generated noise to the signal strength.
Advertisement - Main Function:
- Initializes the signal strength, noise mean, and noise standard deviation.
- Runs multiple simulations (in this case, 10 simulations) to observe the received signal in the presence of noise.
- Prints the received signal strength for each simulation, showing how noise affects the signal reception.
Usage
- Signal Strength: The strength of the signal transmitted from the source, set to 100 dB in this example.
- Noise Parameters: The noise is modeled as a normal distribution with a mean (
noiseMean
) and a standard deviation (noiseStddev
), which affects the received signal. - Simulations: The program runs multiple simulations to provide a sense of how noise impacts signal reception.
Advertisement