#[derive(Clone, Copy)]
pub enum FrequencyScale {
Linear,
Log,
}
pub struct FreqScaler;
impl FreqScaler {
pub fn create(
freq_scale: FrequencyScale,
f_max_orig: usize,
f_max_new: usize,
) -> Box<dyn FreqScalerTrait> {
match freq_scale {
FrequencyScale::Linear => {
Box::new(LinearFreq::init(f_max_orig as f32, f_max_new as f32))
}
FrequencyScale::Log => Box::new(LogFreq::init(f_max_orig as f32, f_max_new as f32)),
}
}
}
pub trait FreqScalerTrait {
fn init(f_max_orig: f32, height: f32) -> Self
where
Self: Sized;
fn scale(&self, y: usize) -> (f32, f32);
}
pub struct LinearFreq {
ratio: f32,
}
impl FreqScalerTrait for LinearFreq {
fn init(f_max_orig: f32, f_max_new: f32) -> Self {
Self {
ratio: f_max_orig / f_max_new,
}
}
fn scale(&self, y: usize) -> (f32, f32) {
let f1 = self.ratio * y as f32;
let f2 = self.ratio * ((y + 1) as f32);
(f1, f2)
}
}
pub struct LogFreq {
log_coef: f32,
}
impl FreqScalerTrait for LogFreq {
fn init(f_max_orig: f32, f_max_new: f32) -> Self {
Self {
log_coef: f_max_orig / f_max_new.ln(),
}
}
fn scale(&self, y: usize) -> (f32, f32) {
let f1 = self.log_coef * (y as f32).ln();
let f2 = self.log_coef * ((y + 1) as f32).ln();
(f1, f2)
}
}