jan@42245: (* Title: Fast Fourier Transform jan@42245: Author: Clemens Ballarin , started 12 April 2005 jan@42245: Maintainer: Clemens Ballarin jan@42245: *) jan@42245: jan@42245: theory FFT jan@42245: imports Complex_Main jan@42245: begin jan@42245: jan@42245: text {* We formalise a functional implementation of the FFT algorithm jan@42245: over the complex numbers, and its inverse. Both are shown jan@42245: equivalent to the usual definitions jan@42245: of these operations through Vandermonde matrices. They are also jan@42245: shown to be inverse to each other, more precisely, that composition jan@42245: of the inverse and the transformation yield the identity up to a jan@42245: scalar. jan@42245: jan@42245: The presentation closely follows Section 30.2 of Cormen \textit{et jan@42245: al.}, \emph{Introduction to Algorithms}, 2nd edition, MIT Press, jan@42245: 2003. *} jan@42245: jan@42245: jan@42245: section {* Preliminaries *} jan@42245: jan@42245: lemma of_nat_cplx: jan@42245: "of_nat n = Complex (of_nat n) 0" jan@42245: by (induct n) (simp_all add: complex_one_def) jan@42245: jan@42245: jan@42245: text {* The following two lemmas are useful for experimenting with the jan@42245: transformations, at a vector length of four. *} jan@42245: jan@42245: lemma Ivl4: jan@42245: "{0..<4::nat} = {0, 1, 2, 3}" jan@42245: proof - jan@42245: have "{0..<4::nat} = {0..i=0..<4::nat. x i) = x 0 + x 1 + x 2 + x 3" jan@42245: by (simp add: Ivl4 eval_nat_numeral) jan@42245: jan@42245: jan@42245: text {* A number of specialised lemmas for the summation operator, jan@42245: where the index set is the natural numbers *} jan@42245: jan@42245: lemma setsum_add_nat_ivl_singleton: jan@42245: assumes less: "m < (n::nat)" jan@42245: shows "f m + setsum f {m<.. {m<.. g i = f i" jan@42245: shows "f m + setsum g {m<.. g i = f i" jan@42245: and h: "!!i. [| k <= i; i < n |] ==> h i = f i" jan@42245: shows "setsum g {m.. ((%i. Suc (2*i)) ` {0.. ((%i. Suc (2*i)) ` {0..i::nat = 0..<2*n. f i) = (\i = 0..i = 0..i::nat = 0..<2*n. f i) = jan@42245: setsum f (op * 2 ` {0..i = 0..i = 0.. complex" where jan@42245: "root n == cis (2*pi/(real (n::nat)))" jan@42245: jan@42245: lemma sin_periodic_pi_diff [simp]: "sin (x - pi) = - sin x" jan@42245: by (simp add: sin_diff) jan@42245: jan@42245: lemma sin_cos_between_zero_two_pi: jan@42245: assumes 0: "0 < x" and pi: "x < 2 * pi" jan@42245: shows "sin x \ 0 \ cos x \ 1" jan@42245: proof - jan@42245: { assume "0 < x" and "x < pi" jan@42245: then have "sin x \ 0" by (auto dest: sin_gt_zero_pi) } jan@42245: moreover jan@42245: { assume "x = pi" jan@42245: then have "cos x \ 1" by simp } jan@42245: moreover jan@42245: { assume pi1: "pi < x" and pi2: "x < 2 * pi" jan@42245: then have "0 < x - pi" and "x - pi < pi" by arith+ jan@42245: then have "sin (x - pi) \ 0" by (auto dest: sin_gt_zero_pi) jan@42245: with pi1 pi2 have "sin x \ 0" by simp } jan@42245: ultimately show ?thesis using 0 pi by arith jan@42245: qed jan@42245: jan@42245: jan@42245: subsection {* Basic Lemmas *} jan@42245: jan@42245: lemma root_nonzero: jan@42245: "root n ~= 0" jan@42245: apply (unfold root_def) jan@42245: apply (unfold cis_def) jan@42245: apply auto jan@42245: apply (drule sin_zero_abs_cos_one) jan@42245: apply arith jan@42245: done jan@42245: jan@42245: lemma root_unity: jan@42245: "root n ^ n = 1" jan@42245: apply (unfold root_def) jan@42245: apply (simp add: DeMoivre) jan@42245: apply (simp add: cis_def) jan@42245: done jan@42245: jan@42245: lemma root_cancel: jan@42245: "0 < d ==> root (d * n) ^ (d * k) = root n ^ k" jan@42245: apply (unfold root_def) jan@42245: apply (simp add: DeMoivre) jan@42245: done jan@42245: jan@42245: lemma root_summation: jan@42245: assumes k: "0 < k" "k < n" jan@42245: shows "(\i=0..i=0..i=0..i=0.. root (2 * n) ^ n = - 1" jan@42245: txt {* Note the space between @{text "-"} and @{text "1"}. *} jan@42245: using root_cancel [where n = 2 and k = 1] jan@42245: apply (simp only: mult_ac) jan@42245: apply (simp add: complex_one_def) jan@42245: done jan@42245: jan@42245: jan@42245: section {* Discrete Fourier Transformation *} jan@42245: jan@42245: text {* jan@42245: We define operations @{text DFT} and @{text IDFT} for the discrete jan@42245: Fourier Transform and its inverse. Vectors are simply functions of jan@42245: type @{text "nat => complex"}. *} jan@42245: jan@42245: text {* jan@42245: @{text "DFT n a"} is the transform of vector @{text a} jan@42245: of length @{text n}, @{text IDFT} its inverse. *} jan@42245: jan@42245: definition DFT :: "nat => (nat => complex) => (nat => complex)" where jan@42245: "DFT n a == (%i. \j=0.. (nat => complex) => (nat => complex)" where jan@42245: "IDFT n a == (%i. (\k=0..j = 0..<2 * m. root (2 * m) ^ (i * j) * a j) = jan@42245: (\j = 0..j = 0..j = 0..j = 0..j = 0..<2 * m. root (2 * m) ^ (i * j) * a j) = jan@42245: (\j = 0..j = 0..j = 0..j = 0..j = 0..<2 * m. a j / root (2 * m) ^ (i * j)) = jan@42245: (\j = 0..j = 0..j = 0..j = 0..j = 0..<2 * m. a j / root (2 * m) ^ (i * j)) = jan@42245: (\j = 0..j = 0..j = 0..j = 0.. (inverse a) ^ (n-m) = (a^m) / (a^n)" jan@42245: apply (induct n m rule: diff_induct) jan@42245: apply (simp add: nonzero_power_inverse jan@42245: nonzero_inverse_eq_divide [THEN sym] nz) jan@42245: apply simp jan@42245: apply (simp add: nz) jan@42245: done jan@42245: jan@42245: lemma power_diff_rev_if: jan@42245: assumes nz: "(a::'a::field) ~= 0" jan@42245: shows "(a^m) / (a^n) = (if n <= m then a ^ (m-n) else (1/a) ^ (n-m))" jan@42245: proof (cases "n <= m") jan@42245: case True with nz show ?thesis jan@42245: by (simp add: power_diff) jan@42245: next jan@42245: case False with nz show ?thesis jan@42245: by (simp add: power_diff_inverse nonzero_inverse_eq_divide [THEN sym]) jan@42245: qed jan@42245: jan@42245: lemma power_divides_special: jan@42245: "(a::'a::field) ~= 0 ==> jan@42245: a ^ (i * j) / a ^ (k * i) = (a ^ j / a ^ k) ^ i" jan@42245: by (simp add: nonzero_power_divide power_mult [THEN sym] mult_ac) jan@42245: jan@42245: theorem DFT_inverse: jan@42245: assumes i_less: "i < n" jan@42245: shows "IDFT n (DFT n a) i = of_nat n * a i" jan@42245: using [[linarith_split_limit = 0]] jan@42245: apply (unfold DFT_def IDFT_def) jan@42245: apply (simp add: setsum_divide_distrib) jan@42245: apply (subst setsum_commute) jan@42245: apply (simp only: times_divide_eq_left [THEN sym]) jan@42245: apply (simp only: power_divides_special [OF root_nonzero]) jan@42245: apply (simp add: power_diff_rev_if root_nonzero) jan@42245: apply (simp add: setsum_divide_distrib [THEN sym] jan@42245: setsum_left_distrib [THEN sym]) jan@42245: proof - jan@42245: from i_less have i_diff: "!!k. i - k < n" by arith jan@42245: have diff_i: "!!k. k < n ==> k - i < n" by arith jan@42245: jan@42245: let ?sum = "%i j n. setsum (op ^ (if i <= j then root n ^ (j - i) jan@42245: else (1 / root n) ^ (i - j))) {0..j = 0..j = 0..j = i..j = i..j\{i} \ {i<..j\{i<.. (nat => complex) => (nat => complex)" where jan@42245: "FFT 0 a = a" jan@42245: | "FFT (Suc k) a = jan@42245: (let (x, y) = (FFT k (%i. a (2*i)), FFT k (%i. a (2*i+1))) jan@42245: in (%i. if i < 2^k jan@42245: then x i + (root (2 ^ (Suc k))) ^ i * y i jan@42245: else x (i- 2^k) - (root (2 ^ (Suc k))) ^ (i- 2^k) * y (i- 2^k)))" jan@42245: jan@42245: primrec IFFT :: "nat => (nat => complex) => (nat => complex)" where jan@42245: "IFFT 0 a = a" jan@42245: | "IFFT (Suc k) a = jan@42245: (let (x, y) = (IFFT k (%i. a (2*i)), IFFT k (%i. a (2*i+1))) jan@42245: in (%i. if i < 2^k jan@42245: then x i + (1 / root (2 ^ (Suc k))) ^ i * y i jan@42245: else x (i - 2^k) - jan@42245: (1 / root (2 ^ (Suc k))) ^ (i - 2^k) * y (i - 2^k)))" jan@42245: jan@42245: text {* Finally, for vectors of length @{text "2 ^ k"}, jan@42245: @{text DFT} and @{text FFT}, and @{text IDFT} and jan@42245: @{text IFFT} are equivalent. *} jan@42245: jan@42245: theorem DFT_FFT: jan@42245: "!!a i. i < 2 ^ k ==> DFT (2 ^ k) a i = FFT k a i" jan@42245: proof (induct k) jan@42245: case 0 jan@42245: then show ?case by (simp add: DFT_def) jan@42245: next jan@42245: case (Suc k) jan@42245: assume i: "i < 2 ^ Suc k" jan@42245: show ?case proof (cases "i < 2 ^ k") jan@42245: case True jan@42245: then show ?thesis apply simp apply (simp add: DFT_lower) jan@42245: apply (simp add: Suc) done jan@42245: next jan@42245: case False jan@42245: from i have "i - 2 ^ k < 2 ^ k" by simp jan@42245: with False i show ?thesis apply simp apply (simp add: DFT_upper) jan@42245: apply (simp add: Suc) done jan@42245: qed jan@42245: qed jan@42245: jan@42245: theorem IDFT_IFFT: jan@42245: "!!a i. i < 2 ^ k ==> IDFT (2 ^ k) a i = IFFT k a i" jan@42245: proof (induct k) jan@42245: case 0 jan@42245: then show ?case by (simp add: IDFT_def) jan@42245: next jan@42245: case (Suc k) jan@42245: assume i: "i < 2 ^ Suc k" jan@42245: show ?case proof (cases "i < 2 ^ k") jan@42245: case True jan@42245: then show ?thesis apply simp apply (simp add: IDFT_lower) jan@42245: apply (simp add: Suc) done jan@42245: next jan@42245: case False jan@42245: from i have "i - 2 ^ k < 2 ^ k" by simp jan@42245: with False i show ?thesis apply simp apply (simp add: IDFT_upper) jan@42245: apply (simp add: Suc) done jan@42245: qed jan@42245: qed jan@42245: jan@42245: schematic_lemma "map (FFT (Suc (Suc 0)) a) [0, 1, 2, 3] = ?x" jan@42245: by simp jan@42245: jan@42245: end