-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbelhadfa_dat_processing.py
390 lines (319 loc) · 14.4 KB
/
belhadfa_dat_processing.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import numpy as np
import glob
import os
import struct
import xml.etree.ElementTree as ET
import struct
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
from scipy.optimize import curve_fit
from scipy.signal import savgol_filter
from scipy.ndimage import gaussian_filter1d
from scipy.signal import butter, filtfilt
data_type_mapping = {
"UnsignedMSB2": ">H", # 2-byte unsigned int (big-endian)
"UnsignedMSB4": ">I", # 4-byte unsigned int (big-endian)
"UnsignedByte": ">B", # 1-byte unsigned int
"IEEE754MSBSingle": ">f", # 4-byte single-precision float (big-endian)
"IEEE754MSBDouble": ">d", # 8-byte double-precision float (big-endian)
}
class Record:
def __init__(self, raw_data, fpath, number, fields):
self.fpath = fpath
self.number = number
self.raw_data = raw_data
self.fields = fields
self.parse_record()
def __repr__(self):
out = ''
out = out + f"Record {self.number}:\n"
for field in self.fields:
value = getattr(self, field["name"])
out = out + f" {field['name']}: {value}\n"
out = out + ("-" * 40)
return out
def parse_record(self):
for field in self.fields:
if field["name"] == "ifgm":
start = field["group_location"]-1
num_ifgm_values = field["repetitions"]
ifgm_values = []
for i in range(num_ifgm_values):
# Extract each `ifgm` value
s = start + i * field["length"]
e = start + (i + 1) * field["length"]
ifgm_data = self.raw_data[s : e]
ifgm_value = struct.unpack(field["type"], ifgm_data)[0]
ifgm_values.append(ifgm_value)
setattr(self, "ifgm", ifgm_values)
else:
start = field["offset"]
end = start + field["length"]
value = struct.unpack(field["type"], self.raw_data[start:end])[0]
setattr(self, field["name"], value)
def parse_XML(xml_file_path):
tree = ET.parse(xml_file_path)
root = tree.getroot()
namespace = {'pds': 'http://pds.nasa.gov/pds4/pds/v1'}
fields = []
records_element = root.find(".//pds:Table_Binary/pds:records", namespace)
# Extract the number of records
num_records = int(records_element.text) if records_element is not None else None
for field in root.findall(".//pds:Field_Binary", namespace):
name = field.find("pds:name", namespace).text
if name == 'ifgm':
continue
offset = int(field.find("pds:field_location", namespace).get("unit") == "byte" and
field.find("pds:field_location", namespace).text or 0)
length = int(field.find("pds:field_length", namespace).get("unit") == "byte" and
field.find("pds:field_length", namespace).text or 0)
data_type = field.find("pds:data_type", namespace).text
struct_format = data_type_mapping.get(data_type, None)
# Append to fields if valid type found
if struct_format:
fields.append({"name": name, "offset": offset-1, "type": struct_format, "length": length})
for field in root.findall(".//pds:Group_Field_Binary", namespace):
name = field.findall(".//pds:name", namespace)[0].text
offset_element = field.findall(".//pds:field_location", namespace)[0]
offset = int(offset_element.text) if offset_element is not None else 0
g_loc = int(field.find("pds:group_location", namespace).get("unit") == "byte" and
field.find("pds:group_location", namespace).text or 0)
length = int(field.findall(".//pds:field_length", namespace)[0].get("unit") == "byte" and
field.findall(".//pds:field_length", namespace)[0].text or 0)
g_length = int(field.find("pds:group_length", namespace).get("unit") == "byte" and
field.find("pds:group_length", namespace).text or 0)
data_type = field.findall(".//pds:data_type", namespace)[0].text
n_reps_element = field.find("pds:repetitions", namespace)
n_reps = int(n_reps_element.text) if n_reps_element is not None else 0
struct_format = data_type_mapping.get(data_type, None)
# Append to fields if valid type found
if struct_format:
fields.append({"name": name, "offset": offset-1, "type": struct_format, "length": length,
"repetitions": n_reps, "group_length":g_length, "group_location": g_loc})
return fields, num_records
def sinusoidal_fit(x, amplitude, frequency, phase, offset):
return amplitude * np.sin(2 * np.pi * frequency * x + phase) + offset
def extract_voltage_spectrum(datafile):
# Datafile is a list of record objects
spectrum = []
for record in datafile:
rr = record.ifgm
spectrum.append(rr)
return spectrum #spectrum is n_records x 1414
def bandpass_filter(data, lowcut, highcut, fs, order=5):
nyquist = 0.5 * fs
low = lowcut / nyquist
high = highcut / nyquist
b, a = butter(order, [low, high], btype='band')
y = filtfilt(b, a, data)
return y
def analyze_landing_site(folder, folder_name, periodicity):
save_dir = '/Users/emmabelhadfa/Documents/Oxford/OTES/results'
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(12, 15))
fig.suptitle(f'Analysis for {folder_name}', fontsize=16)
# Get average spectrum
all_spectra = []
for records in folder:
voltages = [record.ifgm for record in records][0]
spectrum_slice = voltages[600:800]
all_spectra.append(spectrum_slice)
avg_spectrum = np.mean(all_spectra, axis=0)
x = np.arange(600, 800)
# Apply all smoothing methods first
# 1. Savitzky-Golay filter
window_length = 21 # must be odd
polyorder = 3
smoothed_sg = savgol_filter(avg_spectrum, window_length, polyorder)
# 2. Gaussian filter
sigma = 3
smoothed_gaussian = gaussian_filter1d(avg_spectrum, sigma)
# 3. Bandpass filter
fs = 1 # Sample rate
lowcut = 1 / (periodicity + 5)
highcut = 1 / (periodicity - 5)
bandpassed = bandpass_filter(avg_spectrum, lowcut, highcut, fs)
# Now plot everything
# Plot 1: Original and smoothed signals
ax1.plot(x, avg_spectrum, 'b-', label='Original Signal', alpha=0.7)
ax1.plot(x, smoothed_sg, 'r-', label='Savitzky-Golay filter', alpha=0.7)
ax1.plot(x, smoothed_gaussian, 'g-', label='Gaussian filter', alpha=0.7)
ax1.plot(x, bandpassed, 'm-', label='Bandpass filter', alpha=0.7)
ax1.set_title('Original Signal with Different Smoothing Methods')
ax1.set_xlabel('Spectrum Index')
ax1.set_ylabel('Voltage')
ax1.legend()
ax1.grid(True)
# Plot 2: Channeling components
channeling_sg = avg_spectrum - smoothed_sg
channeling_gaussian = avg_spectrum - smoothed_gaussian
channeling_bandpass = avg_spectrum - bandpassed
ax2.plot(x, channeling_sg, 'r-', label='Channeling (SG)', alpha=0.7)
ax2.plot(x, channeling_gaussian, 'g-', label='Channeling (Gaussian)', alpha=0.7)
ax2.plot(x, channeling_bandpass, 'm-', label='Channeling (Bandpass)', alpha=0.7)
ax2.set_title('Extracted Channeling Components')
ax2.set_xlabel('Spectrum Index')
ax2.set_ylabel('Amplitude')
ax2.legend()
ax2.grid(True)
# Plot 3: FFT Analysis
fft_result = np.fft.fft(channeling_sg)
freqs = np.fft.fftfreq(len(channeling_sg), 1)
pos_mask = freqs > 0
freqs = freqs[pos_mask]
power = np.abs(fft_result)[pos_mask]
ax3.semilogy(freqs, power, 'b-', label='FFT of Channeling')
top_indices = np.argsort(power)[-3:][::-1]
print(f"\n{folder_name} channeling frequency analysis:")
for idx, ind in enumerate(top_indices):
period = 1/freqs[ind]
print(f"Peak {idx+1}: frequency = {freqs[ind]:.4f}, period = {period:.2f} samples")
ax3.axvline(x=freqs[ind], color=f'C{idx+1}', linestyle='--',
label=f'Peak {idx+1}: period={period:.2f}')
ax3.set_title('FFT of Channeling (log scale)')
ax3.set_xlabel('Frequency (cycles/sample)')
ax3.set_ylabel('Power')
ax3.legend()
ax3.grid(True)
plt.tight_layout()
filename = os.path.join(save_dir, f'{folder_name}_channellinganalysis.png')
plt.savefig(filename, dpi=300, bbox_inches='tight')
print(f"Saved analysis for {folder_name} to {filename}")
plt.close(fig)
return smoothed_sg, smoothed_gaussian, bandpassed
def display_voltage_plot(folders, folder_names):
datasets = {}
periodicities = [] # Store periodicities for each folder
for i, dataset in enumerate(folders):
specs = []
for datafile in dataset:
# Each file is a list of record objects
spec = extract_voltage_spectrum(datafile)
spec_mean = np.mean(spec, axis=0) # Compute mean along the correct axis
specs.append(spec_mean) # Append the mean spectrum to the list
specs = np.mean(specs, 0)
datasets[folder_names[i]] = specs
# Compute periodicity for each folder
peaks, _ = find_peaks(specs, distance=5) # Adjust distance as needed
if len(peaks) > 1:
peak_distances = np.diff(peaks)
avg_period = np.mean(peak_distances)
periodicities.append(avg_period)
else:
periodicities.append(None) # Handle case with no peaks
plot_data(datasets)
# Plot 1: Voltage Spectrum
plt.subplot(2, 1, 1)
for i, folder in enumerate(folders):
all_spectra = []
for records in folder:
voltages = [record.ifgm for record in records][0]
spectrum_slice = voltages[600:800]
all_spectra.append(spectrum_slice)
avg_spectrum = np.mean(all_spectra, axis=0)
plt.plot(range(600, 800), avg_spectrum, label=folder_names[i])
plt.title('Average Voltage Spectrum (600-800)')
plt.xlabel('Spectrum Index')
plt.ylabel('Voltage')
plt.legend()
plt.grid(True)
# Plot 2: FFT
plt.subplot(2, 1, 2)
for i, folder in enumerate(folders):
all_spectra = []
for records in folder:
voltages = [record.ifgm for record in records][0]
spectrum_slice = voltages[600:800]
all_spectra.append(spectrum_slice)
avg_spectrum = np.mean(all_spectra, axis=0)
# Remove DC component (mean)
avg_spectrum = avg_spectrum - np.mean(avg_spectrum)
# Compute FFT
fft_result = np.fft.fft(avg_spectrum)
# Calculate frequency axis properly
sample_spacing = 1 # assuming uniform spacing of 1
freqs = np.fft.fftfreq(len(avg_spectrum), sample_spacing)
# Get positive frequencies only
pos_freqs = freqs[:len(freqs)//2]
power = np.abs(fft_result[:len(freqs)//2])
# Plot on log scale for better visibility
plt.semilogy(pos_freqs, power, label=folder_names[i])
plt.title('FFT Power Spectrum of Average Signal')
plt.xlabel('Frequency (cycles per sample)')
plt.ylabel('Power (log scale)')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
# Analyze each landing site separately
smoothed_data = {}
for folder, folder_name, periodicity in zip(folders, folder_names, periodicities):
if periodicity is not None:
smoothed_sg, smoothed_gaussian, bandpassed = analyze_landing_site(folder, folder_name, periodicity)
smoothed_data[folder_name] = {
'savgol': smoothed_sg,
'gaussian': smoothed_gaussian,
'bandpassed': bandpassed
}
# Comparison of Bandpass Filters
plt.figure(figsize=(12, 6))
for folder_name in folder_names:
if folder_name in smoothed_data:
plt.plot(range(600, 800), smoothed_data[folder_name]['bandpassed'],
label=f'{folder_name} Bandpass', alpha=0.7)
plt.title('Comparison of Bandpass Filters')
plt.xlabel('Spectrum Index')
plt.ylabel('Voltage')
plt.legend()
plt.grid(True)
plt.show()
# Comparison of Savitzky-Golay Filters
plt.figure(figsize=(12, 6))
for folder_name in folder_names:
if folder_name in smoothed_data:
plt.plot(range(600, 800), smoothed_data[folder_name]['savgol'],
label=f'{folder_name} Savitzky-Golay', alpha=0.7)
plt.title('Comparison of Savitzky-Golay Filters')
plt.xlabel('Spectrum Index')
plt.ylabel('Voltage')
plt.legend()
plt.grid(True)
plt.show()
def plot_data(datasets):
plt.figure(figsize=(10, 6))
# Plot each dataset
for dataset_name, values in datasets.items():
plt.plot(values, label=dataset_name)
# Add grid, labels, title, and legend
plt.grid(True, linestyle='--', alpha=0.6)
plt.title("Original Voltage Spectrums")
plt.xlabel("Spectrum Index")
plt.ylabel("Voltage (V)")
plt.legend()
# Show the plot
plt.tight_layout()
plt.show()
if __name__ == "__main__":
fpath = '/Users/emmabelhadfa/Documents/Oxford/OTES/data/locations/level0/**/*.dat' # replace this with the path to your folder containing all your dat files
folders = []
fprev = None
k = -1
folder_names = []
for filepath in glob.glob(fpath, recursive=True):
path = os.path.normpath(filepath)
fcurr = path.split(os.sep)[-2]
if fcurr != fprev:
k += 1
folders.append([])
folder_names.append(fcurr)
xmlp = filepath[:-4] + '.xml'
fields, num_records = parse_XML(xmlp)
record_length = int(os.path.getsize(filepath)/num_records)
with open(filepath, 'rb') as file:
parsed_records = []
for i in range(num_records):
record = file.read(record_length)
record_obj = Record(record, filepath, i, fields)
parsed_records.append(record_obj)
folders[k].append(parsed_records)
fprev = fcurr
display_voltage_plot(folders, folder_names)