rfcalcs 0.1.1

Radio Frequency equations and calculations
Documentation
//! rust-rfcalcs type definitions
//!
//! Copyright 2017 Ryan Kurte
//!

use consts::*;

/// Frequency type (Hz) to assist with unit coherence
pub struct Frequency(pub f64);

pub fn khz(f: f64) -> Frequency {
    return Frequency(f * 1e3);
}

pub fn mhz(f: f64) -> Frequency {
    return Frequency(f * 1e6);
}

pub fn ghz(f: f64) -> Frequency {
    return Frequency(f * 1e9);
}

impl Frequency {
    pub fn to_wavelength(&self) -> Wavelength {
        Wavelength(C / self.0)
    }
}

/// Wavelength type (m) to assist with unit coherence
pub struct Wavelength(pub f64);

impl Wavelength {
    pub fn to_frequency(&self) -> Frequency {
        Frequency(C / self.0)
    }
}

/// Distance type (m) to assist with unit coherence
pub struct Distance(pub f64);

pub fn m(d: f64) -> Distance {
    return Distance(d);
}

pub fn km(d: f64) -> Distance {
    return Distance(d * 1e3);
}

/// Attenuation type (dB) to assist with unit coherence
pub struct Attenuation(pub f64);

pub fn db(f: f64) -> Attenuation {
    return Attenuation(f);
}

impl Attenuation {
    pub fn to_absolute(&self) -> f64 {
        (10_f64).powf(self.0 / 20_f64)
    }
}


pub fn dbmv_to_mw(dbmv: f64) -> f64 {
    (10_f64).powf(dbmv / 10_f64)
}


pub fn mw_to_dbmv(mw: f64) -> f64 {
    10_f64 * mw.log10()
}