Completed
Pull Request — master (#384)
by
unknown
02:02
created

BasicPulseAnalyzer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 101
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
B analyse_mean() 0 38 5
C analyse_mean_norm() 0 55 9
A __init__() 0 2 1
1
# -*- coding: utf-8 -*-
2
"""
3
This file contains basic pulse analysis methods for Qudi.
4
5
Qudi is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Qudi is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with Qudi. If not, see <http://www.gnu.org/licenses/>.
17
18
Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the
19
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
20
"""
21
22
import numpy as np
23
24
from logic.pulsed.pulse_analyzer import PulseAnalyzerBase
25
26
27
class BasicPulseAnalyzer(PulseAnalyzerBase):
28
    """
29
30
    """
31
    def __init__(self, *args, **kwargs):
32
        super().__init__(*args, **kwargs)
33
34
    def analyse_mean_norm(self, laser_data, signal_start=0.0, signal_end=200e-9, norm_start=300e-9,
35
                          norm_end=500e-9):
36
        """
37
38
        @param laser_data:
39
        @param signal_start:
40
        @param signal_end:
41
        @param norm_start:
42
        @param norm_end:
43
        @return:
44
        """
45
        # Get number of lasers
46
        num_of_lasers = laser_data.shape[0]
47
        # Get counter bin width
48
        bin_width = self.fast_counter_settings.get('bin_width')
49
50
        if not isinstance(bin_width, float):
51
            return np.zeros(num_of_lasers), np.zeros(num_of_lasers)
52
53
        # Convert the times in seconds to bins (i.e. array indices)
54
        signal_start_bin = round(signal_start / bin_width)
55
        signal_end_bin = round(signal_end / bin_width)
56
        norm_start_bin = round(norm_start / bin_width)
57
        norm_end_bin = round(norm_end / bin_width)
58
59
        # initialize data arrays for signal and measurement error
60
        signal_data = np.empty(num_of_lasers, dtype=float)
61
        error_data = np.empty(num_of_lasers, dtype=float)
62
63
        # loop over all laser pulses and analyze them
64
        for ii, laser_arr in enumerate(laser_data):
65
            # calculate the sum and mean of the data in the normalization window
66
            tmp_data = laser_arr[norm_start_bin:norm_end_bin]
67
            reference_sum = np.sum(tmp_data)
68
            reference_mean = (reference_sum / len(tmp_data)) if len(tmp_data) != 0 else 0.0
69
70
            # calculate the sum and mean of the data in the signal window
71
            tmp_data = laser_arr[signal_start_bin:signal_end_bin]
72
            signal_sum = np.sum(tmp_data)
73
            signal_mean = (signal_sum / len(tmp_data)) if len(tmp_data) != 0 else 0.0
74
75
            # Calculate normalized signal while avoiding division by zero
76
            if reference_mean > 0 and signal_mean >= 0:
77
                signal_data[ii] = signal_mean / reference_mean
78
            else:
79
                signal_data[ii] = 0.0
80
81
            # Calculate measurement error while avoiding division by zero
82
            if reference_sum > 0 and signal_sum > 0:
83
                # calculate with respect to gaussian error 'evolution'
84
                error_data[ii] = signal_data[ii] * np.sqrt(1 / signal_sum + 1 / reference_sum)
85
            else:
86
                error_data[ii] = 0.0
87
88
        return signal_data, error_data
89
90
    def analyse_mean(self, laser_data, signal_start=0.0, signal_end=200e-9):
91
        """
92
93
        @param laser_data:
94
        @param signal_start:
95
        @param signal_end:
96
        @return:
97
        """
98
        # Get number of lasers
99
        num_of_lasers = laser_data.shape[0]
100
        # Get counter bin width
101
        bin_width = self.fast_counter_settings.get('bin_width')
102
103
        if not isinstance(bin_width, float):
104
            return np.zeros(num_of_lasers), np.zeros(num_of_lasers)
105
106
        # Convert the times in seconds to bins (i.e. array indices)
107
        signal_start_bin = round(signal_start / bin_width)
108
        signal_end_bin = round(signal_end / bin_width)
109
110
        # initialize data arrays for signal and measurement error
111
        signal_data = np.empty(num_of_lasers, dtype=float)
112
        error_data = np.empty(num_of_lasers, dtype=float)
113
114
        # loop over all laser pulses and analyze them
115
        for ii, laser_arr in enumerate(laser_data):
116
            # calculate the sum and mean of the data in the signal window
117
            signal_mean = laser_arr[signal_start_bin:signal_end_bin].mean()
118
119
            # Avoid numpy C type variables overflow and NaN values
120
            if signal_mean < 0 or signal_mean != signal_mean:
121
                signal_data[ii] = 0.0
122
                error_data[ii] = 0.0
123
            else:
124
                signal_data[ii] = signal_mean
125
                error_data[ii] = np.sqrt(signal_mean)
126
127
        return signal_data, error_data
128