|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Reference class implementations of existing primitives.""" |
| 3 | +from sigpro import contributing, primitive |
| 4 | + |
| 5 | +# Transformations |
| 6 | + |
| 7 | + |
| 8 | +class Identity(primitive.AmplitudeTransformation): |
| 9 | + """Identity primitive class.""" |
| 10 | + |
| 11 | + def __init__(self): |
| 12 | + super().__init__('sigpro.transformations.amplitude.identity.identity') |
| 13 | + |
| 14 | + |
| 15 | +class PowerSpectrum(primitive.AmplitudeTransformation): |
| 16 | + """PowerSpectrum primitive class.""" |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + super().__init__('sigpro.transformations.amplitude.spectrum.power_spectrum') |
| 20 | + primitive_spec = contributing._get_primitive_spec('transformation', 'frequency') |
| 21 | + self.set_primitive_inputs(primitive_spec['args']) |
| 22 | + self.set_primitive_outputs(primitive_spec['output']) |
| 23 | + |
| 24 | + |
| 25 | +class FFT(primitive.FrequencyTransformation): |
| 26 | + """FFT primitive class.""" |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + super().__init__("sigpro.transformations.frequency.fft.fft") |
| 30 | + |
| 31 | + |
| 32 | +class FFTReal(primitive.FrequencyTransformation): |
| 33 | + """FFTReal primitive class.""" |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + super().__init__("sigpro.transformations.frequency.fft.fft_real") |
| 37 | + |
| 38 | + |
| 39 | +class FrequencyBand(primitive.FrequencyTransformation): |
| 40 | + """ |
| 41 | + FrequencyBand primitive class. |
| 42 | +
|
| 43 | + Filter between a high and low band frequency and return the amplitude values and |
| 44 | + frequency values for those. |
| 45 | +
|
| 46 | + Args: |
| 47 | + low (int): Lower band frequency of filter. |
| 48 | + high (int): Higher band frequency of filter. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, low, high): |
| 52 | + super().__init__("sigpro.transformations.frequency.band.frequency_band", |
| 53 | + init_params={'low': low, 'high': high}) |
| 54 | + self.set_primitive_inputs([{"name": "amplitude_values", "type": "numpy.ndarray"}, |
| 55 | + {"name": "frequency_values", "type": "numpy.ndarray"}]) |
| 56 | + self.set_primitive_outputs([{'name': 'amplitude_values', 'type': "numpy.ndarray"}, |
| 57 | + {'name': 'frequency_values', 'type': "numpy.ndarray"}]) |
| 58 | + self.set_fixed_hyperparameters({'low': {'type': 'int'}, 'high': {'type': 'int'}}) |
| 59 | + |
| 60 | + |
| 61 | +class STFT(primitive.FrequencyTimeTransformation): |
| 62 | + """STFT primitive class.""" |
| 63 | + |
| 64 | + def __init__(self): |
| 65 | + super().__init__('sigpro.transformations.frequency_time.stft.stft') |
| 66 | + self.set_primitive_outputs([{"name": "amplitude_values", "type": "numpy.ndarray"}, |
| 67 | + {"name": "frequency_values", "type": "numpy.ndarray"}, |
| 68 | + {"name": "time_values", "type": "numpy.ndarray"}]) |
| 69 | + |
| 70 | + |
| 71 | +class STFTReal(primitive.FrequencyTimeTransformation): |
| 72 | + """STFTReal primitive class.""" |
| 73 | + |
| 74 | + def __init__(self): |
| 75 | + super().__init__('sigpro.transformations.frequency_time.stft.stft_real') |
| 76 | + self.set_primitive_outputs([{"name": "real_amplitude_values", "type": "numpy.ndarray"}, |
| 77 | + {"name": "frequency_values", "type": "numpy.ndarray"}, |
| 78 | + {"name": "time_values", "type": "numpy.ndarray"}]) |
| 79 | + |
| 80 | +# Aggregations |
| 81 | + |
| 82 | + |
| 83 | +class CrestFactor(primitive.AmplitudeAggregation): |
| 84 | + """CrestFactor primitive class.""" |
| 85 | + |
| 86 | + def __init__(self): |
| 87 | + super().__init__('sigpro.aggregations.amplitude.statistical.crest_factor') |
| 88 | + self.set_primitive_outputs([{'name': 'crest_factor_value', 'type': "float"}]) |
| 89 | + |
| 90 | + |
| 91 | +class Kurtosis(primitive.AmplitudeAggregation): |
| 92 | + """ |
| 93 | + Kurtosis primitive class. |
| 94 | +
|
| 95 | + Computes the kurtosis value of the input array. If all values are equal, return |
| 96 | + `-3` for Fisher's definition and `0` for Pearson's definition. |
| 97 | +
|
| 98 | + Args: |
| 99 | + fisher (bool): |
| 100 | + If ``True``, Fisher’s definition is used (normal ==> 0.0). If ``False``, |
| 101 | + Pearson’s definition is used (normal ==> 3.0). Defaults to ``True``. |
| 102 | + bias (bool): |
| 103 | + If ``False``, then the calculations are corrected for statistical bias. |
| 104 | + Defaults to ``True``. |
| 105 | + """ |
| 106 | + |
| 107 | + def __init__(self, fisher=True, bias=True): |
| 108 | + super().__init__('sigpro.aggregations.amplitude.statistical.kurtosis', |
| 109 | + init_params={'fisher': fisher, 'bias': bias}) |
| 110 | + self.set_primitive_outputs([{'name': 'kurtosis_value', 'type': "float"}]) |
| 111 | + self.set_fixed_hyperparameters({'fisher': {'type': 'bool', 'default': True}, |
| 112 | + 'bias': {'type': 'bool', 'default': True}}) |
| 113 | + |
| 114 | + |
| 115 | +class Mean(primitive.AmplitudeAggregation): |
| 116 | + """Mean primitive class.""" |
| 117 | + |
| 118 | + def __init__(self): |
| 119 | + super().__init__('sigpro.aggregations.amplitude.statistical.mean') |
| 120 | + self.set_primitive_outputs([{'name': 'mean_value', 'type': "float"}]) |
| 121 | + |
| 122 | + |
| 123 | +class RMS(primitive.AmplitudeAggregation): |
| 124 | + """RMS primitive class.""" |
| 125 | + |
| 126 | + def __init__(self): |
| 127 | + super().__init__('sigpro.aggregations.amplitude.statistical.rms') |
| 128 | + self.set_primitive_outputs([{'name': 'rms_value', 'type': "float"}]) |
| 129 | + |
| 130 | + |
| 131 | +class Skew(primitive.AmplitudeAggregation): |
| 132 | + """Skew primitive class.""" |
| 133 | + |
| 134 | + def __init__(self): |
| 135 | + super().__init__('sigpro.aggregations.amplitude.statistical.skew') |
| 136 | + self.set_primitive_outputs([{'name': 'skew_value', 'type': "float"}]) |
| 137 | + |
| 138 | + |
| 139 | +class Std(primitive.AmplitudeAggregation): |
| 140 | + """Std primitive class.""" |
| 141 | + |
| 142 | + def __init__(self): |
| 143 | + super().__init__('sigpro.aggregations.amplitude.statistical.std') |
| 144 | + self.set_primitive_outputs([{'name': 'std_value', 'type': "float"}]) |
| 145 | + |
| 146 | + |
| 147 | +class Var(primitive.AmplitudeAggregation): |
| 148 | + """Var primitive class.""" |
| 149 | + |
| 150 | + def __init__(self): |
| 151 | + super().__init__('sigpro.aggregations.amplitude.statistical.var') |
| 152 | + self.set_primitive_outputs([{'name': 'var_value', 'type': "float"}]) |
| 153 | + |
| 154 | + |
| 155 | +class BandMean(primitive.FrequencyAggregation): |
| 156 | + """ |
| 157 | + BandMean primitive class. |
| 158 | +
|
| 159 | + Filters between a high and low band and compute the mean value for this specific band. |
| 160 | +
|
| 161 | + Args: |
| 162 | + min_frequency (int or float): |
| 163 | + Band minimum. |
| 164 | + max_frequency (int or float): |
| 165 | + Band maximum. |
| 166 | + """ |
| 167 | + |
| 168 | + def __init__(self, min_frequency, max_frequency): |
| 169 | + super().__init__('sigpro.aggregations.frequency.band.band_mean', init_params={ |
| 170 | + 'min_frequency': min_frequency, 'max_frequency': max_frequency}) |
| 171 | + self.set_fixed_hyperparameters({'min_frequency': {'type': 'float'}, |
| 172 | + 'max_frequency': {'type': 'float'}}) |
0 commit comments