---
# System prepended metadata

title: 'Week 1 Report: 1D Fourier Transform Practice '
tags: [111 Summer Research]

---

author: NTUAS B11209013 Yu-Chuan, Kan
## Data information
| Item             |      Description       |
| ---------------- |:----------------------:|
| Source           | Taipei_temp_record.csv |
| Time             | Jan, 2009 ~ Dec, 2020  |
| Recording method |    monthly average     |

## Using Package
```
import pandas as pd
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
```

## Importing .csv file
```
csv_data = "Taipei_temp_record.csv"
temp_record = np.asarray(pd.read_csv(csv_data, sep=','))
```
Since the output of `pd.read_csv` is a class, can't be used as numpy array.
Therefore, need to use `np.asarray` to transform it.

## Define function
### Define function that Conducting D.F.T.
By the function of discrete Fourier transfer:
$$\hat{x}[\xi] = \Sigma_{n=0}^{N-1}\; e^{-i\frac{2\pi}{N}nk}\;x[n]$$

|    Notation    |             Meaning              |
|:--------------:|:--------------------------------:|
|     $\xi$      |            frequency             |
| $\hat{x}[\xi]$ |    frequency-domain sequence     |
|     $x[n]$     |        time-domain series        |
|      $N$       |  length of time-domain sequence  |
|      $k$       | wave number, $k=0, 1, 2,...,N-1$ |

By the expression above, it can be written in matrix form:
$$\hat{x}[\xi] = 
\begin{bmatrix}
\omega^0 & \omega^0 & \omega^0 & \dots & \omega^0 \\
\omega^0 & \omega^1 & \omega^2 & \dots & \omega^{N-1} \\
\omega^0 & \omega^2 & \omega^4 & \dots & \omega^{2(N-1)} \\
\vdots   & \vdots   & \vdots   & \ddots& \vdots \\
\omega^0 & \omega^{N-1} & \omega^{2(N-1)} & \dots & \omega^{(N-1)(N-1)}
\end{bmatrix}
x[n]
$$

in the matrix, $\omega = e^{-i\frac{2\pi}{N}}$
Also, the columns axis (axis = 1) represents its time series,
the rows axis (axis = 0) represenets its wave number series.
By the derivative above, it can be written as Python code:
```
omega = np.exp((0+1j)*(-2*np.pi/N))  # operator that needed in DFT

n = np.linspace(0, N-1, N).reshape((N, 1))
k = np.linspace(0, N-1, N).reshape((1, N))

power = n*k
lo = omega**power
```

### Define Function that conducting IDFT
By the function of discrete Fourier transfer:
$$x[n] = \frac{1}{N}\Sigma_{n=0}^{N-1}\; e^{i\frac{2\pi}{N}nk}\;\hat{x}[\xi]$$

|    Notation    |             Meaning              |
|:--------------:|:--------------------------------:|
|     $\xi$      |            frequency             |
| $\hat{x}[\xi]$ |    frequency-domain sequence     |
|     $x[n]$     |        time-domain series        |
|      $N$       |  length of time-domain sequence  |
|      $k$       | wave number, $k=0, 1, 2,...,N-1$ |

By the expression above, it can be written in matrix form:
$$\hat{x}[\xi] = \frac{1}{N}
\begin{bmatrix}
\omega^0 & \omega^0 & \omega^0 & \dots & \omega^0 \\
\omega^0 & \omega^{-1} & \omega^{-2} & \dots & \omega^{-(N-1)} \\
\omega^0 & \omega^{-2} & \omega^(-4) & \dots & \omega^{-2(N-1)} \\
\vdots   & \vdots   & \vdots   & \ddots& \vdots \\
\omega^0 & \omega^{-(N-1)} & \omega^{-2(N-1)} & \dots & \omega^{-(N-1)(N-1)}
\end{bmatrix}
x[n]
$$

in the matrix, $\omega = e^{-i\frac{2\pi}{N}}$
Also, the columns axis (axis = 1) represents its time series,
the rows axis (axis = 0) represenets its wave number series.

By doing matrix product, it is obvious that the operator conductin D.F.T. and I.D.F.T is inverse matrix to each other.

By the derivative above, it can be written as Python code:
```
omega         = np.exp((0+1j)*(-2*np.pi/N))# operator that needed in DFT

n = np.linspace(0, N-1, N).reshape((N, 1))
k = np.linspace(0, N-1, N).reshape((1, N))

power = n*k
lo = omega**power

lo            = np.linalg.inv(lo)          # do inverse process to linear operator
```

## Original Data Visualization
By the data introduced in the first part, it can visualized as figure below:
![](https://hackmd.io/_uploads/ByobaCydn.png)

## Fourier Transform Consequence of Data
By the function defined above, figure below is the frequency-domain data:
![](https://hackmd.io/_uploads/BkyvCAJO3.png)
The reason why there is a downward peak at time 12 is due to the average temperature data has its period approximatly 1 year (annual cycle).

## Check the correction of the D.F.T. function
By using the function **IDFT** defines above, there is the consequence:
![](https://hackmd.io/_uploads/ry85kkedh.png)
This figure shows that the data doing I.D.F.T. from the frequency-domain to time-domain is the same as the origin data, which means that the functions, both DFT and IDFT, are correct.

## Power Spectrum of the Data
Since the order of magnitude of first index is too different with each other.
Therefore, the data is ignored in the plot.
![](https://hackmd.io/_uploads/HymHezDun.png)
The two peaks of the plot are 12th and 132th month.

## Coding File
https://drive.google.com/file/d/1Flbvq0570J6-GHWd9nTfBSOoNp_2oJHG/view?usp=sharing