IT++ Logo
source.cpp
Go to the documentation of this file.
1
29#include <itpp/signal/source.h>
30
31
32namespace itpp
33{
34
36// Sine_Source
38
39Sine_Source::Sine_Source(double freq, double mean, double ampl, double inphase)
40{
41 A = ampl;
42 m = mean;
43 theta = inphase;
44 dtheta = 2.0 * pi * freq;
45}
46
47double Sine_Source::sample()
48{
49 double samp = m + A * sin(theta);
50
51 theta += dtheta;
52 if (theta >= 2.0 * pi)
53 theta -= 2.0 * pi;
54
55 return samp;
56}
57
59{
60 vec v(n);
61
62 for (int i=0; i < n; i++)
63 v(i) = sample();
64
65 return v;
66}
67
68mat Sine_Source::operator()(int h, int w)
69{
70 mat mm(h, w);
71 int i, j;
72
73 for (i = 0; i < h; i++)
74 for (j = 0; j < w; j++)
75 mm(i, j) = sample();
76
77 return mm;
78}
79
81// Square_Source
83
84Square_Source::Square_Source(double freq, double mean, double ampl, double inphase)
85{
86 A = ampl;
87 m = mean;
88 theta = inphase / (2.0 * pi);
89 dtheta = freq;
90}
91
92double Square_Source::sample()
93{
94 double samp = theta < 0.5 ? 1.0 : -1.0;
95
96 theta += dtheta;
97 if (theta >= 1.0)
98 theta -= 1.0;
99
100 return samp;
101}
102
104{
105 vec v(n);
106
107 for (int i=0; i < n; i++)
108 v(i) = sample();
109
110 return v;
111}
112
114{
115 mat mm(h, w);
116 int i, j;
117
118 for (i = 0; i < h; i++)
119 for (j = 0; j < w; j++)
120 mm(i, j) = sample();
121
122 return mm;
123}
124
126// Triangle_Source
128
129Triangle_Source::Triangle_Source(double freq, double mean, double ampl, double inphase)
130{
131 A = ampl;
132 m = mean;
133 theta = inphase / (2.0 * pi);
134 dtheta = freq;
135}
136
137double Triangle_Source::sample()
138{
139 double samp = m + 4.0 * A * (theta < 0.25 ? theta : 0.5 - theta);
140
141 theta += dtheta;
142 if (theta >= 0.75)
143 theta -= 1.0;
144
145 return samp;
146}
147
149{
150 vec v(n);
151
152 for (int i=0; i < n; i++)
153 v(i) = sample();
154
155 return v;
156}
157
159{
160 mat mm(h, w);
161 int i, j;
162
163 for (i = 0; i < h; i++)
164 for (j = 0; j < w; j++)
165 mm(i, j) = sample();
166
167 return mm;
168}
169
171// Sawtooth_Source
173
174Sawtooth_Source::Sawtooth_Source(double freq, double mean, double ampl, double inphase)
175{
176 A = ampl;
177 m = mean;
178 theta = inphase / (2.0 * pi);
179 dtheta = freq;
180}
181
182double Sawtooth_Source::sample()
183{
184 double samp = 2.0 * A * theta;
185
186 theta += dtheta;
187 if (theta >= 0.5)
188 theta -= 1.0;
189
190 return samp;
191}
192
194{
195 vec v(n);
196
197 for (int i=0; i < n; i++)
198 v(i) = sample();
199
200 return v;
201}
202
204{
205 mat mm(h, w);
206 int i, j;
207
208 for (i = 0; i < h; i++)
209 for (j = 0; j < w; j++)
210 mm(i, j) = sample();
211
212 return mm;
213}
214
216// Impulse_Source
218
219Impulse_Source::Impulse_Source(double freq, double ampl, double inphase)
220{
221 A = ampl;
222 pos = inphase / (2.0 * pi);
223 dtheta = freq;
224}
225
226double Impulse_Source::sample()
227{
228 double samp;
229
230 if (pos >= 1.0) {
231 samp = A;
232 pos -= 1.0;
233 }
234 else {
235 samp = 0.0;
236 pos += dtheta;
237 }
238
239 return samp;
240}
241
243{
244 vec v(n);
245
246 for (int i=0; i < n; i++)
247 v(i) = sample();
248
249 return v;
250}
251
253{
254 mat m(h, w);
255 int i, j;
256
257 for (i = 0; i < h; i++)
258 for (j = 0; j < w; j++)
259 m(i, j) = sample();
260
261 return m;
262}
263
265// Pattern_Source
267
268Pattern_Source::Pattern_Source(const vec &pattern, int start_pos)
269{
270 pat = pattern;
271 pos = start_pos;
272
273 // Calculate the mean and variance. Note that the variance shall
274 // be normalied by N and not N-1 in this case
275 mean = var = 0.0;
276 for (int i = pat.size() - 1; i >= 0; i--) {
277 mean += pat(i);
278 var += pat(i) * pat(i);
279 }
280 mean /= pat.size();
281 var /= pat.size();
282 var -= mean * mean;
283}
284
285double Pattern_Source::sample()
286{
287 double samp = pat(pos);
288
289 if (pos >= pat.size() - 1)
290 pos = 0;
291 else
292 pos++;
293
294 return samp;
295}
296
298{
299 vec v(n);
300
301 for (int i=0; i < n; i++)
302 v(i) = sample();
303
304 return v;
305}
306
308{
309 mat m(h, w);
310 int i, j;
311
312 for (i = 0; i < h; i++)
313 for (j = 0; j < w; j++)
314 m(i, j) = sample();
315
316 return m;
317}
318
319} // namespace itpp
double operator()()
Return a single sample.
Definition source.h:134
Impulse_Source(double freq, double ampl=1.0, double inphase=0.0)
Constructor. Set frequency, amplitude and start phase.
Definition source.cpp:219
Pattern_Source(const vec &pattern, int start_pos=0)
Constructor. Set pattern and start position.
Definition source.cpp:268
double operator()()
Return a single sample.
Definition source.h:157
double operator()()
Return a single sample.
Definition source.h:113
Sawtooth_Source(double freq, double mean=0.0, double ampl=1.0, double inphase=0.0)
Constructor. Set frequency, mean, amplitude, and start phase.
Definition source.cpp:174
Sine_Source(double freq, double mean=0.0, double ampl=1.0, double inphase=0.0)
Constructor. Set frequency, mean, amplitude, and start phase.
Definition source.cpp:39
double operator()()
Return a single sample.
Definition source.h:50
Square_Source(double freq, double mean=0.0, double ampl=1.0, double inphase=0.0)
Constructor. Set frequency, mean, amplitude, and start phase.
Definition source.cpp:84
double operator()()
Return a single sample.
Definition source.h:71
double operator()()
Return a single sample.
Definition source.h:92
Triangle_Source(double freq, double mean=0.0, double ampl=1.0, double inphase=0.0)
Constructor. Set frequency, mean, amplitude and start phase.
Definition source.cpp:129
double mean(const vec &v)
The mean value.
Definition misc_stat.cpp:36
vec sin(const vec &x)
Sine function.
Definition trig_hyp.h:54
itpp namespace
Definition itmex.h:37
const double pi
Constant Pi.
Definition misc.h:103
Deterministic sources - header file.
SourceForge Logo

Generated on Tue Dec 10 2024 04:49:37 for IT++ by Doxygen 1.12.0