# Python Homework - Application 3
###### tags: `python2021`
* 要求 (request):
* 格式請依照hwA3的標準 (檔名:hwA3-1.py, hwA3-2.py...,最後將所有檔案壓縮至"學號_hwA3.zip"上傳e3)
* 輸出檔案內容以TA截圖為標準
:::danger
If python complains about `UnicodeDecodeError: 'cp950' codec can't decode byte ...` when using `np.genfromtxt()`, try enforcing __UTF-8__ decoding with
`np.genfromtxt(…, encoding='UTF-8')`
:::
## Manipulating Matrix
Question 1 and Question 2 are related to the file _"randos.txt"_. The file is a txt file that contains a __10*10__ matrix of random integers. Load the file and perform the following tasks:
Question 1
---
Sort the matrix into a new __10*10__ matrix so that the new matrix goes from left to right and then top to down (smallest goes first). Print the original matrix with a title __Before__ and the new matrxi with a title __After__.

Question 2
---
Break the original matrix into 5 __10*2__ blocks from top to bottom. Sort each block as in Question 1 respectively, then stack them back together in the same order. Print the new matrix.
## Simple Biomedical Analysis w/ Numpy
Question 3 to Question 6 are related to the file _“ECG.csv”_, which contains a column of continuous raw ECG (Electrocardiography) record.
An ECG signal usually contains repeating occurrences of the PQRST complex (shown in the figure below). In raw ECG, the R wave is often the most prominent ECG feature, represented by a sharp increase and a sharp decrease of signal. Distinguishing the R wave is a major keystone to ECG analysis.

The signal in _“ECG.csv”_ was recorded when the subject was running a treadmill. The signal was recorded as a sample rate of __250Hz__ (250 data points per second) for __10__ seconds. If you try to plot the record in Microsoft Excel, the plot should be something like this:

Load the ECG record with Numpy and perform the following tasks:
Question 3
---
Reshape the record into a 2d matrix for R wave detection. The matrix should be __20*125__ in size (each row contains half a second of data). Print the first and the last row of the matrix.
:::info
If you're curious how each row looks like, you can copy the numbers and plot them in Google Sheets.
:::
Question 4
---
Get the indexes of each R peak. Convert the indexes to seconds (assuming initial time = 0s) and print them (rounded to 2nd decimal).
:::info
Hints:
1) A row may or may not include a R wave
2) The values of R peaks are much larger than average ECG signal. Use this for your advantage.
:::

Question 5
---
Calculate the interval between R peaks (RR interval, RRI). Use the RRIs to derive the average heart rate in beat-per-minute (BPM). Print the RRI and the average heart rate (RRI in second, heart rate in BPM, both rounded to 3rd decimal).

Question 6
---
Reshape the ECG record to 10*250 (each row contains a second). Save the reshaped ECG record into _ECG_reshaped.txt_.
Additional Tips & Tricks
---
In biomedical processing & interpretation, sometimes large amount of mathematic data needs to be accessed by or passed between multiple programs or projects. For example, in this week's homework, the ECG signal and indexes of R peaks were used repeatly in different questions.
Of course, you can always save your data and statistic as simple text file with `np.savetxt()`. However, this process can get increasingly demanding (both in time and in resources). Instead, you can save you variable as binary file (`.npy`) with the function `np.save()` and then load it with `np.load()`. An example code is shown below:
>
>`import numpy as np`
`A = np.arange(12).reshape(3,4)`
`np.save(‘output.npy’, A)`
`B = np.load(‘output.npy’)`
`print(B)`
>
The variable would retain its shape, value(s), bit depth etc. but not its name. The binary file is also smaller than ordinary text file, which make reading and writing file much faster. Do notice that .npy files are only compatible with Numpy, meaning other tools (ex. Matlab, Excel) may not be able to interpret them.