]> OzVa Git service - rust_fft/commitdiff
Troubleshooting
authorwill <greenwoodw50@gmail.com>
Wed, 16 Oct 2024 15:14:31 +0000 (16:14 +0100)
committerwill <greenwoodw50@gmail.com>
Wed, 16 Oct 2024 15:14:31 +0000 (16:14 +0100)
src/main.rs

index 43bd41f23bad09201ffe7a345e90871cb432df1f..6c1f379f68c992d9f3dda9d003029c98f0bfbca1 100644 (file)
@@ -41,17 +41,24 @@ impl ImageArray {
                for i in 0..self.chunks {
                        let (r, theta): (f32, f32) = buffer[i].to_polar();
 
+                       if i == 100 {println!("theta: {theta}");}
+
                        let amplitude = 20f32 * r.log10();
                        let amplitude = ((amplitude - VOLUME_MIN) / (VOLUME_REL / AMPLITUDE_REL)) + AMPLITUDE_MIN;
 
                        let hue = (180f32 / 255f32) * amplitude;
 
-                       let angle = (theta.to_degrees() + 180f32) / (360f32 / ANGLE_REL) + ANGLE_MIN;
+                       let angle = (theta.to_degrees() + 180f32) * (ANGLE_REL / 360f32) + ANGLE_MIN;
 
                        let d = hue * (1f32 / 30f32);
                        let s = angle / 255f32;
                        let v = amplitude / 255f32;
 
+                       let s1 = s * 255f32;
+                       let v1 = v * 255f32;
+
+                       if i == 100 {println!("h: {hue} s:{s1} v:{v1}");}
+
                        let c = s * v;
                        let m = v - c;
                        let x = c * (1f32 - (d.rem_euclid(2f32) - 1f32).abs());
@@ -79,33 +86,66 @@ impl ImageArray {
 
                        let alpha = r - (g / 2f32) - (b / 2f32);
                        let beta = SQRT3_2 * (g - b);
-                       let c = beta.hypot(alpha);
 
                        // let h = beta.atan2(alpha).to_degrees() + 180f32;
                        let v = r.max(g).max(b);
-                       let s = if v == 0f32 { 0f32 } else { c * (255f32) / v };
+                       let c = (v - r.min(g).min(b)) * 255f32;
+                       let s = if v == 0f32 { 0f32 } else { c / v };
 
-                       let amplitude = (v - AMPLITUDE_MIN) / (AMPLITUDE_REL / VOLUME_REL) + VOLUME_MIN;
+                       let amplitude = (v - AMPLITUDE_MIN) * (VOLUME_REL / AMPLITUDE_REL) + VOLUME_MIN;
 
                        let amplitude = 10f32.powf(amplitude / 20f32);
 
-                       let angle = (s - ANGLE_MIN) * (ANGLE_REL / 360f32) - 180f32;
+                       if i == 100 {println!("alpha: {alpha}, beta: {beta} chroma: {c}");}
+                       if i == 100 {println!("s: {s}, v: {v}");}
+
+                       let angle = (s - ANGLE_MIN) / (ANGLE_REL / 360f32) - 180f32;
                        let angle = angle.to_radians();
 
+                       if i == 100 {println!("theta: {angle}");}
+
                        buffer[i] = Complex::from_polar(amplitude, angle);
                }
        }
 }
 
