@ohos-ports/stdlib-simulate 0.4.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +188 -0
  4. package/SECURITY.md +5 -0
  5. package/docs/types/index.d.ts +43 -0
  6. package/iter/README.md +142 -0
  7. package/iter/awgn/README.md +240 -0
  8. package/iter/awgn/docs/repl.txt +85 -0
  9. package/iter/awgn/docs/types/index.d.ts +155 -0
  10. package/iter/awgn/docs/types/test.ts +150 -0
  11. package/iter/awgn/lib/defaults.js +38 -0
  12. package/iter/awgn/lib/index.js +55 -0
  13. package/iter/awgn/lib/main.js +278 -0
  14. package/iter/awgn/package.json +68 -0
  15. package/iter/awln/README.md +238 -0
  16. package/iter/awln/docs/repl.txt +85 -0
  17. package/iter/awln/docs/types/index.d.ts +155 -0
  18. package/iter/awln/docs/types/test.ts +150 -0
  19. package/iter/awln/lib/defaults.js +38 -0
  20. package/iter/awln/lib/index.js +55 -0
  21. package/iter/awln/lib/main.js +299 -0
  22. package/iter/awln/package.json +72 -0
  23. package/iter/awun/README.md +238 -0
  24. package/iter/awun/docs/repl.txt +85 -0
  25. package/iter/awun/docs/types/index.d.ts +155 -0
  26. package/iter/awun/docs/types/test.ts +150 -0
  27. package/iter/awun/lib/defaults.js +38 -0
  28. package/iter/awun/lib/index.js +55 -0
  29. package/iter/awun/lib/main.js +305 -0
  30. package/iter/awun/package.json +68 -0
  31. package/iter/bartlett-hann-pulse/README.md +331 -0
  32. package/iter/bartlett-hann-pulse/docs/img/equation_bartlett_hann_pulse_waveform.svg +191 -0
  33. package/iter/bartlett-hann-pulse/docs/repl.txt +54 -0
  34. package/iter/bartlett-hann-pulse/docs/types/index.d.ts +105 -0
  35. package/iter/bartlett-hann-pulse/docs/types/test.ts +89 -0
  36. package/iter/bartlett-hann-pulse/lib/index.js +54 -0
  37. package/iter/bartlett-hann-pulse/lib/main.js +192 -0
  38. package/iter/bartlett-hann-pulse/lib/validate.js +97 -0
  39. package/iter/bartlett-hann-pulse/package.json +76 -0
  40. package/iter/bartlett-pulse/README.md +328 -0
  41. package/iter/bartlett-pulse/docs/img/equation_bartlett_pulse_waveform.svg +128 -0
  42. package/iter/bartlett-pulse/docs/repl.txt +54 -0
  43. package/iter/bartlett-pulse/docs/types/index.d.ts +105 -0
  44. package/iter/bartlett-pulse/docs/types/test.ts +89 -0
  45. package/iter/bartlett-pulse/lib/index.js +54 -0
  46. package/iter/bartlett-pulse/lib/main.js +169 -0
  47. package/iter/bartlett-pulse/lib/validate.js +97 -0
  48. package/iter/bartlett-pulse/package.json +72 -0
  49. package/iter/cosine-wave/README.md +292 -0
  50. package/iter/cosine-wave/docs/img/equation_cosine_waveform.svg +55 -0
  51. package/iter/cosine-wave/docs/repl.txt +51 -0
  52. package/iter/cosine-wave/docs/types/index.d.ts +96 -0
  53. package/iter/cosine-wave/docs/types/test.ts +78 -0
  54. package/iter/cosine-wave/lib/index.js +54 -0
  55. package/iter/cosine-wave/lib/main.js +156 -0
  56. package/iter/cosine-wave/lib/validate.js +90 -0
  57. package/iter/cosine-wave/package.json +70 -0
  58. package/iter/dirac-comb/README.md +278 -0
  59. package/iter/dirac-comb/docs/img/equation_dirac_comb.svg +44 -0
  60. package/iter/dirac-comb/docs/repl.txt +48 -0
  61. package/iter/dirac-comb/docs/types/index.d.ts +89 -0
  62. package/iter/dirac-comb/docs/types/test.ts +78 -0
  63. package/iter/dirac-comb/lib/index.js +54 -0
  64. package/iter/dirac-comb/lib/main.js +152 -0
  65. package/iter/dirac-comb/lib/validate.js +82 -0
  66. package/iter/dirac-comb/package.json +72 -0
  67. package/iter/docs/types/index.d.ts +678 -0
  68. package/iter/docs/types/test.ts +29 -0
  69. package/iter/flat-top-pulse/README.md +330 -0
  70. package/iter/flat-top-pulse/docs/img/equation_flat_top_pulse_waveform.svg +177 -0
  71. package/iter/flat-top-pulse/docs/repl.txt +54 -0
  72. package/iter/flat-top-pulse/docs/types/index.d.ts +105 -0
  73. package/iter/flat-top-pulse/docs/types/test.ts +89 -0
  74. package/iter/flat-top-pulse/lib/index.js +54 -0
  75. package/iter/flat-top-pulse/lib/main.js +193 -0
  76. package/iter/flat-top-pulse/lib/validate.js +97 -0
  77. package/iter/flat-top-pulse/package.json +71 -0
  78. package/iter/hann-pulse/README.md +328 -0
  79. package/iter/hann-pulse/docs/img/equation_hann_pulse_waveform.svg +129 -0
  80. package/iter/hann-pulse/docs/repl.txt +54 -0
  81. package/iter/hann-pulse/docs/types/index.d.ts +105 -0
  82. package/iter/hann-pulse/docs/types/test.ts +89 -0
  83. package/iter/hann-pulse/lib/index.js +54 -0
  84. package/iter/hann-pulse/lib/main.js +171 -0
  85. package/iter/hann-pulse/lib/validate.js +97 -0
  86. package/iter/hann-pulse/package.json +72 -0
  87. package/iter/lanczos-pulse/README.md +325 -0
  88. package/iter/lanczos-pulse/docs/img/equation_lanczos_pulse_waveform.svg +122 -0
  89. package/iter/lanczos-pulse/docs/repl.txt +54 -0
  90. package/iter/lanczos-pulse/docs/types/index.d.ts +105 -0
  91. package/iter/lanczos-pulse/docs/types/test.ts +89 -0
  92. package/iter/lanczos-pulse/lib/index.js +54 -0
  93. package/iter/lanczos-pulse/lib/main.js +169 -0
  94. package/iter/lanczos-pulse/lib/validate.js +97 -0
  95. package/iter/lanczos-pulse/package.json +70 -0
  96. package/iter/lib/index.js +186 -0
  97. package/iter/package.json +63 -0
  98. package/iter/periodic-sinc/README.md +301 -0
  99. package/iter/periodic-sinc/docs/img/equation_periodic_sinc.svg +146 -0
  100. package/iter/periodic-sinc/docs/img/equation_periodic_sinc_in_terms_of_period.svg +164 -0
  101. package/iter/periodic-sinc/docs/img/equation_periodic_sinc_in_terms_of_sinc_function.svg +65 -0
  102. package/iter/periodic-sinc/docs/img/equation_periodic_sinc_substitution_even.svg +34 -0
  103. package/iter/periodic-sinc/docs/img/equation_periodic_sinc_substitution_odd.svg +34 -0
  104. package/iter/periodic-sinc/docs/repl.txt +54 -0
  105. package/iter/periodic-sinc/docs/types/index.d.ts +98 -0
  106. package/iter/periodic-sinc/docs/types/test.ts +102 -0
  107. package/iter/periodic-sinc/lib/index.js +50 -0
  108. package/iter/periodic-sinc/lib/main.js +241 -0
  109. package/iter/periodic-sinc/lib/validate.js +90 -0
  110. package/iter/periodic-sinc/package.json +70 -0
  111. package/iter/pulse/README.md +322 -0
  112. package/iter/pulse/docs/img/equation_pulse_waveform.svg +115 -0
  113. package/iter/pulse/docs/repl.txt +58 -0
  114. package/iter/pulse/docs/types/index.d.ts +109 -0
  115. package/iter/pulse/docs/types/test.ts +100 -0
  116. package/iter/pulse/lib/index.js +54 -0
  117. package/iter/pulse/lib/main.js +165 -0
  118. package/iter/pulse/lib/validate.js +105 -0
  119. package/iter/pulse/package.json +69 -0
  120. package/iter/sawtooth-wave/README.md +289 -0
  121. package/iter/sawtooth-wave/docs/img/equation_sawtooth_waveform.svg +73 -0
  122. package/iter/sawtooth-wave/docs/repl.txt +51 -0
  123. package/iter/sawtooth-wave/docs/types/index.d.ts +96 -0
  124. package/iter/sawtooth-wave/docs/types/test.ts +78 -0
  125. package/iter/sawtooth-wave/lib/index.js +54 -0
  126. package/iter/sawtooth-wave/lib/main.js +161 -0
  127. package/iter/sawtooth-wave/lib/validate.js +90 -0
  128. package/iter/sawtooth-wave/package.json +67 -0
  129. package/iter/sine-wave/README.md +292 -0
  130. package/iter/sine-wave/docs/img/equation_sine_waveform.svg +55 -0
  131. package/iter/sine-wave/docs/repl.txt +51 -0
  132. package/iter/sine-wave/docs/types/index.d.ts +96 -0
  133. package/iter/sine-wave/docs/types/test.ts +78 -0
  134. package/iter/sine-wave/lib/index.js +54 -0
  135. package/iter/sine-wave/lib/main.js +156 -0
  136. package/iter/sine-wave/lib/validate.js +90 -0
  137. package/iter/sine-wave/package.json +70 -0
  138. package/iter/square-wave/README.md +291 -0
  139. package/iter/square-wave/docs/img/equation_square_waveform.svg +115 -0
  140. package/iter/square-wave/docs/repl.txt +55 -0
  141. package/iter/square-wave/docs/types/index.d.ts +105 -0
  142. package/iter/square-wave/docs/types/test.ts +89 -0
  143. package/iter/square-wave/lib/index.js +54 -0
  144. package/iter/square-wave/lib/main.js +156 -0
  145. package/iter/square-wave/lib/validate.js +99 -0
  146. package/iter/square-wave/package.json +67 -0
  147. package/iter/triangle-wave/README.md +289 -0
  148. package/iter/triangle-wave/docs/img/equation_triangle_waveform.svg +75 -0
  149. package/iter/triangle-wave/docs/repl.txt +51 -0
  150. package/iter/triangle-wave/docs/types/index.d.ts +96 -0
  151. package/iter/triangle-wave/docs/types/test.ts +78 -0
  152. package/iter/triangle-wave/lib/index.js +54 -0
  153. package/iter/triangle-wave/lib/main.js +160 -0
  154. package/iter/triangle-wave/lib/validate.js +90 -0
  155. package/iter/triangle-wave/package.json +66 -0
  156. package/lib/index.js +51 -0
  157. package/package.json +72 -0
