多项式
什么是多项式?
形如:
P ( x ) = a n x n + a n − 1 x n − 1 + ⋯ + a 0 , n ∈ { 0 , 1 , 2 , 3 ⋯ } P(x) = a_nx^n+a_{n-1}x^{n-1}+\cdots+a_0,\quad n\in\{0,1,2,3\cdots\} P ( x ) = a n x n + a n − 1 x n − 1 + ⋯ + a 0 , n ∈ { 0 , 1 , 2 , 3 ⋯ }
对于确定的 P ( x ) P(x) P ( x ) 我们需要知道 n + 1 n+1 n + 1 个系数,或者需要 n + 1 n+1 n + 1 个点 ( x i , P ( x i ) ) \left(x_i,P(x_i)\right) ( x i , P ( x i ) )
如果存在两个多项式 P ( x ) P(x) P ( x ) 和 Q ( x ) Q(x) Q ( x ) 都过 n + 1 n+1 n + 1 个点,则
( P ( x 0 ) P ( x 1 ) P ( x 2 ) ⋮ P ( x n ) ) = ( 1 x 0 x 0 2 ⋯ x 0 n − 1 1 x 1 x 1 2 ⋯ x 1 n − 1 1 x 2 x 2 2 ⋯ x 2 n − 1 ⋮ ⋮ ⋮ ⋱ ⋮ 1 x n x n 2 ⋯ x n n − 1 ) ( p 0 p 1 p 2 ⋮ p n ) \begin{pmatrix}
P(x_0) \\
P(x_1) \\
P(x_2) \\
\vdots \\
P(x_n)
\end{pmatrix} = \begin{pmatrix}
1 & x_0 & x_0^2 & \cdots & x_0^{n-1} \\
1 & x_1 & x_1^2 & \cdots & x_1^{n-1} \\
1 & x_2 & x_2^2 & \cdots & x_2^{n-1} \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & x_n & x_n^2 & \cdots & x_n^{n-1}
\end{pmatrix}
\begin{pmatrix}
p_0 \\
p_1 \\
p_2 \\
\vdots \\
p_{n}
\end{pmatrix} P ( x 0 ) P ( x 1 ) P ( x 2 ) ⋮ P ( x n ) = 1 1 1 ⋮ 1 x 0 x 1 x 2 ⋮ x n x 0 2 x 1 2 x 2 2 ⋮ x n 2 ⋯ ⋯ ⋯ ⋱ ⋯ x 0 n − 1 x 1 n − 1 x 2 n − 1 ⋮ x n n − 1 p 0 p 1 p 2 ⋮ p n
( Q ( x 0 ) Q ( x 1 ) Q ( x 2 ) ⋮ Q ( x n ) ) = ( 1 x 0 x 0 2 ⋯ x 0 n − 1 1 x 1 x 1 2 ⋯ x 1 n − 1 1 x 2 x 2 2 ⋯ x 2 n − 1 ⋮ ⋮ ⋮ ⋱ ⋮ 1 x n x n 2 ⋯ x n n − 1 ) ( q 0 q 1 q 2 ⋮ q n ) \begin{pmatrix}
Q(x_0) \\
Q(x_1) \\
Q(x_2) \\
\vdots \\
Q(x_n)
\end{pmatrix} = \begin{pmatrix}
1 & x_0 & x_0^2 & \cdots & x_0^{n-1} \\
1 & x_1 & x_1^2 & \cdots & x_1^{n-1} \\
1 & x_2 & x_2^2 & \cdots & x_2^{n-1} \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & x_n & x_n^2 & \cdots & x_n^{n-1}
\end{pmatrix}
\begin{pmatrix}
q_0 \\
q_1 \\
q_2 \\
\vdots \\
q_{n}
\end{pmatrix} Q ( x 0 ) Q ( x 1 ) Q ( x 2 ) ⋮ Q ( x n ) = 1 1 1 ⋮ 1 x 0 x 1 x 2 ⋮ x n x 0 2 x 1 2 x 2 2 ⋮ x n 2 ⋯ ⋯ ⋯ ⋱ ⋯ x 0 n − 1 x 1 n − 1 x 2 n − 1 ⋮ x n n − 1 q 0 q 1 q 2 ⋮ q n
因为 Q ( x i ) = P ( x i ) Q(x_i)=P(x_i) Q ( x i ) = P ( x i ) 并且范德蒙行列式满秩, 所以 p i = q i p_i = q_i p i = q i
什么是多项式乘法?
我所要讲的是现在已知两个多项式 A ( x ) A(x) A ( x ) 与 B ( x ) B(x) B ( x ) ,我们应该如何得到两个多项式的乘积C ( x ) C(x) C ( x ) ?
A ( x ) = a p x p + a p − 1 x p − 1 + ⋯ + a 0 B ( x ) = b q x q + b q − 1 x q − 1 + ⋯ + b 0 C ( x ) = A ( x ) B ( x ) = c r x r + c r − 1 x r − 1 + ⋯ + c 0 } \begin{align}
A(x) &= a_px^p+a_{p-1}x^{p-1}+\cdots+a_0\\
B(x) &= b_qx^q+b_{q-1}x^{q-1}+\cdots+b_0 \\
C(x) &= A(x) \, B(x)= c_{r}x^{r}+c_{r-1}x^{r-1}+\cdots+c_0\}
\end{align} A ( x ) B ( x ) C ( x ) = a p x p + a p − 1 x p − 1 + ⋯ + a 0 = b q x q + b q − 1 x q − 1 + ⋯ + b 0 = A ( x ) B ( x ) = c r x r + c r − 1 x r − 1 + ⋯ + c 0 }
上述的公式中 a ( x ) , b ( x ) a(x),b(x) a ( x ) , b ( x ) 已知,c r c_r c r 未知
通常的算法:多项式乘法(假设 p ⩾ q p \geqslant q p ⩾ q )
a p x p + ⋯ + a q x q + a q − 1 x q − 1 + ⋯ + a 1 x + a 0 b q x q + b q − 1 x q − 1 + ⋯ + b 1 x + b 0 a p b 0 x p + ⋯ + a q b 0 x q + a q − 1 b 0 x q − 1 + ⋯ + a 1 b 0 x + a 0 b 0 a p b 1 x p + 1 + ⋯ + a q − 1 b 1 x q + a q − 2 b 1 x q − 1 + ⋯ + a 0 b 1 x ⋮ a p b q x p + q + ⋯ + a 0 b q x q c p + q x p + q + ⋯ + c q x q + c q − 1 x q − 1 + ⋯ + c 1 x 1 + c 0 \begin{align}
a_px^p + \cdots+ a_{q}&x^{q}+a_{q - 1}x^{q-1}+\cdots+a_1x + a_0\\
b_q&x^q + b_{q - 1}x^{q-1}+\cdots+b_1x + b_0\\
\hline
a_pb_0x^p+\cdots+a_{q}b_0&x^{q}+a_{q - 1}b_0x^{q-1}+\cdots+a_1b_0x+a_0b_0\\
a_pb_1x^{p + 1}+\cdots+a_{q - 1}b_1&x^{q}+a_{q - 2}b_1x^{q-1}+\cdots+a_0b_1x\\
\vdots \\
a_pb_qx^{p + q}+\cdots+a_{0}b_q&x^{q}\\
\hline
c_{p+q}x^{p+q}+\cdots+c_{q}&x^{q}+c_{q-1}x^{q-1}+\cdots+c_{1}x^{1}+c_0
\end{align} a p x p + ⋯ + a q b q a p b 0 x p + ⋯ + a q b 0 a p b 1 x p + 1 + ⋯ + a q − 1 b 1 ⋮ a p b q x p + q + ⋯ + a 0 b q c p + q x p + q + ⋯ + c q x q + a q − 1 x q − 1 + ⋯ + a 1 x + a 0 x q + b q − 1 x q − 1 + ⋯ + b 1 x + b 0 x q + a q − 1 b 0 x q − 1 + ⋯ + a 1 b 0 x + a 0 b 0 x q + a q − 2 b 1 x q − 1 + ⋯ + a 0 b 1 x x q x q + c q − 1 x q − 1 + ⋯ + c 1 x 1 + c 0
多项式乘法代码实现
a = [1 , 2 , 3 , 4 , 5 ]
b = [2 , 3 , 4 , 5 ]
result_length = len (a) + len (b) - 1
result = [0 ] * result_length
a_reversed = a[::-1 ]
b_reversed = b[::-1 ]
for j in range (len (b_reversed)):
for i in range (len (a_reversed)):
result[i + j] += a_reversed[i] * b_reversed[j]
result = result[::-1 ]
print (result)
时间复杂度
我们讨论一下计算的复杂度
所需要的计算:
浮点数加法 p ⋅ q p \cdot q p ⋅ q
浮点数乘法 p ⋅ q p \cdot q p ⋅ q
现在我想告诉你的是有一种算法可以将算法的复杂度减到 O ( N ⋅ log 2 N ) O(N \cdot \log_2N) O ( N ⋅ log 2 N )
我们即将用到的大杀器 快速傅里叶变换
傅里叶变换
泰勒级数
f ( x ) = a n x n + a n − 1 x n − 1 + ⋯ + a 0 , n ∈ { 0 , 1 , 2 , 3 ⋯ } f(x)=a_nx^n+a_{n-1}x^{n-1}+\cdots+a_0,\quad n\in\{0,1,2,3\cdots\} f ( x ) = a n x n + a n − 1 x n − 1 + ⋯ + a 0 , n ∈ { 0 , 1 , 2 , 3 ⋯ }
其中{ x n ∣ n ∈ { 0 , 1 , 2 , 3 ⋯ } \{x^n| n\in\{0,1,2,3\cdots\} { x n ∣ n ∈ { 0 , 1 , 2 , 3 ⋯ } 是一组基(虽然不是正交基)a i a_i a i 是这一组基坐标系下的坐标 。
可以使用一些判别法求出泰勒级数的收敛半径
傅里叶级数
在讨论傅里叶级数之前我们先证明一些之后可能用到的小推论
核函数 D N ( x ) D_N(x) D N ( x )
D N ( x ) = 1 2 + ∑ n = 1 N cos n x = 1 2 + cos x + cos 2 x + cos 3 x + ⋯ + cos N x = 2 sin x 2 2 + 2 sin x 2 cos x + 2 sin x 2 cos 2 x + 2 sin x 2 cos 3 x + ⋯ + 2 sin x 2 cos N x 2 sin x 2 = sin x 2 + sin ( x 2 + x ) + sin ( x 2 − x ) + sin ( x 2 + 2 x ) + sin ( x 2 − 2 x ) + ⋯ + sin ( x 2 + N x ) + sin ( x 2 − N x ) 2 sin x 2 = sin x 2 + sin ( 3 x 2 ) − sin x 2 + sin ( 5 x 2 ) − sin ( 3 x 2 ) + ⋯ + sin ( ( 2 N + 1 ) x 2 ) − sin ( ( 2 N − 1 ) x 2 ) 2 sin x 2 = sin ( 2 N + 1 ) x 2 2 sin x 2 \begin{align}
D_N(x) &= \frac{1}{2}+ \sum_{n = 1}^{N} \cos nx \\
&= \frac{1}{2} + \cos x + \cos 2x + \cos 3x + \cdots + \cos Nx \\
&= \frac{ \frac{ 2 \sin \frac {x} {2}}{2} + 2 \sin \frac {x} {2} \cos x + 2 \sin \frac {x} {2} \cos 2x + 2 \sin \frac {x} {2} \cos 3x + \cdots + 2 \sin \frac {x} {2} \cos Nx }{ 2 \sin \frac {x} {2} } \\
&= \frac{ \sin \frac {x} {2} + \sin \left(\frac {x} {2}+x\right) + \sin \left(\frac {x} {2}-x\right) + \sin \left(\frac {x} {2}+2x\right) + \sin \left(\frac {x} {2}-2x\right) + \cdots + \sin \left(\frac {x} {2}+Nx\right) + \sin \left(\frac {x} {2}-Nx\right) }{ 2 \sin \frac {x} {2} } \\
&= \frac{ \sin \frac {x} {2} + \sin \left(\frac {3x} {2}\right) - \sin \frac {x} {2} + \sin \left(\frac {5x} {2}\right) - \sin \left(\frac {3x} {2}\right) + \cdots + \sin \left(\frac {(2N+1)x} {2}\right) - \sin \left(\frac {(2N-1)x} {2}\right) }{ 2 \sin \frac {x} {2} } \\
&= \frac {\sin \frac {(2N+1)x} {2}} { 2 \sin \frac {x} {2}}
\end{align} D N ( x ) = 2 1 + n = 1 ∑ N cos n x = 2 1 + cos x + cos 2 x + cos 3 x + ⋯ + cos N x = 2 sin 2 x 2 2 s i n 2 x + 2 sin 2 x cos x + 2 sin 2 x cos 2 x + 2 sin 2 x cos 3 x + ⋯ + 2 sin 2 x cos N x = 2 sin 2 x sin 2 x + sin ( 2 x + x ) + sin ( 2 x − x ) + sin ( 2 x + 2 x ) + sin ( 2 x − 2 x ) + ⋯ + sin ( 2 x + N x ) + sin ( 2 x − N x ) = 2 sin 2 x sin 2 x + sin ( 2 3 x ) − sin 2 x + sin ( 2 5 x ) − sin ( 2 3 x ) + ⋯ + sin ( 2 ( 2 N + 1 ) x ) − sin ( 2 ( 2 N − 1 ) x ) = 2 sin 2 x sin 2 ( 2 N + 1 ) x
核函数 D N ( x ) D_N(x) D N ( x ) 的积分
∫ 0 π D N ( x ) d x = π 2 1 π ∫ 0 π 2 D N ( x ) d x = 1 1 π ∫ 0 π 2 s D N ( x ) d x = s \begin{align}
\int_0^{\pi} D_N(x) \,\text{d} x &= \frac{\pi}{2} \\
\frac{1}{\pi}\int_0^{\pi} 2 D_N(x) \,\text{d} x &= 1 \\
\frac{1}{\pi}\int_0^{\pi} 2 s D_N(x) \,\text{d} x &= s \\
\end{align} ∫ 0 π D N ( x ) d x π 1 ∫ 0 π 2 D N ( x ) d x π 1 ∫ 0 π 2 s D N ( x ) d x = 2 π = 1 = s
三角函数的正交
cos ( m x ) \cos(mx) cos ( m x ) 和 cos ( n x ) \cos(nx) cos ( n x ) 当 m ≠ n m \ne n m = n 时正交
sin ( m x ) \sin(mx) sin ( m x ) 和 sin ( n x ) \sin(nx) sin ( n x ) 当 m ≠ n m \ne n m = n 时正交
cos ( m x ) \cos(mx) cos ( m x ) 和 sin ( n x ) \sin(nx) sin ( n x ) 当 m ≠ n m \ne n m = n 时正交
验证三角函数族{ 1 , s i n ( n x ) , c o s ( n x ) ∣ n ∈ { 1 , 2 , 3 ⋯ } \{ \left. 1,sin(nx),cos(nx) \right | n\in\{1,2,3\cdots\} { 1 , s in ( n x ) , cos ( n x ) ∣ n ∈ { 1 , 2 , 3 ⋯ } 在区间[ − π , π ] [-\pi,\pi] [ − π , π ] 上的正交性
我们使用以下内积定义:
⟨ f , g ⟩ = ∫ − π π f ( x ) g ( x ) d x \langle f, g \rangle = \int_{-\pi}^{\pi} f(x) g(x) \,\text dx ⟨ f , g ⟩ = ∫ − π π f ( x ) g ( x ) d x
证明:
∫ − π π cos ( m x ) cos ( n x ) d x = 0 当 m ≠ n ⇓ 使用三角恒等式 ∫ − π π cos ( m x ) cos ( n x ) d x = 1 2 ∫ − π π cos ( ( m + n ) x ) d x + 1 2 ∫ − π π cos ( ( m − n ) x ) d x ⇓ 因为 cos ( k x ) 在一个周期内的积分为零 ∫ − π π cos ( m x ) cos ( n x ) d x = 0 ⇓ 若 m = n 在一个周期内的积分为 π ∫ − π π cos ( m x ) cos ( n x ) d x = π \begin{align}
\int_{-\pi}^{\pi} \cos(mx) \cos(nx) \, \text{d}x &= 0 \quad \text{当} \quad m \neq n \\
\Downarrow \quad &\text{使用三角恒等式} \\
\int_{-\pi}^{\pi} \cos(mx) \cos(nx) \, \text{d}x &= \frac{1}{2} \int_{-\pi}^{\pi} \cos((m+n)x) \, \text{d}x + \frac{1}{2} \int_{-\pi}^{\pi} \cos((m-n)x) \, \text{d}x \\
\Downarrow \quad &\text{因为} \cos(kx) \text{在一个周期内的积分为零} \\
\int_{-\pi}^{\pi} \cos(mx) \cos(nx) \, \text{d}x &= 0 \\
\Downarrow \quad &\text{若} m = n \text{在一个周期内的积分为 } \pi \\
\int_{-\pi}^{\pi} \cos(mx) \cos(nx) \, \text{d}x &= \pi
\end{align} ∫ − π π cos ( m x ) cos ( n x ) d x ⇓ ∫ − π π cos ( m x ) cos ( n x ) d x ⇓ ∫ − π π cos ( m x ) cos ( n x ) d x ⇓ ∫ − π π cos ( m x ) cos ( n x ) d x = 0 当 m = n 使用三角恒等式 = 2 1 ∫ − π π cos (( m + n ) x ) d x + 2 1 ∫ − π π cos (( m − n ) x ) d x 因为 cos ( k x ) 在一个周期内的积分为零 = 0 若 m = n 在一个周期内的积分为 π = π
计算傅里叶级数的系数
若函数f ( x ) f(x) f ( x ) 可以展开成为a 0 , a n , b n a_0, a_{n}, b_{n} a 0 , a n , b n 成立
f ( x ) = a 0 2 + ∑ n = 1 ∞ ( a n cos n x + b n sin n x ) f(x)=\frac{a_{0}}{2}+\sum_{n = 1}^{\infty}(a_{n}\cos nx + b_{n}\sin nx) f ( x ) = 2 a 0 + n = 1 ∑ ∞ ( a n cos n x + b n sin n x )
计算a 0 , a n , b n a_0,a_n,b_n a 0 , a n , b n 分别是多少
f ( x ) = a 0 2 + ∑ n = 1 ∞ ( a n cos n x + b n sin n x ) ⇓ 同时乘 cos n x 然后 ∫ − π π d x ∫ − π π f ( x ) cos n x d x = a n π ⇓ a n = 1 π ∫ − π π f ( x ) cos n x d x b n = 1 π ∫ − π π f ( x ) sin n x d x ⇓ a − n = a n b − n = − b n \begin{align}
f(x)&=\frac{a_{0}}{2}+\sum_{n = 1}^{\infty}(a_{n}\cos nx + b_{n}\sin nx)\\
&\Downarrow \text{ 同时乘 } \cos nx \text{ 然后 } \int_{-\pi}^\pi \text{d}x \\
\int_{-\pi}^\pi f(x) \cos nx \text{d}x&=a_n \pi \\
&\Downarrow \\
a_n &=\frac{1}{\pi} \int_{-\pi}^\pi f(x)\cos nx \, \text{d}x \\
b_n &=\frac{1}{\pi} \int_{-\pi}^\pi f(x)\sin nx \, \text{d}x \\
&\Downarrow \\
a_{-n} &= a_{n} \\
b_{-n} &= -b_{n} \\
\end{align} f ( x ) ∫ − π π f ( x ) cos n x d x a n b n a − n b − n = 2 a 0 + n = 1 ∑ ∞ ( a n cos n x + b n sin n x ) ⇓ 同时乘 cos n x 然后 ∫ − π π d x = a n π ⇓ = π 1 ∫ − π π f ( x ) cos n x d x = π 1 ∫ − π π f ( x ) sin n x d x ⇓ = a n = − b n
贝塞尔不等式的证明
贝塞尔不等式: 其中f ( x ) f(x) f ( x ) 为任意函数
a 0 2 2 + ∑ n = 1 ∞ ( a n 2 + b n 2 ) ≤ 1 π ∫ − π π f ( x ) 2 d x \frac{a_0^2}{2} + \sum_{n=1}^{\infty} (a_n^2 + b_n^2) \leq \frac{1}{\pi} \int_{-\pi}^{\pi} f(x)^2 \, dx 2 a 0 2 + n = 1 ∑ ∞ ( a n 2 + b n 2 ) ≤ π 1 ∫ − π π f ( x ) 2 d x
要证明贝塞尔不等式,我们可以考虑误差项。
定义 S N ( x ) S_N(x) S N ( x ) 为傅里叶级数的第 N N N 部分和:
S N ( x ) = a 0 2 + ∑ n = 1 N ( a n cos ( n x ) + b n sin ( n x ) ) S_N(x) = \frac{a_0}{2} + \sum_{n=1}^{N} \left( a_n \cos(nx) + b_n \sin(nx) \right) S N ( x ) = 2 a 0 + n = 1 ∑ N ( a n cos ( n x ) + b n sin ( n x ) )
误差项 R N ( x ) R_N(x) R N ( x ) 为:
R N ( x ) = f ( x ) − S N ( x ) R_N(x) = f(x) - S_N(x) R N ( x ) = f ( x ) − S N ( x )
误差项 R N 2 ( x ) R_N^2(x) R N 2 ( x ) 为:
∫ − π π R N 2 ( x ) d x = ∫ − π π ( f ( x ) − S N ( x ) ) 2 d x = ∫ − π π f 2 ( x ) − 2 f ( x ) S N ( x ) + S N 2 ( x ) d x = ∫ − π π f 2 ( x ) d x − 2 ( 2 π ⋅ a 0 2 4 + π ⋅ ∑ n = 1 N a n 2 + b n 2 ) + ( 2 π ⋅ a 0 2 4 + π ⋅ ∑ n = 1 N a n 2 + b n 2 ) = ∫ − π π f 2 ( x ) d x − ( 2 π ⋅ a 0 2 4 + π ⋅ ∑ n = 1 N a n 2 + b n 2 ) = ∫ − π π f 2 ( x ) d x − π ( a 0 2 2 + ∑ n = 1 N a n 2 + b n 2 ) = π ⋅ ∑ n = N + 1 ∞ a n 2 + b n 2 ⩾ 0 \begin{align}
\int_{-\pi}^{\pi} R_N^2(x) \, \text dx
&= \int_{-\pi}^{\pi} (f(x) - S_N(x))^2 \, \text dx\\
&=\int_{-\pi}^{\pi} f^2(x)-2f(x)S_N(x)+S^2_N(x) \, \text dx \\
&=\int_{-\pi}^{\pi} f^2(x) \, \text dx -2 \left ( \frac{2 \pi \cdot a^2_0}{4} + \pi \cdot \sum_{n=1}^{N} a^2_n + b^2_n \right ) +
\left ( \frac{2\pi \cdot a^2_0}{4} + \pi \cdot \sum_{n=1}^{N} a^2_n + b^2_n \right ) \\
&=\int_{-\pi}^{\pi} f^2(x) \, \text dx - \left ( \frac{2\pi \cdot a^2_0}{4} + \pi \cdot \sum_{n=1}^{N} a^2_n + b^2_n \right ) \\
&=\int_{-\pi}^{\pi} f^2(x) \, \text dx - \pi \left ( \frac{ a^2_0}{2} + \sum_{n=1}^{N} a^2_n + b^2_n \right ) \\
&=\pi \cdot \sum_{n=N+1}^{\infty} a^2_n + b^2_n \geqslant 0
\end{align} ∫ − π π R N 2 ( x ) d x = ∫ − π π ( f ( x ) − S N ( x ) ) 2 d x = ∫ − π π f 2 ( x ) − 2 f ( x ) S N ( x ) + S N 2 ( x ) d x = ∫ − π π f 2 ( x ) d x − 2 ( 4 2 π ⋅ a 0 2 + π ⋅ n = 1 ∑ N a n 2 + b n 2 ) + ( 4 2 π ⋅ a 0 2 + π ⋅ n = 1 ∑ N a n 2 + b n 2 ) = ∫ − π π f 2 ( x ) d x − ( 4 2 π ⋅ a 0 2 + π ⋅ n = 1 ∑ N a n 2 + b n 2 ) = ∫ − π π f 2 ( x ) d x − π ( 2 a 0 2 + n = 1 ∑ N a n 2 + b n 2 ) = π ⋅ n = N + 1 ∑ ∞ a n 2 + b n 2 ⩾ 0
因为贝塞尔不等式成立 如果 f ( x ) f(x) f ( x ) 可积 → lim n → ∞ a n , b n = 0 , 0 \to \lim_{n \to \infty}a_n,b_n = 0,0 → lim n → ∞ a n , b n = 0 , 0
黎曼引理的另一种证明方式
lim n → + ∞ ∫ 0 π f ( x ) sin ( n + 1 2 ) x d x = 0 ⇓ lim n → + ∞ ∫ 0 π f ( x ) sin n x d x = 0 ⇓ 把积分补齐 ∫ 0 π → ∫ − π π lim n → + ∞ ∫ − π π f ( x ) sin n x d x = lim n → + ∞ b n = 0 \begin{align*}
\lim_{n \to +\infty} \int_0^\pi f(x) \sin \left(n + \frac{1}{2}\right) x \, \mathrm{d}x &= 0 \\
\Downarrow \\
\lim_{n \to +\infty} \int_0^\pi f(x) \sin nx \, \mathrm{d}x &= 0 \\
\Downarrow &\quad \text{把积分补齐 } \int_0^\pi \to \int_{-\pi}^\pi \\
\lim_{n \to +\infty} \int_{-\pi}^\pi f(x) \sin nx \, \mathrm{d}x &= \lim_{n \to +\infty} b_n = 0
\end{align*}
n → + ∞ lim ∫ 0 π f ( x ) sin ( n + 2 1 ) x d x ⇓ n → + ∞ lim ∫ 0 π f ( x ) sin n x d x ⇓ n → + ∞ lim ∫ − π π f ( x ) sin n x d x = 0 = 0 把积分补齐 ∫ 0 π → ∫ − π π = n → + ∞ lim b n = 0
傅里叶级数收敛性的证明
S N ( x ) = a 0 2 + ∑ n = 1 N ( a n cos n x + b n sin n x ) S_N(x)=\frac{a_{0}}{2}+\sum_{n = 1}^{N}(a_{n}\cos nx + b_{n}\sin nx) S N ( x ) = 2 a 0 + n = 1 ∑ N ( a n cos n x + b n sin n x )
将 a 0 , a n , b n a_0, a_n, b_n a 0 , a n , b n 带入:
a 0 = 1 π ∫ − π π f ( x ) d x , a n = 1 π ∫ − π π f ( x ) cos n x d x , b n = 1 π ∫ − π π f ( x ) sin n x d x a_{0}=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\,\text dx ,\quad a_{n}=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\cos nx\,\text dx, \quad b_{n}=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\sin nx\,\text dx a 0 = π 1 ∫ − π π f ( x ) d x , a n = π 1 ∫ − π π f ( x ) cos n x d x , b n = π 1 ∫ − π π f ( x ) sin n x d x
所以:
S N ( x ) = 1 2 π ∫ − π π f ( x ) d x + ∑ n = 1 N ( 1 π ∫ − π π f ( x ) cos n x d x cos n x + 1 π ∫ − π π f ( x ) sin n x d x sin n x ) S_N(x) = \frac{1}{2\pi}\int_{-\pi}^{\pi}f(x)\,\text dx + \sum_{n = 1}^{N}\left(\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\cos nx\,\text dx\cos nx + \frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\sin nx\,\text dx \sin nx\right) S N ( x ) = 2 π 1 ∫ − π π f ( x ) d x + n = 1 ∑ N ( π 1 ∫ − π π f ( x ) cos n x d x cos n x + π 1 ∫ − π π f ( x ) sin n x d x sin n x )
将积分中的变量用别的字母替代 x → t x \to t x → t :
S N ( x ) = 1 2 π ∫ − π π f ( t ) d t + ∑ n = 1 N ( 1 π ∫ − π π f ( t ) cos n t d t cos n x + 1 π ∫ − π π f ( t ) sin n t d x sin n x ) S_N(x) = \frac{1}{2\pi}\int_{-\pi}^{\pi}f(t)\,\text dt + \sum_{n = 1}^{N }\left( \frac{1}{\pi}\int_{-\pi}^{\pi}f(t)\cos nt\,\text dt\cos nx + \frac{1}{\pi}\int_{-\pi}^{\pi}f(t)\sin nt\,\text dx \sin nx \right) S N ( x ) = 2 π 1 ∫ − π π f ( t ) d t + n = 1 ∑ N ( π 1 ∫ − π π f ( t ) cos n t d t cos n x + π 1 ∫ − π π f ( t ) sin n t d x sin n x )
将 cos n x , sin n x \cos nx, \sin nx cos n x , sin n x 放入积分内:
S N ( x ) = 1 2 π ∫ − π π f ( t ) d t + ∑ n = 1 N ( 1 π ∫ − π π f ( t ) cos n t cos n x d t + 1 π ∫ − π π f ( t ) sin n t sin n x d t ) S_N(x) = \frac{1}{2\pi}\int_{-\pi}^{\pi}f(t)\,\text dt + \sum_{n = 1}^{N }\left( \frac{1}{\pi}\int_{-\pi}^{\pi}f(t)\cos nt \cos nx \ \text dt + \frac{1}{\pi}\int_{-\pi}^{\pi}f(t)\sin nt \sin nx \,\text dt \right) S N ( x ) = 2 π 1 ∫ − π π f ( t ) d t + n = 1 ∑ N ( π 1 ∫ − π π f ( t ) cos n t cos n x d t + π 1 ∫ − π π f ( t ) sin n t sin n x d t )
交换积分与求和顺序:
S N ( x ) = 1 2 π ∫ − π π f ( t ) d t + 1 π ∫ − π π ∑ n = 1 N ( f ( t ) cos n t cos n x + f ( t ) sin n t sin n x ) d t S_N(x) = \frac{1}{2\pi}\int_{-\pi}^{\pi}f(t)\,\text dt + \frac{1}{\pi} \int_{-\pi}^{\pi} \sum_{n = 1}^{N} \left( f(t)\cos nt \cos nx + f(t)\sin nt \sin nx \right) \text dt S N ( x ) = 2 π 1 ∫ − π π f ( t ) d t + π 1 ∫ − π π n = 1 ∑ N ( f ( t ) cos n t cos n x + f ( t ) sin n t sin n x ) d t
合并 f ( t ) f(t) f ( t ) 和 cos n t cos n x \cos nt \cos nx cos n t cos n x , sin n t sin n x \sin nt \sin nx sin n t sin n x :
S N ( x ) = 1 2 π ∫ − π π f ( t ) d t + 1 π ∫ − π π ∑ n = 1 N f ( t ) cos n ( t − x ) d t S_N(x) = \frac{1}{2\pi}\int_{-\pi}^{\pi}f(t)\,\text dt + \frac{1}{\pi} \int_{-\pi}^{\pi} \sum_{n = 1}^{N} f(t) \cos n(t-x) \,\text dt S N ( x ) = 2 π 1 ∫ − π π f ( t ) d t + π 1 ∫ − π π n = 1 ∑ N f ( t ) cos n ( t − x ) d t
将 1 2 π ∫ − π π f ( t ) d t \frac{1}{2\pi}\int_{-\pi}^{\pi}f(t)\,\text dt 2 π 1 ∫ − π π f ( t ) d t 拿进去,f ( x ) f(x) f ( x ) 拿出来:
S N ( x ) = 1 π ∫ − π π f ( t ) ( 1 2 + ∑ n = 1 N cos n ( t − x ) ) d t S_N(x) = \frac{1}{\pi} \int_{-\pi}^{\pi} f(t) \left( \frac{1}{2} + \sum_{n = 1}^{N} \cos n(t-x) \right) \,\text dt S N ( x ) = π 1 ∫ − π π f ( t ) ( 2 1 + n = 1 ∑ N cos n ( t − x ) ) d t
换元,令 t − x = u → t = u + x t-x=u \to t = u+x t − x = u → t = u + x :
S N ( x ) = 1 π ∫ − π − x π − x f ( u + x ) ( 1 2 + ∑ n = 1 N cos n u ) d u S_N(x) = \frac{1}{\pi} \int_{-\pi-x}^{\pi-x} f(u+x) \left( \frac{1}{2} + \sum_{n = 1}^{N} \cos nu \right) \,\text du S N ( x ) = π 1 ∫ − π − x π − x f ( u + x ) ( 2 1 + n = 1 ∑ N cos n u ) d u
因为 { cos n u ∣ n ≥ 1 } \{\cos nu | n \ge 1\} { cos n u ∣ n ≥ 1 } 都是周期函数,所以 ∫ − π − x π − x → ∫ − π π \int_{-\pi-x}^{\pi-x} \to \int_{-\pi}^{\pi} ∫ − π − x π − x → ∫ − π π :
S N ( x ) = 1 π ∫ − π π f ( u + x ) ( 1 2 + ∑ n = 1 N cos n u ) d u S_N(x) = \frac{1}{\pi} \int_{-\pi}^{\pi} f(u+x) \left( \frac{1}{2} + \sum_{n = 1}^{N} \cos nu \right) \,\text du S N ( x ) = π 1 ∫ − π π f ( u + x ) ( 2 1 + n = 1 ∑ N cos n u ) d u
由D N ( x ) D_N(x) D N ( x ) :
S N ( x ) = 1 π ∫ − π π f ( u + x ) sin ( 2 N + 1 ) u 2 2 sin u 2 d u S_N(x) = \frac{1}{\pi} \int_{-\pi}^{\pi} f(u+x) \frac{\sin \frac{(2N+1)u}{2}}{2 \sin \frac{u}{2}} \,\text du S N ( x ) = π 1 ∫ − π π f ( u + x ) 2 sin 2 u sin 2 ( 2 N + 1 ) u d u
现在让 N → + ∞ N \to +\infty N → + ∞ , x = x 0 x = x_0 x = x 0 :
lim N → + ∞ S N ( x 0 ) = lim N → + ∞ 1 π ∫ − π π f ( u + x 0 ) sin ( 2 N + 1 ) u 2 2 sin u 2 d u \lim_{N \to +\infty} S_N(x_0) = \lim_{N \to +\infty} \frac{1}{\pi} \int_{-\pi}^{\pi} f(u+x_0) \frac{\sin \frac{(2N+1)u}{2}}{2 \sin \frac{u}{2}} \,\text du N → + ∞ lim S N ( x 0 ) = N → + ∞ lim π 1 ∫ − π π f ( u + x 0 ) 2 sin 2 u sin 2 ( 2 N + 1 ) u d u
将 ∫ − π π → ∫ − π 0 + ∫ 0 π \int_{-\pi}^{\pi} \to \int_{-\pi}^{0} + \int_{0}^{\pi} ∫ − π π → ∫ − π 0 + ∫ 0 π :
lim N → + ∞ S N ( x 0 ) = lim N → + ∞ 1 π ∫ − π 0 f ( u + x 0 ) sin ( 2 N + 1 ) u 2 2 sin u 2 d u + lim N → + ∞ 1 π ∫ 0 π f ( u + x 0 ) sin ( 2 N + 1 ) u 2 2 sin u 2 d u \lim_{N \to +\infty} S_N(x_0) = \lim_{N \to +\infty} \frac{1}{\pi} \int_{-\pi}^{0} f(u+x_0) \frac{\sin \frac{(2N+1)u}{2}}{2 \sin \frac{u}{2}} \,\text du + \lim_{N \to +\infty} \frac{1}{\pi} \int_{0}^{\pi} f(u+x_0) \frac{\sin \frac{(2N+1)u}{2}}{2 \sin \frac{u}{2}} \,\text du N → + ∞ lim S N ( x 0 ) = N → + ∞ lim π 1 ∫ − π 0 f ( u + x 0 ) 2 sin 2 u sin 2 ( 2 N + 1 ) u d u + N → + ∞ lim π 1 ∫ 0 π f ( u + x 0 ) 2 sin 2 u sin 2 ( 2 N + 1 ) u d u
对 [ − π , 0 ] [-\pi,0] [ − π , 0 ] 令 − u = v -u = v − u = v :
lim N → + ∞ S N ( x 0 ) = lim N → + ∞ 1 π ∫ 0 π f ( − v + x 0 ) sin ( 2 N + 1 ) v 2 2 sin v 2 d v + lim N → + ∞ 1 π ∫ 0 π f ( u + x 0 ) sin ( 2 N + 1 ) u 2 2 sin u 2 d u \lim_{N \to +\infty} S_N(x_0) = \lim_{N \to +\infty} \frac{1}{\pi} \int_{0}^{\pi} f(-v+x_0) \frac{\sin \frac{(2N+1)v}{2}}{2 \sin \frac{v}{2}} \,\text dv + \lim_{N \to +\infty} \frac{1}{\pi} \int_{0}^{\pi} f(u+x_0) \frac{\sin \frac{(2N+1)u}{2}}{2 \sin \frac{u}{2}} \,\text du N → + ∞ lim S N ( x 0 ) = N → + ∞ lim π 1 ∫ 0 π f ( − v + x 0 ) 2 sin 2 v sin 2 ( 2 N + 1 ) v d v + N → + ∞ lim π 1 ∫ 0 π f ( u + x 0 ) 2 sin 2 u sin 2 ( 2 N + 1 ) u d u
合并 u , v → t u, v \to t u , v → t :
lim N → + ∞ S N ( x 0 ) = lim N → + ∞ 1 π ∫ 0 π ( f ( x 0 + t ) + f ( x 0 − t ) ) sin ( 2 N + 1 ) t 2 2 sin t 2 d t \begin{align}
\lim_{N \to +\infty} S_N(x_0) = \lim_{N \to +\infty} \frac{1}{\pi} \int_{0}^{\pi} \left( f(x_0+t) + f(x_0-t) \right) \frac{\sin \frac{(2N+1)t}{2}}{2 \sin \frac{t}{2}} \,\text dt
\end{align} N → + ∞ lim S N ( x 0 ) = N → + ∞ lim π 1 ∫ 0 π ( f ( x 0 + t ) + f ( x 0 − t ) ) 2 sin 2 t sin 2 ( 2 N + 1 ) t d t
同时减去 s = f + ( x 0 ) + f − ( x 0 ) 2 s = \frac{f_+(x_0) + f_-(x_0)}{2} s = 2 f + ( x 0 ) + f − ( x 0 ) ,其中 s s s 是 f ( x 0 ) f(x_0) f ( x 0 ) 的左右极限除以 2:
lim N → + ∞ S N ( x 0 ) − f + ( x 0 ) + f − ( x 0 ) 2 = lim N → + ∞ 1 π ∫ 0 π ( f ( x 0 + t ) + f ( x 0 − t ) − f + ( x 0 ) − f − ( x 0 ) ) sin ( 2 N + 1 ) t 2 2 sin t 2 d t \begin{align}
&\lim_{N \to +\infty} S_N(x_0) - \frac{f_+(x_0) + f_-(x_0)}{2} =\\
&\lim_{N \to +\infty} \frac{1}{\pi} \int_{0}^{\pi} \left( f(x_0+t) + f(x_0-t) - f_+(x_0) - f_-(x_0) \right) \frac{\sin \frac{(2N+1)t}{2}}{2 \sin \frac{t}{2}} \,\text dt
\end{align} N → + ∞ lim S N ( x 0 ) − 2 f + ( x 0 ) + f − ( x 0 ) = N → + ∞ lim π 1 ∫ 0 π ( f ( x 0 + t ) + f ( x 0 − t ) − f + ( x 0 ) − f − ( x 0 ) ) 2 sin 2 t sin 2 ( 2 N + 1 ) t d t
将 ∫ 0 π = ∫ 0 δ + ∫ δ π \int_{0}^{\pi} = \int_{0}^{\delta} + \int_{\delta}^{\pi} ∫ 0 π = ∫ 0 δ + ∫ δ π :
lim δ → 0 + lim N → + ∞ S N ( x 0 ) − s = lim N → + ∞ 1 π ∫ 0 δ f ( x 0 + t ) + f ( x 0 − t ) − f + ( x 0 ) − f − ( x 0 ) sin ( 2 N + 1 ) t 2 2 sin t 2 d t + 1 π ∫ δ π ( f ( x 0 + t ) + f ( x 0 − t ) − 2 s ) 2 sin t 2 sin ( 2 N + 1 ) t 2 d t \begin{align}
\lim_{\delta \to 0^+} \lim_{N \to +\infty} S_N(x_0) - s &=\\
\lim_{N \to +\infty} \frac{1}{\pi} &\int_{0}^{\delta} f(x_0+t) + f(x_0-t) - f_+(x_0) - f_-(x_0) \frac{\sin \frac{(2N+1)t}{2}}{2 \sin \frac{t}{2}} \,\text dt + \\
\frac{1}{\pi} &\int_{\delta}^{\pi} \frac{\left( f(x_0+t) + f(x_0-t) - 2s \right)}{2 \sin \frac{t}{2}} \sin \frac{(2N+1)t}{2} \,\text dt
\end{align} δ → 0 + lim N → + ∞ lim S N ( x 0 ) − s N → + ∞ lim π 1 π 1 = ∫ 0 δ f ( x 0 + t ) + f ( x 0 − t ) − f + ( x 0 ) − f − ( x 0 ) 2 sin 2 t sin 2 ( 2 N + 1 ) t d t + ∫ δ π 2 sin 2 t ( f ( x 0 + t ) + f ( x 0 − t ) − 2 s ) sin 2 ( 2 N + 1 ) t d t
由于 ∫ δ π = 0 \int_{\delta}^{\pi} = 0 ∫ δ π = 0 :
lim δ → 0 + lim N → + ∞ S N ( x 0 ) − s = lim δ → 0 + lim N → + ∞ 1 π ∫ 0 δ ( f ( x 0 + t ) − f + ( x 0 ) + f ( x 0 − t ) − f − ( x 0 ) ) t sin ( n + 1 2 ) t d t \begin{align}
&\lim_{\delta \to 0^+} \lim_{N \to +\infty} S_N(x_0) - s = \\
&\lim_{\delta \to 0^+} \lim_{N \to +\infty} \frac{1}{\pi} \int_{0}^{\delta} \frac{\left( f(x_0+t) - f_+(x_0) + f(x_0-t) - f_-(x_0) \right)}{t} \sin \left( n + \frac{1}{2} \right) t \,\text dt
\end{align} δ → 0 + lim N → + ∞ lim S N ( x 0 ) − s = δ → 0 + lim N → + ∞ lim π 1 ∫ 0 δ t ( f ( x 0 + t ) − f + ( x 0 ) + f ( x 0 − t ) − f − ( x 0 ) ) sin ( n + 2 1 ) t d t
由于 f ( x ) f(x) f ( x ) 可积
或者说 ∫ 0 π f ( x 0 + t ) − f + ( x 0 ) + f ( x 0 − t ) − f − ( x 0 ) t d t \int_{0}^{\pi} \frac{f(x_0+t) - f_+(x_0) + f(x_0-t) - f_-(x_0)}{t} \,\text dt ∫ 0 π t f ( x 0 + t ) − f + ( x 0 ) + f ( x 0 − t ) − f − ( x 0 ) d t 收敛
或者 lim δ → 0 + ∫ 0 δ f ( x 0 + t ) − f + ( x 0 ) + f ( x 0 − t ) − f − ( x 0 ) t d t = 0 \lim_{\delta \to 0^+} \int_{0}^{\delta} \frac{f(x_0+t) - f_+(x_0) + f(x_0-t) - f_-(x_0)}{t} \,\text dt = 0 lim δ → 0 + ∫ 0 δ t f ( x 0 + t ) − f + ( x 0 ) + f ( x 0 − t ) − f − ( x 0 ) d t = 0 :
lim N → + ∞ S N ( x 0 ) − s = 0 \lim_{N \to +\infty} S_N(x_0) - s = 0 N → + ∞ lim S N ( x 0 ) − s = 0
最终得出结论:
lim N → + ∞ S N ( x 0 ) = f + ( x 0 ) + f − ( x 0 ) 2 \lim_{N \to +\infty} S_N(x_0) = \frac{f_+(x_0) + f_-(x_0)}{2} N → + ∞ lim S N ( x 0 ) = 2 f + ( x 0 ) + f − ( x 0 )
欧拉公式
e i θ = cos θ + i sin θ e^{i\theta}=\cos\theta + i\sin\theta e i θ = cos θ + i sin θ
根据欧拉公式 e i θ = cos θ + i sin θ e^{i\theta}=\cos\theta + i\sin\theta e i θ = cos θ + i sin θ ,我们可以对其进行变形来得到 cos θ \cos\theta cos θ 和 sin θ \sin\theta sin θ 的表达式。
已知 e i θ = cos θ + i sin θ e^{i\theta}=\cos\theta + i\sin\theta e i θ = cos θ + i sin θ ,同时 e − i θ = cos ( − θ ) + i sin ( − θ ) = cos θ − i sin θ e^{-i\theta}=\cos(-\theta)+i\sin(-\theta)=\cos\theta - i\sin\theta e − i θ = cos ( − θ ) + i sin ( − θ ) = cos θ − i sin θ
cos θ = e i θ + e − i θ 2 sin θ = e i θ − e − i θ 2 i cos n θ = e i n θ + e − i n θ 2 sin n θ = e i n θ − e − i n θ 2 i \begin{align}
\cos\theta&=\frac{e^{i\theta}+e^{-i\theta}}{2} \\
\sin\theta&=\frac{e^{i\theta}-e^{-i\theta}}{2i} \\
\cos n\theta&=\frac{e^{in\theta}+e^{-in\theta}}{2} \\
\sin n\theta&=\frac{e^{in\theta}-e^{-in\theta}}{2i}
\end{align} cos θ sin θ cos n θ sin n θ = 2 e i θ + e − i θ = 2 i e i θ − e − i θ = 2 e in θ + e − in θ = 2 i e in θ − e − in θ
傅里叶级数的复数表示
将 cos n θ , sin n θ \cos n\theta, \sin n\theta cos n θ , sin n θ 带入傅里叶级数:
f ( θ ) = a 0 2 + ∑ n = 1 ∞ ( a n e i n θ + e − i n θ 2 + b n e i n θ − e − i n θ 2 i ) f(\theta) = \frac{a_0}{2} + \sum_{n=1}^{\infty} \left( a_n \frac{e^{in\theta}+e^{-in\theta}}{2} + b_n \frac{e^{in\theta}-e^{-in\theta}}{2i} \right) f ( θ ) = 2 a 0 + n = 1 ∑ ∞ ( a n 2 e in θ + e − in θ + b n 2 i e in θ − e − in θ )
合并 e i n θ , e − i n θ e^{in\theta}, e^{-in\theta} e in θ , e − in θ 的系数 :
f ( θ ) = a 0 2 + ∑ n = 1 ∞ ( a n − i b n 2 e i n θ + a n + i b n 2 e − i n θ ) f(\theta) = \frac{a_0}{2} + \sum_{n=1}^{\infty} \left( \frac{a_n-ib_n}{2} e^{in\theta} + \frac{a_n+ib_n}{2} e^{-in\theta} \right) f ( θ ) = 2 a 0 + n = 1 ∑ ∞ ( 2 a n − i b n e in θ + 2 a n + i b n e − in θ )
合并 ∑ \sum ∑ :
f ( θ ) = ∑ n = − ∞ + ∞ a n − i b n 2 e i n θ f(\theta) = \sum_{n=-\infty}^{+\infty} \frac{a_n-ib_n}{2} e^{in\theta} f ( θ ) = n = − ∞ ∑ + ∞ 2 a n − i b n e in θ
c n c_n c n 的复数表示:
c n = a n − i b n 2 = 1 2 π ∫ − π π f ( x ) ( cos n x − i sin n x ) d x = 1 2 π ∫ − π π f ( x ) e − i n x d x c_n = \frac{a_n-ib_n}{2}= \frac{1}{2\pi} \int_{-\pi}^{\pi} f(x) (\cos nx-i\sin nx) \,\text{d}x = \frac{1}{2\pi} \int_{-\pi}^{\pi} f(x) e^{-inx} \,\text{d}x c n = 2 a n − i b n = 2 π 1 ∫ − π π f ( x ) ( cos n x − i sin n x ) d x = 2 π 1 ∫ − π π f ( x ) e − in x d x
傅里叶级数的复数表示:
f ( θ ) = ∑ n = − ∞ + ∞ c n e i n θ f(\theta) = \sum_{n=-\infty}^{+\infty} c_n e^{in\theta} f ( θ ) = n = − ∞ ∑ + ∞ c n e in θ
周期为T的傅里叶级数的复数表示
f ( t ) = ∑ n = − ∞ ∞ c n e i 2 π T n t \begin{align}
f(t) &= \sum_{n=-\infty}^{\infty} c_n e^{i \frac{2\pi}{T} n t} \\
\end{align} f ( t ) = n = − ∞ ∑ ∞ c n e i T 2 π n t
求 a n , b n a_n, b_n a n , b n :
f ( t ) = a 0 2 + ∑ n = 1 N ( a n cos 2 π T n t + b n sin 2 π T n t ) ∫ 0 T f ( t ) cos 2 π T n t d t = a n ∫ 0 T cos 2 2 π T n t d t = a n T 2 a n = 2 T ∫ 0 T f ( t ) cos 2 π T n t d t \begin{align}
f(t)&=\frac{a_{0}}{2}+\sum_{n = 1}^{N}(a_{n}\cos \frac{2\pi}{T} nt + b_{n}\sin \frac{2\pi}{T} nt) \\
\int_0^{T} f(t) \cos \frac{2\pi}{T} nt \,\text{d}t &= a_n\int_0^{T} \cos^2 \frac{2\pi}{T} nt \,\text{d}t =a_n \frac{T}{2} \\
a_n &= \frac{2}{T} \int_0^{T} f(t) \cos \frac{2\pi}{T} nt \,\text{d}t
\end{align} f ( t ) ∫ 0 T f ( t ) cos T 2 π n t d t a n = 2 a 0 + n = 1 ∑ N ( a n cos T 2 π n t + b n sin T 2 π n t ) = a n ∫ 0 T cos 2 T 2 π n t d t = a n 2 T = T 2 ∫ 0 T f ( t ) cos T 2 π n t d t
c n c_n c n 的复数表示:
c n = a n − i b n 2 = 1 T ∫ 0 T f ( t ) ( cos 2 π T n t − i sin 2 π T n t ) d t = 1 T ∫ 0 T f ( t ) e − i 2 π T n t d t c_n = \frac{a_n-ib_n}{2}= \frac{1}{T} \int_{0}^{T} f(t) (\cos \frac{2\pi}{T} nt - i\sin \frac{2\pi}{T} nt) \,\text{d}t = \frac{1}{T} \int_{0}^{T} f(t) e^{-i \frac{2\pi}{T} nt} \,\text{d}t c n = 2 a n − i b n = T 1 ∫ 0 T f ( t ) ( cos T 2 π n t − i sin T 2 π n t ) d t = T 1 ∫ 0 T f ( t ) e − i T 2 π n t d t
e 形式的正交基
要判断一组复指数函数是否正交,我们可以考虑以下形式的函数:
e i 2 π T n t e^{i\frac{2\pi}{T} nt} e i T 2 π n t
其中 n n n 和 m m m 是不同的整数。为了判断 e i 2 π T n t e^{i\frac{2\pi}{T} nt} e i T 2 π n t 和 e i 2 π T m t e^{i\frac{2\pi}{T} mt} e i T 2 π m t 的正交性,我们需要计算它们在一个周期 T T T 内的内积:
∫ 0 T e i 2 π T n t ⋅ e i 2 π T m t ‾ d t = ∫ 0 T e i 2 π T ( n − m ) t d t \int_0^T e^{i\frac{2\pi}{T} nt} \cdot \overline{e^{i\frac{2\pi}{T} mt}} \, dt = \int_0^T e^{i\frac{2\pi}{T} (n-m)t} \, dt ∫ 0 T e i T 2 π n t ⋅ e i T 2 π m t d t = ∫ 0 T e i T 2 π ( n − m ) t d t
计算这个积分:
当 n = m n = m n = m 时:
∫ 0 T e i 2 π T ( n − m ) t d t = ∫ 0 T 1 d t = T \int_0^T e^{i\frac{2\pi}{T} (n-m)t} \, dt = \int_0^T 1 \, dt = T ∫ 0 T e i T 2 π ( n − m ) t d t = ∫ 0 T 1 d t = T
当 n ≠ m n \neq m n = m 时:
∫ 0 T e i 2 π T ( n − m ) t d t = [ 1 i 2 π T ( n − m ) e i 2 π T ( n − m ) t ] 0 T = 1 i 2 π T ( n − m ) ( e i 2 π ( n − m ) − 1 ) = 1 i 2 π T ( n − m ) ( 1 − 1 ) = 0 \begin{align}
\int_0^T e^{i\frac{2\pi}{T} (n-m)t} \, dt &= \left[ \frac{1}{i\frac{2\pi}{T} (n-m)} e^{i\frac{2\pi}{T} (n-m)t} \right]_0^T \\
&= \frac{1}{i\frac{2\pi}{T} (n-m)} \left( e^{i2\pi(n-m)} - 1 \right) \\
&= \frac{1}{i\frac{2\pi}{T} (n-m)} (1 - 1) = 0
\end{align} ∫ 0 T e i T 2 π ( n − m ) t d t = [ i T 2 π ( n − m ) 1 e i T 2 π ( n − m ) t ] 0 T = i T 2 π ( n − m ) 1 ( e i 2 π ( n − m ) − 1 ) = i T 2 π ( n − m ) 1 ( 1 − 1 ) = 0
连续傅里叶变换
从离散到连续,我们考虑 f ( t ) f(t) f ( t ) 是一个非周期信号,即周期 T → ∞ T \to \infty T → ∞ 。在这种情况下,傅里叶级数变成傅里叶变换。具体做法是将离散的频率分量变为连续的频率分量。
频率分量的连续化
为了将傅里叶级数的频率分量从离散变为连续,我们用 ω = n ω 0 \omega = n \omega_0 ω = n ω 0 替换 n n n 。注意到 ω 0 = 2 π T \omega_0 = \frac{2\pi}{T} ω 0 = T 2 π ,当 T → ∞ T \to \infty T → ∞ 时,ω 0 → 0 \omega_0 \to 0 ω 0 → 0 ,频率分量变得连续。
所以,我们有:
ω = n ω 0 = n 2 π T \omega = n \omega_0 = n \frac{2\pi}{T} ω = n ω 0 = n T 2 π
这时,傅里叶系数 c n c_n c n 可以表示为:
c n = 1 T ∫ 0 T f ( t ) e − i n ω 0 t d t c_n = \frac{1}{T} \int_{0}^{T} f(t) e^{-i n \omega_0 t} \, dt c n = T 1 ∫ 0 T f ( t ) e − in ω 0 t d t
由于 ω = n ω 0 \omega = n \omega_0 ω = n ω 0 ,我们可以改写为:
c ( 2 π n T ) = c ( ω ) = 1 T ∫ 0 T f ( t ) e − i ω t d t c \left(\frac{2\pi n}{T} \right) = c(\omega) = \frac{1}{T} \int_{0}^{T} f(t) e^{-i \omega t} \, dt c ( T 2 πn ) = c ( ω ) = T 1 ∫ 0 T f ( t ) e − iω t d t
极限过程
当 T → ∞ T \to \infty T → ∞ 时,ω 0 → 0 \omega_0 \to 0 ω 0 → 0 ,频率分量变得连续。我们可以将傅里叶系数 c n c_n c n 变为傅里叶变换中的连续频谱 F ( ω ) F(\omega) F ( ω ) 。
考虑到 ω \omega ω 是连续的,我们需要引入一个密度函数来替代 c n c_n c n 。傅里叶系数 c n c_n c n 的表达式变为:
c ( ω ) = 1 T ∫ − T 2 T 2 f ( t ) e − i ω t d t c(\omega) = \frac{1}{T} \int_{-\frac{T}{2}}^{\frac{T}{2}} f(t) e^{-i \omega t} \, dt c ( ω ) = T 1 ∫ − 2 T 2 T f ( t ) e − iω t d t
当 T → ∞ T \to \infty T → ∞ 时,这个积分的上下限变为 − ∞ -\infty − ∞ 到 ∞ \infty ∞ ,所以我们得到傅里叶变换的公式:
F ( ω ) = ∫ − ∞ ∞ f ( t ) e − i ω t d t F(\omega) = \int_{-\infty}^{\infty} f(t) e^{-i \omega t} \, dt F ( ω ) = ∫ − ∞ ∞ f ( t ) e − iω t d t
连续傅里叶逆变换
傅里叶级数的逆变换公式为:
f ( t ) = ∑ n = − ∞ ∞ c n e i n ω 0 t f(t) = \sum_{n=-\infty}^{\infty} c_n e^{i n \omega_0 t} f ( t ) = n = − ∞ ∑ ∞ c n e in ω 0 t
当 T → ∞ T \to \infty T → ∞ 时,离散的和 (sum) 变为积分 (integral),并且 c n c_n c n 变为 F ( ω ) F(\omega) F ( ω ) :
f ( t ) = 1 2 π ∑ n = − ∞ ∞ 2 π T F ( ω ) e i ω t f ( t ) = 1 2 π ∫ − ∞ ∞ F ( ω ) e i ω t d ω \begin{align}
f(t) &= \frac{1}{2\pi} \sum_{n=-\infty}^{\infty} \frac{2\pi}{T} F(\omega) e^{i \omega t} \\
f(t) &= \frac{1}{2\pi} \int_{-\infty}^{\infty} F(\omega) e^{i \omega t} \, d\omega
\end{align} f ( t ) f ( t ) = 2 π 1 n = − ∞ ∑ ∞ T 2 π F ( ω ) e iω t = 2 π 1 ∫ − ∞ ∞ F ( ω ) e iω t d ω
通过将傅里叶级数的周期 T T T 取到无限大,我们可以将离散的频率分量变为连续的频率分量,从而推导出傅里叶变换。这展示了傅里叶级数和傅里叶变换在处理周期性和非周期性信号时的内在联系。
离散傅里叶变换
对于周期信号 x [ n ] x[n] x [ n ] ,我们可以使用傅里叶级数来表示它:
x [ n ] = ∑ k = 0 N − 1 X [ k ] ⋅ e i 2 π N k n x[n] = \sum_{k=0}^{N-1} X[k] \cdot e^{i \frac{2\pi}{N} kn} x [ n ] = k = 0 ∑ N − 1 X [ k ] ⋅ e i N 2 π kn
其中 X [ k ] X[k] X [ k ] 是傅里叶系数。
复指数函数的正交性
∑ n = 0 N − 1 e i 2 π N ( m − k ) n = { N , if m = k 0 , if m ≠ k \begin{align}
\sum_{n=0}^{N-1} e^{i \frac{2\pi}{N} (m-k)n} = \begin{cases} N, & \text{if } m = k \\ 0, & \text{if } m \ne k \end{cases}
\end{align} n = 0 ∑ N − 1 e i N 2 π ( m − k ) n = { N , 0 , if m = k if m = k
离散傅里叶变换系数
为了找到傅里叶变换系数 X [ k ] X[k] X [ k ] ,我们将上述傅里叶级数公式两边乘以 e − i 2 π N k n e^{-i \frac{2\pi}{N} kn} e − i N 2 π kn 并对 n n n 求和:
∑ n = 0 N − 1 x [ n ] ⋅ e − i 2 π N k n = ∑ n = 0 N − 1 ( ∑ m = 0 N − 1 X [ m ] ⋅ e i 2 π N m n ) ⋅ e − i 2 π N k n \sum_{n=0}^{N-1} x[n] \cdot e^{-i \frac{2\pi}{N} kn} = \sum_{n=0}^{N-1} \left( \sum_{m=0}^{N-1} X[m] \cdot e^{i \frac{2\pi}{N} mn} \right) \cdot e^{-i \frac{2\pi}{N} kn} n = 0 ∑ N − 1 x [ n ] ⋅ e − i N 2 π kn = n = 0 ∑ N − 1 ( m = 0 ∑ N − 1 X [ m ] ⋅ e i N 2 π mn ) ⋅ e − i N 2 π kn
交换求和次序:
∑ n = 0 N − 1 x [ n ] ⋅ e − i 2 π N k n = ∑ m = 0 N − 1 X [ m ] ( ∑ n = 0 N − 1 e i 2 π N ( m − k ) n ) \sum_{n=0}^{N-1} x[n] \cdot e^{-i \frac{2\pi}{N} kn} = \sum_{m=0}^{N-1} X[m] \left( \sum_{n=0}^{N-1} e^{i \frac{2\pi}{N} (m-k)n} \right) n = 0 ∑ N − 1 x [ n ] ⋅ e − i N 2 π kn = m = 0 ∑ N − 1 X [ m ] ( n = 0 ∑ N − 1 e i N 2 π ( m − k ) n )
这样 k = 0 , 1 , 2 , 3 ⋯ k=0,1,2,3 \cdots k = 0 , 1 , 2 , 3 ⋯ 代入上式
∑ n = 0 N − 1 x [ n ] ⋅ e − i 2 π N k n = X [ k ] ⋅ N \sum_{n=0}^{N-1} x[n] \cdot e^{-i \frac{2\pi}{N} kn} = X[k] \cdot N n = 0 ∑ N − 1 x [ n ] ⋅ e − i N 2 π kn = X [ k ] ⋅ N
离散傅里叶逆变换
DFT:
X [ k ] = ∑ n = 0 N − 1 x [ n ] ⋅ e − i 2 π N k n X[k] = \sum_{n=0}^{N-1} x[n] \cdot e^{-i \frac{2\pi}{N} kn} X [ k ] = ∑ n = 0 N − 1 x [ n ] ⋅ e − i N 2 π kn
IDFT:
x [ n ] = 1 N ∑ k = 0 N − 1 X [ k ] ⋅ e i 2 π N k n x[n] = \frac{1}{N} \sum_{k=0}^{N-1} X[k] \cdot e^{i \frac{2\pi}{N} kn} x [ n ] = N 1 ∑ k = 0 N − 1 X [ k ] ⋅ e i N 2 π kn
离散傅里叶变换可以用矩阵形式来表示,这种表示方式在计算机实现中非常有用,因为它将DFT视为矩阵与向量的乘积。
离散傅里叶变换矩阵
假设我们有一个长度为 N N N 的离散序列 x = [ x [ 0 ] , x [ 1 ] , … , x [ N − 1 ] ] T \mathbf{x} = [x[0], x[1], \ldots, x[N-1]]^T x = [ x [ 0 ] , x [ 1 ] , … , x [ N − 1 ] ] T 。DFT 可以表示为矩阵乘法:
X = W N ⋅ x \mathbf{X} = \mathbf{W}_N \cdot \mathbf{x} X = W N ⋅ x
其中:
X = [ X [ 0 ] , X [ 1 ] , … , X [ N − 1 ] ] T \mathbf{X} = [X[0], X[1], \ldots, X[N-1]]^T X = [ X [ 0 ] , X [ 1 ] , … , X [ N − 1 ] ] T 是频域表示的结果向量。
W N \mathbf{W}_N W N 是 N × N N \times N N × N 的DFT矩阵。
DFT 矩阵 W N \mathbf{W}_N W N 的元素由以下公式给出:
W N [ k , n ] = e − i 2 π N k n W_{N}[k, n] = e^{-i \frac{2\pi}{N} kn} W N [ k , n ] = e − i N 2 π kn
其中 k , n = 0 , 1 , 2 , … , N − 1 k, n = 0, 1, 2, \ldots, N-1 k , n = 0 , 1 , 2 , … , N − 1 。具体来说,矩阵 W N \mathbf{W}_N W N 的第 k k k 行,第 n n n 列的元素是:
W N [ k , n ] = cos ( 2 π N k n ) − i sin ( 2 π N k n ) W_{N}[k, n] = \cos\left(\frac{2\pi}{N} kn\right) - i \sin\left(\frac{2\pi}{N} kn\right) W N [ k , n ] = cos ( N 2 π kn ) − i sin ( N 2 π kn )
由此可知当n为偶数时, W N [ k , n ] = W N [ k + N 2 , n ] W_{N}[k, n]=W_N[k+\frac{N}{2},n] W N [ k , n ] = W N [ k + 2 N , n ]
矩阵形式的DFT计算
将信号 x \mathbf{x} x 与 DFT 矩阵 W N \mathbf{W}_N W N 相乘,即可得到频域表示 X \mathbf{X} X :
X = [ 1 1 1 ⋯ 1 1 W W 2 ⋯ W N − 1 1 W 2 W 4 ⋯ W 2 ( N − 1 ) ⋮ ⋮ ⋮ ⋱ ⋮ 1 W N − 1 W 2 ( N − 1 ) ⋯ W ( N − 1 ) ( N − 1 ) ] [ x [ 0 ] x [ 1 ] x [ 2 ] ⋮ x [ N − 1 ] ] \mathbf{X} = \begin{bmatrix}
1 & 1 & 1 & \cdots & 1 \\
1 & W & W^2 & \cdots & W^{N-1} \\
1 & W^2 & W^4 & \cdots & W^{2(N-1)} \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & W^{N-1} & W^{2(N-1)} & \cdots & W^{(N-1)(N-1)}
\end{bmatrix}
\begin{bmatrix}
x[0] \\
x[1] \\
x[2] \\
\vdots \\
x[N-1]
\end{bmatrix} X = 1 1 1 ⋮ 1 1 W W 2 ⋮ W N − 1 1 W 2 W 4 ⋮ W 2 ( N − 1 ) ⋯ ⋯ ⋯ ⋱ ⋯ 1 W N − 1 W 2 ( N − 1 ) ⋮ W ( N − 1 ) ( N − 1 ) x [ 0 ] x [ 1 ] x [ 2 ] ⋮ x [ N − 1 ]
其中 W = e − i 2 π N W = e^{-i \frac{2\pi}{N}} W = e − i N 2 π 是单位根。
逆离散傅里叶变换矩阵
逆离散傅里叶变换也可以用矩阵形式表示,其矩阵 W N ∗ \mathbf{W}_N^* W N ∗ 是DFT矩阵的共轭转置,并且需要乘以 1 N \frac{1}{N} N 1 :
x = 1 N W N ∗ ⋅ X \mathbf{x} = \frac{1}{N} \mathbf{W}_N^* \cdot \mathbf{X} x = N 1 W N ∗ ⋅ X
其中 W N ∗ \mathbf{W}_N^* W N ∗ 的元素为:
W N ∗ [ k , n ] = e i 2 π N k n W_{N}^*[k, n] = e^{i \frac{2\pi}{N} kn} W N ∗ [ k , n ] = e i N 2 π kn
快速傅里叶变换
快速傅里叶变换是一种 求解离散傅里叶变换的 算法
离散傅里叶变换
X [ k ] = ∑ n = 0 N − 1 x [ n ] ⋅ e − i 2 π N k n X[k] = \sum_{n=0}^{N-1} x[n] \cdot e^{-i \frac{2\pi}{N} kn} X [ k ] = n = 0 ∑ N − 1 x [ n ] ⋅ e − i N 2 π kn
利用 e − i 2 π N k n e^{−i\frac{2π}{N}kn} e − i N 2 π kn 的周期性,通过观察可以发现,当n n n 为偶数时(我们用2 n 2n 2 n 表示)
e − i 2 π N k 2 n = e − i 2 π N 2 k n = e − i 2 π N 2 ( k + N 2 ) n e^{−i\frac{2π}{N}k\ 2n}=e^{−i\frac{2π}{\frac{N}{2}}kn}=e^{−i\frac{2π}{\frac{N}{2}}(k+\frac{N}{2})n} e − i N 2 π k 2 n = e − i 2 N 2 π kn = e − i 2 N 2 π ( k + 2 N ) n
由此可知当n为偶数时
W N [ k , n ] = W N [ k + N 2 , n ] W_{N}[k, n]=W_N[k+\frac{N}{2},n] W N [ k , n ] = W N [ k + 2 N , n ]
当n n n 为奇数时,我们可以提取一个 e − i 2 π N k e^{−i\frac{2π}{N}k} e − i N 2 π k 公因数,让剩下的 W N [ k , n ] W_{N}[k, n] W N [ k , n ] 满足n n n 为偶数的条件。
将 X [ k ] X[k] X [ k ] 分为 n n n 为偶数以及 n n n 为奇数两个部分。
X [ k ] = ∑ n = 0 N 2 − 1 x [ 2 n ] e − i 2 π N k 2 n + ∑ n = 0 N 2 − 1 x [ 2 n + 1 ] e − i 2 π N k ( 2 n + 1 ) = ∑ n = 0 N 2 − 1 x [ 2 n ] e − i 2 π N k 2 n + e i 2 π N k ∑ n = 0 N 2 − 1 x [ 2 n + 1 ] e − i 2 π N k 2 n = ∑ n = 0 N 2 − 1 x [ 2 n ] e − i 2 π N 2 k n + e − i 2 π N k ∑ n = 0 N 2 − 1 x [ 2 n + 1 ] e − i 2 π N 2 k n = ∑ n = 0 N 2 − 1 x [ 2 n ] W N [ k , 2 n ] + e − i 2 π N k ∑ n = 0 N 2 − 1 x [ 2 n + 1 ] W N [ k , 2 n ] = ∑ n = 0 N 2 − 1 x [ 2 n ] W N 2 [ k , n ] + e − i 2 π N k ∑ n = 0 N 2 − 1 x [ 2 n + 1 ] W N 2 [ k , n ] \begin{align}
X[k]&=\sum_{n=0}^{\frac{N}{2}−1}x[2n]e^{-i \frac{2\pi}{N} k2n} + \sum_{n=0}^{\frac{N}{2}−1}x[2n+1]e^{-i \frac{2\pi}{N} k(2n+1)} \\
&=\sum_{n=0}^{\frac{N}{2}−1}x[2n]e^{-i \frac{2\pi}{N} k2n}+e^{i \frac{2\pi}{N} k}\sum_{n=0}^{\frac{N}{2}−1}x[2n+1]e^{-i \frac{2\pi}{N} k2n} \\
&=\sum_{n=0}^{\frac{N}{2}−1}x[2n]e^{-i \frac{2\pi}{\frac{N}{2}} kn}+e^{-i \frac{2\pi}{N} k}\sum_{n=0}^{\frac{N}{2}−1}x[2n+1]e^{-i \frac{2\pi}{\frac{N}{2}} kn} \\
&=\sum_{n=0}^{\frac{N}{2}−1}x[2n]W_{N}[k, 2n]+e^{-i \frac{2\pi}{N} k}\sum_{n=0}^{\frac{N}{2}−1}x[2n+1]W_{N}[k, 2n] \\
&=\sum_{n=0}^{\frac{N}{2}−1}x[2n]W_{\frac{N}{2}}[k, n]+e^{-i \frac{2\pi}{N} k}\sum_{n=0}^{\frac{N}{2}−1}x[2n+1]W_{\frac{N}{2}}[k, n] \\
\end{align} X [ k ] = n = 0 ∑ 2 N − 1 x [ 2 n ] e − i N 2 π k 2 n + n = 0 ∑ 2 N − 1 x [ 2 n + 1 ] e − i N 2 π k ( 2 n + 1 ) = n = 0 ∑ 2 N − 1 x [ 2 n ] e − i N 2 π k 2 n + e i N 2 π k n = 0 ∑ 2 N − 1 x [ 2 n + 1 ] e − i N 2 π k 2 n = n = 0 ∑ 2 N − 1 x [ 2 n ] e − i 2 N 2 π kn + e − i N 2 π k n = 0 ∑ 2 N − 1 x [ 2 n + 1 ] e − i 2 N 2 π kn = n = 0 ∑ 2 N − 1 x [ 2 n ] W N [ k , 2 n ] + e − i N 2 π k n = 0 ∑ 2 N − 1 x [ 2 n + 1 ] W N [ k , 2 n ] = n = 0 ∑ 2 N − 1 x [ 2 n ] W 2 N [ k , n ] + e − i N 2 π k n = 0 ∑ 2 N − 1 x [ 2 n + 1 ] W 2 N [ k , n ]
我们令偶数部分为 E [ k ] E[k] E [ k ] ,奇数部分提取公因数后的部分为 O [ k ] O[k] O [ k ] ,则
X [ k ] = E [ k ] + W N [ k , 1 ] O [ k ] X[k]=E[k]+W_N[k,1]O[k] X [ k ] = E [ k ] + W N [ k , 1 ] O [ k ]
对于上式 k < N 2 k \lt \frac{N}{2} k < 2 N 成立,当 N 2 ⩽ k < N \frac{N}{2} \leqslant k \lt N 2 N ⩽ k < N 反转一圈
W N 2 [ k , n ] = W N 2 [ k + N 2 , n ] W_{\frac{N}{2}}[k, n]=W_{\frac{N}{2}}\left[k+\frac{N}{2},n\right] W 2 N [ k , n ] = W 2 N [ k + 2 N , n ]
W N [ k , 1 ] W_N[k,1] W N [ k , 1 ] 反转半圈,相当于取反
W N [ k + N 2 , 1 ] = − W N [ k , 1 ] W_N\left[k+\frac{N}{2},1\right]=-W_N[k,1] W N [ k + 2 N , 1 ] = − W N [ k , 1 ]
快速傅里叶变换公式 k < N 2 k \lt \frac{N}{2} k < 2 N
X [ k ] = E [ k ] + W N [ k , 1 ] O [ k ] X [ k + N 2 ] = E [ k ] − W N [ k , 1 ] O [ k ] \begin{align}
X[k]&=E[k]+W_N[k,1]O[k] \\
X\left[k+\frac{N}{2}\right]&=E[k]−W_N[k,1]O[k]
\end{align} X [ k ] X [ k + 2 N ] = E [ k ] + W N [ k , 1 ] O [ k ] = E [ k ] − W N [ k , 1 ] O [ k ]
代码实现
import cmath
def fft (x ):
N = len (x)
if N <= 1 :
return x
elif N % 2 != 0 :
raise ValueError("Size of x must be a power of 2" )
even = fft(x[0 ::2 ])
odd = fft(x[1 ::2 ])
T = [cmath.exp(-2j * cmath.pi * k / N) * odd[k] for k in range (N // 2 )]
return [even[k] + T[k] for k in range (N // 2 )] + \
[even[k] - T[k] for k in range (N // 2 )]
x = [0 , 1 , 0 , -1 ]
result = fft(x)
for i, val in enumerate (result):
print (f"X[{i} ] = {val} " )
时间复杂度
T ( N ) = 2 T ( N 2 ) + N = 2 ( 2 T ( N 4 ) + N 2 ) + N = 2 ( 2 ( 2 T ( N 8 ) + N 4 ) + N 2 ) + N = 2 log 2 N T ( 1 ) + N log 2 N \begin{align}
T(N)&=2T\left(\frac{N}{2}\right)+N \\
&=2\left(2T\left(\frac{N}{4}\right)+\frac{N}{2}\right)+N \\
&=2\left(2\left(2T\left(\frac{N}{8}\right)+\frac{N}{4}\right)+\frac{N}{2}\right)+N \\
&=2\log_2NT(1)+N\log_2N\\
\end{align} T ( N ) = 2 T ( 2 N ) + N = 2 ( 2 T ( 4 N ) + 2 N ) + N = 2 ( 2 ( 2 T ( 8 N ) + 4 N ) + 2 N ) + N = 2 log 2 NT ( 1 ) + N log 2 N
分解到最后只有一项,只需要进行一次乘法运算即可。所以T ( 1 ) = 1 T(1)=1 T ( 1 ) = 1 。
T ( N ) = N ( log 2 N + 1 ) T(N)=N(\log_2N+1) T ( N ) = N ( log 2 N + 1 )
多项式与快速傅里叶变换
A ( x ) = a p x p + a p − 1 x p − 1 + ⋯ + a 0 B ( x ) = b q x q + b q − 1 x q − 1 + ⋯ + b 0 C ( x ) = A ( x ) B ( x ) = c r x r + c r − 1 x r − 1 + ⋯ + c 0 \begin{align}
A(x) &= a_px^p+a_{p-1}x^{p-1}+\cdots+a_0\\
B(x) &= b_qx^q+b_{q-1}x^{q-1}+\cdots+b_0 \\
C(x) &= A(x) \, B(x)= c_{r}x^{r}+c_{r-1}x^{r-1}+\cdots+c_0\
\end{align} A ( x ) B ( x ) C ( x ) = a p x p + a p − 1 x p − 1 + ⋯ + a 0 = b q x q + b q − 1 x q − 1 + ⋯ + b 0 = A ( x ) B ( x ) = c r x r + c r − 1 x r − 1 + ⋯ + c 0
用时间复杂度 O ( N ⋅ log 2 N ) O(N \cdot \log_2N) O ( N ⋅ log 2 N ) 求解 c r c_r c r 需要三步骤
应用快速傅里叶变换转换到点值表示
对于多项式 A ( x ) = a n x n + a n − 1 x n − 1 + ⋯ + a 0 A(x) = a_nx^n+a_{n-1}x^{n-1}+\cdots+a_0 A ( x ) = a n x n + a n − 1 x n − 1 + ⋯ + a 0 选出大于 p + q p+q p + q 的最小的 2 的幂次 N N N 。
对 A ( x ) , B ( x ) A(x),B(x) A ( x ) , B ( x ) 分别做快速傅里叶变换
[ A [ 0 ] A [ 1 ] A [ 2 ] ⋮ A [ N − 1 ] ] = [ 1 1 1 ⋯ 1 1 W W 2 ⋯ W N − 1 1 W 2 W 4 ⋯ W 2 ( N − 1 ) ⋮ ⋮ ⋮ ⋱ ⋮ 1 W N − 1 W 2 ( N − 1 ) ⋯ W ( N − 1 ) ( N − 1 ) ] [ a 0 a 1 a 2 ⋮ a N − 1 ] \begin{bmatrix}
A[0] \\
A[1] \\
A[2] \\
\vdots \\
A[N-1]
\end{bmatrix} = \begin{bmatrix}
1 & 1 & 1 & \cdots & 1 \\
1 & W & W^2 & \cdots & W^{N-1} \\
1 & W^2 & W^4 & \cdots & W^{2(N-1)} \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & W^{N-1} & W^{2(N-1)} & \cdots & W^{(N-1)(N-1)}
\end{bmatrix}
\begin{bmatrix}
a_0 \\
a_1 \\
a_2 \\
\vdots \\
a_{N-1}
\end{bmatrix} A [ 0 ] A [ 1 ] A [ 2 ] ⋮ A [ N − 1 ] = 1 1 1 ⋮ 1 1 W W 2 ⋮ W N − 1 1 W 2 W 4 ⋮ W 2 ( N − 1 ) ⋯ ⋯ ⋯ ⋱ ⋯ 1 W N − 1 W 2 ( N − 1 ) ⋮ W ( N − 1 ) ( N − 1 ) a 0 a 1 a 2 ⋮ a N − 1
[ B [ 0 ] B [ 1 ] B [ 2 ] ⋮ B [ N − 1 ] ] = [ 1 1 1 ⋯ 1 1 W W 2 ⋯ W N − 1 1 W 2 W 4 ⋯ W 2 ( N − 1 ) ⋮ ⋮ ⋮ ⋱ ⋮ 1 W N − 1 W 2 ( N − 1 ) ⋯ W ( N − 1 ) ( N − 1 ) ] [ b 0 b 1 b 2 ⋮ b N − 1 ] \begin{bmatrix}
B[0] \\
B[1] \\
B[2] \\
\vdots \\
B[N-1]
\end{bmatrix} = \begin{bmatrix}
1 & 1 & 1 & \cdots & 1 \\
1 & W & W^2 & \cdots & W^{N-1} \\
1 & W^2 & W^4 & \cdots & W^{2(N-1)} \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & W^{N-1} & W^{2(N-1)} & \cdots & W^{(N-1)(N-1)}
\end{bmatrix}
\begin{bmatrix}
b_0 \\
b_1 \\
b_2 \\
\vdots \\
b_{N-1}
\end{bmatrix} B [ 0 ] B [ 1 ] B [ 2 ] ⋮ B [ N − 1 ] = 1 1 1 ⋮ 1 1 W W 2 ⋮ W N − 1 1 W 2 W 4 ⋮ W 2 ( N − 1 ) ⋯ ⋯ ⋯ ⋱ ⋯ 1 W N − 1 W 2 ( N − 1 ) ⋮ W ( N − 1 ) ( N − 1 ) b 0 b 1 b 2 ⋮ b N − 1
逐点相乘
C [ n ] = A [ n ] ⋅ B [ n ] C[n] = A[n] \cdot B[n] C [ n ] = A [ n ] ⋅ B [ n ]
逆快速傅里叶转换回系数表示
[ c 0 c 1 c 2 ⋮ c N − 1 ] = 1 N [ 1 1 1 ⋯ 1 1 W − 1 W − 2 ⋯ W − ( N − 1 ) 1 W − 2 W − 4 ⋯ W − 2 ( N − 1 ) ⋮ ⋮ ⋮ ⋱ ⋮ 1 W − ( N − 1 ) W − 2 ( N − 1 ) ⋯ W − ( N − 1 ) ( N − 1 ) ] [ C [ 0 ] C [ 1 ] C [ 2 ] ⋮ C [ N − 1 ] ] \begin{bmatrix}
c_0 \\
c_1 \\
c_2 \\
\vdots \\
c_{N-1}
\end{bmatrix} = \frac{1}{N} \begin{bmatrix}
1 & 1 & 1 & \cdots & 1 \\
1 & W^{-1} & W^{-2} & \cdots & W^{-(N-1)} \\
1 & W^{-2} & W^{-4} & \cdots & W^{-2(N-1)} \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & W^{-(N-1)} & W^{-2(N-1)} & \cdots & W^{-(N-1)(N-1)}
\end{bmatrix}
\begin{bmatrix}
C[0] \\
C[1] \\
C[2] \\
\vdots \\
C[N-1]
\end{bmatrix} c 0 c 1 c 2 ⋮ c N − 1 = N 1 1 1 1 ⋮ 1 1 W − 1 W − 2 ⋮ W − ( N − 1 ) 1 W − 2 W − 4 ⋮ W − 2 ( N − 1 ) ⋯ ⋯ ⋯ ⋱ ⋯ 1 W − ( N − 1 ) W − 2 ( N − 1 ) ⋮ W − ( N − 1 ) ( N − 1 ) C [ 0 ] C [ 1 ] C [ 2 ] ⋮ C [ N − 1 ]
代码实现
import cmath
def fft (x ):
N = len (x)
if N <= 1 :
return x
elif N % 2 != 0 :
raise ValueError("Size of x must be a power of 2" )
even = fft(x[0 ::2 ])
odd = fft(x[1 ::2 ])
T = [cmath.exp(-2j * cmath.pi * k / N) * odd[k] for k in range (N // 2 )]
return [even[k] + T[k] for k in range (N // 2 )] + [
even[k] - T[k] for k in range (N // 2 )
]
def ifft (x ):
N = len (x)
if N <= 1 :
return x
elif N % 2 != 0 :
raise ValueError("Size of x must be a power of 2" )
even = ifft(x[0 ::2 ])
odd = ifft(x[1 ::2 ])
T = [cmath.exp(2j * cmath.pi * k / N) * odd[k] for k in range (N // 2 )]
return [even[k] + T[k] for k in range (N // 2 )] + [
even[k] - T[k] for k in range (N // 2 )
]
def next_power_of_two (x ):
return 1 if x == 0 else 2 ** (x - 1 ).bit_length()
def round_and_convert (C, n, m ):
result = []
for c in C:
rounded_int = float (f"{c.real:.4 f} " )
result.append(rounded_int)
return result[: n + m - 1 ]
def fft_multiply (A, B ):
n = len (A)
m = len (B)
N = next_power_of_two(n + m - 1 )
A.extend([0 ] * (N - n))
B.extend([0 ] * (N - m))
A_fft = fft(A)
B_fft = fft(B)
C_fft = [A_fft[i] * B_fft[i] for i in range (N)]
C = [i / N for i in ifft(C_fft)]
return round_and_convert(C, n, m)
A = [1.5 , 2 , 3 ]
B = [4 , 5.3 , 6 ]
C = fft_multiply(A, B)
print (C)