.iq reading
This commit is contained in:
@ -1,12 +1,12 @@
|
||||
use num_complex::Complex;
|
||||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Read};
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::io::{BufReader, ErrorKind, Read};
|
||||
|
||||
pub type IqSample = Complex<f32>;
|
||||
|
||||
// Data chunk
|
||||
#[derive(Debug)]
|
||||
pub struct IqChunk {
|
||||
pub samples: Vec<IqSample>,
|
||||
}
|
||||
@ -21,8 +21,8 @@ pub struct FileSource {
|
||||
}
|
||||
|
||||
impl FileSource {
|
||||
pub fn new(file_path: &str, chunk_samples_size: usize) -> Self {
|
||||
let file = File::open(file_path).unwrap();
|
||||
pub fn new(file_path: &str, chunk_samples_size: usize) -> Result<Self, Box<dyn Error>> {
|
||||
let file = File::open(file_path)?;
|
||||
|
||||
// Init buffer with size 16 Mo
|
||||
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)
|
||||
let raw_reader = vec![0; chunk_samples_size * 2];
|
||||
|
||||
Self {
|
||||
Ok(Self {
|
||||
reader,
|
||||
raw_buffer: raw_reader,
|
||||
chunk_samples_size,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_chunk(&mut self) -> Option<IqChunk> {
|
||||
// TODO : match for EOF
|
||||
self.reader.read_exact(&mut self.raw_buffer);
|
||||
impl Iterator for FileSource {
|
||||
type Item = Result<IqChunk, Box<dyn Error>>;
|
||||
|
||||
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
|
||||
let mut samples = Vec::with_capacity(self.chunk_samples_size);
|
||||
|
||||
// Buffer read
|
||||
for iq in self.raw_buffer.chunks(2) {
|
||||
let i = (iq[0] as f32) / 128.0;
|
||||
let q = (iq[1] as f32) / 128.0;
|
||||
samples.push(Complex::new(i, q));
|
||||
let i = iq[0] as i8;
|
||||
let q = iq[1] as i8;
|
||||
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 }))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user