Data Preparation (STFT) #23

Closed
opened 2024-10-19 02:25:30 +00:00 by nuluh · 0 comments
nuluh commented 2024-10-19 02:25:30 +00:00 (Migrated from github.com)

Step 1: Importing Necessary Libraries

Before data preparation begins, the code imports essential libraries for data manipulation, visualization, and machine learning.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import stft, hann
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

Step 2: Loading the CSV Files

The code reads vibration data from 16 CSV files, each representing a different bolt loosening case.

# Load CSV files into DataFrames
df1 = pd.read_csv("/kaggle/input/.../Case1/Case1_800.csv")
df2 = pd.read_csv("/kaggle/input/.../Case2/Case2_800.csv")
# ...
df16 = pd.read_csv("/kaggle/input/.../Case16/Case16_800.csv")

Visual Representation:

Each DataFrame (df1, df2, ..., df16) contains raw vibration data:

Channel 1 Channel 2
... ...
... ...

Step 3: Storing DataFrames in a List

All DataFrames are stored in a list for easy iteration.

df = [df1, df2, df3, ..., df16]

Step 4: Renaming Columns for Consistency

The code standardizes the column names across all DataFrames to ensure consistency.

for data in df:
    data.columns = ['sensor 1', 'sensor 2']

Visual Representation:

sensor 1 sensor 2
... ...
... ...

Step 5: Extracting Signals from Each DataFrame

Signals from both sensors are extracted and stored in separate lists.

signal_sensor1 = []
signal_sensor2 = []

for data in df:
    signal_sensor1.append(data['sensor 1'].values)
    signal_sensor2.append(data['sensor 2'].values)
  • signal_sensor1: List of NumPy arrays containing data from sensor 1 for each case.
  • signal_sensor2: Similar list for sensor 2.

Visual Representation:

signal_sensor1 = [
    array_case1_sensor1,  # Data from Case 1
    array_case2_sensor1,  # Data from Case 2
    ...
    array_case16_sensor1  # Data from Case 16
]

Step 6: Visualizing Raw Vibration Signals

The code plots the raw signals to understand the data visually.

plt.plot(df1['sensor 1'], label='sensor 1')
plt.plot(df1['sensor 2'], label='sensor 2')
plt.xlabel("Number of samples")
plt.ylabel("Amplitude")
plt.title("Raw vibration signal")
plt.legend()
plt.show()

(Note: As this is a text-based medium, please imagine a plot with two lines representing sensor 1 and sensor 2 signals over time.)


## **Step 1: Importing Necessary Libraries** Before data preparation begins, the code imports essential libraries for data manipulation, visualization, and machine learning. ```python import numpy as np import pandas as pd import matplotlib.pyplot as plt from scipy.signal import stft, hann from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score ``` --- ## **Step 2: Loading the CSV Files** The code reads vibration data from 16 CSV files, each representing a different bolt loosening case. ```python # Load CSV files into DataFrames df1 = pd.read_csv("/kaggle/input/.../Case1/Case1_800.csv") df2 = pd.read_csv("/kaggle/input/.../Case2/Case2_800.csv") # ... df16 = pd.read_csv("/kaggle/input/.../Case16/Case16_800.csv") ``` **Visual Representation:** Each DataFrame (`df1`, `df2`, ..., `df16`) contains raw vibration data: | Channel 1 | Channel 2 | |-----------|-----------| | ... | ... | | ... | ... | --- ## **Step 3: Storing DataFrames in a List** All DataFrames are stored in a list for easy iteration. ```python df = [df1, df2, df3, ..., df16] ``` --- ## **Step 4: Renaming Columns for Consistency** The code standardizes the column names across all DataFrames to ensure consistency. ```python for data in df: data.columns = ['sensor 1', 'sensor 2'] ``` **Visual Representation:** | sensor 1 | sensor 2 | |----------|----------| | ... | ... | | ... | ... | --- ## **Step 5: Extracting Signals from Each DataFrame** Signals from both sensors are extracted and stored in separate lists. ```python signal_sensor1 = [] signal_sensor2 = [] for data in df: signal_sensor1.append(data['sensor 1'].values) signal_sensor2.append(data['sensor 2'].values) ``` - `signal_sensor1`: List of NumPy arrays containing data from sensor 1 for each case. - `signal_sensor2`: Similar list for sensor 2. **Visual Representation:** ```plaintext signal_sensor1 = [ array_case1_sensor1, # Data from Case 1 array_case2_sensor1, # Data from Case 2 ... array_case16_sensor1 # Data from Case 16 ] ``` --- ## **Step 6: Visualizing Raw Vibration Signals** The code plots the raw signals to understand the data visually. ```python plt.plot(df1['sensor 1'], label='sensor 1') plt.plot(df1['sensor 2'], label='sensor 2') plt.xlabel("Number of samples") plt.ylabel("Amplitude") plt.title("Raw vibration signal") plt.legend() plt.show() ``` *(Note: As this is a text-based medium, please imagine a plot with two lines representing sensor 1 and sensor 2 signals over time.)* ---
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nuluh/thesis#23