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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Provides utility functions for generating data sequences

use crate::euclid::Modulus;
use std::f64::consts;
use std::iter::Take;

/// Generates a base 10 log spaced vector of the given length between the
/// specified decade exponents (inclusive). Equivalent to MATLAB logspace
///
/// # Examples
///
/// ```
/// use statrs::generate;
///
/// let x = generate::log_spaced(5, 0.0, 4.0);
/// assert_eq!(x, [1.0, 10.0, 100.0, 1000.0, 10000.0]);
/// ```
pub fn log_spaced(length: usize, start_exp: f64, stop_exp: f64) -> Vec<f64> {
    match length {
        0 => Vec::new(),
        1 => vec![10f64.powf(stop_exp)],
        _ => {
            let step = (stop_exp - start_exp) / (length - 1) as f64;
            let mut vec = (0..length)
                .map(|x| 10f64.powf(start_exp + (x as f64) * step))
                .collect::<Vec<f64>>();
            vec[length - 1] = 10f64.powf(stop_exp);
            vec
        }
    }
}

/// Infinite iterator returning floats that form a periodic wave
pub struct InfinitePeriodic {
    amplitude: f64,
    step: f64,
    phase: f64,
    k: f64,
}

impl InfinitePeriodic {
    /// Constructs a new infinite periodic wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::InfinitePeriodic;
    ///
    /// let x = InfinitePeriodic::new(8.0, 2.0, 10.0, 1.0,
    /// 2).take(10).collect::<Vec<f64>>();
    /// assert_eq!(x, [6.0, 8.5, 1.0, 3.5, 6.0, 8.5, 1.0, 3.5, 6.0, 8.5]);
    /// ```
    pub fn new(
        sampling_rate: f64,
        frequency: f64,
        amplitude: f64,
        phase: f64,
        delay: i64,
    ) -> InfinitePeriodic {
        let step = frequency / sampling_rate * amplitude;
        InfinitePeriodic {
            amplitude: amplitude,
            step: step,
            phase: (phase - delay as f64 * step).modulus(amplitude),
            k: 0.0,
        }
    }

    /// Constructs a default infinite periodic wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::InfinitePeriodic;
    ///
    /// let x = InfinitePeriodic::default(8.0,
    /// 2.0).take(10).collect::<Vec<f64>>();
    /// assert_eq!(x, [0.0, 0.25, 0.5, 0.75, 0.0, 0.25, 0.5, 0.75, 0.0, 0.25]);
    /// ```
    pub fn default(sampling_rate: f64, frequency: f64) -> InfinitePeriodic {
        Self::new(sampling_rate, frequency, 1.0, 0.0, 0)
    }
}

impl Iterator for InfinitePeriodic {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        let mut x = self.phase + self.k * self.step;
        if x >= self.amplitude {
            x %= self.amplitude;
            self.phase = x;
            self.k = 0.0;
        }
        self.k += 1.0;
        Some(x)
    }
}

/// Finite iterator returning floats that form a periodic wave
pub struct Periodic {
    internal: Take<InfinitePeriodic>,
}

impl Periodic {
    /// Constructs a new periodic wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::Periodic;
    ///
    /// let x = Periodic::new(10, 8.0, 2.0, 10.0, 1.0, 2).collect::<Vec<f64>>();
    /// assert_eq!(x, [6.0, 8.5, 1.0, 3.5, 6.0, 8.5, 1.0, 3.5, 6.0, 8.5]);
    /// ```
    #[deprecated(
        since = "0.9.0",
        note = "please use `InfinitePeriodic::new` and `take` instead"
    )]
    pub fn new(
        length: usize,
        sampling_rate: f64,
        frequency: f64,
        amplitude: f64,
        phase: f64,
        delay: i64,
    ) -> Periodic {
        Periodic {
            internal: InfinitePeriodic::new(sampling_rate, frequency, amplitude, phase, delay)
                .take(length),
        }
    }

    /// Constructs a default periodic wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::Periodic;
    ///
    /// let x = Periodic::default(10, 8.0, 2.0).collect::<Vec<f64>>();
    /// assert_eq!(x, [0.0, 0.25, 0.5, 0.75, 0.0, 0.25, 0.5, 0.75, 0.0, 0.25]);
    /// ```
    #[deprecated(
        since = "0.9.0",
        note = "please use `InfinitePeriodic::default` and `take` instead"
    )]
    pub fn default(length: usize, sampling_rate: f64, frequency: f64) -> Periodic {
        Periodic {
            internal: InfinitePeriodic::default(sampling_rate, frequency).take(length),
        }
    }
}

