@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.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +188 -0
- package/SECURITY.md +5 -0
- package/docs/types/index.d.ts +43 -0
- package/iter/README.md +142 -0
- package/iter/awgn/README.md +240 -0
- package/iter/awgn/docs/repl.txt +85 -0
- package/iter/awgn/docs/types/index.d.ts +155 -0
- package/iter/awgn/docs/types/test.ts +150 -0
- package/iter/awgn/lib/defaults.js +38 -0
- package/iter/awgn/lib/index.js +55 -0
- package/iter/awgn/lib/main.js +278 -0
- package/iter/awgn/package.json +68 -0
- package/iter/awln/README.md +238 -0
- package/iter/awln/docs/repl.txt +85 -0
- package/iter/awln/docs/types/index.d.ts +155 -0
- package/iter/awln/docs/types/test.ts +150 -0
- package/iter/awln/lib/defaults.js +38 -0
- package/iter/awln/lib/index.js +55 -0
- package/iter/awln/lib/main.js +299 -0
- package/iter/awln/package.json +72 -0
- package/iter/awun/README.md +238 -0
- package/iter/awun/docs/repl.txt +85 -0
- package/iter/awun/docs/types/index.d.ts +155 -0
- package/iter/awun/docs/types/test.ts +150 -0
- package/iter/awun/lib/defaults.js +38 -0
- package/iter/awun/lib/index.js +55 -0
- package/iter/awun/lib/main.js +305 -0
- package/iter/awun/package.json +68 -0
- package/iter/bartlett-hann-pulse/README.md +331 -0
- package/iter/bartlett-hann-pulse/docs/img/equation_bartlett_hann_pulse_waveform.svg +191 -0
- package/iter/bartlett-hann-pulse/docs/repl.txt +54 -0
- package/iter/bartlett-hann-pulse/docs/types/index.d.ts +105 -0
- package/iter/bartlett-hann-pulse/docs/types/test.ts +89 -0
- package/iter/bartlett-hann-pulse/lib/index.js +54 -0
- package/iter/bartlett-hann-pulse/lib/main.js +192 -0
- package/iter/bartlett-hann-pulse/lib/validate.js +97 -0
- package/iter/bartlett-hann-pulse/package.json +76 -0
- package/iter/bartlett-pulse/README.md +328 -0
- package/iter/bartlett-pulse/docs/img/equation_bartlett_pulse_waveform.svg +128 -0
- package/iter/bartlett-pulse/docs/repl.txt +54 -0
- package/iter/bartlett-pulse/docs/types/index.d.ts +105 -0
- package/iter/bartlett-pulse/docs/types/test.ts +89 -0
- package/iter/bartlett-pulse/lib/index.js +54 -0
- package/iter/bartlett-pulse/lib/main.js +169 -0
- package/iter/bartlett-pulse/lib/validate.js +97 -0
- package/iter/bartlett-pulse/package.json +72 -0
- package/iter/cosine-wave/README.md +292 -0
- package/iter/cosine-wave/docs/img/equation_cosine_waveform.svg +55 -0
- package/iter/cosine-wave/docs/repl.txt +51 -0
- package/iter/cosine-wave/docs/types/index.d.ts +96 -0
- package/iter/cosine-wave/docs/types/test.ts +78 -0
- package/iter/cosine-wave/lib/index.js +54 -0
- package/iter/cosine-wave/lib/main.js +156 -0
- package/iter/cosine-wave/lib/validate.js +90 -0
- package/iter/cosine-wave/package.json +70 -0
- package/iter/dirac-comb/README.md +278 -0
- package/iter/dirac-comb/docs/img/equation_dirac_comb.svg +44 -0
- package/iter/dirac-comb/docs/repl.txt +48 -0
- package/iter/dirac-comb/docs/types/index.d.ts +89 -0
- package/iter/dirac-comb/docs/types/test.ts +78 -0
- package/iter/dirac-comb/lib/index.js +54 -0
- package/iter/dirac-comb/lib/main.js +152 -0
- package/iter/dirac-comb/lib/validate.js +82 -0
- package/iter/dirac-comb/package.json +72 -0
- package/iter/docs/types/index.d.ts +678 -0
- package/iter/docs/types/test.ts +29 -0
- package/iter/flat-top-pulse/README.md +330 -0
- package/iter/flat-top-pulse/docs/img/equation_flat_top_pulse_waveform.svg +177 -0
- package/iter/flat-top-pulse/docs/repl.txt +54 -0
- package/iter/flat-top-pulse/docs/types/index.d.ts +105 -0
- package/iter/flat-top-pulse/docs/types/test.ts +89 -0
- package/iter/flat-top-pulse/lib/index.js +54 -0
- package/iter/flat-top-pulse/lib/main.js +193 -0
- package/iter/flat-top-pulse/lib/validate.js +97 -0
- package/iter/flat-top-pulse/package.json +71 -0
- package/iter/hann-pulse/README.md +328 -0
- package/iter/hann-pulse/docs/img/equation_hann_pulse_waveform.svg +129 -0
- package/iter/hann-pulse/docs/repl.txt +54 -0
- package/iter/hann-pulse/docs/types/index.d.ts +105 -0
- package/iter/hann-pulse/docs/types/test.ts +89 -0
- package/iter/hann-pulse/lib/index.js +54 -0
- package/iter/hann-pulse/lib/main.js +171 -0
- package/iter/hann-pulse/lib/validate.js +97 -0
- package/iter/hann-pulse/package.json +72 -0
- package/iter/lanczos-pulse/README.md +325 -0
- package/iter/lanczos-pulse/docs/img/equation_lanczos_pulse_waveform.svg +122 -0
- package/iter/lanczos-pulse/docs/repl.txt +54 -0
- package/iter/lanczos-pulse/docs/types/index.d.ts +105 -0
- package/iter/lanczos-pulse/docs/types/test.ts +89 -0
- package/iter/lanczos-pulse/lib/index.js +54 -0
- package/iter/lanczos-pulse/lib/main.js +169 -0
- package/iter/lanczos-pulse/lib/validate.js +97 -0
- package/iter/lanczos-pulse/package.json +70 -0
- package/iter/lib/index.js +186 -0
- package/iter/package.json +63 -0
- package/iter/periodic-sinc/README.md +301 -0
- package/iter/periodic-sinc/docs/img/equation_periodic_sinc.svg +146 -0
- package/iter/periodic-sinc/docs/img/equation_periodic_sinc_in_terms_of_period.svg +164 -0
- package/iter/periodic-sinc/docs/img/equation_periodic_sinc_in_terms_of_sinc_function.svg +65 -0
- package/iter/periodic-sinc/docs/img/equation_periodic_sinc_substitution_even.svg +34 -0
- package/iter/periodic-sinc/docs/img/equation_periodic_sinc_substitution_odd.svg +34 -0
- package/iter/periodic-sinc/docs/repl.txt +54 -0
- package/iter/periodic-sinc/docs/types/index.d.ts +98 -0
- package/iter/periodic-sinc/docs/types/test.ts +102 -0
- package/iter/periodic-sinc/lib/index.js +50 -0
- package/iter/periodic-sinc/lib/main.js +241 -0
- package/iter/periodic-sinc/lib/validate.js +90 -0
- package/iter/periodic-sinc/package.json +70 -0
- package/iter/pulse/README.md +322 -0
- package/iter/pulse/docs/img/equation_pulse_waveform.svg +115 -0
- package/iter/pulse/docs/repl.txt +58 -0
- package/iter/pulse/docs/types/index.d.ts +109 -0
- package/iter/pulse/docs/types/test.ts +100 -0
- package/iter/pulse/lib/index.js +54 -0
- package/iter/pulse/lib/main.js +165 -0
- package/iter/pulse/lib/validate.js +105 -0
- package/iter/pulse/package.json +69 -0
- package/iter/sawtooth-wave/README.md +289 -0
- package/iter/sawtooth-wave/docs/img/equation_sawtooth_waveform.svg +73 -0
- package/iter/sawtooth-wave/docs/repl.txt +51 -0
- package/iter/sawtooth-wave/docs/types/index.d.ts +96 -0
- package/iter/sawtooth-wave/docs/types/test.ts +78 -0
- package/iter/sawtooth-wave/lib/index.js +54 -0
- package/iter/sawtooth-wave/lib/main.js +161 -0
- package/iter/sawtooth-wave/lib/validate.js +90 -0
- package/iter/sawtooth-wave/package.json +67 -0
- package/iter/sine-wave/README.md +292 -0
- package/iter/sine-wave/docs/img/equation_sine_waveform.svg +55 -0
- package/iter/sine-wave/docs/repl.txt +51 -0
- package/iter/sine-wave/docs/types/index.d.ts +96 -0
- package/iter/sine-wave/docs/types/test.ts +78 -0
- package/iter/sine-wave/lib/index.js +54 -0
- package/iter/sine-wave/lib/main.js +156 -0
- package/iter/sine-wave/lib/validate.js +90 -0
- package/iter/sine-wave/package.json +70 -0
- package/iter/square-wave/README.md +291 -0
- package/iter/square-wave/docs/img/equation_square_waveform.svg +115 -0
- package/iter/square-wave/docs/repl.txt +55 -0
- package/iter/square-wave/docs/types/index.d.ts +105 -0
- package/iter/square-wave/docs/types/test.ts +89 -0
- package/iter/square-wave/lib/index.js +54 -0
- package/iter/square-wave/lib/main.js +156 -0
- package/iter/square-wave/lib/validate.js +99 -0
- package/iter/square-wave/package.json +67 -0
- package/iter/triangle-wave/README.md +289 -0
- package/iter/triangle-wave/docs/img/equation_triangle_waveform.svg +75 -0
- package/iter/triangle-wave/docs/repl.txt +51 -0
- package/iter/triangle-wave/docs/types/index.d.ts +96 -0
- package/iter/triangle-wave/docs/types/test.ts +78 -0
- package/iter/triangle-wave/lib/index.js +54 -0
- package/iter/triangle-wave/lib/main.js +160 -0
- package/iter/triangle-wave/lib/validate.js +90 -0
- package/iter/triangle-wave/package.json +66 -0
- package/lib/index.js +51 -0
- package/package.json +72 -0
|
@@ -0,0 +1,299 @@
|
|
|
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
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
|
|
24
|
+
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
|
|
25
|
+
var setReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' );
|
|
26
|
+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
|
|
27
|
+
var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
|
|
28
|
+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
|
|
29
|
+
var isFunction = require( '@stdlib/assert/is-function' );
|
|
30
|
+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
|
|
31
|
+
var isUint32Array = require( '@stdlib/assert/is-uint32array' );
|
|
32
|
+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
|
|
33
|
+
var noop = require( '@stdlib/utils/noop' );
|
|
34
|
+
var constantFunction = require( '@stdlib/utils/constant-function' );
|
|
35
|
+
var iteratorSymbol = require( '@stdlib/symbol/iterator' );
|
|
36
|
+
var laplace = require( '@stdlib/random/base/laplace' ).factory;
|
|
37
|
+
var format = require( '@stdlib/string/format' );
|
|
38
|
+
var SQRT_TWO = require( '@stdlib/constants/float64/sqrt-two' );
|
|
39
|
+
var defaults = require( './defaults.js' );
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
// MAIN //
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns an iterator which introduces additive white Laplacian noise with standard deviation `sigma`.
|
|
46
|
+
*
|
|
47
|
+
* ## Method
|
|
48
|
+
*
|
|
49
|
+
* - The variance of a Laplace (biexponential or double-exponential) distribution is given by
|
|
50
|
+
*
|
|
51
|
+
* ```tex
|
|
52
|
+
* \operatorname{Var} = 2b^2
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* where \\( b \\) is the distribution scale parameter.
|
|
56
|
+
*
|
|
57
|
+
* - Accordingly, the standard deviation is then
|
|
58
|
+
*
|
|
59
|
+
* ```tex
|
|
60
|
+
* \sigma = b\sqrt{2}
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* - Solving for \\( b \\), we derive the parameter transform necessary for generating a Laplacian random variable with standard deviation \\( \sigma \\)
|
|
64
|
+
*
|
|
65
|
+
* ```tex
|
|
66
|
+
* b = \frac{\sigma}{\sqrt{2}}
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param {Iterator} iterator - input iterator
|
|
70
|
+
* @param {PositiveNumber} sigma - standard deviation of the noise
|
|
71
|
+
* @param {Options} [options] - function options
|
|
72
|
+
* @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers on the interval `[0,1)`
|
|
73
|
+
* @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
|
|
74
|
+
* @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
|
|
75
|
+
* @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
|
|
76
|
+
* @throws {TypeError} first argument must be an iterator
|
|
77
|
+
* @throws {TypeError} second argument must be a positive number
|
|
78
|
+
* @throws {TypeError} third argument must be an object
|
|
79
|
+
* @throws {TypeError} must provide valid options
|
|
80
|
+
* @throws {Error} must provide a valid state
|
|
81
|
+
* @returns {Iterator} iterator
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* var iterSineWave = require( '@stdlib/simulate/iter/sine-wave' );
|
|
85
|
+
*
|
|
86
|
+
* var sine = iterSineWave({
|
|
87
|
+
* 'iter': 100
|
|
88
|
+
* });
|
|
89
|
+
*
|
|
90
|
+
* var it = iterawln( sine, 0.5 );
|
|
91
|
+
*
|
|
92
|
+
* var v = it.next().value;
|
|
93
|
+
* // returns <number>
|
|
94
|
+
*
|
|
95
|
+
* v = it.next().value;
|
|
96
|
+
* // returns <number>
|
|
97
|
+
*
|
|
98
|
+
* v = it.next().value;
|
|
99
|
+
* // returns <number>
|
|
100
|
+
*
|
|
101
|
+
* // ...
|
|
102
|
+
*/
|
|
103
|
+
function iterawln( iterator, sigma, options ) {
|
|
104
|
+
var rlaplace;
|
|
105
|
+
var opts;
|
|
106
|
+
var iter;
|
|
107
|
+
var FLG;
|
|
108
|
+
if ( !isIteratorLike( iterator ) ) {
|
|
109
|
+
throw new TypeError( format( 'invalid argument. First argument must be an iterator. Value: `%s`.', iterator ) );
|
|
110
|
+
}
|
|
111
|
+
if ( !isPositiveNumber( sigma ) ) {
|
|
112
|
+
throw new TypeError( format( 'invalid argument. Second argument must be a positive number. Value: `%s`.', sigma ) );
|
|
113
|
+
}
|
|
114
|
+
opts = defaults();
|
|
115
|
+
if ( arguments.length > 2 ) {
|
|
116
|
+
if ( !isPlainObject( options ) ) {
|
|
117
|
+
throw new TypeError( format( 'invalid argument. Third argument must be an object. Value: `%s`.', options ) );
|
|
118
|
+
}
|
|
119
|
+
if ( hasOwnProp( options, 'copy' ) ) {
|
|
120
|
+
opts.copy = options.copy;
|
|
121
|
+
if ( !isBoolean( options.copy ) ) {
|
|
122
|
+
throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'copy', options.copy ) );
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if ( hasOwnProp( options, 'prng' ) ) {
|
|
126
|
+
if ( !isFunction( options.prng ) ) {
|
|
127
|
+
throw new TypeError( format( 'invalid option. `%s` option must be a pseudorandom number generator function. Option: `%s`.', 'prng', options.prng ) );
|
|
128
|
+
}
|
|
129
|
+
opts.prng = options.prng;
|
|
130
|
+
}
|
|
131
|
+
// If provided a PRNG, ignore the `state` option, as we don't support getting or setting PRNG state.
|
|
132
|
+
else if ( hasOwnProp( options, 'state' ) ) {
|
|
133
|
+
opts.state = options.state;
|
|
134
|
+
if ( !isUint32Array( options.state ) ) {
|
|
135
|
+
throw new TypeError( format( 'invalid option. `%s` option must be a Uint32Array. Option: `%s`.', 'state', options.state ) );
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// If provided a PRNG, ignore the `seed` option, as a `seed`, by itself, is insufficient to guarantee reproducibility. If provided a state, ignore the `seed` option, as a PRNG state should contain seed information.
|
|
139
|
+
else if ( hasOwnProp( options, 'seed' ) ) {
|
|
140
|
+
opts.seed = options.seed;
|
|
141
|
+
if ( options.seed === void 0 ) {
|
|
142
|
+
throw new TypeError( format( 'invalid option. `%s` option must be either a positive integer less than or equal to the maximum unsigned 32-bit integer or an array-like object containing integer values less than or equal to the maximum unsigned 32-bit integer. Option: `%s`.', 'seed', options.seed ) );
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
rlaplace = laplace( 0.0, sigma/SQRT_TWO, opts );
|
|
147
|
+
|
|
148
|
+
// Create an iterator protocol-compliant object:
|
|
149
|
+
iter = {};
|
|
150
|
+
setReadOnly( iter, 'next', next );
|
|
151
|
+
setReadOnly( iter, 'return', end );
|
|
152
|
+
|
|
153
|
+
// If an environment supports `Symbol.iterator` and the provided iterator is iterable, make the iterator iterable:
|
|
154
|
+
if ( iteratorSymbol && isFunction( iterator[ iteratorSymbol ] ) ) {
|
|
155
|
+
setReadOnly( iter, iteratorSymbol, factory );
|
|
156
|
+
}
|
|
157
|
+
// If we are provided an "external" PRNG, we don't support getting or setting PRNG state, as we'd need to check for compatible state value types, etc, entailing considerable complexity.
|
|
158
|
+
if ( options && options.prng ) {
|
|
159
|
+
setReadOnly( iter, 'seed', null );
|
|
160
|
+
setReadOnly( iter, 'seedLength', null );
|
|
161
|
+
setReadWriteAccessor( iter, 'state', constantFunction( null ), noop );
|
|
162
|
+
setReadOnly( iter, 'stateLength', null );
|
|
163
|
+
setReadOnly( iter, 'byteLength', null );
|
|
164
|
+
setReadOnly( iter, 'PRNG', options.prng );
|
|
165
|
+
} else {
|
|
166
|
+
setReadOnlyAccessor( iter, 'seed', getSeed );
|
|
167
|
+
setReadOnlyAccessor( iter, 'seedLength', getSeedLength );
|
|
168
|
+
setReadWriteAccessor( iter, 'state', getState, setState );
|
|
169
|
+
setReadOnlyAccessor( iter, 'stateLength', getStateLength );
|
|
170
|
+
setReadOnlyAccessor( iter, 'byteLength', getStateSize );
|
|
171
|
+
setReadOnly( iter, 'PRNG', rlaplace.PRNG );
|
|
172
|
+
}
|
|
173
|
+
return iter;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Returns the PRNG seed.
|
|
177
|
+
*
|
|
178
|
+
* @private
|
|
179
|
+
* @returns {PRNGSeedMT19937} seed
|
|
180
|
+
*/
|
|
181
|
+
function getSeed() {
|
|
182
|
+
return rlaplace.seed;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Returns the PRNG seed length.
|
|
187
|
+
*
|
|
188
|
+
* @private
|
|
189
|
+
* @returns {PositiveInteger} seed length
|
|
190
|
+
*/
|
|
191
|
+
function getSeedLength() {
|
|
192
|
+
return rlaplace.seedLength;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Returns the PRNG state length.
|
|
197
|
+
*
|
|
198
|
+
* @private
|
|
199
|
+
* @returns {PositiveInteger} state length
|
|
200
|
+
*/
|
|
201
|
+
function getStateLength() {
|
|
202
|
+
return rlaplace.stateLength;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Returns the PRNG state size (in bytes).
|
|
207
|
+
*
|
|
208
|
+
* @private
|
|
209
|
+
* @returns {PositiveInteger} state size (in bytes)
|
|
210
|
+
*/
|
|
211
|
+
function getStateSize() {
|
|
212
|
+
return rlaplace.byteLength;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Returns the current PRNG state.
|
|
217
|
+
*
|
|
218
|
+
* @private
|
|
219
|
+
* @returns {PRNGStateMT19937} current state
|
|
220
|
+
*/
|
|
221
|
+
function getState() {
|
|
222
|
+
return rlaplace.state;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Sets the PRNG state.
|
|
227
|
+
*
|
|
228
|
+
* @private
|
|
229
|
+
* @param {PRNGStateMT19937} s - generator state
|
|
230
|
+
* @throws {Error} must provide a valid state
|
|
231
|
+
*/
|
|
232
|
+
function setState( s ) {
|
|
233
|
+
rlaplace.state = s;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Returns an iterator protocol-compliant object containing the next iterated value.
|
|
238
|
+
*
|
|
239
|
+
* @private
|
|
240
|
+
* @returns {Object} iterator protocol-compliant object
|
|
241
|
+
*/
|
|
242
|
+
function next() {
|
|
243
|
+
var v;
|
|
244
|
+
if ( FLG ) {
|
|
245
|
+
return {
|
|
246
|
+
'done': true
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
v = iterator.next();
|
|
250
|
+
if ( v.done ) {
|
|
251
|
+
FLG = true;
|
|
252
|
+
return v;
|
|
253
|
+
}
|
|
254
|
+
if ( typeof v.value === 'number' ) {
|
|
255
|
+
v = v.value + rlaplace();
|
|
256
|
+
} else {
|
|
257
|
+
v = NaN;
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
'value': v,
|
|
261
|
+
'done': false
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Finishes an iterator.
|
|
267
|
+
*
|
|
268
|
+
* @private
|
|
269
|
+
* @param {*} [value] - value to return
|
|
270
|
+
* @returns {Object} iterator protocol-compliant object
|
|
271
|
+
*/
|
|
272
|
+
function end( value ) {
|
|
273
|
+
FLG = true;
|
|
274
|
+
if ( arguments.length ) {
|
|
275
|
+
return {
|
|
276
|
+
'value': value,
|
|
277
|
+
'done': true
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
'done': true
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Returns a new iterator.
|
|
287
|
+
*
|
|
288
|
+
* @private
|
|
289
|
+
* @returns {Iterator} iterator
|
|
290
|
+
*/
|
|
291
|
+
function factory() {
|
|
292
|
+
return iterawln( iterator[ iteratorSymbol ](), sigma, opts );
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
// EXPORTS //
|
|
298
|
+
|
|
299
|
+
module.exports = iterawln;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/simulate/iter/awln",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Create an iterator which introduces additive white Laplacian noise.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"benchmark": "./benchmark",
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"example": "./examples",
|
|
21
|
+
"lib": "./lib",
|
|
22
|
+
"test": "./test"
|
|
23
|
+
},
|
|
24
|
+
"types": "./docs/types",
|
|
25
|
+
"scripts": {},
|
|
26
|
+
"homepage": "https://github.com/stdlib-js/stdlib",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git://github.com/stdlib-js/stdlib.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {},
|
|
35
|
+
"devDependencies": {},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=0.10.0",
|
|
38
|
+
"npm": ">2.7.0"
|
|
39
|
+
},
|
|
40
|
+
"os": [
|
|
41
|
+
"aix",
|
|
42
|
+
"darwin",
|
|
43
|
+
"freebsd",
|
|
44
|
+
"linux",
|
|
45
|
+
"macos",
|
|
46
|
+
"openbsd",
|
|
47
|
+
"sunos",
|
|
48
|
+
"win32",
|
|
49
|
+
"windows"
|
|
50
|
+
],
|
|
51
|
+
"keywords": [
|
|
52
|
+
"stdlib",
|
|
53
|
+
"simulate",
|
|
54
|
+
"simulation",
|
|
55
|
+
"noise",
|
|
56
|
+
"additive",
|
|
57
|
+
"white",
|
|
58
|
+
"laplacian",
|
|
59
|
+
"laplace",
|
|
60
|
+
"biexponential",
|
|
61
|
+
"bi-exponential",
|
|
62
|
+
"double-exponential",
|
|
63
|
+
"exponential",
|
|
64
|
+
"information theory",
|
|
65
|
+
"random",
|
|
66
|
+
"iterator",
|
|
67
|
+
"iterable",
|
|
68
|
+
"iterate",
|
|
69
|
+
"iteration",
|
|
70
|
+
"iter"
|
|
71
|
+
]
|
|
72
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
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
|
+
# iterawun
|
|
22
|
+
|
|
23
|
+
> Create an [iterator][mdn-iterator-protocol] which introduces additive white uniform noise (AWUN).
|
|
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 iterawun = require( '@stdlib/simulate/iter/awun' );
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
#### iterawun( iterator, sigma\[, options] )
|
|
42
|
+
|
|
43
|
+
Returns an [iterator][mdn-iterator-protocol] which introduces additive white uniform noise (AWUN) 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 = iterawun( 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).
|
|
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 uniformly distributed pseudorandom numbers on the interval `[0,1)`. 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 [MT19937][@stdlib/random/base/mt19937] algorithm to generate uniformly distributed pseudorandom numbers on the interval `[0,1)`. To use a different PRNG, set the `prng` option.
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
var array2iterator = require( '@stdlib/array/to-iterator' );
|
|
82
|
+
var minstd = require( '@stdlib/random/base/minstd-shuffle' );
|
|
83
|
+
|
|
84
|
+
var arr = array2iterator( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
|
|
85
|
+
var it = iterawun( arr, 0.2, {
|
|
86
|
+
'prng': minstd.normalized
|
|
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 = iterawun( arr, 0.2, {
|
|
100
|
+
'seed': 12345
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
var v1 = it1.next().value;
|
|
104
|
+
// returns <number>
|
|
105
|
+
|
|
106
|
+
var it2 = iterawun( 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 = iterawun( 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 = iterawun( 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 iterawun = require( '@stdlib/simulate/iter/awun' );
|
|
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 uniform noise:
|
|
178
|
+
var it = iterawun( 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/awgn`][@stdlib/simulate/iter/awgn]</span><span class="delimiter">: </span><span class="description">create an iterator which introduces additive white Gaussian noise.</span>
|
|
212
|
+
- <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>
|
|
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
|
+
[@stdlib/array/uint32]: https://www.npmjs.com/package/@stdlib/array-uint32
|
|
225
|
+
|
|
226
|
+
[@stdlib/random/base/mt19937]: https://www.npmjs.com/package/@stdlib/random-base-mt19937
|
|
227
|
+
|
|
228
|
+
<!-- <related-links> -->
|
|
229
|
+
|
|
230
|
+
[@stdlib/simulate/iter/awgn]: https://github.com/stdlib-js/simulate/tree/main/iter/awgn
|
|
231
|
+
|
|
232
|
+
[@stdlib/simulate/iter/awln]: https://github.com/stdlib-js/simulate/tree/main/iter/awln
|
|
233
|
+
|
|
234
|
+
<!-- </related-links> -->
|
|
235
|
+
|
|
236
|
+
</section>
|
|
237
|
+
|
|
238
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( iterator, sigma[, options] )
|
|
3
|
+
Returns an iterator which introduces additive white uniform noise (AWUN)
|
|
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 uniformly
|
|
22
|
+
distributed pseudorandom numbers on the interval [0,1). If provided, the
|
|
23
|
+
`state` and `seed` options are ignored. In order to seed the returned
|
|
24
|
+
iterator, one must seed the provided `prng` (assuming the provided
|
|
25
|
+
`prng` is 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
|
|
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
|
+
|