File indexing completed on 2024-04-06 12:30:41
0001 #ifndef MU_END_ANALOG_SIGNAL
0002 #define MU_END_ANALOG_SIGNAL
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include <cassert>
0026 #include <iosfwd>
0027 #include <vector>
0028
0029
0030 #include <iostream>
0031
0032 class CSCAnalogSignal {
0033 public:
0034 inline CSCAnalogSignal() : theElement(0), invBinSize(0.), theBinValues(0), theTotal(0), theTimeOffset(0.) {}
0035
0036 inline CSCAnalogSignal(
0037 int element, float binSize, std::vector<float> &binValues, float total = 0., float timeOffset = 0.)
0038 : theElement(element), invBinSize(1. / binSize), theBinValues(), theTotal(total), theTimeOffset(timeOffset) {
0039 theBinValues.swap(binValues);
0040 }
0041
0042
0043
0044
0045
0046 inline int getElement() const { return theElement; };
0047 inline void setElement(int element) { theElement = element; };
0048 inline float getBinValue(int i) const {
0049 return (i >= static_cast<int>(theBinValues.size()) || i < 0) ? 0. : theBinValues[i];
0050 }
0051
0052 inline float getValue(float t) const {
0053
0054 float retval = 0.;
0055 float f = (t - theTimeOffset) * invBinSize + 0.000000001;
0056 if (f >= 0.) {
0057 int i = static_cast<int>(f);
0058 f -= static_cast<float>(i);
0059 retval = (1. - f) * getBinValue(i) + f * getBinValue(i + 1);
0060 }
0061 return retval;
0062 }
0063
0064
0065
0066
0067
0068
0069 inline int getSize() const { return theBinValues.size(); };
0070 inline float getBinSize() const { return 1. / invBinSize; };
0071 inline float getTotal() const { return theTotal; };
0072 inline float getTimeOffset() const { return theTimeOffset; };
0073 inline void setTimeOffset(float offset) { theTimeOffset = offset; };
0074
0075 inline void superimpose(const CSCAnalogSignal &signal2) {
0076 size_t n = theBinValues.size();
0077 for (size_t i = 0; i < n; ++i) {
0078 float t = i / invBinSize + theTimeOffset;
0079 theBinValues[i] += signal2.getValue(t);
0080 }
0081 theTotal += signal2.theTotal;
0082 }
0083
0084 inline void operator+=(float offset) {
0085 for (int i = 0; i < getSize(); ++i) {
0086 theBinValues[i] += offset;
0087 }
0088 }
0089
0090 inline void operator*=(float scaleFactor) {
0091 for (int i = 0; i < getSize(); ++i) {
0092 theBinValues[i] *= scaleFactor;
0093 }
0094 theTotal *= scaleFactor;
0095 }
0096
0097 friend std::ostream &operator<<(std::ostream &, const CSCAnalogSignal &);
0098
0099 float &operator[](int i) {
0100 assert(i >= 0 && i < getSize());
0101 return theBinValues[i];
0102 }
0103
0104 const float &operator[](int i) const {
0105 assert(i >= 0 && i < getSize());
0106 return theBinValues[i];
0107 }
0108
0109
0110 float peakTime() const;
0111 unsigned size() const { return theBinValues.size(); }
0112
0113 private:
0114 int theElement;
0115 float invBinSize;
0116 std::vector<float> theBinValues;
0117 float theTotal;
0118 float theTimeOffset;
0119 };
0120
0121 #endif