impl Iterator for Periodic {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        self.internal.next()
    }
}

/// Infinite iterator returning floats that form a sinusoidal wave
pub struct InfiniteSinusoidal {
    amplitude: f64,
    mean: f64,
    step: f64,
    phase: f64,
    i: usize,
}

impl InfiniteSinusoidal {
    /// Constructs a new infinite sinusoidal wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::InfiniteSinusoidal;
    ///
    /// let x = InfiniteSinusoidal::new(8.0, 2.0, 1.0, 5.0, 2.0,
    /// 1).take(10).collect::<Vec<f64>>();
    /// assert_eq!(x,
    ///     [5.416146836547142, 5.909297426825682, 4.583853163452858,
    ///     4.090702573174318, 5.416146836547142, 5.909297426825682,
    ///     4.583853163452858, 4.090702573174318, 5.416146836547142,
    ///     5.909297426825682]);
    /// ```
    pub fn new(
        sampling_rate: f64,
        frequency: f64,
        amplitude: f64,
        mean: f64,
        phase: f64,
        delay: i64,
    ) -> InfiniteSinusoidal {
        let pi2 = consts::PI * 2.0;
        let step = frequency / sampling_rate * pi2;
        InfiniteSinusoidal {
            amplitude: amplitude,
            mean: mean,
            step: step,
            phase: (phase - delay as f64 * step) % pi2,
            i: 0,
        }
    }

    /// Constructs a default infinite sinusoidal wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::InfiniteSinusoidal;
    ///
    /// let x = InfiniteSinusoidal::default(8.0, 2.0,
    /// 1.0).take(10).collect::<Vec<f64>>();
    /// assert_eq!(x,
    ///     [0.0, 1.0, 0.00000000000000012246467991473532,
    ///     -1.0, -0.00000000000000024492935982947064, 1.0,
    ///     0.00000000000000036739403974420594, -1.0,
    ///     -0.0000000000000004898587196589413, 1.0]);
    /// ```
    pub fn default(sampling_rate: f64, frequency: f64, amplitude: f64) -> InfiniteSinusoidal {
        Self::new(sampling_rate, frequency, amplitude, 0.0, 0.0, 0)
    }
}

impl Iterator for InfiniteSinusoidal {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        let x = self.mean + self.amplitude * (self.phase + self.i as f64 * self.step).sin();
        self.i += 1;
        if self.i == 1000 {
            self.i = 0;
            self.phase = (self.phase + 1000.0 * self.step) % (consts::PI * 2.0);
        }
        Some(x)
    }
}

/// Finite iterator returning floats that form a sinusoidal wave
pub struct Sinusoidal {
    internal: Take<InfiniteSinusoidal>,
}

impl Sinusoidal {
    /// Constructs a new sinusoidal wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::Sinusoidal;
    ///
    /// let x = Sinusoidal::new(10, 8.0, 2.0, 1.0, 5.0, 2.0,
    /// 1).collect::<Vec<f64>>();
    /// assert_eq!(x,
    ///     [5.416146836547142, 5.909297426825682, 4.583853163452858,
    ///     4.090702573174318, 5.416146836547142, 5.909297426825682,
    ///     4.583853163452858, 4.090702573174318, 5.416146836547142,
    ///     5.909297426825682]);
    /// ```
    #[deprecated(
        since = "0.9.0",
        note = "please use `InfiniteSinusoidal::new` and `take` instead"
    )]
    pub fn new(
        length: usize,
        sampling_rate: f64,
        frequency: f64,
        amplitude: f64,
        mean: f64,
        phase: f64,
        delay: i64,
    ) -> Sinusoidal {
        Sinusoidal {
            internal: InfiniteSinusoidal::new(
                sampling_rate,
                frequency,
                amplitude,
                mean,
                phase,
                delay,
            )
            .take(length),
        }
    }

    /// Constructs a default sinusoidal wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::Sinusoidal;
    ///
    /// let x = Sinusoidal::default(10, 8.0, 2.0, 1.0).collect::<Vec<f64>>();
    /// assert_eq!(x,
    ///     [0.0, 1.0, 0.00000000000000012246467991473532,
    ///     -1.0, -0.00000000000000024492935982947064, 1.0,
    ///     0.00000000000000036739403974420594, -1.0,
    ///     -0.0000000000000004898587196589413, 1.0]);
    /// ```
    #[deprecated(
        since = "0.9.0",
        note = "please use `InfiniteSinusoidal::default` and `take` instead"
    )]
    pub fn default(
        length: usize,
        sampling_rate: f64,
        frequency: f64,
        amplitude: f64,
    ) -> Sinusoidal {
        Sinusoidal {
            internal: InfiniteSinusoidal::default(sampling_rate, frequency, amplitude).take(length),
        }
    }
}