@@ -0,0 +1,240 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2019 The Stdlib Authors.
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+
19
+ -->
20
+
21
+ # iterawgn
22
+
23
+ > Create an [iterator][mdn-iterator-protocol] which introduces [additive white Gaussian noise][awgn] (AWGN).
24
+
25
+ <section class="intro">
26
+
27
+ </section>
28
+
29
+ <!-- /.intro -->
30
+
31
+ <!-- Package usage documentation. -->
32
+
33
+ <section class="usage">
34
+
35
+ ## Usage
36
+
37
+ ```javascript
38
+ var iterawgn = require( '@stdlib/simulate/iter/awgn' );
39
+ ```
40
+
41
+ #### iterawgn( iterator, sigma\[, options] )
42
+
43
+ Returns an [iterator][mdn-iterator-protocol] which introduces [additive white Gaussian noise][awgn] (AWGN) with standard deviation `sigma`.
44
+
45
+ ```javascript
46
+ var array2iterator = require( '@stdlib/array/to-iterator' );
47
+
48
+ var arr = array2iterator( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
49
+ var it = iterawgn( arr, 0.2 );
50
+
51
+ var v = it.next().value;
52
+ // returns <number>
53
+
54
+ v = it.next().value;
55
+ // returns <number>
56
+
57
+ // ...
58
+ ```
59
+
60
+ The returned iterator protocol-compliant object has the following properties:
61
+
62
+ - **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished.
63
+ - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
64
+ - **PRNG**: underlying pseudorandom number generator (PRNG). If provided a PRNG, the property value is `null`.
65
+ - **seed**: pseudorandom number generator seed. If provided a PRNG, the property value is `null`.
66
+ - **seedLength**: length of generator seed. If provided a PRNG, the property value is `null`.
67
+ - **state**: pseudorandom number generator state. If provided a PRNG, the property value is `null`.
68
+ - **stateLength**: length of generator state. If provided a PRNG, the property value is `null`.
69
+ - **byteLength**: size (in bytes) of generator state. If provided a PRNG, the property value is `null`.
70
+
71
+ The function accepts the following `options`:
72
+
73
+ - **prng**: pseudorandom number generator for generating pseudorandom numbers drawn from a standard normal distribution. If provided, the function **ignores** both the `state` and `seed` options. In order to seed the returned iterator, one must seed the provided `prng` (assuming the provided `prng` is seedable).
74
+ - **seed**: pseudorandom number generator seed.
75
+ - **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
76
+ - **copy**: `boolean` indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that a returned iterator has exclusive control over its internal state. Default: `true`.
77
+
78
+ By default, an iterator uses the [improved ziggurat][@stdlib/random/base/improved-ziggurat] algorithm to generate pseudorandom numbers drawn from a standard normal distribution. To use a different PRNG, set the `prng` option.
79
+
80
+ ```javascript
81
+ var array2iterator = require( '@stdlib/array/to-iterator' );
82
+ var boxMuller = require( '@stdlib/random/base/box-muller' );
83
+
84
+ var arr = array2iterator( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
85
+ var it = iterawgn( arr, 0.2, {
86
+ 'prng': boxMuller
87
+ });
88
+
89
+ var v = it.next().value;
90
+ // returns <number>
91
+ ```
92
+
93
+ To seed an iterator, set the `seed` option.
94
+
95
+ ```javascript
96
+ var array2iterator = require( '@stdlib/array/to-iterator' );
97
+
98
+ var arr = array2iterator( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
99
+ var it1 = iterawgn( arr, 0.2, {
100
+ 'seed': 12345
101
+ });
102
+
103
+ var v1 = it1.next().value;
104
+ // returns <number>
105
+
106
+ var it2 = iterawgn( arr, 0.2, {
107
+ 'seed': 12345
108
+ });
109
+
110
+ var v2 = it2.next().value;
111
+ // returns <number>
112
+
113
+ var bool = ( v1 === v2 );
114
+ // returns true
115
+ ```
116
+
117
+ To return an iterator having a specific initial state, set the iterator `state` option.
118
+
119
+ ```javascript
120
+ var array2iterator = require( '@stdlib/array/to-iterator' );
121
+ var arr = array2iterator( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
122
+
123
+ var it1 = iterawgn( arr, 0.2 );
124
+
125
+ // Progress the underlying generator state:
126
+ var i;
127
+ for ( i = 0; i < 3; i++ ) {
128
+ it1.next();
129
+ }
130
+
131
+ // Create a new iterator initialized to the current state of `it1`:
132
+ var it2 = iterawgn( arr, 0.2, {
133
+ 'state': it1.state
134
+ });
135
+
136
+ // Test that the iterator values are the same:
137
+ var bool = ( it1.next().value === it2.next().value );
138
+ // returns true
139
+ ```
140
+
141
+ </section>
142
+
143
+ <!-- /.usage -->
144
+
145
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146
+
147
+ <section class="notes">
148
+
149
+ ## Notes
150
+
151
+ - If PRNG state is "shared" (meaning a state array was provided during iterator creation and **not** copied) and one sets the iterator state to a state array having a different length, the iterator does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize generated output according to the new shared state array, the state array for **each** relevant PRNG/iterator must be **explicitly** set.
152
+ - If PRNG state is "shared" and one sets the iterator state to a state array of the same length, the PRNG state is updated (along with the state of all other PRNGs/iterators sharing the state array).
153
+ - If an iterated value is non-numeric (including `NaN`), the returned [`iterator`][mdn-iterator-protocol] returns `NaN`. If non-numeric iterated values are possible, you are advised to provide an [`iterator`][mdn-iterator-protocol] which type checks and handles non-numeric values accordingly.
154
+
155
+ </section>
156
+
157
+ <!-- /.notes -->
158
+
159
+ <!-- Package usage examples. -->
160
+
161
+ <section class="examples">
162
+
163
+ ## Examples
164
+
165
+ <!-- eslint no-undef: "error" -->
166
+
167
+ ```javascript
168
+ var iterSineWave = require( '@stdlib/simulate/iter/sine-wave' );
169
+ var iterawgn = require( '@stdlib/simulate/iter/awgn' );
170
+
171
+ // Create an iterator for generating a sine wave:
172
+ var sine = iterSineWave({
173
+ 'iter': 100,
174
+ 'amplitude': 10.0
175
+ });
176
+
177
+ // Create an iterator for introducing additive white Gaussian noise:
178
+ var it = iterawgn( sine, 3.0 );
179
+
180
+ // Perform manual iteration...
181
+ var v;
182
+ while ( true ) {
183
+ v = it.next();
184
+ if ( v.done ) {
185
+ break;
186
+ }
187
+ console.log( v.value );
188
+ }
189
+ ```
190
+
191
+ </section>
192
+
193
+ <!-- /.examples -->
194
+
195
+ <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
196
+
197
+ <section class="references">
198
+
199
+ </section>
200
+
201
+ <!-- /.references -->
202
+
203
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
204
+
205
+ <section class="related">
206
+
207
+ * * *
208
+
209
+ ## See Also
210
+
211
+ - <span class="package-name">[`@stdlib/simulate/iter/awln`][@stdlib/simulate/iter/awln]</span><span class="delimiter">: </span><span class="description">create an iterator which introduces additive white Laplacian noise.</span>
212
+ - <span class="package-name">[`@stdlib/simulate/iter/awun`][@stdlib/simulate/iter/awun]</span><span class="delimiter">: </span><span class="description">create an iterator which introduces additive white uniform noise.</span>
213
+
214
+ </section>
215
+
216
+ <!-- /.related -->
217
+
218
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
219
+
220
+ <section class="links">
221
+
222
+ [mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
223
+
224
+ [awgn]: https://en.wikipedia.org/wiki/Additive_white_Gaussian_noise
225
+
226
+ [@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32
227
+
228
+ [@stdlib/random/base/improved-ziggurat]: https://www.npmjs.com/package/@stdlib/random-base-improved-ziggurat
229
+
230
+ <!-- <related-links> -->
231
+
232
+ [@stdlib/simulate/iter/awln]: https://github.com/stdlib-js/simulate/tree/main/iter/awln
233
+
234
+ [@stdlib/simulate/iter/awun]: https://github.com/stdlib-js/simulate/tree/main/iter/awun
235
+
236
+ <!-- </related-links> -->
237
+
238
+ </section>
239
+
240
+ <!-- /.links -->
@@ -0,0 +1,85 @@
1
+
2
+ {{alias}}( iterator, sigma[, options] )
3
+ Returns an iterator which introduces additive white Gaussian noise (AWGN)
4
+ with standard deviation `sigma`.
5
+
6
+ If an environment supports Symbol.iterator and the provided iterator is
7
+ iterable, the returned iterator is iterable.
8
+
9
+ Parameters
10
+ ----------
11
+ iterator: Object
12
+ Input iterator.
13
+
14
+ sigma: number
15
+ Standard deviation of the noise.
16
+
17
+ options: Object (optional)
18
+ Options.
19
+
20
+ options.prng: Function (optional)
21
+ Pseudorandom number generator (PRNG) for generating pseudorandom numbers
22
+ drawn from a standard normal distribution. If provided, the `state` and
23
+ `seed` options are ignored. In order to seed the returned iterator, one
24
+ must seed the provided `prng` (assuming the provided `prng` is
25
+ seedable).
26
+
27
+ options.seed: integer|ArrayLikeObject<integer> (optional)
28
+ Pseudorandom number generator seed. The seed may be either a positive
29
+ unsigned 32-bit integer or, for arbitrary length seeds, an array-like
30
+ object containing unsigned 32-bit integers.
31
+
32
+ options.state: Uint32Array (optional)
33
+ Pseudorandom number generator state. If provided, the `seed` option is
34
+ ignored.
35
+
36
+ options.copy: boolean (optional)
37
+ Boolean indicating whether to copy a provided pseudorandom number
38
+ generator state. Setting this option to `false` allows sharing state
39
+ between two or more pseudorandom number generators. Setting this option
40
+ to `true` ensures that a returned iterator has exclusive control over
41
+ its internal state. Default: true.
42
+
43
+ Returns
44
+ -------
45
+ iterator: Object
46
+ Iterator.
47
+
48
+ iterator.next(): Function
49
+ Returns an iterator protocol-compliant object containing the next
50
+ iterated value (if one exists) and a boolean flag indicating whether the
51
+ iterator is finished.
52
+
53
+ iterator.return( [value] ): Function
54
+ Finishes an iterator and returns a provided value.
55
+
56
+ iterator.PRNG: Function|null
57
+ Underlying pseudorandom number generator.
58
+
59
+ iterator.seed: Uint32Array|null
60
+ Pseudorandom number generator seed.
61
+
62
+ iterator.seedLength: integer|null
63
+ Length of generator seed.
64
+
65
+ iterator.state: Uint32Array|null
66
+ Pseudorandom random number generator state.
67
+
68
+ iterator.stateLength: integer|null
69
+ Length of generator state.
70
+
71
+ iterator.byteLength: integer|null
72
+ Size (in bytes) of generator state.
73
+
74
+ Examples
75
+ --------
76
+ > var src = {{alias:@stdlib/simulate/iter/sine-wave}}();
77
+ > var it = {{alias}}( src, 0.5 );
78
+ > var v = it.next().value
79
+ <number>
80
+ > v = it.next().value
81
+ <number>
82
+
83
+ See Also
84
+ --------
85
+
@@ -0,0 +1,155 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2019 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ // TypeScript Version: 4.1
20
+
21
+ /// <reference types="@stdlib/types"/>
22
+
23
+ import { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';
24
+ import * as random from '@stdlib/types/random';
25
+
26
+ // Define a union type representing both iterable and non-iterable iterators:
27
+ type Iterator = Iter | IterableIterator;
28
+
29
+ /**
30
+ * Interface defining function options.
31
+ */
32
+ interface Options {
33
+ /**
34
+ * Pseudorandom number generator which generates pseudorandom numbers drawn from a standard normal distribution.
35
+ *
36
+ * ## Notes
37
+ *
38
+ * - If provided, the `state` and `seed` options are ignored. In order to seed the returned iterator, one must seed the provided `prng` (assuming the provided `prng` is seedable).
39
+ */
40
+ prng?: random.PRNG;
41
+
42
+ /**
43
+ * Pseudorandom number generator state.
44
+ *
45
+ * ## Notes
46
+ *
47
+ * - If provided, the `seed` option is ignored.
48
+ */
49
+ state?: random.PRNGStateMT19937;
50
+
51
+ /**
52
+ * Pseudorandom number generator seed.
53
+ */
54
+ seed?: random.PRNGSeedMT19937;
55
+
56
+ /**
57
+ * Specifies whether to copy a provided pseudorandom number generator state.
58
+ *
59
+ * ## Notes
60
+ *
61
+ * - Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that a returned iterator has exclusive control over its internal state.
62
+ */
63
+ copy?: boolean;
64
+ }
65
+
66
+ /**
67
+ * Interface defining a returned iterator.
68
+ */
69
+ interface ExtendedIter extends Iter {
70
+ /**
71
+ * Underlying PRNG.
72
+ */
73
+ readonly PRNG: random.PRNG | null;
74
+
75
+ /**
76
+ * PRNG seed.
77
+ */
78
+ readonly seed: random.PRNGSeedMT19937 | null;
79
+
80
+ /**
81
+ * PRNG seed length.
82
+ */
83
+ readonly seedLength: number | null;
84
+
85
+ /**
86
+ * PRNG state.
87
+ */
88
+ state: random.PRNGStateMT19937 | null;
89
+
90
+ /**
91
+ * PRNG state length.
92
+ */
93
+ readonly stateLength: number | null;
94
+
95
+ /**
96
+ * PRNG state size (in bytes).
97
+ */
98
+ readonly byteLength: number | null;
99
+ }
100
+
101
+ /**
102
+ * Interface defining a returned iterable iterator.
103
+ */
104
+ interface ExtendedIterableIterator extends ExtendedIter {
105
+ /**
106
+ * Returns a new iterable iterator.
107
+ *
108
+ * @returns iterable iterator
109
+ */
110
+ [Symbol.iterator](): IterableIterator;
111
+ }
112
+
113
+ // Define a union type representing both iterable and non-iterable returned iterators:
114
+ type ExtendedIterator = ExtendedIter | ExtendedIterableIterator;
115
+
116
+ /**
117
+ * Returns an iterator which introduces additive white Gaussian noise with standard deviation `sigma`.
118
+ *
119
+ * @param iterator - input iterator
120
+ * @param sigma - standard deviation of the noise
121
+ * @param options - function options
122
+ * @param options.prng - pseudorandom number generator for generating pseudorandom numbers drawn from a standard normal distribution
123
+ * @param options.state - pseudorandom number generator state
124
+ * @param options.seed - pseudorandom number generator seed
125
+ * @param options.copy - boolean indicating whether to copy a provided pseudorandom number generator state (default: true)
126
+ * @throws `sigma` must be a positive number
127
+ * @throws must provide a valid state
128
+ * @returns iterator
129
+ *
130
+ * @example
131
+ * var iterSineWave = require( '@stdlib/simulate/iter/sine-wave' );
132
+ *
133
+ * var sine = iterSineWave({
134
+ * 'iter': 100
135
+ * });
136
+ *
137
+ * var it = iterawgn( sine, 0.5 );
138
+ *
139
+ * var v = it.next().value;
140
+ * // returns <number>
141
+ *
142
+ * v = it.next().value;
143
+ * // returns <number>
144
+ *
145
+ * v = it.next().value;
146
+ * // returns <number>
147
+ *
148
+ * // ...
149
+ */
150
+ declare function iterawgn( iterator: Iterator, sigma: number, options?: Options ): ExtendedIterator;
151
+
152
+
153
+ // EXPORTS //
154
+
155
+ export = iterawgn;
@@ -0,0 +1,150 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2019 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ import iterawgn = require( './index' );
20
+
21
+ /**
22
+ * Returns an iterator protocol-compliant object.
23
+ *
24
+ * @returns iterator protocol-compliant object
25
+ */
26
+ function iterator() {
27
+ return {
28
+ 'next': next
29
+ };
30
+
31
+ /**
32
+ * Implements the iterator protocol `next` method.
33
+ *
34
+ * @returns iterator protocol-compliant object
35
+ */
36
+ function next() {
37
+ return {
38
+ 'value': true,
39
+ 'done': false
40
+ };
41
+ }
42
+ }
43
+
44
+
45
+ // TESTS //
46
+
47
+ // The function returns an iterator...
48
+ {
49
+ iterawgn( iterator(), 0.2 ); // $ExpectType ExtendedIterator
50
+ iterawgn( iterator(), 0.2, {} ); // $ExpectType ExtendedIterator
51
+ iterawgn( iterator(), 0.2, { 'seed': 12345 } ); // $ExpectType ExtendedIterator
52
+ }
53
+
54
+ // The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object...
55
+ {
56
+ iterawgn( '5', 1.0 ); // $ExpectError
57
+ iterawgn( 5, 1.0 ); // $ExpectError
58
+ iterawgn( true, 1.0 ); // $ExpectError
59
+ iterawgn( false, 1.0 ); // $ExpectError
60
+ iterawgn( null, 1.0 ); // $ExpectError
61
+ iterawgn( undefined, 1.0 ); // $ExpectError
62
+ iterawgn( [], 1.0 ); // $ExpectError
63
+ iterawgn( {}, 1.0 ); // $ExpectError
64
+ iterawgn( ( x: number ): number => x, 1.0 ); // $ExpectError
65
+
66
+ iterawgn( '5', 1.0, {} ); // $ExpectError
67
+ iterawgn( 5, 1.0, {} ); // $ExpectError
68
+ iterawgn( true, 1.0, {} ); // $ExpectError
69
+ iterawgn( false, 1.0, {} ); // $ExpectError
70
+ iterawgn( null, 1.0, {} ); // $ExpectError
71
+ iterawgn( undefined, 1.0, {} ); // $ExpectError
72
+ iterawgn( [], 1.0, {} ); // $ExpectError
73
+ iterawgn( {}, 1.0, {} ); // $ExpectError
74
+ iterawgn( ( x: number ): number => x, 1.0, {} ); // $ExpectError
75
+ }
76
+
77
+ // The compiler throws an error if the function is provided a second argument which is not a number...
78
+ {
79
+ iterawgn( iterator(), '5' ); // $ExpectError
80
+ iterawgn( iterator(), true ); // $ExpectError
81
+ iterawgn( iterator(), false ); // $ExpectError
82
+ iterawgn( iterator(), null ); // $ExpectError
83
+ iterawgn( iterator(), undefined ); // $ExpectError
84
+ iterawgn( iterator(), [] ); // $ExpectError
85
+ iterawgn( iterator(), {} ); // $ExpectError
86
+ iterawgn( iterator(), ( x: number ): number => x ); // $ExpectError
87
+
88
+ iterawgn( iterator(), '5', {} ); // $ExpectError
89
+ iterawgn( iterator(), true, {} ); // $ExpectError
90
+ iterawgn( iterator(), false, {} ); // $ExpectError
91
+ iterawgn( iterator(), null, {} ); // $ExpectError
92
+ iterawgn( iterator(), undefined, {} ); // $ExpectError
93
+ iterawgn( iterator(), [], {} ); // $ExpectError
94
+ iterawgn( iterator(), {}, {} ); // $ExpectError
95
+ iterawgn( iterator(), ( x: number ): number => x, {} ); // $ExpectError
96
+ }
97
+
98
+ // The compiler throws an error if the function is provided a third argument which is not an object...
99
+ {
100
+ iterawgn( iterator(), 1.0, null ); // $ExpectError
101
+ }
102
+
103
+ // The compiler throws an error if the function is provided an invalid `seed` option...
104
+ {
105
+ iterawgn( iterator(), 1.0, { 'seed': '5' } ); // $ExpectError
106
+ iterawgn( iterator(), 1.0, { 'seed': true } ); // $ExpectError
107
+ iterawgn( iterator(), 1.0, { 'seed': false } ); // $ExpectError
108
+ iterawgn( iterator(), 1.0, { 'seed': null } ); // $ExpectError
109
+ iterawgn( iterator(), 1.0, { 'seed': {} } ); // $ExpectError
110
+ iterawgn( iterator(), 1.0, { 'seed': ( x: number ): number => x } ); // $ExpectError
111
+ }
112
+
113
+ // The compiler throws an error if the function is provided an invalid `prng` option...
114
+ {
115
+ iterawgn( iterator(), 1.0, { 'prng': '5' } ); // $ExpectError
116
+ iterawgn( iterator(), 1.0, { 'prng': 5 } ); // $ExpectError
117
+ iterawgn( iterator(), 1.0, { 'prng': true } ); // $ExpectError
118
+ iterawgn( iterator(), 1.0, { 'prng': false } ); // $ExpectError
119
+ iterawgn( iterator(), 1.0, { 'prng': null } ); // $ExpectError
120
+ iterawgn( iterator(), 1.0, { 'prng': [] } ); // $ExpectError
121
+ iterawgn( iterator(), 1.0, { 'prng': {} } ); // $ExpectError
122
+ }
123
+
124
+ // The compiler throws an error if the function is provided an invalid `state` option...
125
+ {
126
+ iterawgn( iterator(), 1.0, { 'state': '5' } ); // $ExpectError
127
+ iterawgn( iterator(), 1.0, { 'state': 5 } ); // $ExpectError
128
+ iterawgn( iterator(), 1.0, { 'state': true } ); // $ExpectError
129
+ iterawgn( iterator(), 1.0, { 'state': false } ); // $ExpectError
130
+ iterawgn( iterator(), 1.0, { 'state': null } ); // $ExpectError
131
+ iterawgn( iterator(), 1.0, { 'state': [] } ); // $ExpectError
132
+ iterawgn( iterator(), 1.0, { 'state': {} } ); // $ExpectError
133
+ iterawgn( iterator(), 1.0, { 'state': ( x: number ): number => x } ); // $ExpectError
134
+ }
135
+
136
+ // The compiler throws an error if the function is provided an invalid `copy` option...
137
+ {
138
+ iterawgn( iterator(), 1.0, { 'copy': '5' } ); // $ExpectError
139
+ iterawgn( iterator(), 1.0, { 'copy': 5 } ); // $ExpectError
140
+ iterawgn( iterator(), 1.0, { 'copy': null } ); // $ExpectError
141
+ iterawgn( iterator(), 1.0, { 'copy': [] } ); // $ExpectError
142
+ iterawgn( iterator(), 1.0, { 'copy': {} } ); // $ExpectError
143
+ iterawgn( iterator(), 1.0, { 'copy': ( x: number ): number => x } ); // $ExpectError
144
+ }
145
+
146
+ // The compiler throws an error if the function is provided insufficient arguments...
147
+ {
148
+ iterawgn(); // $ExpectError
149
+ iterawgn( iterator() ); // $ExpectError
150
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2023 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MAIN //
22
+
23
+ /**
24
+ * Returns default options.
25
+ *
26
+ * @private
27
+ * @returns {Object} default options
28
+ */
29
+ function defaults() {
30
+ return {
31
+ 'copy': true
32
+ };
33
+ }
34
+
35
+
36
+ // EXPORTS //
37
+
38
+ module.exports = defaults;