> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/iot-development-with-infineon-psoc-tm-6-and-bdh-platform/bdh-x-brain-data-analytics/psoc6-data-collection-to-csv-log-file.md).

# PSoC6 Data Collection to CSV log file

## 1. Collect dataset using Eclipse ModusToolbox

1.1 Create Project through ModusToolbox

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FYI6de5KYXSVacfyecOni%2Fimage.png?alt=media&amp;token=fb77a6b8-1fc5-4b39-b1b9-75817f67ed61" alt=""><figcaption><p>Create application project</p></figcaption></figure>

1.2 Steps to Configure the Code:

1. **Navigate to the `gesture.h` File:**
   * Locate the `gesture.h` file within the `source` folder of the project directory.
2. **Modify the Macro for Data Collection Mode:**
   * Open the `gesture.h` file.
   * Find the line defining `GESTURE_DATA_COLLECTION_MODE`.
   * Change its value from `0u` to `1u`.

     ```cpp
     #define GESTURE_DATA_COLLECTION_MODE 1u
     ```
   * This change enables printing sensor data to the terminal instead of running the inference engine.

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FtwdQTd4Km6No1HZehfWh%2Fimage.png?alt=media&amp;token=6c1bca42-4aff-4268-9700-cc7b9ebf3fde" alt=""><figcaption></figcaption></figure>

3. **Navigate to the `Makefile`:**
   * Locate the `Makefile` in the root directory of the project.
4. **Update the Shield:**

   * Open the `Makefile`.
   * Look for the shield configuration line:&#x20;

   <pre><code><strong>SHIELD_DATA_COLLECTION = CY_028_SENSE_SHIELD_v1
   </strong></code></pre>

   * Change this to:

   ```
   SHIELD_DATA_COLLECTION = CY_028_SENSE_SHIELD_v2
   ```

   * This updates the configuration to use the `CY_028_SENSE_SHIELD_v2` for gathering IMU data.

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FhxfleNKjuiLyygwAnzwB%2Fimage.png?alt=media&amp;token=27f925b9-fe70-4c98-a8dc-39c138b514d9" alt=""><figcaption></figcaption></figure>

1.3 Build and Launch the application

1.4 Let's collect dataset by Eclipse

* Open modus-shell

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FG7ag0uVPQiiXwZkXGdjh%2Fimage.png?alt=media&amp;token=e15aa682-011c-401e-8ece-bf0c5458e508" alt=""><figcaption></figcaption></figure>

* Navigate to the folder and install the library.

```cfscript
pip install -r requirements.txt
```

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2Fo6TtlqNS9Npa9bdqIy4d%2Fimage.png?alt=media&amp;token=4461d0c8-5fdd-4486-ab6c-be8fef6cf5c3" alt=""><figcaption><p>install library: pip install -r requirements.txt</p></figcaption></figure>

* To collect data from the sensor, use the command line as below:

```
./collect.sh <COM PORT> <GESTURE NAME> <PERSON
```

ตัวอย่าง:

```
./collect.sh COM6 circle Chhun
```

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FQDMpP0HwH3Mp1w7bBNhJ%2Fimage.png?alt=media&amp;token=166be9fe-c306-40a5-a0f2-2ca3e3b8696e" alt=""><figcaption><p>Press 'c' to start collect data and 's' to stop and exit</p></figcaption></figure>

## 2. Convert .txt dataset to .csv dataset

After data collection, the dataset is stored in the Machine\_Learning\_Gesture\_Classification\train\gesture\_data\\\<your gesture name> directory as a .txt file

2.1 Plot graph

* Create project in google colab: <https://colab.research.google.com/drive/1zkqpE8NGNUJv6uIUV-4ItKIbkxr1n4qj?usp=sharing>
* Install library

  ```notebook-python
  !pip install tsaug
  ```

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FUhBbc4nc8kWwNkSRjN1U%2Fimage.png?alt=media&amp;token=8ae57f32-975a-4d98-9a66-d779a929c119" alt=""><figcaption></figcaption></figure>

* Using below code for plot the graph for each sensor dataset:

```python
import matplotlib.pyplot as plt

# Define the file name
filename = '<your .txt file location'

# Initialize lists to store data
gyro_x, gyro_y, gyro_z, accel_x, accel_y, accel_z = [], [], [], [], [], []

# Read data from the file
with open(filename, 'r') as file:
    for line in file:
        # Split the line by comma
        data = line.strip().split(',')
        # Check if the line has 6 values
        if len(data) == 6:
            # Check if all values in the line are not '-'
            if all(value != '-' for value in data):
                # Append data to respective lists
                gyro_x.append(float(data[0]))
                gyro_y.append(float(data[1]))
                gyro_z.append(float(data[2]))
                accel_x.append(float(data[3]))
                accel_y.append(float(data[4]))
                accel_z.append(float(data[5]))

# Create time values (assuming each line represents a time step)
time = list(range(len(accel_x)))

# Plot accelerometer and gyroscope data with a wider figure
plt.figure(figsize=(16, 8))  # Increase width for a longer x-axis

# Set a main title for the entire figure
plt.suptitle('Square Dataset', fontsize=20)

# Plot accelerometer data
plt.subplot(2, 1, 1)
plt.plot(time, accel_x, label='Accel X')
plt.plot(time, accel_y, label='Accel Y')
plt.plot(time, accel_z, label='Accel Z')
plt.title('Accelerometer Data', fontsize=16)  # Title for this subplot
plt.xlabel('Time', fontsize=12)
plt.ylabel('Acceleration', fontsize=12)
plt.legend()

# Plot gyroscope data
plt.subplot(2, 1, 2)
plt.plot(time, gyro_x, label='Gyro X')
plt.plot(time, gyro_y, label='Gyro Y')
plt.plot(time, gyro_z, label='Gyro Z')
plt.title('Gyroscope Data', fontsize=16)  # Title for this subplot
plt.xlabel('Time', fontsize=12)
plt.ylabel('Angular Velocity', fontsize=12)
plt.legend()

# Adjust layout to prevent overlap
plt.tight_layout(rect=[0, 0, 1, 0.95])  # Leave space for main title
plt.show()

```

* Sample Graph Outputs from the Dataset

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FBnFrhu5QQ4IWHBFjTrON%2Fimage.png?alt=media&amp;token=441e33b5-fa1e-4420-94b3-5b348071318d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FCoRgIAmtiI3KfXAxQFUV%2Fimage.png?alt=media&amp;token=d2c572b0-5a6b-4b00-be09-bbb0a226bc13" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FjVhUlvc1kAsDXSKudTLA%2Fimage.png?alt=media&amp;token=b696e934-a248-4528-b8df-3efc67d6c1f3" alt=""><figcaption></figcaption></figure>

2.2 Covert to .csv file

Colab: <https://colab.research.google.com/drive/1wdsgtEFlWhmXoo3XLQOzVhtO8u_F96Nt?usp=sharing>

Open Colab and create your own copy before running the code:

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FJWh84Bo49LkfGHezNsza%2Fimage.png?alt=media&amp;token=e834fa32-e724-4969-bbac-b8c7d28fc9be" alt=""><figcaption></figcaption></figure>

This session in Colab is used to convert a `.txt` dataset to a `.csv` dataset.

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F2cbjeVL5Gde56oT4SGMr%2Fimage.png?alt=media&amp;token=cf03ac8e-5199-49a9-816c-d5c8b2e4c1f3" alt=""><figcaption></figcaption></figure>