impl Iterator for Sinusoidal {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        self.internal.next()
    }
}

/// Infinite iterator returning floats forming a square wave starting
/// with the high phase
pub struct InfiniteSquare {
    periodic: InfinitePeriodic,
    high_duration: f64,
    high_value: f64,
    low_value: f64,
}

impl InfiniteSquare {
    /// Constructs a new infinite square wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::InfiniteSquare;
    ///
    /// let x = InfiniteSquare::new(3, 7, 1.0, -1.0,
    /// 1).take(12).collect::<Vec<f64>>();
    /// assert_eq!(x, [-1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0,
    /// -1.0, 1.0])
    /// ```
    pub fn new(
        high_duration: i64,
        low_duration: i64,
        high_value: f64,
        low_value: f64,
        delay: i64,
    ) -> InfiniteSquare {
        let duration = (high_duration + low_duration) as f64;
        InfiniteSquare {
            periodic: InfinitePeriodic::new(1.0, 1.0 / duration, duration, 0.0, delay),
            high_duration: high_duration as f64,
            high_value: high_value,
            low_value: low_value,
        }
    }
}

impl Iterator for InfiniteSquare {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        self.periodic.next().and_then(|x| {
            if x < self.high_duration {
                Some(self.high_value)
            } else {
                Some(self.low_value)
            }
        })
    }
}

/// Finite iterator returning floats forming a square wave starting
/// with the high phase
pub struct Square {
    internal: Take<InfiniteSquare>,
}

impl Square {
    /// Constructs a new square wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::Square;
    ///
    /// let x = Square::new(12, 3, 7, 1.0, -1.0, 1).collect::<Vec<f64>>();
    /// assert_eq!(x, [-1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0,
    /// -1.0, 1.0])
    /// ```
    #[deprecated(
        since = "0.9.0",
        note = "please use `InfiniteSquare::new` and `take` instead"
    )]
    pub fn new(
        length: usize,
        high_duration: i64,
        low_duration: i64,
        high_value: f64,
        low_value: f64,
        delay: i64,
    ) -> Square {
        Square {
            internal: InfiniteSquare::new(
                high_duration,
                low_duration,
                high_value,
                low_value,
                delay,
            )
            .take(length),
        }
    }
}

impl Iterator for Square {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        self.internal.next()
    }
}

/// Infinite iterator returning floats forming a triangle wave starting with
/// the raise phase from the lowest sample
pub struct InfiniteTriangle {
    periodic: InfinitePeriodic,
    raise_duration: f64,
    raise: f64,
    fall: f64,
    high_value: f64,
    low_value: f64,
}

impl InfiniteTriangle {
    /// Constructs a new infinite triangle wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// #[macro_use]
    /// extern crate statrs;
    ///
    /// use statrs::generate::InfiniteTriangle;
    ///
    /// # fn main() {
    /// let x = InfiniteTriangle::new(4, 7, 1.0, -1.0,
    /// 1).take(12).collect::<Vec<f64>>();
    /// let expected: [f64; 12] = [-0.714, -1.0, -0.5, 0.0, 0.5, 1.0, 0.714,
    /// 0.429, 0.143, -0.143, -0.429, -0.714];
    /// for (&left, &right) in x.iter().zip(expected.iter()) {
    ///     assert_almost_eq!(left, right, 1e-3);
    /// }
    /// # }
    /// ```
    pub fn new(
        raise_duration: i64,
        fall_duration: i64,
        high_value: f64,
        low_value: f64,
        delay: i64,
    ) -> InfiniteTriangle {
        let duration = (raise_duration + fall_duration) as f64;
        let height = high_value - low_value;
        InfiniteTriangle {
            periodic: InfinitePeriodic::new(1.0, 1.0 / duration, duration, 0.0, delay),
            raise_duration: raise_duration as f64,
            raise: height / raise_duration as f64,
            fall: height / fall_duration as f64,
            high_value: high_value,
            low_value: low_value,
        }
    }
}

