---
title: Fourier Analysis
tags: 111course
description: Use `{%hackmd theme-dark %}` syntax to include this theme.
toc: true
toc_float: true
toc_collapsed: true
toc_depth: 4
---
# <div id="animation_title">**Fourier Analysis**</div>
:::spoiler <span class="NOselect">參考資料們</span>
[But what is the Fourier Transform? A visual introduction ←3Blue1Brown](https://www.youtube.com/watch?v=spUNpyF58BY)
[傅立葉變換如何理解?美顏和變聲都是什麼原理?李永樂老師告訴你←李永乐老师
](https://www.youtube.com/watch?v=0LuyxzqI3Hk)[Fourier Analysis \[Data-Driven Science and Engineering\]
](https://youtube.com/playlist?list=PLMrJAkhIeNNT_Xh3Oy0Y4LTj0Oxo8GqsC)[The Book](http://databookuw.com/databook.pdf)
:::
## <div id="green_tittle">Fourier Series</div>
### Prat I
+ Inner product in Hilbert space
$\qquad<f,g>=\int_a^bf(x)\bar{g}(x)dx$
+ Fourier Series
$$f(x)=\frac{A_0}{2}+\sum_{k=0}^\infty A_k\cos(kx)+B_k\sin(kx)$$
$\frac{A_0}{2}$ is constant where is zero frenquency.
$f(x)$ is made of combination of Cos and Sin function.
+ Fourier cofficients
$$A_k=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\cos(kx)dx=\frac{1}{\lVert \cos(kx) \rVert^2}<f,\cos(kx)>$$
$$B_k=\frac{1}{\pi}\int_{-\pi}^\pi f(x)\sin(kx)dx=\frac{1}{\lVert \sin(kx) \rVert^2}<f,\sin(kx)>$$
$\overrightarrow{f}=<\overrightarrow{f},\overrightarrow{x}>\frac{\overrightarrow{x}}{\lVert x \rVert^2}+<\overrightarrow{f},\overrightarrow{y}>\frac{\overrightarrow{y}}{\lVert y \rVert^2}$
$=<\overrightarrow{f},\overrightarrow{u}>\frac{\overrightarrow{u}}{\lVert u \rVert^2}+<\overrightarrow{f},\overrightarrow{v}>\frac{\overrightarrow{v}}{\lVert v \rVert^2}$

### Conclude
SO, Fourier serie is combination of two orthogonal bases Sin and Cos
And , the infinite means combine all period of Sin and Cos.
SO, we can rewrite the equity.
$$f(x)\approx \frac{A_0}{2}+\sum_{k=0}^{100} A_k\cos(kx)+B_k\sin(kx)$$
to somehow approximate the f(x).
### Prat II
Then what we focus is from $[-\pi,\pi ]$ to $[0,L ]$
Therefore, $f(x)\in L_2(0,L)$
Then, we rewrite the above things.
$$f(x)=\frac{A_0}{2}+\sum_{k=0}^\infty A_k\cos(\frac{2\pi}{L}kx)+B_k\sin(\frac{2\pi}{L}kx)$$
$$A_k=\frac{2}{L}\int_{-\pi}^\pi f(x)\cos(\frac{2\pi}{L}kx)dx\qquad B_k=\frac{2}{L}\int_{-\pi}^\pi f(x)\sin(\frac{2\pi}{L}kx)dx$$
## <div id="green_tittle">Inner Product in Hilbert Space</div>
$\qquad<f,g>=\int_a^bf(x)\bar{g}(x)dx$

+ Let $f=[f_1,f_2,...f_n]\quad g=[g_1,g_2,...g_n]^T$ 
+ We discreatize the f and g into k=1~n.
+ <$f,g$>$=\sum^n_{k=1} f_k\bar{g_k}$
We take the $\Delta x \; \text{to} \; 0$, it will be the whole function as f and g.
## Complex Fourier Series
## Fourier Series Codes
:::spoiler MATLAB code
```javascript=
%Define domain
L = pi;
N = 1024;
dx = 2*L/(N-1);
x = -L:dx:L;
%Define Hat Function
f = 0*x;
f(N/4:N/2) = 4*(1:N/4+1)/N;
f(N/2+1:3*N/4) = 1-4*(0:N/4-1)/N;
plot(x,f,"-k","LineWidth",3.5);
hold on ;
```
\begin{align*} \text{Define the hat function} \end{align*}
```javascript=13
%% Compute Fourier series
CC = jet(20);
A0 = sum(f.*ones(size(x)))*dx/pi;
ffs = A0/2;
for k=1:20
A(k) = sum(f.*cos(pi*k*x/L))*dx/pi;
B(k) = sum(f.*sin(pi*k*x/L))*dx/pi;
ffs = ffs + A(k)*cos(k*pi*x/L) + B(k)*sin(k*pi*x/L);
plot(x,ffs,'-','Color',CC(k,:),'LineWidth',1.5)
pause(.1)
end
```
\begin{align*} \text{Fourier Series} \end{align*}
:::

:::spoiler MATLAB code
\begin{align*} \text{Appoximation of a hat funciton using Fourier series} \end{align*}
```javascript=24
%% Plot Amplitudes
clear A;
clear ERR;
ffs = A0/2;
A(1) = A0/2;
ERR(1) = norm(f-ffs);
kmax = 100;
for k=1:kmax
A(k+1) = sum(f.*cos(pi*k*x/L))*dx/pi;
B(k+1) = sum(f.*sin(pi*k*x/L))*dx/pi;
ffs = ffs + A(k+1)*cos(k*pi*x/L) + B(k+1)*sin(k*pi*x/L);
ERR(k+1) = norm(f-ffs)/norm(f);
end
f2 = figure;
threshold = median(ERR) *sqrt(kmax)*4/sqrt(3);
r = max(find(ERR>threshold));
r = 7;
```
```javascript=42
subplot(2,1,1)
semilogy(0:1:kmax,A,"k","LineWidth",1.5)
hold on;
semilogy(r,A(r+1),"go","LineWidth",5)
xlim([0 kmax]);
ylim([10^(-7) 1]);
ylabel("Mode Amplitude","FontSize",17)
subplot(2,1,2)
semilogy(0:1:kmax,ERR,"k","LineWidth",1.5)
hold on;
semilogy(r,ERR(r+1),"go","LineWidth",5)
xlim([0 kmax]);
xlabel("Mode Number k","Fontsize",17);
ylabel("Reconstruction Error","FontSize",17);
```
\begin{align*} \text{Ploting the two graph} \end{align*}
:::

\begin{align*} \text{Mode Amplitude and Reconstruction Error} \end{align*}
## Gibbs Phenomena Codes
:::spoiler MATLAB code
```javascript=
//Using Fourier Series to approximatin a square hat funcion
L = 10;
N = 1024;
dx = L/(N-1);
x = 0:dx:L;
f = zeros(size(x));
f(256:768) = 1;
//Fourier Series
A0 = sum(f.*ones(size(x)))*dx*2/L;
ffs = A0/2;
for k=1:100
A(k) = sum(f.*cos(pi*k*x/L))*dx*2/L;
B(k) = sum(f.*sin(pi*k*x/L))*dx*2/L;
ffs = ffs + A(k)*cos(k*pi*x/L) + B(k)*sin(k*pi*x/L);
end
//Plot
plot(x,f,'k',"LineWidth",2);
hold on;
title("Gibbs Phenomena");
plot(x,ffs,"r-","LineWidth",2.5);
legend("Exact Funcion","Fourier Series");
```
:::

:::spoiler MATLAB code
```javascript=
L = 2*pi;
N = 1024;
dx = L/(N-1);
x = 0:dx:L;
f = zeros(size(x));
f(256:768) = 1;
//Fourier Series do many times
A0 = (1/pi)*sum(f.*ones(size(x)))*dx;
for m=1:100
ffs = A0/2;
for k=1:m
Ak = (1/pi)*sum(f.*cos(2*pi*k*x/L))*dx;
Bk = (1/pi)*sum(f.*sin(2*pi*k*x/L))*dx;
ffs = ffs + Ak*cos(2*k*pi*x/L) + Bk*sin(2*k*pi*x/L);
end
plot(x,f,'b',"LineWidth",4);
hold on;
plot(x,ffs,"r-","LineWidth",0.001);
pause(0.1);
xlim([0,6.3])
end
title('Gibbs Phenomena Display',"FontSize",25);
```
:::

## The Fourier Transform

<style>
@-webkit-keyframes A
{
0% { color:#fc4444;
padding-left:0px}
5% { color:;
padding-left:5px;}
10% { color:#fc7844;
padding-left:10px}
15% { color:;
padding-left:15px;}
20% { color:#fc7844;
padding-left:20px}
25% { color:;
padding-left:25px;}
30% { color:#e7fc44;
padding-left:30px}
35% { color:;
padding-left:35px;}
40% { color:#e7fc44;
padding-left:40px}
45% { color:;
padding-left:45px;}
50% { color:#7bfc44;
padding-left:50px}
55% { color:;
padding-left:45px;}
60% { color:#44fc9a;
padding-left:40px}
65% { color:;
padding-left:35px;}
70% { color:#44fc9a;
padding-left:30px}
75% { color:;
padding-left:25px;}
80% { color: #44fc9a;
padding-left:20px}
85% { color:;
padding-left:15x;}
90% { color: #9144fc;
padding-left:10px}
95% { color:;
padding-left:5px;}
100% { color: #fc44d7;
padding-left:0px}
}
#animation_title{
-webkit-user-select: none;
animation: A 3s ease 0s infinite alternate;
-webkit-animation: A 3s ease 0s infinite alternate;
}
#green_tittle{
color: #66bf56
}
.NOselect{
-webkit-user-select: none;
}
.bg_w{
background-color:#FFFFFF;
}
html, body, .ui-content {
background-color: #333;
color: #ccc;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
color: #ddd;
}
.markdown-body h1,
.markdown-body h2 {
border-bottom-color: #ffffff69;
}
.markdown-body h1 .octicon-link,
.markdown-body h2 .octicon-link,
.markdown-body h3 .octicon-link,
.markdown-body h4 .octicon-link,
.markdown-body h5 .octicon-link,
.markdown-body h6 .octicon-link {
color: #fff;
}
.markdown-body img {
background-color: transparent;
}
.ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a {
color: white;
border-left: 2px solid white;
}
.expand-toggle:hover,
.expand-toggle:focus,
.back-to-top:hover,
.back-to-top:focus,
.go-to-bottom:hover,
.go-to-bottom:focus {
color: white;
}
.ui-toc-dropdown {
background-color: #333;
}
.ui-toc-label.btn {
background-color: #191919;
color: white;
}
.ui-toc-dropdown .nav>li>a:focus,
.ui-toc-dropdown .nav>li>a:hover {
color: white;
border-left: 1px solid white;
}
.markdown-body blockquote {
color: #bcbcbc;
}
.markdown-body table tr {
background-color: #5f5f5f;
}
.markdown-body table tr:nth-child(2n) {
background-color: #4f4f4f;
}
.markdown-body code,
.markdown-body tt {
color: #eee;
background-color: rgba(230, 230, 230, 0.36);
}
a,
.open-files-container li.selected a {
color: #5EB7E0;
}
.cblue {
color: #2890eb;
}
.sblue{
color: #91e069;
}
.kblue{
color: #c562fc;
}
.purple{
color: #8c03ab;
font-weight: 650;
}
.bgc{
background-color: #496968;
}
.marker{
background-color:#786060;
color: #faf26e;
}
.smtittle{
font-size: 20px;
}
</style>