Skip to content

Commit

Permalink
Create receiver struct (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
iancleary authored Jan 17, 2024
1 parent 2b24204 commit dd4f8ca
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/conversions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod angle;
pub mod frequency;
pub mod noise;
pub mod power;
2 changes: 1 addition & 1 deletion src/conversions/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn noise_figure_from_noise_factor(noise_factor: f64) -> f64 {
10.0_f64 * noise_factor.log10()
}

pub fn noise_power_from_bandwidth(temperature: f64,bandwidth: f64) -> f64 {
pub fn noise_power_from_bandwidth(temperature: f64, bandwidth: f64) -> f64 {
1.38e-23 * temperature * bandwidth
}

Expand Down
50 changes: 50 additions & 0 deletions src/conversions/power.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pub fn watts_to_dbm(watts: f64) -> f64 {
10.0 * (watts.log10() + 3.0)
}

pub fn dbm_to_watts(dbm: f64) -> f64 {
10.0_f64.powf((dbm - 30.0) / 10.0)
}

#[cfg(test)]
mod tests {

#[test]
fn watts_to_dbm() {
let watts: f64 = 1.0;

let dbm: f64 = super::watts_to_dbm(watts);

assert_eq!(30.0, dbm);
}

#[test]
fn another_watts_to_dbm() {
let watts: f64 = 20.0;

let dbm: f64 = super::watts_to_dbm(watts);

// not worrying about floating point precision here
assert_eq!(43.01029995663981, dbm);
}

#[test]
fn dbm_to_watts() {
// not worrying about floating point precision here
let dbm: f64 = 43.0102999566398087;

let watts: f64 = super::dbm_to_watts(dbm);

// not worrying about floating point precision here
assert_eq!(19.99999999999997, watts);
}

#[test]
fn another_dbm_to_watts() {
let dbm: f64 = 30.0;

let watts: f64 = super::dbm_to_watts(dbm);

assert_eq!(1.0, watts);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pub mod constants;
pub mod conversions;
pub mod fspl;
pub mod orbits;
pub mod receiver;
pub mod utils;
102 changes: 102 additions & 0 deletions src/receiver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use crate::utils::print::{print_row, print_separator};

pub struct Receiver {
pub gain: f64, // dB
pub temperature: f64, // K
pub noise_figure: f64, // dB
pub bandwidth: f64, // Hz
}

impl Receiver {
pub fn calculate_noise_floor(&self) -> f64 {
let receiver_noise_floor_power =
crate::conversions::noise::noise_power_from_bandwidth(self.temperature, self.bandwidth);

crate::conversions::power::watts_to_dbm(receiver_noise_floor_power)
}

pub fn calculate_noise_power(&self) -> f64 {
self.calculate_noise_floor() + self.noise_figure
}

pub fn calculate_snr(&self, input_power: f64) -> f64 {
let receiver_noise_floor_dbm = self.calculate_noise_floor();

let receiver_total_noise_power = receiver_noise_floor_dbm + self.noise_figure;

// Assumes receiver input power is spread across the bandwidth
// returns value in dB
input_power - receiver_total_noise_power
}

pub fn print(&self, input_power: f64) {
print_row("Gain", &self.gain.to_string(), "dB");
print_row("Temperature", &self.temperature.to_string(), "K");
print_row("Noise Figure", &self.noise_figure.to_string(), "dB");
print_row("Bandwidth", &self.bandwidth.to_string(), "Hz");
print_separator();
print_row(
"Noise Floor",
&self.calculate_noise_floor().to_string(),
"dBm",
);
print_row(
"Noise Power",
&self.calculate_noise_power().to_string(),
"dBm",
);
print_row("SNR", &self.calculate_snr(input_power).to_string(), "dB");
}
}

#[cfg(test)]
mod tests {
use crate::receiver::Receiver;

#[test]
fn calculate_noise_floor() {
let receiver = Receiver {
gain: 10.0, // not used
temperature: 290.0,
noise_figure: 3.0, // not used
bandwidth: 100.0e6,
};

let noise_floor: f64 = receiver.calculate_noise_floor();

assert_eq!(-93.97722915699808, noise_floor);
}

#[test]
fn calculate_noise_power() {
let receiver = Receiver {
gain: 10.0, // not used
temperature: 290.0,
noise_figure: 3.0,
bandwidth: 100.0e6,
};

let noise_power: f64 = receiver.calculate_noise_power();

// noise floor + noise figure
assert_eq!(-90.97722915699808, noise_power);
}

#[test]
fn calculate_snr() {
let receiver = Receiver {
gain: 10.0, // not used
temperature: 290.0,
noise_figure: 3.0,
bandwidth: 100.0e6,
};

let input_power: f64 = -70.0; // dBm

// Assumes receiver input power is spread across the bandwidth
// returns value in dB
let snr: f64 = receiver.calculate_snr(input_power);

assert_eq!(20.977229156998078, snr);
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod print;
23 changes: 23 additions & 0 deletions src/utils/print.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[allow(dead_code)]
pub fn print_row(msg1: &str, msg2: &str, msg3: &str) {
println!("{0: <20} | {1: <20} | {2: <5}", msg1, msg2, msg3);
}

#[allow(dead_code)]
pub fn print_header() {
print_row("--------------", "-----", "----");
}

#[allow(dead_code)]
pub fn print_separator() {
// println!("");
println!("--------------------------------------------------");
println!("");
}

#[allow(dead_code)]
pub fn print_title(title: &str) {
println!(" {}", title);
println!("--------------------------------------------------");
println!("");
}

0 comments on commit dd4f8ca

Please sign in to comment.