-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnalyzeData.py
executable file
·1115 lines (750 loc) · 37 KB
/
AnalyzeData.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
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
from __future__ import print_function, division, absolute_import
import argparse
import os
import sys
import math
import csv
from datetime import datetime
from GetRDMConfig import RDMConfig, readConfig
import numpy as np
import scipy.signal
import scipy.optimize
import scipy.interpolate
import matplotlib.mlab
from getRDMData import getRDMData
def initNotchFilter(sps, freq, band, ripple, order, filter_type):
""" Initializes a noth filter with the given parameters.
Arguments:
sps: [float] Samples per second.
freq: [float] Middle frequency of the filter in Hz.
band: [float] Width of the band in Hz.
ripple: [float] Ripple in the bandpass.
order: [int] Filter order.
filter_type: [str] Filter type, e.g. 'cheby1'
Return:
(b, a): [tuple] Filter parameters.
"""
# Compute Nyquist frequency
nyq = sps/2.0
# Lower frequency cutoff
low = freq - band/2.0
# Upper frequency cutoff
high = freq + band/2.0
low = low/nyq
high = high/nyq
# Init the filter
b, a = scipy.signal.iirfilter(order, [low, high], rp=ripple, btype='bandstop', analog=False,
ftype=filter_type)
return b, a
def butterworthBandpassFilter(lowcut, highcut, fs, order=5):
""" Butterworth bandpass filter.
Argument:
lowcut: [float] Lower bandpass frequency (Hz).
highcut: [float] Upper bandpass frequency (Hz).
fs: [float] Sampling rate (Hz).
Keyword arguments:
order: [int] Butterworth filter order.
Return:
(b, a): [tuple] Butterworth filter.
"""
# Calculate the Nyquist frequency
nyq = 0.5*fs
low = lowcut/nyq
high = highcut/nyq
# Init the filter
b, a = scipy.signal.butter(order, [low, high], analog=False, btype='band')
# Check if the filter is unstable
if np.all(np.abs(np.roots(a)) < 1):
print("""The filter with bands from {:.2f} Hz to {:.2f} Hz is unstable and should not be \
used! It's roots are smaller than 1.""".format(
lowcut, highcut))
return b, a
def filterBandpass(data, sps, bandpass_low, bandpass_high, order=6):
""" Run bandpass filtering. """
# Init the butterworth bandpass filter
butter_b, butter_a = butterworthBandpassFilter(bandpass_low, bandpass_high, sps, order=order)
# Filter the data
waveform_data = scipy.signal.lfilter(butter_b, butter_a, np.copy(data))
return waveform_data
def filterLP(data, sps, mains_freq, lowpass=True, filter_order=3, additional=None, low_pass_cutoff=500):
""" Filter out the light pollution using Chebyshev filters.
Arguments:
data: [ndarray] Unfiltered data.
sps: [float] Samples per second.
mains_freq: [float] Electric grid frequency in Hz (50 for Europe, 60 for NA).
Keyword arguments:
lowpass: [bool] Apply at lowpass filter (cutoff freq at the mains freq). True by default.
filter_oder: [int] Order of the Chebyasev filter (3 by default).
additional: [list] A list of (frequency, band width) tuples which define additional frequencies for
filtering.
Return:
[ndarray]: Filtered data.
"""
filter_type = 'cheby1'
ripple = 10.0
# Generate filter parameters for all harmonics
filters_params = []
for i in range(int((sps/2)/mains_freq)):
# Compute the current harmonic frequency
f_har = mains_freq*(i + 1)
# If the lowpass filter is on, skip all frequencies above 5x the mains frequency
if lowpass:
if f_har > 2*low_pass_cutoff:
continue
# Set proper filter band width, depending on the harmonic number (first ones are wider)
if i == 0:
band_har = 7.5
elif i == 1:
band_har = 5.5
else:
band_har = 3.5
filters_params.append([sps, f_har, band_har, ripple, filter_order, filter_type])
if additional is not None:
for freq, band in additional:
# If the lowpass filter is on, skip all frequencies above 5x the mains frequency
if lowpass:
if freq > 2*low_pass_cutoff:
continue
filters_params.append([sps, freq, band, ripple, filter_order, filter_type])
filtered_data = np.copy(data)
# Detrend the data
filtered_data = scipy.signal.detrend(filtered_data)
# Filter data using notch filters
for filt_param in filters_params:
print(filt_param)
# Init the filter
b, a = initNotchFilter(*filt_param)
# Filter the data
filtered_data = scipy.signal.lfilter(b, a, filtered_data)
if lowpass:
### Apply a lowpass filter which will filter out everything above the grid frequency ###
# Init the lowpass filter
Wn = low_pass_cutoff/(sps/2.0)
b, a = scipy.signal.butter(6, Wn)
# Filter the data
filtered_data = scipy.signal.filtfilt(b, a, filtered_data)
##############################
return filtered_data
def movingAverage(arr, n=3):
""" Perform a moving average on an array with the window size n.
Arguments:
arr: [ndarray] Numpy array of values.
Keyword arguments:
n: [int] Averaging window.
Return:
[ndarray] Averaged array. The size of the array is always by n-1 smaller than the input array.
"""
ret = np.cumsum(arr, dtype=float)
ret[n:] = ret[n:] - ret[:-n]
return ret[n - 1:]/n
def datestr2UnixTime(time_str, UT_corr=0.0):
""" Convert date and time to Unix time.
Arguments:
time_str: [str]
Kwargs:
millisecond: [int] milliseconds (optional)
UT_corr: [float] UT correction in hours (difference from local time to UT)
Return:
[float] Unix time
"""
# Convert the time string to datetime
dt = datetime.datetime.strptime(time_str, "%Y%m%d-%H%M%S.%f") - datetime.timedelta(hours=UT_corr)
# UTC unix timestamp
unix_timestamp = (dt - datetime.datetime(1970, 1, 1)).total_seconds()
return unix_timestamp
def sine(t, a, f, phi, c):
return np.abs(a)*np.sin(2*np.pi*f*t + phi%(2*np.pi)) + c
def fitSine(time_data, intensity_data, f0, p0 = None):
""" Fits a sine to the given data.
Arguments:
f0: [float] Initial guess of the frequency.
"""
a0 = np.std(intensity_data)
phi0 = 0
c0 = np.mean(intensity_data)
# Initial guess
if(p0 is None):
p0 = [a0, f0, phi0, c0]
popt, _ = scipy.optimize.curve_fit(sine, time_data, intensity_data, p0=p0)
a, f, phi, c = popt
popt[0] = np.abs(a)
popt[2] = phi%(2*np.pi)
return popt
def sineSlide(time_data, intensity_data, f0, window_width, shift_width):
""" Fits a sines over windows that cover the entire signal to estimate the frequency drift.
Arguments:
time_data: [list of floats] The time stamp of the samples.
intensity_data: [list of ints] The intensity of the data.
f0: [float] Initial guess of the frequency.
window_width: [float] Window width in seconds.
shift_width: [float] How far over the window shifts for the next fitting.
"""
times = []
amplitudes = []
frequencies = []
phases = []
offsets = []
residuals = []
time_relative = time_data - np.min(time_data)
sps = len(time_relative)/(time_relative[-1] - time_relative[0])
width = math.ceil(sps*window_width)
shift = math.ceil(sps*shift_width)
loops = int((len(intensity_data) - width)//shift)
print("Data points:", len(intensity_data))
print("SPS:", sps)
print("Samples per windows:", width)
print("Samples per window shift:", shift)
print("Number of window shifts:", loops)
for i in range(loops):
temp_time = time_relative[int(i*shift): int((i*shift) + width)]
temp_data = intensity_data[int(i*shift): int((i*shift) + width)]
if(i == 0):
sine_fit = fitSine(temp_time, temp_data, f0=f0)
else:
sine_fit = fitSine(temp_time, temp_data, f0=f0, p0 = [amplitudes[i-1], frequencies[i-1], phases[i-1], offsets[i-1]])
times.append([temp_time[0] + np.min(time_data), temp_time[-1] + np.min(time_data)])
amplitudes.append(sine_fit[0])
frequencies.append(sine_fit[1])
phases.append(sine_fit[2])
offsets.append(sine_fit[3])
# Compute the sttdev of residuals
temp_fit = list(sine_fit)
temp_fit[-1] = 0
res_data = temp_data - sine(temp_time, *sine_fit)
# plt.plot(temp_time, temp_data, label='original')
# plt.plot(temp_time, temp_data - sine(temp_time, *temp_fit), label='filtered')
# plt.plot(temp_time, sine(temp_time, *sine_fit), label='fit')
# plt.legend()
# plt.show()
residuals.append(np.std(res_data))
if(i != (loops - 1)):
print("Sines fitted: {:.2%}".format(i/(loops - 1)),end = "\r")
else:
print("Sines fitted: {:.2%}".format(i/(loops - 1)),end = "\n")
# Unwrap the phase
phases = np.unwrap(phases)
coefs = [times, amplitudes, frequencies,phases, offsets, residuals]
return coefs
def filterInterpolatedSines(time_data, intensity_data, sine_fits):
sine_times_unix, amplitudes, frequencies, phases, offsets = sine_fits
amp_interp = scipy.interpolate.PchipInterpolator(sine_times_unix, amplitudes)
freq_interp = scipy.interpolate.PchipInterpolator(sine_times_unix, frequencies)
phase_interp = scipy.interpolate.PchipInterpolator(sine_times_unix, phases)
offset_interp = scipy.interpolate.PchipInterpolator(sine_times_unix, offsets)
fitted_sines = sine(time_data, amp_interp(time_data), freq_interp(time_data), phase_interp(time_data), offset_interp(time_data))
filtered_data = intensity_data - sine(time_data, amp_interp(time_data), freq_interp(time_data), phase_interp(time_data), np.zeros_like(time_data))
return filtered_data, fitted_sines
def generateEventName(station_code, station_channel, unix_times):
""" Generate a template file name for the event. """
beg_time = datetime.utcfromtimestamp(unix_times[0])
end_time = datetime.utcfromtimestamp(unix_times[-1])
event_name = "{:s}_{:s}_{:04d}{:02d}{:02d}-{:02d}{:02d}{:02d}.{:06d}_{:02d}{:02d}{:02d}.{:06d}".format(
station_code,
station_channel,
beg_time.year,
beg_time.month,
beg_time.day,
beg_time.hour,
beg_time.minute,
beg_time.second,
beg_time.microsecond,
end_time.hour,
end_time.minute,
end_time.second,
end_time.microsecond
)
return event_name
def exportCSV(dir_path, station_code, station_channel, unix_times, intensity_data, filtered):
# Create an event name
event_name = generateEventName(station_code, station_channel, unix_times)
if(filtered):
file_name = "{:s}_filtered.csv".format(event_name)
else:
file_name = "{:s}.csv".format(event_name)
header = ["# Unix Times", "Intensity"]
data = [[unix_times[i], intensity_data[i]] for i in range(len(unix_times))]
with open(os.path.join(dir_path, file_name), 'w') as csvfile:
file = csv.writer(csvfile)
file.writerow(header)
file.writerows(data)
def backgroundPSDModel(coefs, x, y=None):
""" Three term gaussian model used to model the background frequency.
f(x) = a1*exp(-((x-b1)/c1)^2) + a2*exp(-((x-b2)/c2)^2) + a3*exp(-((x-b3)/c3)^2) + d
"""
# Compute the function value
val = coefs[0] \
+ coefs[1]*np.exp(-((x - coefs[2])/coefs[3])**2) \
+ coefs[4]*np.exp(-((x - coefs[5])/coefs[6])**2) \
+ coefs[7]*np.exp(-((x - coefs[8])/coefs[9])**2)
# If the data are given, compute the residuals
if y is not None:
return val - y
else:
return val
def backgroundPSDModelResidualSum(coefs, x, y):
# Squared value of each residual
res = backgroundPSDModel(coefs, x, y=y)**2
# Smooth approximation of l1 (absolute value) loss
return np.sum(2*((1 + res)**0.5 - 1))
def linearBackgroundModel(coefs, x, y=None):
# Compute the function value
val = coefs[0] + coefs[1]*x
# If the data are given, compute the residuals
if y is not None:
return val - y
else:
return val
def showNightPlot(event_time, station_code, station_channel):
""" Given the time and the station, show the night plot. """
yr_mon_day = event_time[:8]
# Find the appropriate night folder
night = [folder for folder in os.listdir(archived_data_path) if folder.startswith(cml_args.code \
+ "_" + cml_args.channel + "_" + yr_mon_day)]
if not night:
print('The night was not found, night plot will not be shown!')
return False
# Get a list of PNGs in a given folder
pngs = [plot for plot in os.listdir(os.path.join(archived_data_path, *night)) if(plot.endswith(".png"))]
if not pngs:
print('The night plot PNG was not found!')
return False
night_plot = os.path.join(os.path.join(archived_data_path, *night),pngs[0])
max_minus = os.path.join(os.path.join(archived_data_path, *night),pngs[1])
img = mpimg.imread(night_plot)
fig = plt.imshow(img)
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.tight_layout()
plt.box(on=None)
plt.show()
img = mpimg.imread(max_minus)
fig = plt.imshow(img)
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.tight_layout()
plt.box(on=None)
plt.show()
if __name__ == "__main__":
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Name of the config file
CONFIG = "config.txt"
# Default path to data files
data_path = os.path.expanduser("~/RadiometerData")
archived_data_path = os.path.expanduser("~/RadiometerData/ArchivedData")
work_dir = os.getcwd()
home_dir = os.path.dirname(work_dir)
csv_storage_dir = "Exported_Data"
csv_path = os.path.join(home_dir,csv_storage_dir)
# Create a directory on the disk for exporting events
if (not os.path.isdir(csv_path)):
os.mkdir(csv_path, 0o755)
# Set up input arguments
arg_p = argparse.ArgumentParser(description="Get radiometer files.")
arg_p.add_argument('code', metavar='CODE', nargs='?', \
help="""Radiometer station code, e.g. US0001. The first two letters are the ISO code of the country,
and the last 4 characters is the alphanumeric code of the station.""", type=str, default=None)
arg_p.add_argument('channel', metavar='CHANNEL', nargs='?', \
help='Channel of the station. Only single letters are accepted, such as A, B, C, etc.', type=str, \
default=None)
arg_p.add_argument('time', metavar='TIME', nargs='?', \
help="""Time of the event in the YYYYMMDD-HHMMSS.ms format. Data will be centred around this time.
""", type=str, default=None)
arg_p.add_argument('range', metavar='DURATION_SECONDS', type=float, nargs=1,
help="""Duration in seconds of the data chunk that will be taken. The data will be taken in the range
of (-range/2, range/2), centered around the given time.""")
arg_p.add_argument('-f', '--mainsfreq', metavar='MAINS_FREQ', nargs=1, \
help="Frequency of the mains hum.", type=float)
arg_p.add_argument('-e', '--exportcsv', action = 'store_true', \
help="""If enabled produces and exports a csv file.""")
arg_p.add_argument('-n', '--nightplot', action = 'store_true', \
help="""Loads the night plots. They show an overview of all recorded intensities during the night.""")
arg_p.add_argument('-p', '--showplots', action = 'store_true', \
help="""Show the plots on the screen.""")
arg_p.add_argument('-a', '--archivepath', metavar='ARCHIVE_PATH', type=str,
help="""Path to the directory with archived. If not given, the default path will be used.""",
default=None)
arg_p.add_argument('-i', '--integrate', action = 'store_true', \
help="""If enabled, the program will attempt to automatically find the fireball and
compute the integrated area under the curve.""")
arg_p.add_argument('--addharms', type=float, nargs='+', \
help="""List of additional harmonics to remove from the spectrum. Only the first frequency should be given, the higher order harmonics will be assumed to be multiples of the mains frequency.""")
# Parse input arguments
cml_args = arg_p.parse_args()
cml_args.range = cml_args.range[0]
##########################################################################################################
# Check if there is a config file in either the source dir of the data dir
config_path_workdir = os.path.join(work_dir, "config.txt")
config_path_datadir = os.path.join(data_path, "config.txt")
config_path = None
if os.path.isfile(config_path_workdir):
config_path = config_path_workdir
elif os.path.isfile(config_path_datadir):
config_path = config_path_datadir
else:
print()
print("Could not find the config file in:\n{:s}\nnor in:\n{:s},\nusing default values!\n".format(
config_path_workdir, config_path_datadir))
config = RDMConfig()
# If the config file exists, read it
if config_path is not None:
# Read the config in the lib path
config = readConfig(os.path.join(config_path))
print("Using config file: {:s}".format(config_path))
# Check if the server flag is set in the config
if config.read_from_server:
archived_data_path = os.path.join(os.path.join("/home", "rdm_" + cml_args.code.lower()), "files")
# Assign the mains frequency hum if given
if cml_args.mainsfreq:
config.mains_frequency = cml_args.mainsfreq[0]
print("Using mains frequency: {:d} Hz".format(int(config.mains_frequency)))
# Take the archive path from the command line if given
if cml_args.archivepath:
archived_data_path = cml_args.archivepath
if not os.path.exists(archived_data_path):
print('The archived data path: {:s} does not exist!'.format(archived_data_path))
# Extract the additional harmonics to remove from the spectrum
additional_harmonics = None
if cml_args.addharms:
additional_harmonics = cml_args.addharms
print("Additional harmonics to remove: ", additional_harmonics)
# Gather the radiometric data and the time stamps around the given time period
intensity, unix_times = getRDMData(archived_data_path, cml_args.code, cml_args.channel, cml_args.time, cml_args.range)
filtered = False
# Make a name for the event
event_name = generateEventName(cml_args.code, cml_args.channel, unix_times)
# Export data to CSV
if cml_args.exportcsv:
exportCSV(csv_path, cml_args.code, cml_args.channel, unix_times, intensity, filtered)
filtered = True
# Show the night plot
if cml_args.nightplot:
showNightPlot(cml_args.time, cml_args.code, cml_args.channel)
# Compute relative time since the beginning of the recording
time_relative = unix_times - np.min(unix_times)
# Create a list containing all times as datetime objects
all_datetime = [datetime.utcfromtimestamp(t) for t in unix_times]
# Compute samples per second
sps = len(time_relative)/(time_relative[-1] - time_relative[0])
print('SPS:', sps)
# Design notch filter
# Filtering setup
# Redefine the sampling frequency for simplicity
fs = sps # Sample frequency (Hz)
# Calculate the original power spectral density function and clear the figure
p_xx, freqs = plt.psd(intensity, Fs=sps, detrend='linear', NFFT=2048, noverlap=0)
plt.clf()
# Generate an array that contains the frequencies found in the mains hum
# Mains hum being the 60/50 Hz interference and its higher order harmonics
mains_hum = np.arange(config.mains_frequency, fs/2, config.mains_frequency)
# Add additional harmonics if given
if additional_harmonics is not None:
for harm in additional_harmonics:
# Generate frequencies of the harmonics, with the same multiple as the mains hum
harm_hum = np.arange(harm, fs/2, config.mains_frequency)
# Add the harmonics to the list of mains hum frequencies
mains_hum = np.append(mains_hum, harm_hum)
# Sort the list of mains hum frequencies
mains_hum = np.sort(mains_hum)
# Convert the powers into decibels (dB)
power_db = 10*np.log10(p_xx)
# Create a list of indices where all those frequencies are
index = []
for i in range(len(mains_hum)):
index = np.append(index, np.abs(freqs - mains_hum[i]).argmin()).astype(int)
mean_indices = np.copy(index)
mean_indices = np.append(np.array([0]), mean_indices)
# Define a list to store the indices of the maximum powers, which are near but not exactly the same as the main frequency indices
max_powers = []
# Define a list to store the indices of the powers that aren't the mains, in order to make a fit that doesn't depend on the peaks
fit_powers = list(np.copy(power_db))
fit_freqs = list(np.copy(freqs))
# Search 10 elements on either side of the nearest mains hum for the peak power index
# This will be identified as the center of the mains hum harmonic
search_width = 10
for i in range(len(mains_hum)):
max_powers = np.append(max_powers,index[i] - search_width + (p_xx[index[i]-search_width:index[i]+search_width]).argmax()).astype(int)
for i in range(len(mains_hum)):
j = len(mains_hum) - i - 1
del fit_powers[max_powers[j] - search_width:max_powers[j] + search_width]
del fit_freqs[max_powers[j] - search_width:max_powers[j] + search_width]
# Begin a least-squares optimization fit that follows the noise in the psd
x0 = np.array([np.median(fit_powers), # Background
np.mean(fit_powers), 2.0, 5.0, # First gaussian (mostly DC component)
0.0, config.mains_frequency, 1.0, # Second Gaussian
0.0, 2*config.mains_frequency, 1.0 # Third Gaussian
])
# Fit the function using Nelder-Mead
res = scipy.optimize.minimize(backgroundPSDModelResidualSum, x0, args=(fit_freqs, fit_powers),
method='Nelder-Mead')
### OLD CODE ###
# Begin initial guess's, 0 where additions/subtractions are and 1 where multiplications/divisions are
#x0 = np.array([0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0])
# The optimization function in question is a three term gaussian model
# f(x) = a1*exp(-((x-b1)/c1)^2) + a2*exp(-((x-b2)/c2)^2) + a3*exp(-((x-b3)/c3)^2) + d
# # Fit the function using least-squares
# res = scipy.optimize.least_squares(backgroundPSDModel, x0, loss='soft_l1', args=(fit_freqs, fit_powers))
### ###
print("Three term gaussian fit on the PSD of the background noise")
print(res.x)
# Calculate the mean power using the least-squares solution
power_means = backgroundPSDModel(res.x, freqs)
# Calculate the standard deviation of the fit
power_std = np.std(power_db)
power_lower_lim = power_means - power_std/4.0
power_upper_lim = power_means + power_std/4.0
# Plot the raw signal
plt.plot(freqs, power_db, label='Raw signal PSD')
# Plot the fit with a half-sigma sigma confidence bound for filtering
plt.plot(freqs, power_means, 'g', label='Background power fit')
plt.plot(freqs, power_lower_lim, linestyle = '--', color = 'm', label=r'Confidence region: $\frac{1}{4}$$\sigma$')
plt.plot(freqs, power_upper_lim, linestyle = '--', color = 'm')
# Plot the identified peaks of the mains hum
plt.plot(freqs[max_powers], 10*np.log10(p_xx[max_powers]), 'r+', label='Mains hum peaks')
plt.title("Initial Power spectral Density of Raw Signal")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Power Spectral Density (dB/Hz)")
plt.grid(which='both')
plt.legend(loc=0)
plt.savefig(os.path.join(csv_path, event_name + "_psd.png"), dpi=150)
if cml_args.showplots:
plt.show()
else:
plt.clf()
plt.close()
# Begin recursive filtering of mains hum harmonics
filtered_data = np.copy(intensity)
good_filtered_data = np.copy(filtered_data)
i = 0
for peak_freq in max_powers:
# Read the power at the peak
power_to_verify = power_db[peak_freq]
# Read the boundaries of the confidence region
lower_bound = power_lower_lim[peak_freq]
background_power = power_means[peak_freq]
upper_bound = power_upper_lim[peak_freq]
# Define the notch filter parameters
w0 = mains_hum[i]/(fs/2)
Q = 1.0
print("Filtering {:}Hz".format(int(mains_hum[i])))
def applyNotchFilter(data, w0, Q):
""" Apply a notch filter to the data at the considered frequency
Arguments:
data: [ndarray] The data to be filtered
w0: [float] The normalized frequency to be filtered
Q: [float] The quality factor of the filter
Returns:
[ndarray] The filtered data
"""
# Apply a notch filter to the data at the considered frequency
b, a = scipy.signal.iirnotch(w0, Q)
return scipy.signal.filtfilt(b, a, data)
# Treat the quality factor as a variable to be optimized. Minimize the difference between the
# predicted background power and the filtered power at the peak frequency
def filterResiduals(Q, w0, data, background_power, peak_freq):
# Apply a notch filter to the data at the considered frequency
filtered_data = applyNotchFilter(data, w0, Q)
# Compute the PSD of the filtered data)
#p_xx, f = plt.psd(filtered_data, Fs=sps, detrend='linear', NFFT=2048, noverlap=0)
#plt.clf()
p_xx, _ = matplotlib.mlab.psd(filtered_data, Fs=sps, detrend='linear', NFFT=2048, noverlap=0)
# Compute the power in dB
power_db = 10*np.log10(p_xx)
# Return the difference between the background power and the filtered power at the peak frequency
return (background_power - power_db[peak_freq])**2
# Begin the optimization
res = scipy.optimize.minimize(filterResiduals, Q,
args=(w0, good_filtered_data, background_power, peak_freq),
method='Nelder-Mead')
print(" - Optimized notch filter Q = {:.2f}".format(res.x[0]))
# Apply the optimized filter
good_filtered_data = applyNotchFilter(good_filtered_data, w0, res.x[0])
# ### OLD METHOD ###
# # Increase the notch filter quality factor until the power at the peak is within the confidence region
# while (power_to_verify > upper_bound) or (power_to_verify < lower_bound):
# # Apply a notch filter to the data at the considered frequency
# b, a = scipy.signal.iirnotch(w0, Q)
# #filtered_data = scipy.signal.lfilter(b, a, good_filtered_data) # old code, broken now
# filtered_data = scipy.signal.filtfilt(b, a, good_filtered_data)
# # Compute the PSD of the filtered data
# p_xx, f = plt.psd(filtered_data, Fs=sps, detrend='linear', NFFT=2048, noverlap=0)
# # Read the power at the peak frequency
# power_db = 10*np.log10(p_xx)
# power_to_verify = power_db[peak_freq]
# # # Plot the power to verify and the confidence region
# # plt.plot(peak_freq, power_to_verify, 'r+')
# # plt.axhline(y=lower_bound, color='m', linestyle='--')
# # plt.axhline(y=upper_bound, color='m', linestyle='--')
# # plt.show()
# plt.clf()
# print(" - Q = {:}, ".format(Q))
# # If the power is within the confidence region, save the filtered data
# if (power_to_verify < upper_bound) and (power_to_verify > lower_bound):
# good_filtered_data = np.copy(filtered_data)
# # If the power is still outside the confidence region, increase the quality factor
# else:
# filtered_data = np.copy(good_filtered_data)
# Q += mains_hum[i]/config.mains_frequency
# ### ###
i += 1
print("All frequencies filtered!")
print()
print("Finding background level")
# Begin another least-squares optimization fit that follows the background level in the data
# Begin initial guess's, 0 where additions/subtractions are and 1 where multiplications/divisions are
x0 = np.array([1.0, 1.0])
# Attempt to fit the background model on the filtered data
res_lsq_back = scipy.optimize.least_squares(linearBackgroundModel, x0,
args=(time_relative, good_filtered_data)
)
print("Data background fit: {}".format(res_lsq_back.x))
# Calculate the mean power using the least-squares solution
background_level_arr = linearBackgroundModel(res_lsq_back.x, time_relative)
### Try to locate the fireball in the data and determine its boundaries ###
if cml_args.integrate:
# Calculate number of samples in window, since the main harmonice is 60/50 Hz which is 1/freq s, that will be the smallest window we should use
window_width = int(fs/mains_hum[0])
window_shift = int(window_width/2.0)
# To find the area beneath the curve, find the highest point in the data becuase it's likely the meteor
peak_index = np.argmax(good_filtered_data)
i = peak_index
while(True):
if(good_filtered_data[i]<background_level_arr[i] or i==0):
left_std_start = i
break
else:
i -= 1
i = peak_index
while(True):
if(good_filtered_data[i]<background_level_arr[i] or i==len(good_filtered_data)-1):
right_std_start = i
break
else:
i += 1
left_back_low = background_level_arr - 3*np.std(good_filtered_data[0:left_std_start])
left_back_upp = background_level_arr + 3*np.std(good_filtered_data[0:left_std_start])
right_back_low = background_level_arr - 3*np.std(good_filtered_data[right_std_start:-1])
right_back_upp = background_level_arr + 3*np.std(good_filtered_data[right_std_start:-1])
i = 0
window_mean = np.mean(good_filtered_data[peak_index - (i + 1)*window_shift:peak_index - (i - 1)*window_shift])
while(True):
i += 1
window_mean = np.mean(good_filtered_data[peak_index - (i + 1)*window_shift:peak_index - (i - 1)*window_shift])
try:
if(window_mean < left_back_upp[peak_index - i*window_shift] and window_mean > left_back_low[peak_index - i*window_shift]):
peak_lower_bound = peak_index - i*window_shift
break
except IndexError:
break
i = 0
window_mean = np.mean(good_filtered_data[peak_index + (i - 1)*window_shift:peak_index + (i + 1)*window_shift])
while(True):
i += 1
window_mean = np.mean(good_filtered_data[peak_index + (i - 1)*window_shift:peak_index + (i + 1)*window_shift])
try:
if(window_mean < right_back_upp[peak_index + i*window_shift] and window_mean > right_back_low[peak_index + i*window_shift]):
peak_upper_bound = peak_index + i*window_shift
break
except IndexError:
break
try:
filtered_curve_area = scipy.integrate.trapz(good_filtered_data[peak_lower_bound:peak_upper_bound], time_relative[peak_lower_bound:peak_upper_bound])
background_area = scipy.integrate.trapz(background_level_arr[peak_lower_bound:peak_upper_bound], time_relative[peak_lower_bound:peak_upper_bound])
light_curve_relative_to_background = filtered_curve_area - background_area
except NameError:
print("Integration bounds could not be found.")
### ###
### Plot the spectrograms of the original data and the filtered data ###
# Compute the spectrogram
spectrum1, freqs1, t1, im1 = plt.specgram(intensity, Fs=sps, cmap='inferno', detrend='linear', NFFT=256, noverlap=0)
spectrum2, freqs2, t2, im2 = plt.specgram(good_filtered_data, Fs=sps, cmap='inferno', detrend='linear', NFFT=256, noverlap=0)
plt.close()
min_val = 10 * np.log10(min(spectrum1.min(), spectrum2.min()))
max_val = 10 * np.log10(min(spectrum1.max(), spectrum2.max()))