.iq reading

This commit is contained in:
2026-06-11 12:17:43 +02:00
parent f3f99a412e
commit bfea615351

View File

@ -1,12 +1,12 @@
use num_complex::Complex; use num_complex::Complex;
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, Read}; use std::io::{BufReader, ErrorKind, Read};
use std::os::unix::process::CommandExt;
pub type IqSample = Complex<f32>; pub type IqSample = Complex<f32>;
// Data chunk // Data chunk
#[derive(Debug)]
pub struct IqChunk { pub struct IqChunk {
pub samples: Vec<IqSample>, pub samples: Vec<IqSample>,
} }
@ -21,8 +21,8 @@ pub struct FileSource {
} }
impl FileSource { impl FileSource {
pub fn new(file_path: &str, chunk_samples_size: usize) -> Self { pub fn new(file_path: &str, chunk_samples_size: usize) -> Result<Self, Box<dyn Error>> {
let file = File::open(file_path).unwrap(); let file = File::open(file_path)?;
// Init buffer with size 16 Mo // Init buffer with size 16 Mo
let reader = BufReader::with_capacity(16 * 1024 * 1024, file); let reader = BufReader::with_capacity(16 * 1024 * 1024, file);
@ -30,27 +30,44 @@ impl FileSource {
// 1 sample = 2 bytes (1 byte : I, 1 byte : Q) // 1 sample = 2 bytes (1 byte : I, 1 byte : Q)
let raw_reader = vec![0; chunk_samples_size * 2]; let raw_reader = vec![0; chunk_samples_size * 2];
Self { Ok(Self {
reader, reader,
raw_buffer: raw_reader, raw_buffer: raw_reader,
chunk_samples_size, chunk_samples_size,
} })
} }
}
pub fn read_chunk(&mut self) -> Option<IqChunk> { impl Iterator for FileSource {
// TODO : match for EOF type Item = Result<IqChunk, Box<dyn Error>>;
self.reader.read_exact(&mut self.raw_buffer);
fn next(&mut self) -> Option<Self::Item> {
// Buffer read
match self.reader.read_exact(&mut self.raw_buffer) {
Ok(_) => {}
// EOF
Err(e) if e.kind() == ErrorKind::UnexpectedEof => {
return None;
}
Err(e) => {
return Some(Err(e.into()));
}
}
// Output samples // Output samples
let mut samples = Vec::with_capacity(self.chunk_samples_size); let mut samples = Vec::with_capacity(self.chunk_samples_size);
// Buffer read // Buffer read
for iq in self.raw_buffer.chunks(2) { for iq in self.raw_buffer.chunks(2) {
let i = (iq[0] as f32) / 128.0; let i = iq[0] as i8;
let q = (iq[1] as f32) / 128.0; let q = iq[1] as i8;
samples.push(Complex::new(i, q)); let i_f32 = (i as f32) / 128.0;
let q_f32 = (q as f32) / 128.0;
samples.push(Complex::new(i_f32, q_f32));
} }
Some(IqChunk { samples }) Some(Ok(IqChunk { samples }))
} }
} }