feat: Add initial time domain feature extraction class
The code changes add a new file `time_domain_features.py` that contains a `FeatureExtractor` class. This class calculates various time domain features for a given dataset. The features include mean, max, peak, peak-to-peak, RMS, variance, standard deviation, power, crest factor, form factor, pulse indicator, margin, kurtosis, and skewness. The class takes a file path as input and reads the data from a CSV file. It assumes the data to analyze is in the first column. The calculated features are stored in a dictionary. The commit message suggests that the purpose of the changes is to add a new class for time domain feature extraction.
This commit is contained in:
42
code/src/features/time_domain_features.py
Normal file
42
code/src/features/time_domain_features.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
from scipy.stats import kurtosis, skew
|
||||||
|
|
||||||
|
class FeatureExtractor:
|
||||||
|
def __init__(self, file_path):
|
||||||
|
# Read data from CSV file
|
||||||
|
self.data = pd.read_csv(file_path)
|
||||||
|
# Assuming the data to analyze is in the first column
|
||||||
|
self.x = self.data.iloc[:, 0].values
|
||||||
|
|
||||||
|
# Calculate all features
|
||||||
|
self.features = {
|
||||||
|
'Mean': np.mean(self.x),
|
||||||
|
'Max': np.max(self.x),
|
||||||
|
'Peak (Pm)': np.max(np.abs(self.x)),
|
||||||
|
'Peak-to-Peak (Pk)': np.max(self.x) - np.min(self.x),
|
||||||
|
'RMS': np.sqrt(np.mean(self.x**2)),
|
||||||
|
'Variance': np.var(self.x, ddof=0),
|
||||||
|
'Standard Deviation': np.std(self.x, ddof=1),
|
||||||
|
'Power': np.mean(self.x**2),
|
||||||
|
'Crest Factor': np.max(np.abs(self.x)) / np.sqrt(np.mean(self.x**2)),
|
||||||
|
'Form Factor': np.sqrt(np.mean(self.x**2)) / np.mean(self.x),
|
||||||
|
'Pulse Indicator': np.max(np.abs(self.x)) / np.mean(self.x),
|
||||||
|
'Margin': np.max(np.abs(self.x)) / (np.mean(np.sqrt(np.abs(self.x)))),
|
||||||
|
'Kurtosis': kurtosis(self.x, fisher=False),
|
||||||
|
'Skewness': skew(self.x, bias=False)
|
||||||
|
}
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
result = "Feature Extraction Results:\n"
|
||||||
|
for feature, value in self.features.items():
|
||||||
|
result += f"{feature}: {value:.4f}\n"
|
||||||
|
return result
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
# Assume you have a CSV file with numerical data in the first column
|
||||||
|
# Create an instance of the class and pass the path to your CSV file
|
||||||
|
# For example:
|
||||||
|
# extractor = FeatureExtractor('path_to_your_data.csv')
|
||||||
|
# When you call the variable in a notebook or in the interpreter, it will print the results
|
||||||
|
# print(extractor)
|
||||||
Reference in New Issue
Block a user