1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
//! Chalharu's Fastest Fourier Transform. //! //! # Licensing //! This Source Code is subject to the terms of the Mozilla Public License //! version 2.0 (the "License"). You can obtain a copy of the License at //! http://mozilla.org/MPL/2.0/ . use crate::CFft1D; use num_complex::Complex; use num_traits::float::{Float, FloatConst}; use num_traits::identities::{one, zero}; use num_traits::{cast, NumAssign}; /// Perform a complex-to-complex two-dimensional Fourier transform /// /// <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML"></script> /// /// # Example /// /// ```rust /// use num_complex::Complex; /// use chfft::CFft2D; /// /// fn main() { /// let input = [ /// vec![ /// Complex::new(2.0, 0.0), /// Complex::new(1.0, 1.0), /// Complex::new(0.0, 3.0), /// Complex::new(2.0, 4.0), /// ], /// vec![ /// Complex::new(5.0, 0.0), /// Complex::new(3.0, 1.0), /// Complex::new(2.0, 3.0), /// Complex::new(2.0, 8.0), /// ], /// vec![ /// Complex::new(2.0, 5.0), /// Complex::new(2.0, 3.0), /// Complex::new(3.0, 7.0), /// Complex::new(2.0, 1.0), /// ], /// vec![ /// Complex::new(5.0, 4.0), /// Complex::new(1.0, 2.0), /// Complex::new(4.0, 3.0), /// Complex::new(2.0, 1.0), /// ], /// ]; /// /// let mut fft = CFft2D::<f64>::with_len(input.len(), input[0].len()); /// /// let output = fft.forward(&input); /// /// println!("the transform of {:?} is {:?}", input, output); /// } /// ``` #[derive(Debug)] pub struct CFft2D<T> { len_m: usize, len_n: usize, scaler_n: T, scaler_u: T, fft_m: CFft1D<T>, fft_n: CFft1D<T>, work: Vec<Vec<Complex<T>>>, } impl<T: Float + FloatConst + NumAssign> CFft2D<T> { /// Returns a instances to execute FFT /// /// ```rust /// use chfft::CFft2D; /// let mut fft = CFft2D::<f64>::new(); /// ``` pub fn new() -> Self { Self { len_m: 0, len_n: 0, scaler_n: zero(), scaler_u: zero(), fft_m: CFft1D::new(), fft_n: CFft1D::new(), work: Vec::new(), } } /// Returns a instances to execute length initialized FFT /// /// ```rust /// use chfft::CFft2D; /// let mut fft = CFft2D::<f64>::with_len(1024, 1024); /// ``` pub fn with_len(len_m: usize, len_n: usize) -> Self { Self { len_m, len_n, scaler_n: T::one() / cast(len_m * len_n).unwrap(), scaler_u: T::one() / cast::<_, T>(len_m * len_n).unwrap().sqrt(), fft_m: CFft1D::with_len(len_m), fft_n: CFft1D::with_len(len_n), work: vec![vec![zero(); len_m]; len_n], } } /// Reinitialize length /// /// ```rust /// use chfft::CFft2D; /// let mut fft = CFft2D::<f64>::with_len(1024, 1024); /// /// // reinitialize /// fft.setup(2048, 2048); /// ``` pub fn setup(&mut self, len_m: usize, len_n: usize) { self.len_m = len_m; self.len_n = len_n; self.scaler_n = T::one() / cast(len_m * len_n).unwrap(); self.scaler_u = self.scaler_n.sqrt(); self.fft_m.setup(len_m); self.fft_n.setup(len_n); if self.work.len() != len_n || (!self.work.is_empty() && self.work[0].len() != len_m) { self.work = vec![vec![zero(); len_m]; len_n]; } } /// The 1 scaling factor forward transform /// /// ```rust /// use num_complex::Complex; /// use chfft::CFft2D; /// /// let input = [ /// vec![ /// Complex::new(2.0, 0.0), /// Complex::new(1.0, 1.0), /// Complex::new(0.0, 3.0), /// Complex::new(2.0, 4.0), /// ], /// vec![ /// Complex::new(5.0, 0.0), /// Complex::new(3.0, 1.0), /// Complex::new(2.0, 3.0), /// Complex::new(2.0, 8.0), /// ], /// ]; /// /// let mut fft = CFft2D::<f64>::with_len(input.len(), input[0].len()); /// let output = fft.forward(&input); /// ``` pub fn forward(&mut self, source: &[Vec<Complex<T>>]) -> Vec<Vec<Complex<T>>> { self.convert(source, false, one()) } /// The 1 scaling factor forward transform /// /// ```rust /// use num_complex::Complex; /// use chfft::CFft2D; /// /// let input = [ /// vec![ /// Complex::new(2.0, 0.0), /// Complex::new(1.0, 1.0), /// Complex::new(0.0, 3.0), /// Complex::new(2.0, 4.0), /// ], /// vec![ /// Complex::new(5.0, 0.0), /// Complex::new(3.0, 1.0), /// Complex::new(2.0, 3.0), /// Complex::new(2.0, 8.0), /// ], /// ]; /// /// let mut fft = CFft2D::<f64>::with_len(input.len(), input[0].len()); /// let output = fft.forward0(&input); /// ``` pub fn forward0(&mut self, source: &[Vec<Complex<T>>]) -> Vec<Vec<Complex<T>>> { self.convert(source, false, one()) } /// The \\(\frac 1 {\sqrt n}\\) scaling factor forward transform /// /// ```rust /// use num_complex::Complex; /// use chfft::CFft2D; /// /// let input = [ /// vec![ /// Complex::new(2.0, 0.0), /// Complex::new(1.0, 1.0), /// Complex::new(0.0, 3.0), /// Complex::new(2.0, 4.0), /// ], /// vec![ /// Complex::new(5.0, 0.0), /// Complex::new(3.0, 1.0), /// Complex::new(2.0, 3.0), /// Complex::new(2.0, 8.0), /// ], /// ]; /// /// let mut fft = CFft2D::<f64>::with_len(input.len(), input[0].len()); /// let output = fft.forwardu(&input); /// ``` pub fn forwardu(&mut self, source: &[Vec<Complex<T>>]) -> Vec<Vec<Complex<T>>> { let scaler = self.scaler_u; self.convert(source, false, scaler) } /// The \\(\frac 1 n\\) scaling factor forward transform /// /// ```rust /// use num_complex::Complex; /// use chfft::CFft2D; /// /// let input = [ /// vec![ /// Complex::new(2.0, 0.0), /// Complex::new(1.0, 1.0), /// Complex::new(0.0, 3.0), /// Complex::new(2.0, 4.0), /// ], /// vec![ /// Complex::new(5.0, 0.0), /// Complex::new(3.0, 1.0), /// Complex::new(2.0, 3.0), /// Complex::new(2.0, 8.0), /// ], /// ]; /// /// let mut fft = CFft2D::<f64>::with_len(input.len(), input[0].len()); /// let output = fft.forwardn(&input); /// ``` pub fn forwardn(&mut self, source: &[Vec<Complex<T>>]) -> Vec<Vec<Complex<T>>> { let scaler = self.scaler_n; self.convert(source, false, scaler) } /// The \\(\frac 1 n\\) scaling factor backward transform /// /// ```rust /// use num_complex::Complex; /// use chfft::CFft2D; /// /// let input = [ /// vec![ /// Complex::new(2.0, 0.0), /// Complex::new(1.0, 1.0), /// Complex::new(0.0, 3.0), /// Complex::new(2.0, 4.0), /// ], /// vec![ /// Complex::new(5.0, 0.0), /// Complex::new(3.0, 1.0), /// Complex::new(2.0, 3.0), /// Complex::new(2.0, 8.0), /// ], /// ]; /// /// let mut fft = CFft2D::<f64>::with_len(input.len(), input[0].len()); /// let output = fft.backward(&input); /// ``` pub fn backward(&mut self, source: &[Vec<Complex<T>>]) -> Vec<Vec<Complex<T>>> { let scaler = self.scaler_n; self.convert(source, true, scaler) } /// The 1 scaling factor backward transform /// /// ```rust /// use num_complex::Complex; /// use chfft::CFft2D; /// /// let input = [ /// vec![ /// Complex::new(2.0, 0.0), /// Complex::new(1.0, 1.0), /// Complex::new(0.0, 3.0), /// Complex::new(2.0, 4.0), /// ], /// vec![ /// Complex::new(5.0, 0.0), /// Complex::new(3.0, 1.0), /// Complex::new(2.0, 3.0), /// Complex::new(2.0, 8.0), /// ], /// ]; /// /// let mut fft = CFft2D::<f64>::with_len(input.len(), input[0].len()); /// let output = fft.backward0(&input); /// ``` pub fn backward0(&mut self, source: &[Vec<Complex<T>>]) -> Vec<Vec<Complex<T>>> { self.convert(source, true, one()) } /// The \\(\frac 1 {\sqrt n}\\) scaling factor backward transform /// /// ```rust /// use num_complex::Complex; /// use chfft::CFft2D; /// /// let input = [ /// vec![ /// Complex::new(2.0, 0.0), /// Complex::new(1.0, 1.0), /// Complex::new(0.0, 3.0), /// Complex::new(2.0, 4.0), /// ], /// vec![ /// Complex::new(5.0, 0.0), /// Complex::new(3.0, 1.0), /// Complex::new(2.0, 3.0), /// Complex::new(2.0, 8.0), /// ], /// ]; /// /// let mut fft = CFft2D::<f64>::with_len(input.len(), input[0].len()); /// let output = fft.backwardu(&input); /// ``` pub fn backwardu(&mut self, source: &[Vec<Complex<T>>]) -> Vec<Vec<Complex<T>>> { let scaler = self.scaler_u; self.convert(source, true, scaler) } #[inline] fn convert( &mut self, source: &[Vec<Complex<T>>], is_back: bool, scaler: T, ) -> Vec<Vec<Complex<T>>> { if source.is_empty() { return Vec::new(); } if source.len() != self.len_m || source[0].len() != self.len_n { self.setup(source.len(), source[0].len()); } for (i, si) in source.iter().enumerate() { let work = if is_back { self.fft_m.backward0(si) } else { self.fft_m.forward0(si) }; for (j, &wi) in work.iter().enumerate() { self.work[j][i] = wi; } } let mut ret = vec![Vec::with_capacity(self.len_n); self.len_m]; for i in 0..self.work.len() { let work = if is_back { self.fft_n.backward0(&self.work[i]) } else { self.fft_n.forward0(&self.work[i]) }; for j in 0..work.len() { ret[j].push(work[j] * scaler); } } ret } } impl<T: Float + FloatConst + NumAssign> Default for CFft2D<T> { /// Returns a instances to execute FFT /// /// ```rust /// use chfft::CFft2D; /// let mut fft = CFft2D::<f64>::default(); /// ``` fn default() -> Self { Self::new() } } #[cfg(test)] mod tests { use super::*; use crate::assert_appro_eq; use crate::FloatEps; use appro_eq::AbsError; use rand::distributions::{Distribution, Standard}; use rand::{Rng, SeedableRng}; use rand_xorshift::XorShiftRng; use std::fmt::Debug; fn convert<T: Float + FloatConst>( source: &[Vec<Complex<T>>], scalar: T, ) -> Vec<Vec<Complex<T>>> { (0..source.len()) .map(|i| { (0..source[0].len()) .map(|k| { (0..source.len()).fold(zero(), |x: Complex<T>, j| { x + (0..source[0].len()).fold(zero(), |y: Complex<T>, l| { y + source[j][l] * Complex::<T>::from_polar( one(), -cast::<_, T>(2).unwrap() * T::PI() * ((cast::<_, T>(i * j).unwrap() / cast(source.len()).unwrap()) + cast::<_, T>(k * l).unwrap() / cast(source[0].len()).unwrap()), ) }) }) * scalar }) .collect::<Vec<_>>() }) .collect::<Vec<_>>() } fn test_with_source<T: Float + FloatConst + NumAssign + Debug + AbsError + FloatEps>( fft: &mut CFft2D<T>, source: &[Vec<Complex<T>>], ) { let expected = convert(source, one()); let actual = fft.forward(source); assert_appro_eq(&expected, &actual); let actual_source = fft.backward(&actual); assert_appro_eq(source, &actual_source); } fn test_with_len<T: Float + FloatConst + NumAssign + Debug + AbsError + FloatEps>( fft: &mut CFft2D<T>, len_m: usize, len_n: usize, ) where Standard: Distribution<T>, { let mut rng = XorShiftRng::from_seed([ 0xDA, 0xE1, 0x4B, 0x0B, 0xFF, 0xC2, 0xFE, 0x64, 0x23, 0xFE, 0x3F, 0x51, 0x6D, 0x3E, 0xA2, 0xF3, ]); // 10パターンのテスト for _ in 0..10 { let arr = (0..len_m) .map(|_| { (0..len_n) .map(|_| Complex::new(rng.gen::<T>(), rng.gen::<T>())) .collect::<Vec<Complex<T>>>() }) .collect::<Vec<Vec<Complex<T>>>>(); test_with_source(fft, &arr); } } #[test] fn f64_new() { for i in 1..10 { for j in 1..10 { test_with_len(&mut CFft2D::<f64>::new(), i, j); } } } #[test] fn f32_new() { for i in 1..10 { for j in 1..10 { test_with_len(&mut CFft2D::<f32>::new(), i, j); } } } #[test] fn f64_with_len() { for i in 1..10 { for j in 1..10 { test_with_len(&mut CFft2D::<f64>::with_len(i, j), i, j); } } } #[test] fn f32_with_len() { for i in 1..10 { for j in 1..10 { test_with_len(&mut CFft2D::<f32>::with_len(i, j), i, j); } } } }