-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData_preparation.py
122 lines (99 loc) · 5.03 KB
/
Data_preparation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import pandas as pd
import numpy as np
file_path = "C:\Users\user\Documents\Rally\Telemetry\data preperation\data_path.txt"
data_dir = ""
with open(file_path, "r") as file:
for line in file:
if "data_dir" in line:
data_dir = line.split("=")[1].strip()
break
print(data_dir)
# List of CSV files
csv_files = [
'Accelerometer.csv',
'Gyroscope.csv',
'Location.csv',
'Magnetometer.csv'
]
# Target path for the combined and resampled data
combined_output_path = r'C:\Users\user\Documents\Rally\Telemetry\data preperation\combined_data.csv'
# Target path for the combined and resampled data
prepared_output_path = r'C:\Users\user\Documents\Rally\Telemetry\data preperation\prepared_data.csv'
# Sampling rate for resampling
target_sampling_rate = 10 # 10 Hz
# Initialize an empty list to store DataFrames
dfs = []
# Iterate over the CSV files and process them
for csv_file in csv_files:
input_path = os.path.join(data_dir, csv_file)
# Load the CSV file into a Pandas DataFrame
df = pd.read_csv(input_path)
# Set the time as the index (assuming "Time (s)" is the time column)
df['Time (s)'] = pd.to_datetime(df['Time (s)'], unit='s')
df.set_index('Time (s)', inplace=True)
# Resample and linearly interpolate
df_resampled = df.resample(f'{1/target_sampling_rate}S').mean().interpolate(method='linear')
# Append the resampled DataFrame to the list
dfs.append(df_resampled)
# Combine the resampled DataFrames into one DataFrame
combined_df = pd.concat(dfs, axis=1)
# Remove rows with NaN values from the DataFrame
combined_df = combined_df.dropna()
# Save the combined DataFrame to a CSV file
combined_df.to_csv(combined_output_path)
#############################################################################
combined_df_inter=combined_df
# Berechnen Sie die Mittelwerte der Beschleunigungsspalten
combined_df_inter['Acceleration x (m/s^2)'] = combined_df['Acceleration x (m/s^2)'] - combined_df['Acceleration x (m/s^2)'].mean()
combined_df_inter['Acceleration y (m/s^2)'] = combined_df['Acceleration y (m/s^2)'] - combined_df['Acceleration y (m/s^2)'].mean()
combined_df_inter['Acceleration z (m/s^2)'] = combined_df['Acceleration z (m/s^2)'] - combined_df['Acceleration z (m/s^2)'].mean()
# Berechnen Sie die Spalte atan(Magnetic field y / Magnetic field z)
# Define the conversion function
def convert_to_positive_azimuth(azimuth_degrees):
if azimuth_degrees < 0:
azimuth_degrees += 360.0
elif azimuth_degrees > 360.0:
azimuth_degrees -= 360.0
return azimuth_degrees
combined_df_inter["B"] = np.sqrt(combined_df["Magnetic field x (µT)"]**2 + combined_df["Magnetic field y (µT)"]**2 + combined_df["Magnetic field z (µT)"]**2)
combined_df_inter["Magnetic_Field_x_normalized"] = combined_df["Magnetic field x (µT)"] / combined_df_inter["B"]
combined_df_inter["Magnetic_Field_y_normalized"] = combined_df["Magnetic field y (µT)"] / combined_df_inter["B"]
combined_df_inter["Magnetic_Field_z_normalized"] = combined_df["Magnetic field z (µT)"] / combined_df_inter["B"]
combined_df_inter["inclination"] = np.arctan2(combined_df_inter["Magnetic_Field_z_normalized"], np.sqrt(combined_df_inter["Magnetic_Field_x_normalized"]**2 + combined_df_inter["Magnetic_Field_y_normalized"]**2))
combined_df_inter["azimuth"] = np.arctan2(combined_df_inter["Magnetic_Field_y_normalized"], combined_df_inter["Magnetic_Field_x_normalized"])
combined_df_inter["inclination_degrees"] = combined_df_inter["inclination"] * (180.0 / np.pi)
combined_df_inter["azimuth_degrees"] = combined_df_inter["azimuth"] * (360.0 / np.pi)+90
# Apply the conversion function to the 'azimuth' column
combined_df_inter['Mag Direction (°)'] = combined_df_inter["azimuth_degrees"].apply(convert_to_positive_azimuth)
# Die gewünschten Spalten auswählen
prepared_df = combined_df_inter[[
'Acceleration x (m/s^2)',
'Acceleration y (m/s^2)',
'Acceleration z (m/s^2)',
'Gyroscope x (rad/s)',
'Gyroscope y (rad/s)',
'Gyroscope z (rad/s)',
'Latitude (°)',
'Longitude (°)',
'Height (m)',
'Velocity (m/s)',
'Direction (°)',
'Mag Direction (°)']]
# Umbenennen der Spalten
prepared_df.columns = [
'Acceleration x (m/s^2)',
'Acceleration y (m/s^2)',
'Acceleration z (m/s^2)',
'Gyroscope x (rad/s)',
'Gyroscope y (rad/s)',
'Gyroscope z (rad/s)',
'Latitude (°)',
'Longitude (°)',
'Height (m)',
'Velocity (m/s)',
'Direction (°)',
'Mag Direction (°)']
# Save the combined DataFrame to a CSV file
prepared_df.to_csv(prepared_output_path)
print("Resampling and combining completed.")