impl Iterator for InfiniteTriangle {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        self.periodic.next().and_then(|x| {
            if x < self.raise_duration {
                Some(self.low_value + x * self.raise)
            } else {
                Some(self.high_value - (x - self.raise_duration) * self.fall)
            }
        })
    }
}

/// Finite iterator returning floats forming a triangle wave
/// starting with the raise phase from the lowest sample
pub struct Triangle {
    internal: Take<InfiniteTriangle>,
}

impl Triangle {
    /// Constructs a new triangle wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// #[macro_use]
    /// extern crate statrs;
    ///
    /// use statrs::generate::Triangle;
    ///
    /// # fn main() {
    /// let x = Triangle::new(12, 4, 7, 1.0, -1.0, 1).collect::<Vec<f64>>();
    /// let expected: [f64; 12] = [-0.714, -1.0, -0.5, 0.0, 0.5, 1.0, 0.714,
    /// 0.429, 0.143, -0.143, -0.429, -0.714];
    /// for (&left, &right) in x.iter().zip(expected.iter()) {
    ///     assert_almost_eq!(left, right, 1e-3);
    /// }
    /// # }
    /// ```
    #[deprecated(
        since = "0.9.0",
        note = "please use `InfiniteTriangle::new` and `take` instead"
    )]
    pub fn new(
        length: usize,
        raise_duration: i64,
        fall_duration: i64,
        high_value: f64,
        low_value: f64,
        delay: i64,
    ) -> Triangle {
        Triangle {
            internal: InfiniteTriangle::new(
                raise_duration,
                fall_duration,
                high_value,
                low_value,
                delay,
            )
            .take(length),
        }
    }
}

impl Iterator for Triangle {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        self.internal.next()
    }
}

/// Infinite iterator returning floats forming a sawtooth wave
/// starting with the lowest sample
pub struct InfiniteSawtooth {
    periodic: InfinitePeriodic,
    low_value: f64,
}

impl InfiniteSawtooth {
    /// Constructs a new infinite sawtooth wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::InfiniteSawtooth;
    ///
    /// let x = InfiniteSawtooth::new(5, 1.0, -1.0,
    /// 1).take(12).collect::<Vec<f64>>();
    /// assert_eq!(x, [1.0, -1.0, -0.5, 0.0, 0.5, 1.0, -1.0, -0.5, 0.0, 0.5,
    /// 1.0, -1.0]);
    /// ```
    pub fn new(period: i64, high_value: f64, low_value: f64, delay: i64) -> InfiniteSawtooth {
        let height = high_value - low_value;
        let period = period as f64;
        InfiniteSawtooth {
            periodic: InfinitePeriodic::new(
                1.0,
                1.0 / period,
                height * period / (period - 1.0),
                0.0,
                delay,
            ),
            low_value: low_value as f64,
        }
    }
}

impl Iterator for InfiniteSawtooth {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        self.periodic.next().and_then(|x| Some(x + self.low_value))
    }
}

/// Finite iterator returning floats forming a sawtooth wave
/// starting with the lowest sample
pub struct Sawtooth {
    internal: Take<InfiniteSawtooth>,
}

impl Sawtooth {
    /// Constructs a new sawtooth wave generator
    ///
    /// # Examples
    ///
    /// ```
    /// use statrs::generate::Sawtooth;
    ///
    /// let x = Sawtooth::new(12, 5, 1.0, -1.0, 1).collect::<Vec<f64>>();
    /// assert_eq!(x, [1.0, -1.0, -0.5, 0.0, 0.5, 1.0, -1.0, -0.5, 0.0, 0.5,
    /// 1.0, -1.0]);
    /// ```
    #[deprecated(
        since = "0.9.0",
        note = "please use `InfiniteSawtooth::new` and `take` instead"
    )]
    pub fn new(
        length: usize,
        period: i64,
        high_value: f64,
        low_value: f64,
        delay: i64,
    ) -> Sawtooth {
        Sawtooth {
            internal: InfiniteSawtooth::new(period, high_value, low_value, delay).take(length),
        }
    }
}

impl Iterator for Sawtooth {
    type Item = f64;

    fn next(&mut self) -> Option<f64> {
        self.internal.next()
    }
}