Wrappers for sweep signal. Some cleanup and documentation

This commit is contained in:
Anne de Jong 2024-10-13 13:12:37 +02:00
parent cde2c74467
commit 45da6370ec
3 changed files with 27 additions and 7 deletions

View File

@ -61,6 +61,8 @@ fn lasprs(m: &Bound<'_, PyModule>) -> PyResult<()> {
// Signal generator // Signal generator
m.add_class::<siggen::Siggen>()?; m.add_class::<siggen::Siggen>()?;
m.add_class::<siggen::SiggenCommand>()?;
m.add_class::<siggen::SweepType>()?;
// SLM // SLM
m.add_class::<slm::TimeWeighting>()?; m.add_class::<slm::TimeWeighting>()?;

View File

@ -1,7 +1,7 @@
use super::siggenchannel::SiggenChannelConfig; use super::siggenchannel::SiggenChannelConfig;
use super::SiggenCommand;
use super::source::{self, *}; use super::source::{self, *};
use super::sweep::SweepType; use super::sweep::SweepType;
use super::SiggenCommand;
use crate::config::*; use crate::config::*;
use crate::filter::Filter; use crate::filter::Filter;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
@ -46,6 +46,21 @@ impl Siggen {
fn newSine_py(fs: Flt, freq: Flt, nchannels: usize) -> PyResult<Siggen> { fn newSine_py(fs: Flt, freq: Flt, nchannels: usize) -> PyResult<Siggen> {
Ok(Siggen::newSine(fs, nchannels, freq)?) Ok(Siggen::newSine(fs, nchannels, freq)?)
} }
#[pyo3(name = "newSweep")]
#[staticmethod]
fn newSweep_py(
fs: Flt,
nchannels: usize,
fl: Flt,
fu: Flt,
sweep_time: Flt,
quiet_time: Flt,
sweep_type: SweepType,
) -> Result<Self> {
Ok(Siggen::newSweep(
fs, nchannels, fl, fu, sweep_time, quiet_time, sweep_type,
)?)
}
} }
impl Siggen { impl Siggen {

View File

@ -6,6 +6,8 @@ use {
const NITER_NEWTON: usize = 20; const NITER_NEWTON: usize = 20;
const twopi: Flt = 2. * pi; const twopi: Flt = 2. * pi;
/// Enumerator representing the type of sweep source to create. Used as
/// parameter in [Siggen::newSweep].
#[cfg_attr(feature = "python-bindings", pyclass)] #[cfg_attr(feature = "python-bindings", pyclass)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum SweepType { pub enum SweepType {
@ -37,8 +39,8 @@ pub struct SweepParams {
impl SweepParams { impl SweepParams {
pub fn new( pub fn new(
fs: Flt, fs: Flt,
fl_: Flt, fl: Flt,
fu_: Flt, fu: Flt,
sweep_time: Flt, sweep_time: Flt,
quiet_time: Flt, quiet_time: Flt,
sweeptype: SweepType, sweeptype: SweepType,
@ -46,10 +48,10 @@ impl SweepParams {
if fs <= 0. { if fs <= 0. {
bail!("Invalid sampling frequency: {} Hz", fs); bail!("Invalid sampling frequency: {} Hz", fs);
} }
if fl_ > fu_ { if fl > fu {
bail!("Lower frequency should be smaller than upper frequency"); bail!("Lower frequency should be smaller than upper frequency");
} }
if fu_ >= fs / 2. { if fu >= fs / 2. {
bail!("Upper frequency should be smaller than sampling frequency"); bail!("Upper frequency should be smaller than sampling frequency");
} }
if sweep_time <= 0. { if sweep_time <= 0. {
@ -59,10 +61,11 @@ impl SweepParams {
bail!("Invalid sweep time: too short"); bail!("Invalid sweep time: too short");
} }
// For backward sweeps, we just reverse the start and stop frequency.
let (fl, fu) = if matches!(sweeptype, SweepType::BackwardLin | SweepType::BackwardLog) { let (fl, fu) = if matches!(sweeptype, SweepType::BackwardLin | SweepType::BackwardLog) {
(fu_, fl_) (fu, fl)
} else { } else {
(fl_, fu_) (fl, fu)
}; };
Ok(SweepParams { Ok(SweepParams {
fs, fs,