now it has command line arguments

This commit is contained in:
Ellie D 2019-06-16 21:21:47 -05:00
parent 482a4a3c8a
commit 01a17e4331
3 changed files with 39 additions and 18 deletions

7
Cargo.lock generated
View File

@ -3,6 +3,11 @@ name = "autocfg"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lapp"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num-complex"
version = "0.2.3"
@ -50,6 +55,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "termfall"
version = "0.1.0"
dependencies = [
"lapp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustfft 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -60,6 +66,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf"
"checksum lapp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60bf485afeba9437a275ad29a9383b03f2978450e7feceffb55be8c0dbad9829"
"checksum num-complex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fcb0cf31fb3ff77e6d2a6ebd6800df7fdcd106f2ad89113c9130bcd07f93dffc"
"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09"
"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"

View File

@ -6,3 +6,4 @@ edition = "2018"
[dependencies]
rustfft = "3.0.0"
lapp = "0.4.0"

View File

@ -1,3 +1,5 @@
extern crate lapp;
extern crate rustfft;
use rustfft::FFTplanner;
use rustfft::num_complex::Complex;
@ -5,25 +7,34 @@ use rustfft::num_traits::Zero;
use std::io::prelude::*;
use std::io::ErrorKind;
use std::io::stdin;
static FFT_SIZE:usize = 1024;
static FFT_WINDOW_SIZE:usize = 512;
static WIDTH:usize = 120;
static SCALE_MAX:f32 = 16384.0;
static DECIMATION:usize = 1;
fn main() {
let mut ibuffer:Vec<u8> = vec![0x00; FFT_SIZE];
let mut source = stdin();
let args = lapp::parse_args("
termfall terminal waterfall plotter v0.1
-i, --input (default stdin) file from which to read samples
-s, --size (default 1024) FFT size to use
-d, --decimation (default 1) decimation factor
-a, --autoscale whether or not to automatically vary sensitivity with input
-m, --max (default 10000.0) scaling factor for plot sensitivity, smaller is more sensitive
-w, --width (default 120) width of the plot in characters
");
let mut source = args.get_infile("input");
let fft_size = args.get_integer("size") as usize;
let fft_window_size = fft_size/2;
let decimation = args.get_integer("decimation") as usize;
let autoscale = args.get_bool("autoscale");
let startmax = args.get_float("max");
let width = args.get_integer("width") as usize;
let mut fft_i:Vec<Complex<f32>> = vec![Zero::zero(); FFT_SIZE];
let mut fft_o:Vec<Complex<f32>> = vec![Zero::zero(); FFT_SIZE];
let mut ibuffer:Vec<u8> = vec![0x00; fft_size];
let mut fft_i:Vec<Complex<f32>> = vec![Zero::zero(); fft_size];
let mut fft_o:Vec<Complex<f32>> = vec![Zero::zero(); fft_size];
let mut planner = FFTplanner::new(false);
let fft = planner.plan_fft(FFT_SIZE);
let fft = planner.plan_fft(fft_size);
let mut cyclecount:usize = 0;
let mut scalemax:f32 = SCALE_MAX;
let mut scalemax:f32 = startmax;
loop {
match source.read_exact(&mut ibuffer) {
Err(why) => match why.kind() {
@ -36,16 +47,16 @@ fn main() {
Ok(_) => (),
};
cyclecount = cyclecount.wrapping_add(1);
if cyclecount % DECIMATION != 0 {
if cyclecount % decimation != 0 {
continue;
}
for i in 0..FFT_SIZE {
for i in 0..fft_size {
fft_i[i] = Complex::new(ibuffer[i] as f32, 0.0f32);
}
fft.process(&mut fft_i, &mut fft_o);
let fft_window = fft_o[..FFT_WINDOW_SIZE].to_vec();
let fft_window = fft_o[..fft_window_size].to_vec();
let mut linemax:f32 = 0.0;
let mut chunks = fft_window.chunks(FFT_WINDOW_SIZE/WIDTH);
let mut chunks = fft_window.chunks(fft_window_size/width);
let _ = chunks.next(); // toss DC
for chunk in chunks {
let mut power:f32 = 0.0;
@ -64,6 +75,8 @@ fn main() {
//print!("{:10}", power.round());
}
println!("\x1b[0m");
scalemax = scalemax*0.99 + linemax*0.01;
if autoscale {
scalemax = scalemax*0.99 + linemax*0.01;
}
}
}