-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMokuLabDataProcessing.py
More file actions
1347 lines (969 loc) · 64.1 KB
/
MokuLabDataProcessing.py
File metadata and controls
1347 lines (969 loc) · 64.1 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 pandas as pd
import matplotlib.pyplot as pyplot
import numpy
from scipy import optimize
#pyplot.rcdefaults()
pyplot.rcParams.update({'font.size': 18})
from scipy.signal import find_peaks
# Some functions from LIA trigger code for fitting two flat regions to see if this beats mean
def FlatPiecewiseCurve(x, x0, a, b):
result = numpy.piecewise(x, [x<x0], [a,b])
return result
def MatchFlatPiecewiseCurve(times, LIAOutput):
optimisedParameters, estimatedCovariance = optimize.curve_fit(FlatPiecewiseCurve, times, LIAOutput, p0=[0.5,0.001,0.001])
return optimisedParameters
def FlatPeakCurve(x, x0, x1, a, b, c):
# print(type(x))
result = numpy.piecewise(x, [ x<=x0 , numpy.logical_and(x0<x,x<x1) , x>=x1 ], [a,b,c])
return result
def MatchFlatPeakCurve(times, LIAOutput):
optimisedParameters, estimatedCovariance = optimize.curve_fit(FlatPeakCurve, times, LIAOutput, p0=[0.35,0.65,0.001,0.002,0.001])
return optimisedParameters
def GetRMS(inputArray):
sumOfSquares = 0
for i in range(len(inputArray)):
sumOfSquares+=inputArray[i]**2
return numpy.sqrt( sumOfSquares/len(inputArray) )
# def InvestigatePerformance():
# timesSignalHigherInvestigate = []
# bestPerformingShift = 0
# previousBestNumberBeatingNoise = 0
# bestPerformingMeans = []
# indexShifts = numpy.arange(0,stepLength*4,step=1012//92)
# #indexShifts = [0]
# print("Start time:", mokulabData2[0][0])
# print("End time:", mokulabData2[0][stepLength])
# for j in range(len(indexShifts)):
# #
# numberOfSignalBeatingNoise = 0
# fitSignalBeatNoise = 0
# fitPeakSignalBeatNoise = 0
# signalMeans = []
# noiseMeans = []
# # For the matched sweeps, use every other chunk as the noise
# for i in range(1000):
# # Loop over 2000ms (1000 for signal chunks, 1000 for noise chunks)
# startIndexSignal = 2*i*stepLength + indexShifts[j] # *2 for skipping every other chunk
# startIndexNoise = 2*i*stepLength + indexShifts[j] + stepLength # the next chunk along?
# stopIndexSignal = startIndexSignal + stepLength
# stopIndexNoise = startIndexNoise + stepLength
# times = ( mokulabData2[0][startIndexSignal : stopIndexSignal] - mokulabData2[0][startIndexSignal] ) *1000
# times = times.to_numpy()
# LIAOutputNoise = mokulabData2[1][startIndexNoise : stopIndexNoise]
# LIAOutputSignalAndNoise = mokulabData2[1][startIndexSignal : stopIndexSignal]
# currentMeanNoise = numpy.mean(LIAOutputNoise)
# currentMeanSignalAndNoise = numpy.mean(LIAOutputSignalAndNoise)
# if currentMeanSignalAndNoise > currentMeanNoise:
# numberOfSignalBeatingNoise+=1
# signalMeans.append(currentMeanSignalAndNoise)
# noiseMeans.append(currentMeanNoise)
# # for i in range(1000):
# # # Loop over the first 1000ms for now.
# # startIndex = i*stepLength + indexShifts[j]
# # stopIndex = (i+1)*stepLength + indexShifts[j]
# # # times = mokulabData[0][i*stepLength : (i+1)*stepLength] * 1000 - mokulabData[0][i*stepLength] # Get times and shift to between 0 and ~1ms, converted into ms to match expected input for optimised curve
# # times = ( mokulabData[0][startIndex : stopIndex] - mokulabData[0][startIndex] ) * 1000
# # times = times.to_numpy()
# # LIAOutputNoise = mokulabData[1][startIndex : stopIndex]
# # LIAOutputSignalAndNoise = mokulabData2[1][startIndex : stopIndex]
# # currentMeanNoise = numpy.mean(LIAOutputNoise)
# # currentMeanSignalAndNoise = numpy.mean(LIAOutputSignalAndNoise)
# # if currentMeanSignalAndNoise > currentMeanNoise:
# # numberOfSignalBeatingNoise+=1
# # signalMeans.append(currentMeanSignalAndNoise)
# # noiseMeans.append(currentMeanNoise)
# # # # Try fitting with flat regions
# # # optimisedParametersNoise = MatchFlatPiecewiseCurve(times, LIAOutputNoise)
# # # optimisedParametersSignal = MatchFlatPiecewiseCurve(times, LIAOutputSignalAndNoise)
# # # noiseHeight = max(optimisedParametersNoise[1:])
# # # signalHeight = max(optimisedParametersSignal[1:])
# # # if noiseHeight < signalHeight:
# # # fitSignalBeatNoise+=1
# # # # Fit with 3 flat regions for peak potential
# # # optimisedParametersNoise = MatchFlatPeakCurve(times, LIAOutputNoise)
# # # optimisedParametersSignal = MatchFlatPiecewiseCurve(times, LIAOutputSignalAndNoise)
# # # noiseHeight = max(optimisedParametersNoise[2:])
# # # signalHeight = max(optimisedParametersSignal[2:])
# # # if noiseHeight < signalHeight:
# # # fitPeakSignalBeatNoise+=1
# print("Number beating:", numberOfSignalBeatingNoise)
# print("Previous best:", previousBestNumberBeatingNoise)
# if numberOfSignalBeatingNoise > previousBestNumberBeatingNoise:
# print("UPDATING BEST SHIFT")
# bestPerformingShift = indexShifts[j]
# previousBestNumberBeatingNoise = numberOfSignalBeatingNoise
# print("Times signal mean higher than noise mean:", numberOfSignalBeatingNoise, "in 1000 attempts.")
# signalMeans = numpy.array(signalMeans)
# noiseMeans = numpy.array(noiseMeans)
# thresholds = numpy.linspace(0.0,0.06, num=300)
# signalsPassingThreshold = []
# noisePassingThreshold = []
# for threshold in thresholds:
# signalsPassingThreshold.append( (signalMeans > threshold).sum() )
# noisePassingThreshold.append( (noiseMeans > threshold).sum() )
# timesSignalHigherInvestigate.append(numberOfSignalBeatingNoise)
# print(len(timesSignalHigherInvestigate))
# # There are like 5s worth of data, I take 1s each time, so I can go up to like 3999ms large scale shifts
# # I only want even numbers though once I have the small scale shift
# # so I want like 0, 2, 4, 6, 8, ...,3998 ms which is then *stepLength and +bestSmallShift
# largeScaleShifts = numpy.arange(0, 2956, step=10) * stepLength + bestPerformingShift
# timesSignalHigherInvestigateLong = []
# bestPerformingLongShift = 0
# previousBestNumberBeatingNoise = 0
# bestPerformingMeans = []
# for j in range(len(largeScaleShifts)):
# # Need to change the times thing to loop properly and skip every other 1ms.
# numberOfSignalBeatingNoise = 0
# signalMeans = []
# noiseMeans = []
# # For the matched sweeps, use every other chunk as the noise
# for i in range(1000):
# # Loop over 2000ms (1000 for signal chunks, 1000 for noise chunks)
# startIndexSignal = 2*i*stepLength + largeScaleShifts[j] # *2 for skipping every other chunk
# startIndexNoise = 2*i*stepLength + stepLength + largeScaleShifts[j] # the next chunk along
# stopIndexSignal = startIndexSignal + stepLength
# stopIndexNoise = startIndexNoise + stepLength
# times = ( mokulabData2[0][startIndexSignal : stopIndexSignal] - mokulabData2[0][startIndexSignal] ) *1000
# times = times.to_numpy()
# LIAOutputNoise = mokulabData2[1][startIndexNoise : stopIndexNoise]
# LIAOutputSignalAndNoise = mokulabData2[1][startIndexSignal : stopIndexSignal]
# currentMeanNoise = numpy.mean(LIAOutputNoise)
# currentMeanSignalAndNoise = numpy.mean(LIAOutputSignalAndNoise)
# if currentMeanSignalAndNoise > currentMeanNoise:
# numberOfSignalBeatingNoise+=1
# signalMeans.append(currentMeanSignalAndNoise)
# noiseMeans.append(currentMeanNoise)
# # print("Number beating:", numberOfSignalBeatingNoise)
# # print("Previous best:", previousBestNumberBeatingNoise)
# if numberOfSignalBeatingNoise > previousBestNumberBeatingNoise:
# # print("UPDATING BEST SHIFT")
# bestPerformingLongShift = largeScaleShifts[j]
# previousBestNumberBeatingNoise = numberOfSignalBeatingNoise
# # print("Times signal mean higher than noise mean:", numberOfSignalBeatingNoise, "in 1000 attempts.")
# signalMeans = numpy.array(signalMeans)
# noiseMeans = numpy.array(noiseMeans)
# thresholds = numpy.linspace(0.0,0.06, num=300)
# signalsPassingThreshold = []
# noisePassingThreshold = []
# for threshold in thresholds:
# signalsPassingThreshold.append( (signalMeans > threshold).sum() )
# noisePassingThreshold.append( (noiseMeans > threshold).sum() )
# timesSignalHigherInvestigateLong.append(numberOfSignalBeatingNoise)
# # print(len(timesSignalHigherInvestigateLong))
# # Now that the best performing shift has been found, run again with that index shift
# # For the matched sweeps, use every other chunk as the noise
# numberOfSignalBeatingNoise = 0
# fitSignalBeatNoise = 0
# fitPeakSignalBeatNoise = 0
# signalMeans = []
# noiseMeans = []
# for i in range(1000):
# # Loop over 2000ms (1000 for signal chunks, 1000 for noise chunks)
# startIndexSignal = 2*i*stepLength + bestPerformingLongShift # *2 for skipping every other chunk
# startIndexNoise = 2*i*stepLength + bestPerformingLongShift + stepLength # the next chunk along
# stopIndexSignal = startIndexSignal + stepLength
# stopIndexNoise = startIndexNoise + stepLength
# times = ( mokulabData2[0][startIndexSignal : stopIndexSignal] - mokulabData2[0][startIndexSignal] ) *1000
# times = times.to_numpy()
# LIAOutputNoise = mokulabData2[1][startIndexNoise : stopIndexNoise]
# LIAOutputSignalAndNoise = mokulabData2[1][startIndexSignal : stopIndexSignal]
# currentMeanNoise = numpy.mean(LIAOutputNoise)
# currentMeanSignalAndNoise = numpy.mean(LIAOutputSignalAndNoise)
# if currentMeanSignalAndNoise > currentMeanNoise:
# numberOfSignalBeatingNoise+=1
# signalMeans.append(currentMeanSignalAndNoise)
# noiseMeans.append(currentMeanNoise)
# print("Times signal mean higher than noise mean:", numberOfSignalBeatingNoise, "in 1000 attempts.")
# signalMeans = numpy.array(signalMeans)
# noiseMeans = numpy.array(noiseMeans)
# thresholds = numpy.linspace(0.0,0.06, num=300)
# signalsPassingThreshold = []
# noisePassingThreshold = []
# for threshold in thresholds:
# signalsPassingThreshold.append( (signalMeans > threshold).sum() )
# noisePassingThreshold.append( (noiseMeans > threshold).sum() )
# print("Best Performing Shift:", bestPerformingShift)
# print("Best Performing Long Shift:", bestPerformingLongShift)
# figInv, axInv = pyplot.subplots(1, 1, figsize=[16,8])
# axInv.plot(numpy.arange(1,len(indexShifts)+1), timesSignalHigherInvestigate)
# axInv.set_xlabel("Index shift number")
# figInv2, axInv2 = pyplot.subplots(1, 1, figsize=[16,8])
# axInv2.plot(numpy.arange(1,len(largeScaleShifts)+1), timesSignalHigherInvestigateLong)
# axInv2.set_xlabel("Long Index shift number")
# signalsPassingThreshold = numpy.array(signalsPassingThreshold)
# noisePassingThreshold = numpy.array(noisePassingThreshold)
# figROC, axROC = pyplot.subplots(1, 1, figsize=[16,8])
# axROC.plot(noisePassingThreshold / len(signalMeans), signalsPassingThreshold / len(signalMeans))
# axROC.set_xlabel("False Alarm Rate")
# axROC.set_ylabel("Correct Detection Rate")
# #axROC.plot(numpy.arange(0,1,step=0.001), numpy.arange(0,1,step=0.001), "--")
# # print("Times signal fit higher than noise fit:", fitSignalBeatNoise, "in 1000 attempts.")
# # print("Times signal peak fit higher than noise fit:", fitPeakSignalBeatNoise, "in 1000 attempts.")
def FindIndexOfClosestToValue(array, value):
if not isinstance(array, numpy.ndarray):
array = numpy.array(array)
differenceArray = array-value
absoluteDifferenceArray = numpy.abs(differenceArray)
closestValueIndex = numpy.argmin(absoluteDifferenceArray)
return closestValueIndex
def GetMatchedFilterTriggerPerformance(fileName): # , triggerThresholds):
signalData = pd.read_csv(fileName + "_Sig.csv", skiprows=range(7), header=None).to_numpy()
pureNoiseData = pd.read_csv(fileName + "_Noise.csv", skiprows=range(7), header=None).to_numpy()
# figDataPlot, axDataPlot = pyplot.subplots(3, 1, figsize=[16,24])
# axDataPlot[0].set_title("Signal + Noise Repsonse")
# axDataPlot[0].plot(signalData[:5001,0], signalData[:5001,1], color="blue")
# axDataPlot[0].set_xlabel("Time (s)")
# axDataPlot[0].set_ylabel("Voltage (V)")
# axDataPlot[1].set_title("Pure Noise Response")
# axDataPlot[1].plot(pureNoiseData[:5001,0], pureNoiseData[:5001,1],color="orange")
# axDataPlot[1].set_xlabel("Time (s)")
# axDataPlot[1].set_ylabel("Voltage (V)")
# axDataPlot[2].set_title("Comparison Response")
# axDataPlot[2].plot(signalData[:5001,0], signalData[:5001,1], color="blue")
# axDataPlot[2].plot(pureNoiseData[:5001,0], pureNoiseData[:5001,1], color="orange")
# axDataPlot[2].set_xlabel("Time (s)")
# axDataPlot[2].set_ylabel("Voltage (V)")
# figDataPlot.tight_layout()
print("Number of signals:",len(find_peaks(signalData[:,2], height=0)[0]))
sampleRate = 1/(signalData[1,0] - signalData[0,0])
print("Sample Rate:",sampleRate)
samplesIn2ms = int(0.002*sampleRate)
print("Samples in 2ms:", samplesIn2ms)
# position of first trigger position
firstTriggerPositionSig = numpy.argmax(signalData[:samplesIn2ms,2])
firstTriggerPositionNoise = numpy.argmax(pureNoiseData[:samplesIn2ms,2])
startTriggerOffsetSig = firstTriggerPositionSig
startTriggerOffsetNoise = firstTriggerPositionNoise
# if firstTriggerPosition > 150:
# startTriggerOffset = firstTriggerPosition-150# is 150 less than the first trigger position if the first trigger position is greater than 150, just 0 otherwise.
# else:
# startTriggerOffset = 0
signalStartLocations = find_peaks(signalData[:,2], height=0)[0]
noiseStartLocations = find_peaks(pureNoiseData[:,2], height=0)[0]
numberOfSignals = len(signalStartLocations) # len(signalData)//samplesIn2ms # For sampling of 1MSps, 2ms = 2000 samples. For 0.5Msps, 2ms = 1000 samples
numberOfNoise = len(noiseStartLocations) # len(pureNoiseData)//samplesIn2ms
print("Number of Signals:", numberOfSignals)
print("Number of Noise:", numberOfNoise)
numberOfSignals = min([numberOfSignals, numberOfNoise]) # limited by whichever has the lowest number of data points since the mokulab's data collection seems to be a little bit inconsistent on this front.
baselineVoltageForThreshold = GetRMS(pureNoiseData[:,1])
print("Baseline voltage for threshold:", baselineVoltageForThreshold)
thresholds = numpy.arange(baselineVoltageForThreshold/30, baselineVoltageForThreshold*3, step = baselineVoltageForThreshold/1000)
# thresholds = triggerThresholds
triggerDecisionsSignal_ThresholdAverage = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsNoise_ThresholdAverage = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsSignal_ThresholdMax = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsNoise_ThresholdMax = numpy.zeros( (len(thresholds), numberOfSignals) )
for i in range(numberOfSignals): # -2 because some strange things were happening with the last 2, one was empty (makes more sense) and one was short (Presumably just the remainder of the data) - actually, it was just that not all the data had the same length, changed that so it should work now
#timeChunk = signalData[ (startTriggerOffsetSig + i*samplesIn2ms) : (startTriggerOffsetSig + ((i+1)*samplesIn2ms)), 0]
#signalDataChunk = signalData[ (startTriggerOffsetSig + i*samplesIn2ms) : (startTriggerOffsetSig + ((i+1)*samplesIn2ms)), 1 ]
#testTriggerChunk = signalData[(startTriggerOffsetSig + i*samplesIn2ms) : (startTriggerOffsetSig + ((i+1)*samplesIn2ms)), 2 ]
#noiseDataChunk = pureNoiseData[ (startTriggerOffsetNoise + i*samplesIn2ms) : (startTriggerOffsetNoise + ((i+1)*samplesIn2ms)), 1 ]
timeChunk = signalData[ signalStartLocations[i] : signalStartLocations[i]+samplesIn2ms//2 , 0]
signalDataChunk = signalData[ signalStartLocations[i] : signalStartLocations[i]+samplesIn2ms//2 , 1]
testTriggerChunk = signalData[ signalStartLocations[i] : signalStartLocations[i]+samplesIn2ms//2 , 2]
noiseDataChunk = pureNoiseData[ noiseStartLocations[i] : noiseStartLocations[i]+samplesIn2ms//2 , 1]
testExample = True
if testExample == True and i==1000:
figTestExample, axTestExample = pyplot.subplots(2, 1, figsize=[16,16])
axTestExample[0].plot(timeChunk, signalDataChunk)
axTestExample[1].plot(timeChunk, noiseDataChunk)
axTestExample[0].plot(timeChunk, testTriggerChunk)
signalDataChunkMean = numpy.average(signalDataChunk)
noiseDataChunkMean = numpy.average(noiseDataChunk)
# signalDataChunkMax = numpy.max(signalDataChunk) # use [:(len(signalDataChunk)//2)] to get only the first ms for the new triggered data
# noiseDataChunkMax = numpy.max(noiseDataChunk)
signalDataChunkMax = numpy.max(signalDataChunk[:(len(signalDataChunk))])
noiseDataChunkMax = numpy.max(noiseDataChunk[:(len(noiseDataChunk))])
for t in range(len(thresholds)):
# Do mean based threshold triggers
if signalDataChunkMean > thresholds[t]:
triggerDecisionsSignal_ThresholdAverage[t,i] = 1
if noiseDataChunkMean > thresholds[t]:
triggerDecisionsNoise_ThresholdAverage[t,i] = 1
# Do max based threshold triggers
if signalDataChunkMax > thresholds[t]:
triggerDecisionsSignal_ThresholdMax[t,i] = 1
if noiseDataChunkMax > thresholds[t]:
triggerDecisionsNoise_ThresholdMax[t,i] = 1
numCorrectSignal_ThresholdAverage = []
numMissedSignal_ThresholdAverage = []
numFalsePositiveNoise_ThresholdAverage = []
numCorrectNoise_ThresholdAverage = []
numCorrectSignal_ThresholdMax = []
numMissedSignal_ThresholdMax = []
numFalsePositiveNoise_ThresholdMax = []
numCorrectNoise_ThresholdMax = []
for t in range(len(thresholds)):
numCorrectSignal_ThresholdAverage.append( numpy.count_nonzero(triggerDecisionsSignal_ThresholdAverage[t,:]) )
numMissedSignal_ThresholdAverage.append( numberOfSignals-numCorrectSignal_ThresholdAverage[t] )
numFalsePositiveNoise_ThresholdAverage.append( numpy.count_nonzero(triggerDecisionsNoise_ThresholdAverage[t,:]) )
numCorrectNoise_ThresholdAverage.append( numberOfSignals-numFalsePositiveNoise_ThresholdAverage[t] )
numCorrectSignal_ThresholdMax.append( numpy.count_nonzero(triggerDecisionsSignal_ThresholdMax[t,:]) )
numMissedSignal_ThresholdMax.append( numberOfSignals-numCorrectSignal_ThresholdMax[t] )
numFalsePositiveNoise_ThresholdMax.append( numpy.count_nonzero(triggerDecisionsNoise_ThresholdMax[t,:]) )
numCorrectNoise_ThresholdMax.append( numberOfSignals-numFalsePositiveNoise_ThresholdMax[t] )
AverageStats = {
"Correct Signal": numCorrectSignal_ThresholdAverage,
"Incorrect Signal": numMissedSignal_ThresholdAverage,
"False Positive Noise": numFalsePositiveNoise_ThresholdAverage,
"Correct Noise": numCorrectNoise_ThresholdAverage,
"Number of Signals": numberOfSignals
}
MaxStats = {
"Correct Signal": numCorrectSignal_ThresholdMax,
"Incorrect Signal": numMissedSignal_ThresholdMax,
"False Positive Noise": numFalsePositiveNoise_ThresholdMax,
"Correct Noise": numCorrectNoise_ThresholdMax,
"Number of Signals": numberOfSignals
}
return AverageStats, MaxStats
### Matched filter trigger processing
def MatchedFilterTriggerAnalysis():
# dataFolder = "D:/Project Stuff/Red Pitaya Matched Filter Data/Chirping Data Collection/1024pt/"
dataFolder = "D:/Project Stuff/Red Pitaya Matched Filter Data/DataWithTrigger/2048_pt_Data/"
# dataFile = "Chirp_300kHzTemplate_0p08SNR_280Start"
# fileName = dataFolder + dataFile
FileNameSNRs = ["0p04","0p08","0p12","0p16","0p32"] #
resultsDictionary = {}
for SNR in FileNameSNRs:
fileName = dataFolder + "33Cycle_2048pt_" + SNR + "SNR" + "_280Start"
resultsDictionary.update( { SNR : GetMatchedFilterTriggerPerformance(fileName) } )
figROCSNR, axROCSNR = pyplot.subplots(1, 1, figsize=[16,8])
for SNR in FileNameSNRs:
workingDictionary = resultsDictionary[SNR][1] # for max have [1], average is [0], maxMean is [2]
# axROCThreshold.plot(numpy.array(resultsDictionary["0p32"][0]["False Positive Noise"])/resultsDictionary["0p32"][0]["Number of Signals"]*100, numpy.array(resultsDictionary["0p32"][0]["Correct Signal"])/resultsDictionary["0p32"][0]["Number of Signals"]*100, label="Threshold Average")
# axROCThreshold.plot(numpy.array(resultsDictionary["0p32"][1]["False Positive Noise"])/resultsDictionary["0p32"][1]["Number of Signals"]*100, numpy.array(resultsDictionary["0p32"][1]["Correct Signal"])/resultsDictionary["0p32"][1]["Number of Signals"]*100, label="Threshold Max")
axROCSNR.plot( numpy.array(workingDictionary["False Positive Noise"]) / workingDictionary["Number of Signals"]*100 , numpy.array(workingDictionary["Correct Signal"]) / workingDictionary["Number of Signals"]*100, label=SNR )
axROCSNR.set_xlabel("False Positive Rate")
axROCSNR.set_ylabel("Correct Signal Detection Rate")
axROCSNR.set_xscale("log")
axROCSNR.set_xlim(1/1200*100,1e2)
axROCSNR.legend()
# # Bandwidth study
# startFrequencies = ["280","295","310","325","340","355","370"]
# bandwidthResults = {}
# for startFrequency in startFrequencies:
# fileName = dataFolder + "ChirpSig_NoChirpTemp_3p45e8Grad_0p04SNR_" + startFrequency + "p5kHz"
# print("Start frequency:",startFrequency)
# bandwidthResults.update( { startFrequency : GetMatchedFilterTriggerPerformance(fileName) } )
# figROCBandwidth, axROCBandwidth = pyplot.subplots(1, 1, figsize = [16,8])
# for startFrequency in startFrequencies:
# workingDictionary = bandwidthResults[startFrequency][1] # for max have [1]
# axROCBandwidth.plot( numpy.array(workingDictionary["False Positive Noise"]) / workingDictionary["Number of Signals"]*100 , numpy.array(workingDictionary["Correct Signal"]) / workingDictionary["Number of Signals"]*100, label=startFrequency )
# axROCBandwidth.set_xlabel("False Positive Rate")
# axROCBandwidth.set_ylabel("Correct Signal Detection Rate")
# axROCBandwidth.legend()
# # Need to get all the bandwidths and all the SNRs, get the results for them and then get the 90% values, then make a graph.
# startFreqAndSNRResults = {}
# falsePositiveRatesAt90Detection = {}
# for SNR in FileNameSNRs:
# for startFrequency in startFrequencies:
# fileName = dataFolder + "ChirpSig_NoChirpTemp_3p45e8Grad_" + SNR + "SNR_" + startFrequency + "p5kHz"
# print("Start Frequency, SNR:", startFrequency, SNR)
# resultsAverage, resultsMax = GetMatchedFilterTriggerPerformance(fileName)
# startFreqAndSNRResults.update( { (SNR,startFrequency) : resultsMax })
# currentFalsePositiveRateAt90Detection = (resultsMax["False Positive Noise"][FindIndexOfClosestToValue(resultsMax["Correct Signal"], 0.9 * resultsMax["Number of Signals"])]) / resultsMax["Number of Signals"] * 100
# falsePositiveRatesAt90Detection.update( { (SNR,startFrequency) : currentFalsePositiveRateAt90Detection } )
# print(falsePositiveRatesAt90Detection)
# falsePositiveRatesAt90DetectionArray = numpy.zeros((len(FileNameSNRs),len(startFrequencies)))
# for i in range(len(FileNameSNRs)):
# for j in range(len(startFrequencies)):
# falsePositiveRatesAt90DetectionArray[i,j] = falsePositiveRatesAt90Detection[(FileNameSNRs[i],startFrequencies[j])]
# print(falsePositiveRatesAt90DetectionArray)
# figFPAt90Detection, axFPAt90Detection = pyplot.subplots(1, 1, figsize=[10,8])
# im = axFPAt90Detection.imshow(falsePositiveRatesAt90DetectionArray)
# colorbar = figFPAt90Detection.colorbar(im)
# colorbar.ax.set_ylabel("False Positives")
# axFPAt90Detection.set_ylabel("SNR")
# axFPAt90Detection.set_xlabel("Start Frequency (kHz")
# axFPAt90Detection.set_yticks( numpy.arange(0, len(FileNameSNRs), dtype='int') )
# axFPAt90Detection.set_xticks( numpy.arange(0, len(startFrequencies), dtype='int') )
# axFPAt90Detection.set_yticklabels(FileNameSNRs)
# axFPAt90Detection.set_xticklabels(startFrequencies, rotation=90)
# figFPAt90Detection.tight_layout()
return 0
def CalculatePeakMean(data, maxIndex, peakWidth):
# peakWidth = 50 # samples, gives half the width of the peak to be investigated
if maxIndex-peakWidth < 0:
lowerPeakIndex = 0
else:
lowerPeakIndex = maxIndex-peakWidth
if maxIndex+peakWidth > len(data)-1:
upperPeakIndex = -2
else:
upperPeakIndex = maxIndex+peakWidth
dataMeanAroundMax = numpy.average(data[lowerPeakIndex : upperPeakIndex+1])
return dataMeanAroundMax
def FindBroadnessOfPeak(data, peakIndex, relativeHeight = 0.5):
# start from peak, head forwards and backwards, when you find the first value below peak height*relativeHeight, stop and say the one before it is the width
# if the peak hits the edge of the data before it finds anything, use just the other half and double, if it hits both sides, use the full width of the data
foundWidth = False
foundForwardsWidth = False
foundBackwardsWidth =False
upperSideCutOff = False
lowerSideCutOff = False
i=1
upperWidth = 0
lowerWidth = 0
while foundWidth == False:
# Look forwards first
if foundForwardsWidth == False:
if peakIndex+i > len(data)-1:
upperSideCutOff = True
foundForwardsWidth = True
else:
if data[peakIndex+i] < data[peakIndex]*relativeHeight:
upperWidth = i # samples
foundForwardsWidth = True
# Then look backwards
if foundBackwardsWidth == False:
if peakIndex-i < 0:
lowerSideCutOff = True
foundBackwardsWidth = True
else:
if data[peakIndex-i] < data[peakIndex]*relativeHeight:
lowerWidth = i # samples
foundBackwardsWidth = True
# if both forwards and backwards have been found, stop iterating, otherwise, iterate up i by 1
if foundForwardsWidth and foundBackwardsWidth:
foundWidth = True
i+=1
# Then process the sides and check whether the sides have been cut off, change width calculation accordingly.
if upperSideCutOff and lowerSideCutOff:
#print("Both sides cut off, returning full length of data")
return len(data)
elif upperSideCutOff:
#print("Upper side cut off, returning twice lower width")
return lowerWidth*2
elif lowerSideCutOff:
#print("Lower side cut off, returning twice upper width")
return upperWidth*2
else:
return upperWidth+lowerWidth
def CalculateBiggestMaxMeanOfNPeaks(data, nPeaks, peakWidth, distance=25, printGraphs = False, broadnessArray = None, label=None):
peakIndices, peakDict = find_peaks(data, height=(None, None), distance=distance)
peakHeights = peakDict["peak_heights"]
# highest_peak_index = peak_indices[numpy.argmax(peak_heights)]
# second_highest_peak_index = peak_indices[numpy.argpartition(peak_heights,-2)[-2]]
locationsOfHighestNPeaks = peakIndices[ numpy.argpartition(peakHeights,-nPeaks)[-nPeaks:] ]
biggestMaxMean = 0
biggestPeakIndex = 0
for location in locationsOfHighestNPeaks:
currentMaxMean = CalculatePeakMean(data, location, peakWidth)
if currentMaxMean > biggestMaxMean:
biggestMaxMean = currentMaxMean
biggestPeakIndex = location
if printGraphs == True:
print("Location of highest N peaks:", locationsOfHighestNPeaks)
figTestPeaks,axTestPeaks = pyplot.subplots(1, 1, figsize=[16,8])
axTestPeaks.plot(data)
axTestPeaks.scatter(locationsOfHighestNPeaks, numpy.zeros(len(locationsOfHighestNPeaks)))
axTestPeaks.set_title(label)
if broadnessArray is not None:
biggestPeakBroadness = FindBroadnessOfPeak(data, biggestPeakIndex, relativeHeight = 0.75)
broadnessArray.append(biggestPeakBroadness)
return biggestMaxMean
def GetLIATriggerPerformance(fileName): # , triggerThresholds):
signalData = pd.read_csv(fileName + "_Sig.csv", skiprows=range(7), header=None).to_numpy()
signalData = signalData[:len(signalData)]
pureNoiseData = pd.read_csv(fileName + "_Noise.csv", skiprows=range(7), header=None).to_numpy()
pureNoiseData = pureNoiseData[:len(pureNoiseData)]
sampleRate = 1/(signalData[1,0] - signalData[0,0])
print("Sample Rate:",sampleRate)
timeBetweenSignals = 0.003333333333333333
samplesIn1Signal = int(timeBetweenSignals*sampleRate)
actualSignalLength = int(0.001*sampleRate)
print("Samples in 1 signal:", samplesIn1Signal)
# position of first trigger position
# firstTriggerPosition = numpy.argmax(signalData[:samplesIn1Signal,2])
# startTriggerOffset = firstTriggerPosition
numberOfSignals = len(signalData)//samplesIn1Signal # For sampling of 1MSps, 2ms = 2000 samples. For 0.5Msps, 2ms = 1000 samples
numberOfNoise = len(pureNoiseData)//samplesIn1Signal
print("Number of Signals:", numberOfSignals)
print("Number of Noise:", numberOfNoise)
numberOfSignals = min([numberOfSignals, numberOfNoise]) # limited by whichever has the lowest number of data points since the mokulab's data collection seems to be a little bit inconsistent on this front.
baselineVoltageForThreshold = GetRMS(pureNoiseData[:,1])
print("Baseline voltage for threshold:", baselineVoltageForThreshold)
thresholds = numpy.arange(baselineVoltageForThreshold/30, baselineVoltageForThreshold*30, step = baselineVoltageForThreshold/100)
# thresholds = triggerThresholds
triggerDecisionsSignal_ThresholdAverage = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsNoise_ThresholdAverage = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsSignal_ThresholdMax = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsNoise_ThresholdMax = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsSignal_ThresholdMeanMax = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsNoise_ThresholdMeanMax = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsSignal_BiggestMeanMax = numpy.zeros( (len(thresholds), numberOfSignals) )
triggerDecisionsNoise_BiggestMeanMax = numpy.zeros( (len(thresholds), numberOfSignals) )
### Get data locations from trigger signal
# print(signalData[:300,2])
signalStartLocations = find_peaks(signalData[:,2], height=0)[0]
noiseStartLocations = find_peaks(pureNoiseData[:,2], height=0)[0]
print(len(signalData[:,2]))
print("Noise peaks:",len(noiseStartLocations))
print("Found",len(signalStartLocations),"peaks.")
print("Expected:", len(signalData)/(0.00333333333333*sampleRate))
numberOfSignals = min([len(signalStartLocations),len(noiseStartLocations)])
signalBroadnessArray = []
noiseBroadnessArray = []
for i in range(numberOfSignals-1):
#print(i)
#print(noiseStartLocations[i])
#print(actualSignalLength)
timeChunk = signalData[ (signalStartLocations[i]) : (signalStartLocations[i] + actualSignalLength), 0]
signalDataChunk = signalData[ (signalStartLocations[i]) : (signalStartLocations[i] + actualSignalLength), 1 ]
testTriggerChunk = signalData[(signalStartLocations[i]) : (signalStartLocations[i] + actualSignalLength), 2 ]
noiseDataChunk = pureNoiseData[ (noiseStartLocations[i]) : (noiseStartLocations[i] + actualSignalLength), 1 ]
testExample = False
if testExample == True and i==1000:
figTestExample, axTestExample = pyplot.subplots(2, 1, figsize=[16,16])
axTestExample[0].plot(timeChunk, signalDataChunk)
axTestExample[1].plot(timeChunk, noiseDataChunk)
axTestExample[0].plot(timeChunk, testTriggerChunk)
signalDataChunkMean = numpy.average(signalDataChunk)
noiseDataChunkMean = numpy.average(noiseDataChunk)
# signalDataChunkMax = numpy.max(signalDataChunk) # use [:(len(signalDataChunk)//2)] to get only the first ms for the new triggered data
# noiseDataChunkMax = numpy.max(noiseDataChunk)
signalDataChunkMax = numpy.max(signalDataChunk)# [:(len(signalDataChunk)//1)])
noiseDataChunkMax = numpy.max(noiseDataChunk)# [:(len(noiseDataChunk)//1)])
# for the max-mean
# First want to identify some number of maxima,
# Then take the mean (or sum) around these maxima
# Can use numpy argpartition, which basically makes an array in which you have [lower than selected point, selected point, higher than selected point]
# This means if you want the top n points, you fix the position of the Nth highest peak (-N) and then all the ones from there and above will be the N highest points in the array.
# Argpartition gives you their arguments in the original array. They're not ordered on the low/high sides, but they are all higher or lower than the point you selected to be placed correctly.
# Argpartition uses elements rather than index though, so watch out for that.
# Actually, this just gets the top 5 points, which could actually all come from the same peak...
# topNMaximaLocations = numpy.argpartition(signalDataChunk,-5)[-5:]
# Instead, just going to start with one maximum and take the mean around that, see what changes.
signalArgMax = numpy.argmax(signalDataChunk)
#print("Max:", numpy.max(signalDataChunk))
#print("Max 2:",numpy.max(signalDataChunk[:(len(signalDataChunk)//1)]))
#print("Max 3:", signalDataChunk[signalArgMax])
noiseArgMax = numpy.argmax(noiseDataChunk)
peakWidth = 50 # samples, gives half the width of the peak to be investigated
signalMeanAroundMax = CalculatePeakMean(signalDataChunk, signalArgMax, peakWidth)
noiseMeanAroundMax = CalculatePeakMean(noiseDataChunk, noiseArgMax, peakWidth)
#print("SignalDiff:", signalMeanAroundMax - signalDataChunkMax)
#print("NoiseDiff:", noiseMeanAroundMax - noiseDataChunkMax)
#print("NoiseMeanMax:", noiseMeanAroundMax)
#print("NMax:", numpy.max(noiseDataChunk))
#print("NMax 2:",numpy.max(noiseDataChunk[:(len(noiseDataChunk)//1)]))
#print("NMax 3:", noiseDataChunk[noiseArgMax])
# Find n biggest peaks and then find broadness of these, use biggest average for discrimination and do a plot for signal and noise,
# highlighting the difference and justifying this approach to selection.
nPeaks = 5
if i==0:
printGraphs = False
else:
printGraphs = False
signalBiggestMaxMean = CalculateBiggestMaxMeanOfNPeaks(signalDataChunk, nPeaks, peakWidth, distance=25, printGraphs=printGraphs, broadnessArray = signalBroadnessArray, label="Signal peaks example, "+fileName)
noiseBiggestMaxMean = CalculateBiggestMaxMeanOfNPeaks(noiseDataChunk, nPeaks, peakWidth, distance=25, printGraphs = printGraphs, broadnessArray = noiseBroadnessArray, label="Noise peaks example, "+fileName)
for t in range(len(thresholds)):
# Do mean based threshold triggers
if signalDataChunkMean > thresholds[t]:
triggerDecisionsSignal_ThresholdAverage[t,i] = 1
if noiseDataChunkMean > thresholds[t]:
triggerDecisionsNoise_ThresholdAverage[t,i] = 1
# Do max based threshold triggers
if signalDataChunkMax > thresholds[t]:
triggerDecisionsSignal_ThresholdMax[t,i] = 1
if noiseDataChunkMax > thresholds[t]:
triggerDecisionsNoise_ThresholdMax[t,i] = 1
# Do max-mean based threshold triggers
if signalMeanAroundMax > thresholds[t]:
triggerDecisionsSignal_ThresholdMeanMax[t,i] = 1
if noiseMeanAroundMax > thresholds[t]:
triggerDecisionsNoise_ThresholdMeanMax[t,i] = 1
if signalBiggestMaxMean > thresholds[t]:
triggerDecisionsSignal_BiggestMeanMax[t,i] = 1
if noiseBiggestMaxMean > thresholds[t]:
triggerDecisionsNoise_BiggestMeanMax[t,i] = 1
# # Check broadness of peaks:
# bins = numpy.arange(0,120,3)
# figBroadness, axBroadness = pyplot.subplots(1, 1, figsize=[16,8])
# axBroadness.hist(signalBroadnessArray, label="Signal", bins=bins, alpha = 0.7)
# axBroadness.hist(noiseBroadnessArray, label="Noise", bins=bins, alpha = 0.7)
# axBroadness.legend()
# axBroadness.set_xlabel("Width (samples)")
# axBroadness.set_ylabel("Counts")
# #axBroadness.set_title(fileName+" Data")
numCorrectSignal_ThresholdAverage = []
numMissedSignal_ThresholdAverage = []
numFalsePositiveNoise_ThresholdAverage = []
numCorrectNoise_ThresholdAverage = []
numCorrectSignal_ThresholdMax = []
numMissedSignal_ThresholdMax = []
numFalsePositiveNoise_ThresholdMax = []
numCorrectNoise_ThresholdMax = []
numCorrectSignal_ThresholdMeanMax = []
numMissedSignal_ThresholdMeanMax = []
numFalsePositiveNoise_ThresholdMeanMax = []
numCorrectNoise_ThresholdMeanMax = []
numCorrectSignal_BiggestMeanMax = []
numMissedSignal_BiggestMeanMax = []
numFalsePositiveNoise_BiggestMeanMax = []
numCorrectNoise_BiggestMeanMax = []
for t in range(len(thresholds)):
numCorrectSignal_ThresholdAverage.append( numpy.count_nonzero(triggerDecisionsSignal_ThresholdAverage[t,:]) )
numMissedSignal_ThresholdAverage.append( numberOfSignals-numCorrectSignal_ThresholdAverage[t] )
numFalsePositiveNoise_ThresholdAverage.append( numpy.count_nonzero(triggerDecisionsNoise_ThresholdAverage[t,:]) )
numCorrectNoise_ThresholdAverage.append( numberOfSignals-numFalsePositiveNoise_ThresholdAverage[t] )
numCorrectSignal_ThresholdMax.append( numpy.count_nonzero(triggerDecisionsSignal_ThresholdMax[t,:]) )
numMissedSignal_ThresholdMax.append( numberOfSignals-numCorrectSignal_ThresholdMax[t] )
numFalsePositiveNoise_ThresholdMax.append( numpy.count_nonzero(triggerDecisionsNoise_ThresholdMax[t,:]) )
numCorrectNoise_ThresholdMax.append( numberOfSignals-numFalsePositiveNoise_ThresholdMax[t] )
numCorrectSignal_ThresholdMeanMax.append( numpy.count_nonzero(triggerDecisionsSignal_ThresholdMeanMax[t,:]) )
numMissedSignal_ThresholdMeanMax.append( numberOfSignals-numCorrectSignal_ThresholdMeanMax[t] )
numFalsePositiveNoise_ThresholdMeanMax.append( numpy.count_nonzero(triggerDecisionsNoise_ThresholdMeanMax[t,:]) )
numCorrectNoise_ThresholdMeanMax.append( numberOfSignals-numFalsePositiveNoise_ThresholdMeanMax[t] )
numCorrectSignal_BiggestMeanMax.append( numpy.count_nonzero(triggerDecisionsSignal_BiggestMeanMax[t,:]) )
numMissedSignal_BiggestMeanMax.append( numberOfSignals-numCorrectSignal_BiggestMeanMax[t] )
numFalsePositiveNoise_BiggestMeanMax.append( numpy.count_nonzero(triggerDecisionsNoise_BiggestMeanMax[t,:]) )
numCorrectNoise_BiggestMeanMax.append( numberOfSignals-numFalsePositiveNoise_BiggestMeanMax[t] )
AverageStats = {
"Correct Signal": numCorrectSignal_ThresholdAverage,
"Incorrect Signal": numMissedSignal_ThresholdAverage,
"False Positive Noise": numFalsePositiveNoise_ThresholdAverage,
"Correct Noise": numCorrectNoise_ThresholdAverage,
"Number of Signals": numberOfSignals
}
MaxStats = {
"Correct Signal": numCorrectSignal_ThresholdMax,
"Incorrect Signal": numMissedSignal_ThresholdMax,
"False Positive Noise": numFalsePositiveNoise_ThresholdMax,
"Correct Noise": numCorrectNoise_ThresholdMax,
"Number of Signals": numberOfSignals
}
MeanMaxStats = {
"Correct Signal": numCorrectSignal_ThresholdMeanMax,
"Incorrect Signal": numMissedSignal_ThresholdMeanMax,
"False Positive Noise": numFalsePositiveNoise_ThresholdMeanMax,
"Correct Noise": numCorrectNoise_ThresholdMeanMax,
"Number of Signals": numberOfSignals
}
BiggestMeanMaxStats = {
"Correct Signal": numCorrectSignal_BiggestMeanMax,
"Incorrect Signal": numMissedSignal_BiggestMeanMax,
"False Positive Noise": numFalsePositiveNoise_BiggestMeanMax,
"Correct Noise": numCorrectNoise_BiggestMeanMax,
"Number of Signals": numberOfSignals
}
return AverageStats, MaxStats, MeanMaxStats, BiggestMeanMaxStats
def LIATriggerAnalysis():
dataFolder = "D:/Project Stuff/Red Pitaya LIA Data/1ms Data/"
FileNameSNRs = ["0p04","0p08","0p12","0p16"]# ,"0p32"]
resultsDictionary = {}
for SNR in FileNameSNRs:
fileName = dataFolder + SNR + "SNR_1em5TC" # _0p1msChirp
resultsDictionary.update( { SNR : GetLIATriggerPerformance(fileName) } )
figROCSNR, axROCSNR = pyplot.subplots(1, 1, figsize=[16,8])
for SNR in FileNameSNRs:
workingDictionary = resultsDictionary[SNR][0] # for max have [1] # For Average have 0 # For biggest maxmean have 3
# axROCThreshold.plot(numpy.array(resultsDictionary["0p32"][0]["False Positive Noise"])/resultsDictionary["0p32"][0]["Number of Signals"]*100, numpy.array(resultsDictionary["0p32"][0]["Correct Signal"])/resultsDictionary["0p32"][0]["Number of Signals"]*100, label="Threshold Average")
# axROCThreshold.plot(numpy.array(resultsDictionary["0p32"][1]["False Positive Noise"])/resultsDictionary["0p32"][1]["Number of Signals"]*100, numpy.array(resultsDictionary["0p32"][1]["Correct Signal"])/resultsDictionary["0p32"][1]["Number of Signals"]*100, label="Threshold Max")
axROCSNR.plot( numpy.array(workingDictionary["False Positive Noise"]) / workingDictionary["Number of Signals"]*100 , numpy.array(workingDictionary["Correct Signal"]) / workingDictionary["Number of Signals"]*100, label=SNR , linestyle="-")
axROCSNR.set_xlabel("False Positive Rate")
axROCSNR.set_ylabel("Correct Signal Detection Rate")
axROCSNR.set_xscale("log")
axROCSNR.set_xlim(1/1200*100,1e2)
axROCSNR.legend()
return 0
def GetLIATriggerDataPerformance(fileName):
# For a given file, read the triggers and get the statistics
signalData = pd.read_csv(fileName + "_Sig.csv", skiprows=range(7), header=None).to_numpy()
pureNoiseData = pd.read_csv(fileName + "_Noise.csv", skiprows=range(7), header=None).to_numpy()
sampleRate = 1/(signalData[1,0] - signalData[0,0])
print("Sample Rate:",sampleRate)
timeBetweenSignals = 0.003333333333333333
samplesIn1Signal = int(timeBetweenSignals*sampleRate)
actualSignalLength = int(0.001*sampleRate)
print("Samples in 1 signal:", samplesIn1Signal)
numberOfSignals = len(signalData)//samplesIn1Signal # For sampling of 1MSps, 2ms = 2000 samples. For 0.5Msps, 2ms = 1000 samples
numberOfNoise = len(pureNoiseData)//samplesIn1Signal
print("Number of Signals:", numberOfSignals)
print("Number of Noise:", numberOfNoise)
numberOfSignals = min([numberOfSignals, numberOfNoise]) # limited by whichever has the lowest number of data points since the mokulab's data collection seems to be a little bit inconsistent on this front.
### Get data locations from trigger signal
# print(signalData[:300,2])
signalStartLocations = find_peaks(signalData[:,2], height=0)[0]
noiseStartLocations = find_peaks(pureNoiseData[:,2], height=0)[0]
print(len(signalData[:,2]))
print("Found",len(signalStartLocations),"peaks.")
print("Expected:", len(signalData)/(0.00333333333333*sampleRate))
numCorrectSignal = 0
numFalsePositiveNoise = 0
numCorrectNoise = 0
numIncorrectSignal = 0
for i in range(numberOfSignals-1): # -2
timeChunk = signalData[ (signalStartLocations[i]) : (signalStartLocations[i] + actualSignalLength*2), 0]
signalDataChunk = signalData[ (signalStartLocations[i]) : (signalStartLocations[i] + actualSignalLength*2), 1 ]
testTriggerChunk = signalData[(signalStartLocations[i]) : (signalStartLocations[i] + actualSignalLength*2), 2 ]
noiseDataChunk = pureNoiseData[ (noiseStartLocations[i]) : (noiseStartLocations[i] + actualSignalLength*2), 1 ]
testExample = True
if testExample == True and i==1000:
figTestExample, axTestExample = pyplot.subplots(2, 1, figsize=[16,16])
axTestExample[0].plot(timeChunk, signalDataChunk)
axTestExample[1].plot(timeChunk, noiseDataChunk)