Skip to content

Commit

Permalink
Added FFT Size MessageIO to FFT Block
Browse files Browse the repository at this point in the history
  • Loading branch information
K4HVH authored and bastibl committed Jul 5, 2024
1 parent d8f3e4d commit d5bd64f
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/blocks/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::runtime::BlockMetaBuilder;
use crate::runtime::Kernel;
use crate::runtime::MessageIo;
use crate::runtime::MessageIoBuilder;
use crate::runtime::Pmt;
use crate::runtime::StreamIo;
use crate::runtime::StreamIoBuilder;
use crate::runtime::WorkIo;
Expand All @@ -26,6 +27,10 @@ use crate::runtime::WorkIo;
///
/// `out`: FFT results (Complex32)
///
/// # Messages
///
/// `fft_size`: Change the FFT size (Pmt::Usize)
///
/// # Usage
/// ```
/// use futuresdr::blocks::Fft;
Expand Down Expand Up @@ -80,7 +85,9 @@ impl Fft {
.add_input::<Complex32>("in")
.add_output::<Complex32>("out")
.build(),
MessageIoBuilder::<Fft>::new().build(),
MessageIoBuilder::<Fft>::new()
.add_input("fft_size", Self::fft_size_handler)
.build(),
Fft {
len,
plan,
Expand All @@ -91,6 +98,37 @@ impl Fft {
},
)
}

/// Handle incoming messages to change FFT size
#[message_handler]
fn fft_size_handler(
&mut self,
_io: &mut WorkIo,
_mio: &mut MessageIo<Self>,
_meta: &mut BlockMeta,
p: Pmt,
) -> Result<Pmt> {
match p {
Pmt::Usize(new_len) => self.set_fft_size(new_len),
Pmt::Null => Ok(Pmt::Usize(self.len)),
_ => Ok(Pmt::InvalidValue),
}
}

/// Set a new FFT size
fn set_fft_size(&mut self, new_len: usize) -> Result<Pmt> {
let mut planner = FftPlanner::<f32>::new();
let new_plan = match self.direction {
FftDirection::Forward => planner.plan_fft_forward(new_len),
FftDirection::Inverse => planner.plan_fft_inverse(new_len),
};

self.len = new_len;
self.plan = new_plan;
self.scratch = vec![Complex32::new(0.0, 0.0); new_len * 10].into_boxed_slice();

Ok(Pmt::Ok)
}
}

#[doc(hidden)]
Expand Down

0 comments on commit d5bd64f

Please sign in to comment.