@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,54 @@
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
+ /**
22
+ * Create an iterator which generates a square wave.
23
+ *
24
+ * @module @stdlib/simulate/iter/square-wave
25
+ *
26
+ * @example
27
+ * var iterSquareWave = require( '@stdlib/simulate/iter/square-wave' );
28
+ *
29
+ * var opts = {
30
+ * 'period': 4
31
+ * };
32
+ *
33
+ * var iter = iterSquareWave( opts );
34
+ *
35
+ * var v = iter.next().value;
36
+ * // returns 1.0
37
+ *
38
+ * v = iter.next().value;
39
+ * // returns 1.0
40
+ *
41
+ * v = iter.next().value;
42
+ * // returns -1.0
43
+ *
44
+ * // ...
45
+ */
46
+
47
+ // MODULES //
48
+
49
+ var main = require( './main.js' );
50
+
51
+
52
+ // EXPORTS //
53
+
54
+ module.exports = main;
@@ -0,0 +1,156 @@
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 iteratorSymbol = require( '@stdlib/symbol/iterator' );
25
+ var validate = require( './validate.js' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ /**
31
+ * Returns an iterator which generates a square wave.
32
+ *
33
+ * @param {Options} [options] - function options
34
+ * @param {PositiveInteger} [options.period=10] - number of iterations before a square wave repeats (this must be a positive even integer)
35
+ * @param {number} [options.min=-1.0] - minimum amplitude
36
+ * @param {number} [options.max=1.0] - maximum amplitude
37
+ * @param {integer} [options.offset=0] - phase offset (in units of iterations; zero-based)
38
+ * @param {NonNegativeInteger} [options.iter=1e308] - number of iterations
39
+ * @throws {TypeError} options argument must be an object
40
+ * @throws {TypeError} must provide valid options
41
+ * @returns {Iterator} iterator
42
+ *
43
+ * @example
44
+ * var opts = {
45
+ * 'period': 4
46
+ * };
47
+ *
48
+ * var iter = iterSquareWave( opts );
49
+ *
50
+ * var v = iter.next().value;
51
+ * // returns 1.0
52
+ *
53
+ * v = iter.next().value;
54
+ * // returns 1.0
55
+ *
56
+ * v = iter.next().value;
57
+ * // returns -1.0
58
+ *
59
+ * // ...
60
+ */
61
+ function iterSquareWave( options ) {
62
+ var opts;
63
+ var iter;
64
+ var FLG;
65
+ var err;
66
+ var t;
67
+ var h;
68
+ var i;
69
+
70
+ opts = {
71
+ 'period': 10,
72
+ 'min': -1.0,
73
+ 'max': 1.0,
74
+ 'offset': 0,
75
+ 'iter': 1e308
76
+ };
77
+ if ( arguments.length ) {
78
+ err = validate( opts, options );
79
+ if ( err ) {
80
+ throw err;
81
+ }
82
+ }
83
+ t = ( opts.period-opts.offset ) % opts.period;
84
+ if ( t < 0 ) {
85
+ t += opts.period;
86
+ }
87
+ t -= 1;
88
+ h = opts.period / 2;
89
+ i = 0;
90
+
91
+ // Create an iterator protocol-compliant object:
92
+ iter = {};
93
+ setReadOnly( iter, 'next', next );
94
+ setReadOnly( iter, 'return', end );
95
+
96
+ // If an environment supports `Symbol.iterator`, make the iterator iterable:
97
+ if ( iteratorSymbol ) {
98
+ setReadOnly( iter, iteratorSymbol, factory );
99
+ }
100
+ return iter;
101
+
102
+ /**
103
+ * Returns an iterator protocol-compliant object containing the next iterated value.
104
+ *
105
+ * @private
106
+ * @returns {Object} iterator protocol-compliant object
107
+ */
108
+ function next() {
109
+ i += 1;
110
+ if ( FLG || i > opts.iter ) {
111
+ return {
112
+ 'done': true
113
+ };
114
+ }
115
+ t += 1;
116
+ return {
117
+ 'value': ( (t%opts.period) < h ) ? opts.max : opts.min,
118
+ 'done': false
119
+ };
120
+ }
121
+
122
+ /**
123
+ * Finishes an iterator.
124
+ *
125
+ * @private
126
+ * @param {*} [value] - value to return
127
+ * @returns {Object} iterator protocol-compliant object
128
+ */
129
+ function end( value ) {
130
+ FLG = true;
131
+ if ( arguments.length ) {
132
+ return {
133
+ 'value': value,
134
+ 'done': true
135
+ };
136
+ }
137
+ return {
138
+ 'done': true
139
+ };
140
+ }
141
+
142
+ /**
143
+ * Returns a new iterator.
144
+ *
145
+ * @private
146
+ * @returns {Iterator} iterator
147
+ */
148
+ function factory() {
149
+ return iterSquareWave( opts );
150
+ }
151
+ }
152
+
153
+
154
+ // EXPORTS //
155
+
156
+ module.exports = iterSquareWave;
@@ -0,0 +1,99 @@
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 isPlainObject = require( '@stdlib/assert/is-plain-object' );
24
+ var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25
+ var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
26
+ var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
27
+ var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
28
+ var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
29
+ var isEven = require( '@stdlib/math/base/assert/is-even' );
30
+ var isnan = require( '@stdlib/math/base/assert/is-nan' );
31
+ var format = require( '@stdlib/string/format' );
32
+
33
+
34
+ // MAIN //
35
+
36
+ /**
37
+ * Validates function options.
38
+ *
39
+ * @private
40
+ * @param {Object} opts - destination object
41
+ * @param {Options} options - function options
42
+ * @param {PositiveInteger} [options.period] - number of iterations before a square wave repeats
43
+ * @param {number} [options.min] - minimum amplitude
44
+ * @param {number} [options.max] - maximum amplitude
45
+ * @param {integer} [options.offset] - phase offset (in units of iterations)
46
+ * @param {NonNegativeInteger} [options.iter] - number of iterations
47
+ * @returns {(Error|null)} null or an error object
48
+ *
49
+ * @example
50
+ * var opts = {};
51
+ * var options = {
52
+ * 'iter': 100
53
+ * };
54
+ * var err = validate( opts, options );
55
+ * if ( err ) {
56
+ * throw err;
57
+ * }
58
+ */
59
+ function validate( opts, options ) {
60
+ if ( !isPlainObject( options ) ) {
61
+ return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
62
+ }
63
+ if ( hasOwnProp( options, 'period' ) ) {
64
+ opts.period = options.period;
65
+ if ( !isPositiveInteger( options.period ) || !isEven( options.period ) ) { // eslint-disable-line max-len
66
+ return new TypeError( format( 'invalid option. `%s` option must be an positive even integer. Option: `%s`.', 'period', options.period ) );
67
+ }
68
+ }
69
+ if ( hasOwnProp( options, 'min' ) ) {
70
+ opts.min = options.min;
71
+ if ( !isNumber( options.min ) || isnan( options.min ) ) {
72
+ return new TypeError( format( 'invalid option. `%s` option must be a number. Option: `%s`.', 'min', options.min ) );
73
+ }
74
+ }
75
+ if ( hasOwnProp( options, 'max' ) ) {
76
+ opts.max = options.max;
77
+ if ( !isNumber( options.max ) || isnan( options.max ) ) {
78
+ return new TypeError( format( 'invalid option. `%s` option must be a number. Option: `%s`.', 'max', options.max ) );
79
+ }
80
+ }
81
+ if ( hasOwnProp( options, 'offset' ) ) {
82
+ opts.offset = options.offset;
83
+ if ( !isInteger( options.offset ) ) {
84
+ return new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%s`.', 'offset', options.offset ) );
85
+ }
86
+ }
87
+ if ( hasOwnProp( options, 'iter' ) ) {
88
+ opts.iter = options.iter;
89
+ if ( !isNonNegativeInteger( options.iter ) ) {
90
+ return new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', 'iter', options.iter ) );
91
+ }
92
+ }
93
+ return null;
94
+ }
95
+
96
+
97
+ // EXPORTS //
98
+
99
+ module.exports = validate;
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@stdlib/simulate/iter/square-wave",
3
+ "version": "0.0.0",
4
+ "description": "Create an iterator which generates a square wave.",
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
+ "square",
56
+ "pulse",
57
+ "wave",
58
+ "signal",
59
+ "processing",
60
+ "waveform",
61
+ "periodic",
62
+ "iterator",
63
+ "iterate",
64
+ "iteration",
65
+ "iter"
66
+ ]
67
+ }
@@ -0,0 +1,289 @@
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
+ # iterTriangleWave
22
+
23
+ > Create an iterator which generates a triangle wave.
24
+
25
+ <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26
+
27
+ <section class="intro">
28
+
29
+ A triangle waveform is represented by the following equation
30
+
31
+ <!-- <equation class="equation" label="eq:triangle_waveform" align="center" raw="f(t; \tau, a, \varphi) = \frac{2a}{\pi} \operatorname{arcsin} \sin \frac{2\pi(t-\varphi)}{\tau}" alt="Equation for a triangle waveform."> -->
32
+
33
+ <div class="equation" align="center" data-raw-text="f(t; \tau, a, \varphi) = \frac{2a}{\pi} \operatorname{arcsin} \sin \frac{2\pi(t-\varphi)}{\tau}" data-equation="eq:triangle_waveform">
34
+ <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@5827c258000edcb03496265cdfd372d686b4c656/lib/node_modules/@stdlib/simulate/iter/triangle-wave/docs/img/equation_triangle_waveform.svg" alt="Equation for a triangle waveform.">
35
+ <br>
36
+ </div>
37
+
38
+ <!-- </equation> -->
39
+
40
+ where `τ` is the period, `a` is the peak amplitude, and `φ` is the phase offset.
41
+
42
+ <!-- TODO: add a figure showing a triangle wave -->
43
+
44
+ </section>
45
+
46
+ <!-- /.intro -->
47
+
48
+ <!-- Package usage documentation. -->
49
+
50
+ <section class="usage">
51
+
52
+ ## Usage
53
+
54
+ ```javascript
55
+ var iterTriangleWave = require( '@stdlib/simulate/iter/triangle-wave' );
56
+ ```
57
+
58
+ #### iterTriangleWave( \[options] )
59
+
60
+ Returns an iterator which generates a triangle wave.
61
+
62
+ ```javascript
63
+ var it = iterTriangleWave();
64
+ // returns <Object>
65
+
66
+ var v = it.next().value;
67
+ // returns <number>
68
+
69
+ v = it.next().value;
70
+ // returns <number>
71
+
72
+ v = it.next().value;
73
+ // returns <number>
74
+
75
+ // ...
76
+ ```
77
+
78
+ The returned iterator protocol-compliant object has the following properties:
79
+
80
+ - **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.
81
+ - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
82
+
83
+ The function supports the following `options`:
84
+
85
+ - **period**: period (i.e., the number of iterations before a waveform repeats). Default: `10`.
86
+ - **amplitude**: peak amplitude. Default: `1.0`.
87
+ - **offset**: phase offset (in units of iterations; zero-based). A negative offset translates a waveform to the left. A positive offset translates a waveform to the right. Default: `0`.
88
+ - **iter**: number of iterations. Default: `1e308`.
89
+
90
+ By default, the function returns an iterator which generates a triangle wave that repeats every `10` iterations. To specify an alternative period, set the `period` option.
91
+
92
+ ```javascript
93
+ var opts = {
94
+ 'period': 4
95
+ };
96
+
97
+ var it = iterTriangleWave( opts );
98
+ // returns <Object>
99
+
100
+ var v = it.next().value;
101
+ // returns 0.0
102
+
103
+ v = it.next().value;
104
+ // returns 1.0
105
+
106
+ v = it.next().value;
107
+ // returns 0.0
108
+
109
+ v = it.next().value;
110
+ // returns -1.0
111
+
112
+ v = it.next().value;
113
+ // returns 0.0
114
+
115
+ // ...
116
+ ```
117
+
118
+ To adjust at what point the iterator begins in the waveform cycle, set the phase `offset` option. For example, to translate the waveform to the left,
119
+
120
+ ```javascript
121
+ var opts = {
122
+ 'period': 4,
123
+ 'offset': -1
124
+ };
125
+
126
+ var it = iterTriangleWave( opts );
127
+ // returns <Object>
128
+
129
+ var v = it.next().value;
130
+ // returns 1.0
131
+
132
+ v = it.next().value;
133
+ // returns 0.0
134
+
135
+ v = it.next().value;
136
+ // returns -1.0
137
+
138
+ v = it.next().value;
139
+ // returns 0.0
140
+
141
+ v = it.next().value;
142
+ // returns 1.0
143
+
144
+ // ...
145
+ ```
146
+
147
+ To translate the waveform to the right,
148
+
149
+ ```javascript
150
+ var opts = {
151
+ 'period': 4,
152
+ 'offset': 1
153
+ };
154
+
155
+ var it = iterTriangleWave( opts );
156
+ // returns <Object>
157
+
158
+ var v = it.next().value;
159
+ // returns -1.0
160
+
161
+ v = it.next().value;
162
+ // returns 0.0
163
+
164
+ v = it.next().value;
165
+ // returns 1.0
166
+
167
+ v = it.next().value;
168
+ // returns 0.0
169
+
170
+ v = it.next().value;
171
+ // returns -1.0
172
+
173
+ // ...
174
+ ```
175
+
176
+ By default, the function returns an infinite iterator (i.e., an iterator which never ends). To limit the number of iterations, set the `iter` option.
177
+
178
+ ```javascript
179
+ var opts = {
180
+ 'iter': 2
181
+ };
182
+ var it = iterTriangleWave( opts );
183
+ // returns <Object>
184
+
185
+ var v = it.next().value;
186
+ // returns <number>
187
+
188
+ v = it.next().value;
189
+ // returns <number>
190
+
191
+ var bool = it.next().done;
192
+ // returns true
193
+ ```
194
+
195
+ </section>
196
+
197
+ <!-- /.usage -->
198
+
199
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
200
+
201
+ <section class="notes">
202
+
203
+ ## Notes
204
+
205
+ - If an environment supports `Symbol.iterator`, the returned iterator is iterable.
206
+
207
+ </section>
208
+
209
+ <!-- /.notes -->
210
+
211
+ <!-- Package usage examples. -->
212
+
213
+ <section class="examples">
214
+
215
+ ## Examples
216
+
217
+ <!-- eslint no-undef: "error" -->
218
+
219
+ ```javascript
220
+ var iterTriangleWave = require( '@stdlib/simulate/iter/triangle-wave' );
221
+
222
+ // Create an iterator:
223
+ var opts = {
224
+ 'period': 10,
225
+ 'amplitude': 10.0,
226
+ 'offset': -5,
227
+ 'iter': 100
228
+ };
229
+ var it = iterTriangleWave( opts );
230
+
231
+ // Perform manual iteration...
232
+ var v;
233
+ while ( true ) {
234
+ v = it.next();
235
+ if ( v.done ) {
236
+ break;
237
+ }
238
+ console.log( v.value );
239
+ }
240
+ ```
241
+
242
+ </section>
243
+
244
+ <!-- /.examples -->
245
+
246
+ <!-- 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. -->
247
+
248
+ <section class="references">
249
+
250
+ </section>
251
+
252
+ <!-- /.references -->
253
+
254
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
255
+
256
+ <section class="related">
257
+
258
+ * * *
259
+
260
+ ## See Also
261
+
262
+ - <span class="package-name">[`@stdlib/simulate/iter/pulse`][@stdlib/simulate/iter/pulse]</span><span class="delimiter">: </span><span class="description">create an iterator which generates a pulse waveform.</span>
263
+ - <span class="package-name">[`@stdlib/simulate/iter/sawtooth-wave`][@stdlib/simulate/iter/sawtooth-wave]</span><span class="delimiter">: </span><span class="description">create an iterator which generates a sawtooth wave.</span>
264
+ - <span class="package-name">[`@stdlib/simulate/iter/sine-wave`][@stdlib/simulate/iter/sine-wave]</span><span class="delimiter">: </span><span class="description">create an iterator which generates a sine wave.</span>
265
+ - <span class="package-name">[`@stdlib/simulate/iter/square-wave`][@stdlib/simulate/iter/square-wave]</span><span class="delimiter">: </span><span class="description">create an iterator which generates a square wave.</span>
266
+
267
+ </section>
268
+
269
+ <!-- /.related -->
270
+
271
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
272
+
273
+ <section class="links">
274
+
275
+ <!-- <related-links> -->
276
+
277
+ [@stdlib/simulate/iter/pulse]: https://github.com/stdlib-js/simulate/tree/main/iter/pulse
278
+
279
+ [@stdlib/simulate/iter/sawtooth-wave]: https://github.com/stdlib-js/simulate/tree/main/iter/sawtooth-wave
280
+
281
+ [@stdlib/simulate/iter/sine-wave]: https://github.com/stdlib-js/simulate/tree/main/iter/sine-wave
282
+
283
+ [@stdlib/simulate/iter/square-wave]: https://github.com/stdlib-js/simulate/tree/main/iter/square-wave
284
+
285
+ <!-- </related-links> -->
286
+
287
+ </section>
288
+
289
+ <!-- /.links -->