-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagnetofft.py
More file actions
1345 lines (1126 loc) · 42.9 KB
/
magnetofft.py
File metadata and controls
1345 lines (1126 loc) · 42.9 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import convolve
import h5py
from scipy.signal import ShortTimeFFT
from scipy.signal.windows import gaussian
import polars as pl
import pandas as pd
from pathlib import Path
from matplotlib.colors import LogNorm
from matplotlib import colors,cm
import os
#################################################
# Reading environmental logs
#################################################
def load_monitorpi_csv(filepath):
df = pd.read_csv(filepath)
# Convert 'localtime' from string to datetime
df["timestamp"] = pd.to_datetime(df["localtime"], format="%Y_%m_%d_%H_%M")
return df
def plot_monitorpi_data(df):
time = df["timestamp"]
columns_to_plot = [
("extvolt(V)", "External Voltage (V)"),
("cputemp(C)", "CPU Temperature (°C)"),
("envtemperature(C)", "Environment Temperature (°C)"),
("envhumidity(%)", "Environment Humidity (%)"),
("envpressure(hPa)", "Environment Pressure (hPa)")
]
num_plots = len(columns_to_plot)
fig, axs = plt.subplots(num_plots, 1, figsize=(12, 2.5 * num_plots), sharex=True)
for ax, (col, label) in zip(axs, columns_to_plot):
ax.plot(time, df[col], marker="o", linestyle="-")
ax.set_ylabel(label)
ax.grid(True)
axs[-1].set_xlabel("Time")
fig.suptitle("Raspberry Pi Sensor Readings Over Time", fontsize=14)
fig.tight_layout(rect=[0, 0, 1, 0.97])
plt.xticks(rotation=45)
plt.show()
def plot_monitorpi_fromcsv(filepath):
df = load_monitorpi_csv(filepath)
plot_monitorpi_data(df)
#################################################
# Loading data
#################################################
plt.rcParams.update({'font.size': 14})
SLOPE=1.0083421207668117
OFFSET=-295.30861087311496
def calibrate_data(dataraw):
data = np.zeros_like(dataraw,dtype=float)
if dataraw.dtype == np.uint16:
# calibrate only int16 data
data = (dataraw * SLOPE) + OFFSET
# convert into voltage
data = ( data / 65536.0 ) * 20.0 -10.0
else:
data = dataraw
print("No calibration, assuming data is calibrated.")
return data
def read_hdf5_times(pathh5,get_startime=False):
"""
Read 'end_time' and 'measure_time' from the 'voltage' dataset attributes
in an HDF5 file without loading the dataset itself.
Returns
(end_time, measure_time)
"""
with h5py.File(pathh5, 'r') as f:
attrs = f['voltage'].attrs
end_time = pd.to_datetime(attrs['end_time'], format="%Y_%m_%d_%H_%M")
measure_time = attrs['measure_time']
if get_startime:
start_time = pd.to_datetime(attrs['start_time'], format="%Y_%m_%d_%H_%M")
return end_time, measure_time, start_time
else:
return end_time, measure_time
def load_hdf5(pathh5):
'''
load hdf5 and extract metadata, convert into magnetic field value in μT
'''
f = h5py.File(pathh5,'r')
data = f['voltage']
dset = {}
dset['sample_rate'] = data.attrs['sample_rate']
dset['measure_time'] = data.attrs['measure_time']
dset['end_time'] = data.attrs['end_time']
dset['start_time'] = data.attrs['start_time']
data = np.array(data)
data = calibrate_data(data)
data = data * 1000./143
if data.ndim == 1 or data.shape[1] == 1:
# Single-channel case
dset['x'] = data.flatten()
else:
# Multi-channel case, assuming channel order 0, 1, 4 -> x, z, y
dset['x'] = data[:, 0]
dset['y'] = data[:, 2]
dset['z'] = data[:, 1]
return dset
def load_csv_pl(pathcsv):
df = pl.read_csv(pathcsv)
data = df.to_numpy()
data = calibrate_data(data)
data = data * 1000. / 143.
dset = {}
if data.ndim == 1 or data.shape[1] == 1:
# Single-channel case
dset['x'] = data.flatten()
else:
# Multi-channel case, assuming channel order 0, 1, 4 -> x, z, y
dset['x'] = data[:, 0]
dset['y'] = data[:, 2]
dset['z'] = data[:, 1]
return dset
def load_csv(pathcsv):
'''
load csv magnetometer result and convert into magnetic field value in μT
'''
data = np.genfromtxt(pathcsv,delimiter=',')
data = calibrate_data(data)
data = data * 1000. / 143.
dset = {}
if data.ndim == 1 or data.shape[1] == 1:
# Single-channel case
dset['x'] = data.flatten()
else:
# Multi-channel case, assuming channel order 0, 1, 4 -> x, z, y
dset['x'] = data[:, 0]
dset['y'] = data[:, 2]
dset['z'] = data[:, 1]
return dset
#################################################
# FFT Power Spectrum prelim (not used!)
#################################################
def fft_timeseries(chs,samplerate):
n = len(chs)
freq = np.fft.fftfreq(n, d=1/samplerate)
fft_values = np.fft.fft(chs)
return freq, fft_values.real, fft_values.imag
def fft_power_spectrum(chs,samplerate):
freq, real, imag = fft_timeseries(chs, samplerate)
power_spectrum = real**2 + imag**2
dt = 1. / samplerate
power_spectrum = power_spectrum * dt**2 / 2. /np.pi
return freq, power_spectrum
def fft_LSD(chs,samplerate):
freq, real, imag = fft_timeseries(chs, samplerate)
power_spectrum = real**2 + imag**2
dt = 1. / samplerate
N = len(chs)
psd = power_spectrum * dt / N / 2. / np.pi # Convert to power spectral density
LSD = np.sqrt(psd)
return freq, LSD
def fft_amplitude(chs, samplerate):
freq, real, imag = fft_timeseries(chs, samplerate)
amplitude = np.sqrt(real**2 + imag**2)
N = len(chs)
amplitude = amplitude / N * 2 # Normalize amplitude
return freq, amplitude
def slide_window_average(data,window_array):
# use np.convolve
return convolve(data, window_array, mode='wrap')
def gaussian_stft(chs, samplerate, g_std = 1000, g_length = 10000, mfft = 1000, hop=1000):
fs = samplerate
N = len(chs)
t_x = np.arange(N) * 1.0/ fs
w = gaussian(g_length,std=g_std,sym=True)
SFT = ShortTimeFFT(w, hop=hop, fs=fs, mfft=mfft, scale_to='magnitude')
Sx = SFT.stft(chs)
extent = SFT.extent(N)
window_width = SFT.m_num*SFT.T
sigma_t = g_std*SFT.T
delta_t = SFT.delta_t
delta_f = SFT.delta_f
res = {}
res['sx'] = Sx
res['extent'] = extent
res['window_width'] = window_width
res['sigma_t'] = sigma_t
res['delta_t'] = delta_t
res['delta_f'] = delta_f
return res
#################################################
# Signal Preprocessing
#################################################
# def lowpass(chs,fs,fmax):
#################################################
# FFT Power Spectrum and Power Spectral Density
#################################################
def compute_psd(chs,fs,nodc=True):
# remove DC
chs = chs - np.mean(chs)
N = len(chs)
# add window
ws = np.hamming(N)
ws1 = np.sum(ws)
ws2 = np.sum(ws**2)
# rfft
f = np.fft.rfftfreq(N,1./fs)
ak = np.fft.rfft(chs*ws)
# normalize into psd
psd = np.abs(ak)**2/fs/ws2
psd[1:-1] *= 2
if nodc:
f = f[1:]
psd = psd[1:]
return f,psd
def compute_ps(chs,fs,nodc=True):
# remove DC
chs = chs - np.mean(chs)
N = len(chs)
# add window
ws = np.hamming(N)
ws1 = np.sum(ws)
ws2 = np.sum(ws**2)
# rfft
f = np.fft.rfftfreq(N,1./fs)
ak = np.fft.rfft(chs*ws)
# normalize into psd
ps = np.abs(ak)**2/ws1**2
ps[1:-1] *= 2
if nodc:
f = f[1:]
ps = ps[1:]
return f,ps
def compute_ensemble_psd(chs,fs,Lbin:int,overlapratio:float=0.5,nodc=True,fmax:float=None):
Nhop = max(1, int(round(Lbin * (1.0 - overlapratio))))
N = len(chs)
nbins = (N-Lbin)//Nhop
f = np.fft.rfftfreq(Lbin,1./fs)
nf = len(f)
ensemb = np.zeros((nbins,nf))
for n in range(nbins):
binmin = n*Nhop
binmax = binmin+Lbin
_,psd = compute_psd(chs[binmin:binmax],fs,nodc=False)
ensemb[n] = psd
# avg_psd = np.mean(ensemb,axis=0)
if nodc:
f = f[1:]
ensemb = ensemb[:,1:]
if fmax is not None:
mask = f < fmax
ensemb = ensemb[mask]
f = f[mask]
return f,ensemb
def compute_averaged_psd(chs,fs,Lbin:int,overlapratio:float=0.5,nodc=True,fmax:float=None):
Nhop = max(1, int(round(Lbin * (1.0 - overlapratio))))
N = len(chs)
nbins = (N-Lbin)//Nhop
f = np.fft.rfftfreq(Lbin,1./fs)
nf = len(f)
ensemb = np.zeros((nbins,nf))
for n in range(nbins):
binmin = n*Nhop
binmax = binmin+Lbin
_,psd = compute_psd(chs[binmin:binmax],fs,nodc=False)
ensemb[n] = psd
avg_psd = np.mean(ensemb,axis=0)
if nodc:
f = f[1:]
avg_psd = avg_psd[1:]
if fmax is not None:
mask = f < fmax
avg_psd = avg_psd[mask]
f = f[mask]
return f,avg_psd
#################################################
# psd from-file save to file
#################################################
def segment_indices(N: int, Lbin: int, overlap: float) -> np.ndarray:
"""
Return start indices for overlapping segments of length Lbin.
overlap: fraction in [0,1). overlap=0.5 -> 50% overlap.
"""
if not (0 <= overlap < 1):
raise ValueError("overlap must be in [0,1).")
if Lbin <= 0 or Lbin > N:
raise ValueError("Lbin must be >0 and <= N.")
hop = max(1, int(round(Lbin * (1.0 - overlap))))
# Number of complete segments that fit
nbins = 1 + (N - Lbin) // hop
starts = np.arange(nbins) * hop
return starts.astype(int)
def ps_psd_from_windowed_fft(x_win: np.ndarray, fs: float, window: np.ndarray,
nodc: bool = True,fmax:float=None):
"""
Given a *windowed* real segment x_win (length L), compute:
- rFFT frequencies
- one-sided PS (|FFT|^2 normalization)
- one-sided PSD (per Hz)
Normalizations:
PS = |A_k|^2 / (sum(window))^2
PSD = |A_k|^2 / (fs * sum(window^2))
Both are doubled for bins 1..-2 (one-sided). DC & Nyquist kept then removed if nodc.
"""
L = len(x_win)
if len(window) != L:
raise ValueError("window length must equal segment length.")
# rFFT
A = np.fft.rfft(x_win)
f = np.fft.rfftfreq(L, d=1.0/fs)
ws1 = np.sum(window)
ws2 = np.sum(window**2)
# Raw two-sided power
pow_spec = np.abs(A)**2
# PS and PSD (two-sided)
ps = pow_spec / (ws1**2)
psd = pow_spec / (fs * ws2)
# Convert to one-sided (double interior bins for real signals)
if len(ps) > 2:
ps[1:-1] *= 2.0
psd[1:-1] *= 2.0
if nodc:
f = f[1:]
ps = ps[1:]
psd = psd[1:]
if fmax is not None:
mask = f < fmax
f = f[mask]
ps = ps[mask]
psd = psd[mask]
return f, ps, psd
# -------------------------------------
# Main pipeline: load -> segment -> save
# -------------------------------------
def save_windowed_ps_psd_hdf5(
h5_in: str,
out_dir: str | None = None,
channels=('x','y','z'),
Lbin: int = None,
overlap: float = 0.5,
nodc: bool = True,
fmax: float = None,
fmin:float = None
):
"""
1) Load HDF5 via your load_hdf5()
2) Segment into overlapping Hamming windows (length Lbin, fraction 'overlap')
3) Compute PS and PSD per segment for requested channels
4) Save structured HDF5 with:
/spectral (attrs: sample_rate, Lbin, overlap, window, nodc, start_time, end_time, measure_time)
/spectral/{ch}/
ps [nbins, nf]
psd [nbins, nf]
frequency [nf]
seg_start_ix [nbins]
seg_start_unix [nbins] (UNIX seconds for segment start time)
"""
# ---- Load raw data & metadata
dset = load_hdf5(h5_in) # uses your function (returns dict)
fs = float(dset['sample_rate'])
if Lbin is None:
if fmin is not None:
# print('! hint, no Lbin, taken from fmin')
N = len(dset['x'])
Lbin = int(round(fs / fmin))
else:
print('! Warning, no Lbin, taken default')
N = len(dset['x'])
Lbin = N//20
end_time_str = dset['end_time'] # e.g. "YYYY_MM_DD_HH_MM"
measure_time = float(dset['measure_time']) # seconds
# times
end_ts = pd.to_datetime(end_time_str, format="%Y_%m_%d_%H_%M")
start_ts = end_ts - pd.to_timedelta(measure_time, unit='s')
start_unix = start_ts.value / 1e9 # convert ns -> s
# Choose output path
in_path = Path(h5_in)
out_dir_path = Path(out_dir) if out_dir is not None else in_path.parent
out_dir_path.mkdir(parents=True, exist_ok=True)
# infer filename from input; ensure .hdf5 extension
if in_path.suffix.lower() in (".h5", ".hdf5"):
out_name = f"{in_path.stem}_spectral.hdf5"
else:
# input isn't .h5/.hdf5 — keep name and add suffix
out_name = f"{in_path.name}_spectral.hdf5"
h5_out = out_dir_path / out_name
# Validate presence of channels
available = [k for k in dset.keys() if k in ('x','y','z')]
chans = [ch for ch in channels if ch in available]
if not chans:
raise ValueError(f"No requested channels found in input. Available: {available}")
# Prepare segmentation on the *longest* requested channel
N = len(dset[chans[0]])
if any(len(dset[ch]) != N for ch in chans):
raise ValueError("All requested channels must have the same length.")
starts = segment_indices(N, Lbin=Lbin, overlap=overlap)
nbins = len(starts)
window = np.hamming(Lbin)
# Frequency vector from a dummy segment (we’ll store the one after nodc handling)
# build once
dummy_segment = (dset[chans[0]][starts[0]:starts[0]+Lbin] - np.mean(dset[chans[0]][starts[0]:starts[0]+Lbin]))
f_dummy, _, _ = ps_psd_from_windowed_fft(dummy_segment * window, fs, window, nodc=nodc,fmax=fmax)
nf = len(f_dummy)
# Segment absolute times
seg_start_unix = start_unix + starts / fs
# ---- Write out results
with h5py.File(h5_out, "w") as fout:
grp = fout.create_group("spectral")
# Global attrs
grp.attrs['sample_rate'] = fs
grp.attrs['Lbin'] = int(Lbin)
grp.attrs['overlap'] = float(overlap)
grp.attrs['window'] = "hamming"
grp.attrs['nodc'] = bool(nodc)
grp.attrs['measure_time'] = float(measure_time)
grp.attrs['start_time'] = start_ts.strftime("%Y_%m_%d_%H_%M")
grp.attrs['end_time'] = end_time_str
# Create a shared frequency dataset at top-level for convenience
grp.create_dataset("frequency", data=f_dummy)
# Also store segment indexing & times
grp.create_dataset("seg_start_ix", data=starts, dtype='i8')
grp.create_dataset("seg_start_unix", data=seg_start_unix, dtype='f8')
# Per-channel groups
for ch in chans:
ch_grp = grp.create_group(ch)
# Allocate matrices
ps_mat = np.empty((nbins, nf), dtype=np.float64)
psd_mat = np.empty((nbins, nf), dtype=np.float64)
x = np.asarray(dset[ch], dtype=float)
for i, s0 in enumerate(starts):
seg = x[s0:s0+Lbin]
# detrend / remove DC *before* windowing (as in your funcs)
seg = seg - np.mean(seg)
xw = seg * window
f, ps, psd = ps_psd_from_windowed_fft(xw, fs, window, nodc=nodc,fmax=fmax)
# f is same for all, already stored as /spectral/frequency
ps_mat[i, :] = ps
psd_mat[i, :] = psd
ch_grp.create_dataset("ps", data=ps_mat)
ch_grp.create_dataset("psd", data=psd_mat)
print(f"[ok] Saved windowed PS/PSD to: {h5_out}")
def save_averaged_ps_psd_hdf5(
h5_in: str,
out_dir: str | None = None,
channels=('x','y','z'),
Lbin: int = None,
overlap: float = 0.5,
nodc: bool = True,
fmax: float = None,
fmin:float = None
):
dset = load_hdf5(h5_in)
fs = float(dset['sample_rate'])
if Lbin is None:
if fmin is not None:
N = len(dset['x'])
Lbin = int(round(fs / fmin))
else:
# print('! Warning, no Lbin, taken default')
N = len(dset['x'])
Lbin = N//20
end_time_str = dset['end_time']
start_time_str = dset['start_time']
measure_time = dset['measure_time']
# times
end_ts = pd.to_datetime(end_time_str, format="%Y_%m_%d_%H_%M")
start_ts = pd.to_datetime(start_time_str, format="%Y_%m_%d_%H_%M")
start_unix = start_ts.value / 1e9
# Choose output path
in_path = Path(h5_in)
out_dir_path = Path(out_dir) if out_dir is not None else in_path.parent
out_dir_path.mkdir(parents=True, exist_ok=True)
# infer filename from input; ensure .hdf5 extension
if in_path.suffix.lower() in (".h5", ".hdf5"):
out_name = f"{in_path.stem}_spectral.hdf5"
else:
# input isn't .h5/.hdf5 — keep name and add suffix
out_name = f"{in_path.name}_spectral.hdf5"
h5_out = out_dir_path / out_name
# Validate presence of channels
available = [k for k in dset.keys() if k in ('x','y','z')]
chans = [ch for ch in channels if ch in available]
if not chans:
raise ValueError(f"No requested channels found in input. Available: {available}")
# Prepare segmentation on the *longest* requested channel
N = len(dset[chans[0]])
if any(len(dset[ch]) != N for ch in chans):
raise ValueError("All requested channels must have the same length.")
starts = segment_indices(N, Lbin=Lbin, overlap=overlap)
nbins = len(starts)
window = np.hamming(Lbin)
# Frequency vector from a dummy segment (we’ll store the one after nodc handling)
# build once
dummy_segment = (dset[chans[0]][starts[0]:starts[0]+Lbin] - np.mean(dset[chans[0]][starts[0]:starts[0]+Lbin]))
f_dummy, _, _ = ps_psd_from_windowed_fft(dummy_segment * window, fs, window, nodc=nodc,fmax=fmax)
nf = len(f_dummy)
# Segment absolute times
seg_start_unix = start_unix + starts / fs
# ---- Write out results
with h5py.File(h5_out, "w") as fout:
grp = fout.create_group("spectral")
# Global attrs
grp.attrs['sample_rate'] = fs
grp.attrs['Lbin'] = int(Lbin)
grp.attrs['overlap'] = float(overlap)
grp.attrs['window'] = "hamming"
grp.attrs['nodc'] = bool(nodc)
grp.attrs['measure_time'] = float(measure_time)
grp.attrs['start_time'] = start_time_str
grp.attrs['end_time'] = end_time_str
# Create a shared frequency dataset at top-level for convenience
grp.create_dataset("frequency", data=f_dummy)
# Also store segment indexing & times
grp.create_dataset("seg_start_ix", data=starts, dtype='i8')
grp.create_dataset("seg_start_unix", data=seg_start_unix, dtype='f8')
# Per-channel groups
for ch in chans:
ch_grp = grp.create_group(ch)
# Allocate matrices
ps_mat = np.empty((nbins, nf), dtype=np.float64)
psd_mat = np.empty((nbins, nf), dtype=np.float64)
x = np.asarray(dset[ch], dtype=float)
for i, s0 in enumerate(starts):
seg = x[s0:s0+Lbin]
# detrend / remove DC *before* windowing (as in your funcs)
seg = seg - np.mean(seg)
xw = seg * window
f, ps, psd = ps_psd_from_windowed_fft(xw, fs, window, nodc=nodc,fmax=fmax)
# f is same for all, already stored as /spectral/frequency
ps_mat[i, :] = ps
psd_mat[i, :] = psd
ps_avg = np.mean(ps_mat,axis=0)
psd_avg = np.mean(psd_mat,axis=0)
ch_grp.create_dataset("ps", data=ps_avg)
ch_grp.create_dataset("psd", data=psd_avg)
print(f"[ok] Saved averaged PS/PSD to: {h5_out}")
def load_averaged_ps_psd(h5_path: str, channel: str = "x"):
with h5py.File(h5_path, "r") as f:
if "spectral" not in f:
raise ValueError("File does not contain /spectral group. Is this the right HDF5?")
g = f["spectral"]
if channel not in ("x","y","z","amp","avg"):
raise ValueError(f"Channel '{channel}' not found.")
# read global attrs
fs = float(g.attrs["sample_rate"])
Lbin = int(g.attrs["Lbin"])
overlap = float(g.attrs["overlap"])
nodc = bool(g.attrs["nodc"])
start_time_str = g.attrs.get("start_time", None)
end_time_str = g.attrs.get("end_time", None)
measure_time = float(g.attrs.get("measure_time", np.nan))
# frequency axis and segment timing
frequency = g["frequency"][:]
# matrices
if channel in ("x","y","z"):
ps = g[channel]["ps"][:]
psd = g[channel]["psd"][:]
elif channel =="amp":
ps_x = g["x"]["ps"][:]
ps_y = g["y"]["ps"][:]
ps_z = g["z"]["ps"][:]
psd_x = g["x"]["psd"][:]
psd_y = g["y"]["psd"][:]
psd_z = g["z"]["psd"][:]
ps = ps_x+ps_y+ps_z
psd = psd_x+psd_y+psd_z
else:
# output avg of three directions
ps_x = g["x"]["ps"][:]
ps_y = g["y"]["ps"][:]
ps_z = g["z"]["ps"][:]
psd_x = g["x"]["psd"][:]
psd_y = g["y"]["psd"][:]
psd_z = g["z"]["psd"][:]
ps = (ps_x+ps_y+ps_z)/3.0
psd = (psd_x+psd_y+psd_z)/3.0
meta = {
"sample_rate": fs,
"Lbin": Lbin,
"overlap": overlap,
"nodc": nodc,
"start_time": start_time_str,
"end_time": end_time_str,
"measure_time": measure_time,
"n_freq": ps.shape[0]
}
return ps, psd, frequency, meta
def load_windowed_ps_psd(h5_path: str, channel: str = "x"):
"""
Load per-segment PS and PSD matrices plus segment-center datetimes
from the HDF5 produced by `save_windowed_ps_psd_hdf5`.
Parameters
----------
h5_path : str
Path to the spectral HDF5 file.
channel : {"x","y","z"}
Which channel group to read.
Returns
-------
ps : np.ndarray, shape (n_segments, n_freq)
One-sided power spectrum (windowed-segment PS).
psd : np.ndarray, shape (n_segments, n_freq)
One-sided power spectral density (per Hz).
seg_center_times : pd.DatetimeIndex, length n_segments
Center time of each segment as timezone-naive pandas timestamps.
frequency : np.ndarray, shape (n_freq,)
One-sided frequency axis corresponding to the matrix columns.
(If `nodc=True` during save, DC is omitted.)
meta : dict
Useful metadata: sample_rate, Lbin, overlap, nodc, start_time, end_time, measure_time.
"""
with h5py.File(h5_path, "r") as f:
if "spectral" not in f:
raise ValueError("File does not contain /spectral group. Is this the right HDF5?")
g = f["spectral"]
if channel not in ("x","y","z","amp","avg"):
raise ValueError(f"Channel '{channel}' not found.")
# read global attrs
fs = float(g.attrs["sample_rate"])
Lbin = int(g.attrs["Lbin"])
overlap = float(g.attrs["overlap"])
nodc = bool(g.attrs["nodc"])
start_time_str = g.attrs.get("start_time", None)
end_time_str = g.attrs.get("end_time", None)
measure_time = float(g.attrs.get("measure_time", np.nan))
# frequency axis and segment timing
frequency = g["frequency"][:]
seg_start_ix = g["seg_start_ix"][:]
seg_start_unix = g["seg_start_unix"][:]
# center time = start + (Lbin/2)/fs
center_unix = seg_start_unix + (Lbin / (2.0 * fs))
seg_center_times = pd.to_datetime(center_unix, unit="s")
# matrices
if channel in ("x","y","z"):
ps = g[channel]["ps"][:]
psd = g[channel]["psd"][:]
elif channel == "amp":
ps_x = g["x"]["ps"][:]
ps_y = g["y"]["ps"][:]
ps_z = g["z"]["ps"][:]
psd_x = g["x"]["psd"][:]
psd_y = g["y"]["psd"][:]
psd_z = g["z"]["psd"][:]
ps = ps_x+ps_y+ps_z
psd = psd_x+psd_y+psd_z
else:
ps_x = g["x"]["ps"][:]
ps_y = g["y"]["ps"][:]
ps_z = g["z"]["ps"][:]
psd_x = g["x"]["psd"][:]
psd_y = g["y"]["psd"][:]
psd_z = g["z"]["psd"][:]
ps = (ps_x+ps_y+ps_z)/3.0
psd = (psd_x+psd_y+psd_z)/3.0
meta = {
"sample_rate": fs,
"Lbin": Lbin,
"overlap": overlap,
"nodc": nodc,
"start_time": start_time_str,
"end_time": end_time_str,
"measure_time": measure_time,
"n_segments": ps.shape[0],
"n_freq": ps.shape[1],
}
return ps, psd, seg_center_times, frequency, meta
#################################################
# PLotting
#################################################
def plot_sample_ps(path,fs=1000.,ax=None,label=None,orientation=['x','y','z'],alpha=0.6):
if path.endswith('.hdf5'):
print('->loading hdf5...',end='\r')
dset = load_hdf5(path)
if path.endswith('.csv'):
print('->loading csv ...',end='\r')
dset = load_csv_pl(path)
print('->load complete ',end='\r')
if ax is None:
fig,ax = plt.subplots(figsize = (8,6))
ax.set_xlabel('f[Hz]')
ax.set_ylabel(r'FFT Amplitude[$\mu T$]')
else:
fig=None
for vec in orientation:
print('->plotting direction '+vec,end='\r')
data = dset[vec]
f,ps = compute_ps(data,fs)
ax.loglog(f,np.sqrt(ps),label=label+vec,alpha=alpha)
print('plot complete '+path[-20:-4]+'.')
return fig,ax
def plot_sample_psd(path,fs=1000.,ax=None,label=None,orientation=['x','y','z'],Lbin=None,overlap=0.5,alpha=0.6,fmax:float=None,fmin:float=None):
print('plot sample '+path+':')
if path.endswith('.hdf5'):
print('->loading hdf5...',end='\r')
dset = load_hdf5(path)
if path.endswith('.csv'):
print('->loading csv ...',end='\r')
dset = load_csv_pl(path)
print('->load complete ',end='\r')
if Lbin is None:
if fmin is not None:
print('! hint, no Lbin, taken from fmin')
N = len(dset['x'])
Lbin = int(round(fs / fmin))
else:
print('! Warning, no Lbin, taken default')
N = len(dset['x'])
Lbin = N//20
if ax is None:
fig,ax = plt.subplots(figsize = (8,6))
ax.set_xlabel('f[Hz]')
ax.set_ylabel(r'LSD[$\mu T/ \sqrt{Hz}$]')
else:
fig=None
for vec in orientation:
print('->plotting direction '+vec,end='\r')
data = dset[vec]
f,ps = compute_averaged_psd(data,fs,Lbin,overlap,fmax=fmax)
ax.loglog(f,np.sqrt(ps),label=vec,alpha=alpha)
print('plot complete '+path+'.')
return fig,ax
#################################################
# Spectral batch analysis
#################################################
def spectral_average(
h5_path: str,
channel: str = "x",
which: str = "psd", # {"ps", "psd"}
):
ps, psd, seg_center_times, f, meta = load_windowed_ps_psd(h5_path, channel=channel)
ps = np.mean(ps,axis=0)
psd = np.mean(psd,axis=0)
ps = np.sqrt(ps)
psd = np.sqrt(psd)
if which =='psd':
return psd,meta
elif which =='ps':
return ps,meta
def spectral_load_average(
h5_path: str,
channel: str = 'x',
which:str = 'psd'
):
ps, psd, frequency, meta = load_averaged_ps_psd(h5_path,channel=channel)
ps = np.sqrt(ps)
psd = np.sqrt(psd)
if which =='psd':
return psd,meta
elif which =='ps':
return ps,meta
def spectral_directory_read(
h5_dir:str|None = None,
h5_pathlist = None,
channel: str = "x",
which:str = 'psd',
min_timespan: float|None = None,
time_of_day: tuple = None,
averaged_sample: bool=False
):
if h5_dir is not None:
names = sorted(os.listdir(h5_dir))
h5_pathlist = [
os.path.join(h5_dir, n)
for n in names
if n.lower().endswith((".h5", ".hdf5"))
]
elif h5_pathlist is None:
raise ValueError("Must provide either a data directory (h5_dir) or a list of paths (h5_pathlist).")
spectrum_list = []
time_center_list = []
for h5_path in h5_pathlist:
try:
if averaged_sample == False:
spectrum, meta = spectral_average(h5_path, channel=channel, which=which)
else:
spectrum, meta = spectral_load_average(h5_path,channel=channel,which=which)
except Exception as e:
print(f"! skip {h5_path}: {e}")
continue
fmt = "%Y_%m_%d_%H_%M"
start_time = pd.to_datetime(meta["start_time"], format=fmt)
end_time = pd.to_datetime(meta["end_time"],format=fmt)
centre_time = start_time + (end_time - start_time)/2
hour = centre_time.hour
measure_time = (end_time - start_time).seconds
if (min_timespan is not None) and (measure_time < float(min_timespan)):
print(f'! warn: {h5_path} too short (measure_time={measure_time}s < {min_timespan}s); skipping')
continue
if time_of_day is not None:
lo, hi = time_of_day
if not (0 <= lo <= 23 and 0 <= hi <= 23):
raise ValueError("time_of_day must be a pair of ints in [0, 23].")
# Inclusive range; handle overnight windows (e.g., 22→6)
if lo <= hi:
in_window = (lo <= hour <= hi)
else:
in_window = (hour >= lo) or (hour <= hi)
if not in_window:
continue
spectrum_list.append(spectrum)
time_center_list.append(centre_time)
if len(time_center_list) > 1:
order = np.argsort(np.array(time_center_list, dtype="datetime64[ns]"))
spectrum_list = [spectrum_list[i] for i in order]
time_center_list = [time_center_list[i] for i in order]
return spectrum_list,time_center_list
def _xedges_from_centers(f: np.ndarray,strictly_positive=True) -> np.ndarray:
"""Build pcolormesh x-edges from center frequencies (linear scale)."""
f = np.asarray(f, float)
df = np.diff(f)
# interior edges: midpoints
mids = f[:-1] + 0.5 * df
# extrapolate first/last edge
first = f[0] - 0.5 * df[0] if len(df) else f[0] - 0.5
last = f[-1] + 0.5 * df[-1] if len(df) else f[0] + 0.5
if not strictly_positive:
return np.concatenate([[first], mids, [last]])
else:
return np.concatenate([mids, [last]])
def compute_amplitude_hist_per_frequency(
spectra_list: list[np.ndarray],
f: np.ndarray,
val_bins: int | np.ndarray = 200,
val_range: tuple[float, float] | None = None,
log_val: bool = True,
density: bool = False,
):
# ---- Consistency check
f = np.asarray(f, float)
Nf = f.size
for i, S in enumerate(spectra_list):
if np.asarray(S).ndim != 1 or len(S) != Nf:
raise ValueError(f"spectra_list[{i}] has length {len(S)} != Nf={Nf}")
# ---- Stack to (M, Nf)
S = np.vstack([np.asarray(s, float) for s in spectra_list]) # M x Nf
# ---- Choose amplitude range
if val_range is None:
# robust percentiles across all positive values (useful if log bins)