use std::fs::File; use std::io::{BufWriter, Result, Write}; use microspectrogram::{Spectrogram, Window}; fn main() -> Result<()> { let spec = Spectrogram::<16>::new(1, Window::Hann); let mut transformed = [0.0; 354]; SPECTRUM.iter().enumerate().for_each(|(i, x)| { transformed[i] = 1.0 / x.exp(); }); let mut out = spec.compute(&transformed); let mut min = f32::MAX; let mut max = f32::MIN; for row in out.iter_mut() { for v in row.iter_mut() { let new_v = v.ln(); if new_v < min { min = new_v; }; if new_v > max { max = new_v; }; *v = new_v; } } let height = out.len(); let width = out[0].len(); let mut f = BufWriter::new(File::create("microspectrogram.ppm")?); writeln!(f, "P3")?; writeln!(f, "{} {}", width, height)?; writeln!(f, "255")?; for row in out.iter().rev() { for v in row.iter() { let idx = (49.0 * (v - min) / (max - min)) as usize; let col = VIRIDIS[idx]; write!(f, "{} {} {} ", col[0], col[1], col[2])?; } writeln!(f)?; } Ok(()) } #[allow(clippy::excessive_precision)] const SPECTRUM: [f32; 354] = [ 6.56001728e-01, 6.39518880e-01, 6.23481755e-01, 6.07988413e-01, 5.90667605e-01, 5.75688330e-01, 5.60634769e-01, 5.45569321e-01, 5.30432557e-01, 5.13245466e-01, 4.98453395e-01, 4.84112991e-01, 4.68610734e-01, 4.55687729e-01, 4.42830098e-01, 4.28299518e-01, 4.15655833e-01, 4.02875460e-01, 3.88309222e-01, 3.75540735e-01, 3.62296810e-01, 3.46134883e-01, 3.28020687e-01, 3.09680659e-01, 2.87932333e-01, 2.58523509e-01, 2.23614459e-01, 1.83249756e-01, 1.44596631e-01, 1.04214099e-01, 5.87503213e-02, 1.74852572e-02, -1.60063923e-02, -3.87115377e-02, -4.91682066e-02, -4.95485571e-02, -4.36768954e-02, -3.36570354e-02, -2.51258912e-02, -2.35658596e-02, -3.17225962e-02, -4.64760378e-02, -6.01419145e-02, -6.42960557e-02, -5.38304724e-02, -2.91374010e-02, 4.89803095e-03, 4.20980984e-02, 7.81927721e-02, 1.12049915e-01, 1.44453999e-01, 1.76332131e-01, 2.07898255e-01, 2.39089973e-01, 2.73785075e-01, 3.07446099e-01, 3.35098774e-01, 3.58971714e-01, 3.77112653e-01, 3.87711953e-01, 3.89908478e-01, 3.84101594e-01, 3.74179793e-01, 3.61984804e-01, 3.48764650e-01, 3.33547656e-01, 3.18277176e-01, 3.04718273e-01, 2.89353299e-01, 2.73883134e-01, 2.60342060e-01, 2.47318025e-01, 2.32963951e-01, 2.18363839e-01, 2.03460634e-01, 1.88491462e-01, 1.75618973e-01, 1.61220327e-01, 1.46058603e-01, 1.30297827e-01, 1.13681262e-01, 9.54886194e-02, 7.65346814e-02, 5.91782157e-02, 3.92704297e-02, 1.93127228e-02, -6.44984137e-04, -2.30523864e-02, -4.31509419e-02, -6.29535371e-02, -8.27133429e-02, -1.02269899e-01, -1.23493460e-01, -1.42823589e-01, -1.62339138e-01, -1.81735233e-01, -2.01641237e-01, -2.24369560e-01, -2.46883935e-01, -2.66556379e-01, -2.85209009e-01, -3.05091834e-01, -3.22494655e-01, -3.39416095e-01, -3.57901132e-01, -3.76685695e-01, -3.95741258e-01, -4.14076532e-01, -4.29236473e-01, -4.44321533e-01, -4.61266150e-01, -4.77554663e-01, -4.92994518e-01, -5.07885243e-01, -5.22269625e-01, -5.36768113e-01, -5.51765811e-01, -5.66613746e-01, -5.80670076e-01, -5.93421329e-01, -6.05766082e-01, -6.18167887e-01, -6.30305824e-01, -6.42693366e-01, -6.55865382e-01, -6.69265607e-01, -6.83036675e-01, -6.98509811e-01, -7.13281676e-01, -7.26760349e-01, -7.40060732e-01, -7.52512458e-01, -7.65974491e-01, -7.80054593e-01, -7.93162424e-01, -8.07771452e-01, -8.21774295e-01, -8.33391627e-01, -8.45586617e-01, -8.57617581e-01, -8.68678650e-01, -8.80341149e-01, -8.91349921e-01, -9.03114639e-01, -9.14784270e-01, -9.26149620e-01, -9.36906409e-01, -9.46914383e-01, -9.56853419e-01, -9.67387940e-01, -9.78019927e-01, -9.88016015e-01, -9.96939990e-01, -1.00564883e+00, -1.01524080e+00, -1.02326976e+00, -1.02991401e+00, -1.03665334e+00, -1.04517557e+00, -1.05404249e+00, -1.06444271e+00, -1.07627517e+00, -1.08589685e+00, -1.09271939e+00, -1.09677488e+00, -1.10051658e+00, -1.10632287e+00, -1.11329398e+00, -1.12081779e+00, -1.12928059e+00, -1.13766019e+00, -1.14562378e+00, -1.15227397e+00, -1.15729579e+00, -1.16048122e+00, -1.16172330e+00, -1.16234137e+00, -1.16307236e+00, -1.16232524e+00, -1.15975024e+00, -1.15537026e+00, -1.14972443e+00, -1.14540983e+00, -1.14408370e+00, -1.14552275e+00, -1.14664597e+00, -1.14645240e+00, -1.14302670e+00, -1.13752350e+00, -1.13073747e+00, -1.12381305e+00, -1.11727408e+00, -1.11358519e+00, -1.11275911e+00, -1.11383055e+00, -1.11642848e+00, -1.11905867e+00, -1.12217449e+00, -1.12515022e+00, -1.12857762e+00, -1.13421666e+00, -1.14057225e+00, -1.14832698e+00, -1.16091421e+00, -1.17661386e+00, -1.19074286e+00, -1.20580746e+00, -1.22706974e+00, -1.25804114e+00, -1.29459045e+00, -1.32349370e+00, -1.34450127e+00, -1.36700139e+00, -1.39980662e+00, -1.43988606e+00, -1.46135210e+00, -1.44977348e+00, -1.42665190e+00, -1.40722346e+00, -1.38645530e+00, -1.36301789e+00, -1.33227190e+00, -1.31765090e+00, -1.34799065e+00, -1.36495361e+00, -1.26456653e+00, -1.06616617e+00, -8.75579975e-01, -6.88228460e-01, -4.77123712e-01, -2.14553841e-01, 7.43862950e-02, 3.76845219e-01, 7.04257007e-01, 1.03222786e+00, 1.32115017e+00, 1.51039473e+00, 1.63272413e+00, 1.71992544e+00, 1.76785407e+00, 1.77145106e+00, 1.73881074e+00, 1.68161106e+00, 1.61670929e+00, 1.54394051e+00, 1.45851763e+00, 1.36534363e+00, 1.27270896e+00, 1.18273079e+00, 1.09254314e+00, 1.00374168e+00, 9.15403780e-01, 8.28175728e-01, 7.42458678e-01, 6.53394742e-01, 5.66901638e-01, 4.89457213e-01, 4.11959301e-01, 3.40153276e-01, 2.79049067e-01, 2.21609695e-01, 1.70100919e-01, 1.24885756e-01, 8.66213999e-02, 5.37879351e-02, 2.56934996e-02, 7.56345634e-03, -6.03407611e-03, -1.83400015e-02, -2.89185013e-02, -3.88809126e-02, -5.04954715e-02, -6.49329448e-02, -8.07174930e-02, -9.54719250e-02, -1.08035379e-01, -1.18962534e-01, -1.25701870e-01, -1.27460992e-01, -1.24128962e-01, -1.18205399e-01, -1.11870581e-01, -1.09239823e-01, -1.10667326e-01, -1.15408633e-01, -1.23938787e-01, -1.32568387e-01, -1.34473705e-01, -1.29066784e-01, -1.17964114e-01, -1.07746947e-01, -1.05486238e-01, -1.03316655e-01, -9.31700118e-02, -6.95038869e-02, -3.64633093e-02, -5.61925628e-03, 2.44902448e-02, 6.08969247e-02, 1.00252510e-01, 1.33852916e-01, 1.55739714e-01, 1.66094758e-01, 1.52509111e-01, 1.16951088e-01, 8.85878501e-02, 8.49695493e-02, 1.02367994e-01, 1.39401280e-01, 1.97237364e-01, 2.70718499e-01, 3.44355231e-01, 4.05013149e-01, 4.60421581e-01, 5.16219656e-01, 5.79758730e-01, 6.49893536e-01, 7.29145734e-01, 8.21781625e-01, 9.34535035e-01, 1.04037406e+00, 1.12590431e+00, 1.18668584e+00, 1.20499994e+00, 1.22049490e+00, 1.25609982e+00, 1.30005408e+00, 1.35125229e+00, 1.41058752e+00, 1.45186582e+00, 1.45545997e+00, 1.45960520e+00, 1.48264504e+00, 1.52707285e+00, 1.59648386e+00, 1.67557300e+00, 1.74437782e+00, 1.80493676e+00, 1.87128713e+00, 1.95064965e+00, 2.04829872e+00, 2.16489994e+00, 2.29402598e+00, 2.41544695e+00, 2.55541116e+00, 2.70791276e+00, 2.85826712e+00, 3.02412282e+00, 3.16961971e+00, 3.29862255e+00, 3.43391946e+00, 3.53807608e+00, 3.61787838e+00, 3.66924922e+00, ]; const VIRIDIS: [[u8; 3]; 50] = [ [68, 1, 84], [69, 8, 91], [71, 15, 98], [71, 22, 105], [72, 29, 111], [71, 37, 117], [71, 43, 122], [70, 49, 126], [68, 55, 129], [66, 62, 133], [64, 68, 135], [61, 74, 137], [59, 80, 138], [57, 85, 139], [54, 91, 140], [51, 96, 141], [49, 101, 141], [47, 106, 141], [44, 112, 142], [42, 117, 142], [40, 122, 142], [39, 126, 142], [37, 131, 141], [35, 137, 141], [33, 141, 140], [31, 146, 140], [30, 151, 138], [30, 156, 137], [31, 161, 135], [33, 166, 133], [36, 170, 130], [41, 175, 127], [48, 180, 122], [56, 185, 118], [64, 189, 114], [73, 193, 109], [85, 198, 102], [96, 201, 96], [107, 205, 89], [119, 208, 82], [131, 211, 75], [146, 215, 65], [159, 217, 56], [173, 220, 48], [186, 222, 39], [202, 224, 30], [215, 226, 25], [228, 227, 24], [241, 229, 28], [253, 231, 36], ];