aboutsummaryrefslogtreecommitdiff
path: root/src/optical_signal.h
blob: b597b9804063c747ba7116f17d17f8623b127e32 (plain)
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
#pragma once

#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <stdlib.h>

#include <systemc.h>

using namespace std;

class OpticalSignal {
public:
    typedef complex<double> field_type;

private:
    // class variable for holding next id to attribute
    static unsigned int nextId;

public:
    field_type m_field; // complex field of the signal (V/m)
    uint32_t m_wavelength_id; // accesses the global vector of wavelengths
    unsigned int m_id;

    // Constructor from values
    OpticalSignal(const field_type &field = 0.0,
                  const double &wavelength = numeric_limits<double>::quiet_NaN())
        : m_field(field)
    {
        if (!isnan(wavelength))
            m_wavelength_id = getIDFromWavelength(wavelength);
        else
            m_wavelength_id = 0;
        getNewId();
    }

    // Constructor sending an id
    OpticalSignal(const field_type &field,
                  const uint32_t &wavelength_id)
        : m_field(field)
        , m_wavelength_id(wavelength_id)
    {
        getNewId();
    }

    // Copy constructor
    OpticalSignal(const OpticalSignal &s)
        : m_field(s.m_field)
        , m_wavelength_id(s.m_wavelength_id)
        , m_id(s.m_id)
    { getNewId(); }

    virtual ~OpticalSignal() {}

    inline void getNewId()
    {
        m_id = nextId++;
    }

    void setWavelength(const double &wavelength)
    {
        if (!isnan(wavelength))
            m_wavelength_id = getIDFromWavelength(wavelength);
        else
            m_wavelength_id = 0;
    }

    double getWavelength() const;

    static double getWavelength(const uint32_t &wavelength_id);

    uint32_t getIDFromWavelength(const double &wavelength);

    inline static complex<double> amplitudePhaseToField(double amplitude, double phase)
    {
        return polar(amplitude, phase);
    }

    inline double modulus() const {
        return abs(m_field);
    }

    inline double power() const {
        return norm(m_field);
    }

    inline double phase() const {
        return arg(m_field);
    }

    // Equality is used to determine if a signal changes
    // which is why true `==` is used here in field
    // TODO: check if we need to use is_close() instead
    // For wavelength, there should be no need not to use `==`
    // for now, as no calculation is done on it. But in the future,
    // we might need to review this as well
    bool operator==(const OpticalSignal &rhs) const
    {
#if 0
        // Work with ID
        return rhs.m_id == m_id
            || (isnan(rhs.m_wavelength) && isnan(m_wavelength));
#else
        // Work with field value
        return rhs.m_field == m_field
            && rhs.m_wavelength_id == m_wavelength_id;
#endif
    }

    bool operator!=(const OpticalSignal &rhs) const
    {
        return !(*this == rhs);
    }

    bool operator<(const OpticalSignal &rhs) const
    {
        return rhs.modulus() < modulus();
    }

    inline void advancePhaseByPhase(const double &phase_rad)
    {
        m_field *= polar(1.0, phase_rad);
    }

    inline void advancePhaseByTime(const double &time_s)
    {
        // phase in rad = w * t = (2pi * (c/lambda)) * t
        constexpr const auto c_2pi = 2 * M_PI * 299792458.0;
        const auto phase_rad = (c_2pi / this->getWavelength()) * time_s;

        advancePhaseByPhase(phase_rad);
    }

    static OpticalSignal sumSignals(
        OpticalSignal s0,
        OpticalSignal s1);

    operator field_type() const {
        return m_field;
    }

    OpticalSignal operator=(const OpticalSignal &rhs)
    {
        m_field = rhs.m_field;
        m_wavelength_id = rhs.m_wavelength_id;
        getNewId();
        return *this;
    }

    OpticalSignal &operator+=(const OpticalSignal &rhs);
    OpticalSignal &operator-=(const OpticalSignal &rhs);

    OpticalSignal &operator*=(const field_type &rhs)
    {
        m_field *= rhs;
        return *this;
    }

    OpticalSignal &operator*=(const double &rhs)
    {
        m_field *= rhs;
        return *this;
    }

    OpticalSignal &operator/=(const double &rhs)
    {
        (*this) *= (1 / rhs);
        return *this;
    }

    inline friend OpticalSignal operator+(OpticalSignal lhs, const OpticalSignal &rhs);
    inline friend OpticalSignal operator-(OpticalSignal lhs, const OpticalSignal &rhs);

    template <typename T>
    inline friend OpticalSignal operator*(OpticalSignal lhs, const T &rhs);

    template <typename T>
    inline friend OpticalSignal operator*(const T &lhs, OpticalSignal rhs);

    template <typename T>
    inline friend OpticalSignal operator/(OpticalSignal lhs, const T &rhs);

    inline friend std::ostream &operator<<(std::ostream &os, const OpticalSignal &s);
    inline friend void
    sc_trace(sc_trace_file *tf, const OpticalSignal &s, const std::string &NAME);
};

OpticalSignal operator+(OpticalSignal lhs, const OpticalSignal &rhs)
{
    return lhs += rhs;
}

OpticalSignal operator-(OpticalSignal lhs, const OpticalSignal &rhs)
{
    return lhs -= rhs;
}

template <typename T>
OpticalSignal operator*(OpticalSignal lhs, const T &rhs)
{
    return lhs *= rhs;
}

template <typename T>
OpticalSignal operator*(const T &lhs, OpticalSignal rhs)
{
    return rhs *= lhs;
}

template <typename T>
inline OpticalSignal operator/(OpticalSignal lhs, const T &rhs)
{
    return lhs /= rhs;
}

inline std::ostream &operator<<(std::ostream &os, const OpticalSignal &s)
{
    using std::defaultfloat;
    using std::fixed;
    using std::setfill;
    using std::setprecision;
    using std::setw;

    std::ostringstream oss;

    oss << "[" << setw(4) << setfill('0') << s.m_id << setfill(' ') << "] ";
    oss << " @ " << setw(4) << setprecision(8) << s.getWavelength() * 1e9 << setw(2)
        << (isnan(s.getWavelength()) ? "" : " nm");
    oss << " (" << setprecision(6) << fixed << s.modulus() << " V.m⁻¹";
    oss << ", " << setprecision(6) << fixed << s.power() << " W";
    oss << ", " << setprecision(4) << fixed << s.phase() << " rad";
    oss << ")";

    os << oss.str();
    return os;
}

inline void sc_trace(sc_trace_file *tf, const OpticalSignal &s, const std::string &NAME)
{
    cerr << "Use of deprecated" << __FUNCTION__<< endl;

    // Note: C++ complex doesn't provide access to real and imaginary part
    // Therefore we cannot have references or pointers to it...
    sc_trace(tf, s.m_id, NAME + ".id");
    sc_trace(tf, s.m_wavelength_id, NAME + ".wavelengthid");
}