-fn process <'a, T: Fft<f32>> (transform: T, buffer: &'a mut [Complex<f32>], scratch: &'a mut [Complex<f32>]) -> () {
-       match transform.fft_direction() {
-               FftDirection::Forward => { // process the data if the forward fft is to be performed
-                       transform.process_with_scratch(buffer, scratch);
-               }
-               FftDirection::Inverse => { // process the data if the inverse fft is to be performed
-                       transform.process_with_scratch(buffer, scratch);
-               }
+fn _test () -> Result<(), Box<dyn std::error::Error>>  {
+       let forward_transform = Radix4::<f32>::new(WINDOW_SIZE, FftDirection::Forward);
+       let inverse_transform = Radix4::<f32>::new(WINDOW_SIZE, FftDirection::Inverse);
+       let scratch_space = forward_transform.get_inplace_scratch_len();
+
+       let mut buffer = vec![Complex{re: 0f32, im: 0f32}; SPECTOGRAM_AREA];
+       let mut origional = vec![Complex{re: 0f32, im: 0f32}; SPECTOGRAM_AREA];
+       let mut scratch = vec![Complex{re: 0f32, im: 0f32}; scratch_space];
+
+       for i in 0..SPECTOGRAM_AREA {
+               let rand1 = rand::random::<f32>();
+               let rand2 = rand::random::<f32>();
+               buffer[i] = Complex{re: rand1, im: rand2};
+               origional[i] = Complex{re: rand1, im: rand2};
+       }
+
+       forward_transform.process_with_scratch(&mut buffer, &mut scratch);
+       inverse_transform.process_with_scratch(&mut buffer, &mut scratch);
+
+       let mut real_diff = [0f32; SPECTOGRAM_AREA];
+       let mut imag_diff = [0f32; SPECTOGRAM_AREA];
+
+       for i in 0..SPECTOGRAM_AREA {
+               real_diff[i] = origional[i].re - buffer[i].re;
+               imag_diff[i] = origional[i].im - buffer[i].im;
        }
+
+       let mut fg = Figure::new();
+       fg.axes2d()
+               .lines(
+                       &real_diff,
+                       &imag_diff,
+                       &[Caption("Difference"), Color("blue")]
+               );
+       fg.show().unwrap();
+
+       Ok(())
 }
 
 #[show_image::main]
@@ -117,7 +157,6 @@ fn main () -> Result<(), Box<dyn std::error::Error>> {
        let scratch_space = forward_transform.get_inplace_scratch_len();
 
        // register the buffer and scratch space
-       let mut origional = [0i16; SPECTOGRAM_AREA];
        let mut buffer = vec![Complex{re: 0f32, im: 0f32}; SPECTOGRAM_AREA];
        let mut scratch = vec![Complex{re: 0f32, im: 0f32}; scratch_space];
 
@@ -128,28 +167,48 @@ fn main () -> Result<(), Box<dyn std::error::Error>> {
                                Ok(t) => t,
                                Err(_) => 0i16
                        };
-                       origional[i] = value;
                        buffer[i] = Complex{re: value as f32, im: 0f32};
                }
 
        // register the image
        let mut image_array = ImageArray::new();
 
-       process(forward_transform, &mut buffer, &mut scratch);
+       // get the time, real and imag components of the fft buffer
+       let mut origional_real = [0f32; SPECTOGRAM_AREA];
+       let mut origional_imag = [0f32; SPECTOGRAM_AREA];
+       for i in 0..SPECTOGRAM_AREA {
+               origional_real[i] = buffer[i].re;
+               origional_imag[i] = buffer[i].im;
+       }
+
+       forward_transform.process_with_scratch(&mut buffer, &mut scratch);
 
-       image_array.from_buffer(&buffer);
-       image_array.to_buffer(&mut buffer);
+       let mut fft_real = [0f32; SPECTOGRAM_AREA];
+       let mut fft_imag = [0f32; SPECTOGRAM_AREA];
+       for i in 0..SPECTOGRAM_AREA {
+               fft_real[i] = buffer[i].re;
+               fft_imag[i] = buffer[i].im;
+       }
+
+       //image_array.from_buffer(&buffer);
 
-       process(inverse_transform, &mut buffer, &mut scratch);
+       //image_array.to_buffer(&mut buffer);
 
-       let mut processed = [0i16; SPECTOGRAM_AREA];
+       let mut transformed_real = [0f32; SPECTOGRAM_AREA];
+       let mut transformed_imag = [0f32; SPECTOGRAM_AREA];
        for i in 0..SPECTOGRAM_AREA {
-               processed[i] = buffer[i].re as i16;
+               transformed_real[i] = buffer[i].re;
+               transformed_imag[i] = buffer[i].im;
        }
 
-       let mut time = [0i16; SPECTOGRAM_AREA];
+       inverse_transform.process_with_scratch(&mut buffer, &mut scratch);
+
+       // get the time, real and imag components of the fft buffer
+       let mut processed_real = [0f32; SPECTOGRAM_AREA];
+       let mut processed_imag = [0f32; SPECTOGRAM_AREA];
        for i in 0..SPECTOGRAM_AREA {
-               time[i] = i as i16;
+               processed_real[i] = buffer[i].re;
+               processed_imag[i] = buffer[i].im;
        }
 
        let image = ImageView::new(ImageInfo::rgb8(WINDOW_SIZE as u32, CHUNK_SIZE as u32), &image_array.data);
@@ -161,14 +220,24 @@ fn main () -> Result<(), Box<dyn std::error::Error>> {
        let mut fg = Figure::new();
        fg.axes2d()
                .lines(
-                       &time,
-                       &origional,
-                       &[Caption("Origional"), Color("blue")]
+                       &fft_real,
+                       &fft_imag,
+                       &[Caption("FFT"), Color("red")]
                )
                .lines(
-                       &time,
-                       &processed,
-                       &[Caption("Processed"), Color("red")]
+                       &transformed_real,
+                       &transformed_imag,
+                       &[Caption("Transfomed"), Color("green")]
+               )
+               .lines(
+                       &processed_real,
+                       &processed_imag,
+                       &[Caption("Processed"), Color("yellow")]
+               )
+               .lines(
+                       &origional_real,
+                       &origional_imag,
+                       &[Caption("Origional"), Color("blue")]
                );
        fg.show().unwrap();