-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavelet.html
639 lines (586 loc) · 29.2 KB
/
wavelet.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smile - Wavelet</title>
<meta name="description" content="Statistical Machine Intelligence and Learning Engine">
<!-- prettify js and CSS -->
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?lang=scala&lang=kotlin&lang=clj"></script>
<style>
.prettyprint ol.linenums > li { list-style-type: decimal; }
</style>
<!-- Bootstrap core CSS -->
<link href="css/cerulean.min.css" rel="stylesheet">
<link href="css/custom.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!-- slider -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.transitions.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css" type="text/css" />
<!-- table of contents auto generator -->
<script src="js/toc.js" type="text/javascript"></script>
<!-- styles for pager and table of contents -->
<link rel="stylesheet" href="css/pager.css" type="text/css" />
<link rel="stylesheet" href="css/toc.css" type="text/css" />
<!-- Vega-Lite Embed -->
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-57GD08QCML"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-57GD08QCML');
</script>
<!-- Sidebar and testimonial-slider -->
<script type="text/javascript">
$(document).ready(function(){
// scroll/follow sidebar
// #sidebar is defined in the content snippet
// This script has to be executed after the snippet loaded.
// $.getScript("js/follow-sidebar.js");
$("#testimonial-slider").owlCarousel({
items: 1,
singleItem: true,
pagination: true,
navigation: false,
loop: true,
autoPlay: 10000,
stopOnHover: true,
transitionStyle: "backSlide",
touchDrag: true
});
});
</script>
</head>
<body>
<div class="container" style="max-width: 1200px;">
<header>
<div class="masthead">
<p class="lead">
<a href="index.html">
<img src="images/smile.jpg" style="height:100px; width:auto; vertical-align: bottom; margin-top: 20px; margin-right: 20px;">
<span class="tagline">Smile — Statistical Machine Intelligence and Learning Engine</span>
</a>
</p>
</div>
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Overview <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="quickstart.html">Quick Start</a></li>
<li><a href="overview.html">What's Machine Learning</a></li>
<li><a href="data.html">Data Processing</a></li>
<li><a href="visualization.html">Data Visualization</a></li>
<li><a href="vegalite.html">Declarative Visualization</a></li>
<li><a href="gallery.html">Gallery</a></li>
<li><a href="faq.html">FAQ</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Supervised Learning <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="classification.html">Classification</a></li>
<li><a href="regression.html">Regression</a></li>
<li><a href="deep-learning.html">Deep Learning</a></li>
<li><a href="feature.html">Feature Engineering</a></li>
<li><a href="validation.html">Model Validation</a></li>
<li><a href="missing-value-imputation.html">Missing Value Imputation</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Unsupervised Learning <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="clustering.html">Clustering</a></li>
<li><a href="vector-quantization.html">Vector Quantization</a></li>
<li><a href="association-rule.html">Association Rule Mining</a></li>
<li><a href="mds.html">Multi-Dimensional Scaling</a></li>
<li><a href="manifold.html">Manifold Learning</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">LLM & NLP <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="llm.html">Large Language Model (LLM)</a></li>
<li><a href="nlp.html">Natural Language Processing (NLP)</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Math <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="linear-algebra.html">Linear Algebra</a></li>
<li><a href="statistics.html">Statistics</a></li>
<li><a href="wavelet.html">Wavelet</a></li>
<li><a href="interpolation.html">Interpolation</a></li>
<li><a href="graph.html">Graph Data Structure</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">API <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="api/java/index.html" target="_blank">Java</a></li>
<li><a href="api/scala/index.html" target="_blank">Scala</a></li>
<li><a href="api/kotlin/index.html" target="_blank">Kotlin</a></li>
<li><a href="api/clojure/index.html" target="_blank">Clojure</a></li>
<li><a href="api/json/index.html" target="_blank">JSON</a></li>
</ul>
</li>
<li><a href="https://mybinder.org/v2/gh/haifengl/smile/notebook?urlpath=lab%2Ftree%2Fshell%2Fsrc%2Funiversal%2Fnotebooks%2Findex.ipynb" target="_blank">Try It Online</a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
</header>
<div id="content" class="row">
<div class="col-md-3 col-md-push-9 hidden-xs hidden-sm">
<div id="sidebar">
<div class="sidebar-toc" style="margin-bottom: 20px;">
<p class="toc-header">Contents</p>
<div id="toc"></div>
</div>
<div id="search">
<script>
(function() {
var cx = '010264411143030149390:ajvee_ckdzs';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchbox-only></gcse:searchbox-only>
</div>
</div>
</div>
<div class="col-md-9 col-md-pull-3">
<h1 id="wavelet-top" class="title">Wavelet</h1>
<p>A wavelet is a wave-like oscillation with an amplitude that begins
at zero, increases, and then decreases back to zero. Generally,
wavelets are intentionally crafted to have specific properties
that make them useful for signal processing. Wavelets can be combined,
with portions of a known signal by convolution to extract
information from the unknown signal.</p>
<p>In continuous wavelet transforms, a given signal of finite energy
is projected on a continuous family of frequency bands (or similar
subspaces of the <code>L<sup>p</sup></code> function space
<code>L<sup>2</sup>(R)</code>). For instance the signal may be
represented on every frequency band of the form <code>[f, 2f]</code>
for all positive frequencies <code>f > 0</code>. Then, the original
signal can be reconstructed by a suitable integration over all
the resulting frequency components.</p>
<p>It is computationally impossible to analyze a signal using all
wavelet coefficients. Instead, one may pick a discrete subset of
the upper halfplane to be able to reconstruct a signal from the
corresponding wavelet coefficients. That is, a discrete wavelet transform
(DWT) is any wavelet transform for which the wavelets are
discretely sampled.</p>
<p>Like the fast Fourier transform (FFT), the discrete wavelet transform
is a fast, linear operation that operates on a data vector whose
length is an integer power of 2, transforming it into a numerically
different vector of the same length. The wavelet transform is
invertible and in fact orthogonal. Both FFT and DWT can be viewed as
a rotation in function space. A key advantage it has over Fourier
transforms is temporal resolution: it captures both frequency
and location information (location in time).</p>
<h2 id="filter" class="title">Wavelet Filters</h2>
<p>In Java, Smile provides classes for well known wavelets such as
Haar, Daubechies, Least Asymetric, Best Localized and Coiflet.
One may also define new filters by provide coefficients.
In Scala/Kotlin, The below function returns a wavelet filter.
The filter name is derived from one of four classes of wavelet
transform filters: Daubechies, Least Asymetric, Best Localized and Coiflet.
The prefixes for filters of these classes are <code>d</code>, <code>la</code>,
<code>bl</code> and <code>c</code>, respectively. Following the
prefix, the filter name consists of an integer indicating length.
Supported lengths are as follows:</p>
<dl>
<dt>Daubechies</dt>
<dd>4,6,8,10,12,14,16,18,20.</dd>
<dt>Least Asymetric</dt>
<dd>8,10,12,14,16,18,20.</dd>
<dt>Best Localized</dt>
<dd>14,18,20.</dd>
<dt>Coiflet</dt>
<dd>6,12,18,24,30.</dd>
</dl>
<p>Additionally, "haar" is supported for Haar wavelet. Besides, "d4", the
simplest and most localized wavelet, uses a different centering method
from other Daubechies wavelet.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_1" data-toggle="tab">Java</a></li>
<li><a href="#scala_1" data-toggle="tab">Scala</a></li>
<li><a href="#kotlin_1" data-toggle="tab">Kotlin</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="java_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public Wavelet(double[] coefficients)
</code></pre>
</div>
</div>
<div class="tab-pane" id="scala_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def wavelet(filter: String): Wavelet
</code></pre>
</div>
</div>
<div class="tab-pane" id="kotlin_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-kotlin"><code>
fun wavelet(filter: String): Wavelet
</code></pre>
</div>
</div>
</div>
<h2 id="dwt" class="title">Discrete Wavelet Transform</h2>
<p>With a wavelet object, we can compute the discrete wavelet transform
coefficients for a univariate time series. The size of time series
array should be a power of 2. For time series of size no power of 2,
0 padding can be applied.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_2" data-toggle="tab">Java</a></li>
<li><a href="#scala_2" data-toggle="tab">Scala</a></li>
<li><a href="#kotlin_2" data-toggle="tab">Kotlin</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_2">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val sp500 = Array(
1103.96, 1107.84, 1114.11, 1108.61, 1106.36, 1097.86, 1105.31,
1114.51, 1118.84, 1121.08, 1127.53, 1128.55, 1125.53, 1126.60,
1116.56, 1132.66, 1135.71, 1136.27, 1140.52, 1145.96, 1143.81,
1137.31, 1145.68, 1147.72, 1136.03, 1147.95, 1138.68, 1115.49,
1092.40, 1095.80, 1091.94, 1096.93, 1087.61, 1073.89, 1090.05,
1100.67, 1097.25, 1064.12, 1065.51, 1060.06, 1069.68, 1067.10,
1075.95, 1079.13, 1096.14, 1099.03, 1105.49, 1110.00, 1107.49,
1095.89, 1101.24, 1103.10, 1105.36, 1117.01, 1119.36, 1119.12,
1125.12, 1138.40, 1137.56, 1140.22, 1143.96, 1151.71, 1148.53,
1150.83, 1159.94, 1166.13, 1166.68, 1157.25, 1166.47, 1172.70,
1170.03, 1167.58, 1167.71, 1173.75, 1171.75, 1171.23, 1178.71,
1186.01, 1188.23, 1181.75, 1187.47, 1194.94, 1195.94, 1198.69,
1210.77, 1210.17, 1192.06, 1199.04, 1207.16, 1202.52, 1207.87,
1217.07, 1209.92, 1184.59, 1193.30, 1206.77, 1188.58, 1197.50,
1169.24, 1164.38, 1127.04, 1122.27, 1156.39, 1155.43, 1170.04,
1157.19, 1136.52, 1138.78, 1119.57, 1107.34, 1067.26, 1084.78,
1067.42, 1075.51, 1074.27, 1102.59, 1087.30, 1073.01, 1098.82,
1098.43, 1065.84, 1050.81, 1062.75, 1058.77, 1082.65, 1095.00,
1091.21, 1114.02, 1115.98, 1116.16, 1122.79, 1113.90, 1095.57,
1090.93, 1075.10, 1077.50, 1071.10, 1040.56, 1031.10, 1027.65,
1028.09, 1028.54, 1062.92, 1070.50, 1077.23, 1080.65, 1095.61,
1094.46, 1093.85, 1066.85, 1064.53, 1086.67, 1072.14, 1092.17,
1102.89, 1117.36, 1112.84, 1108.07, 1098.44, 1107.53, 1125.34,
1121.06, 1125.78, 1122.07, 1122.80, 1122.92, 1116.89, 1081.48,
1082.22, 1077.49, 1081.16, 1092.08, 1092.44, 1075.63, 1073.36,
1063.20, 1048.98, 1056.28, 1049.27, 1062.90, 1046.88, 1049.72,
1080.66, 1093.61, 1102.60, 1092.36, 1101.15, 1104.57, 1113.38,
1121.16, 1119.43, 1123.89, 1126.39, 1126.57, 1142.82, 1139.49,
1131.10, 1131.69, 1148.64, 1142.31, 1146.75, 1145.97, 1143.49,
1144.96, 1140.68, 1159.81, 1161.57, 1158.36, 1165.32, 1164.28,
1171.32, 1177.82, 1177.47, 1176.83, 1178.64, 1166.74, 1179.82,
1180.52, 1184.74, 1184.88, 1183.84, 1184.47, 1183.87, 1185.71,
1187.86, 1193.79, 1198.34, 1221.20, 1223.24, 1223.59, 1213.14,
1213.04, 1209.07, 1200.44, 1194.79, 1178.33, 1183.75, 1196.12,
1198.07, 1192.51, 1183.70, 1194.16, 1189.08, 1182.96, 1186.60,
1206.81, 1219.93, 1223.87, 1227.25, 1225.02, 1230.14, 1233.85,
1242.52, 1241.84, 1241.58, 1236.34
)
val haar = wavelet("haar")
haar.transform(sp500)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_2">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
double[] sp500 = {
1103.96, 1107.84, 1114.11, 1108.61, 1106.36, 1097.86, 1105.31,
1114.51, 1118.84, 1121.08, 1127.53, 1128.55, 1125.53, 1126.60,
1116.56, 1132.66, 1135.71, 1136.27, 1140.52, 1145.96, 1143.81,
1137.31, 1145.68, 1147.72, 1136.03, 1147.95, 1138.68, 1115.49,
1092.40, 1095.80, 1091.94, 1096.93, 1087.61, 1073.89, 1090.05,
1100.67, 1097.25, 1064.12, 1065.51, 1060.06, 1069.68, 1067.10,
1075.95, 1079.13, 1096.14, 1099.03, 1105.49, 1110.00, 1107.49,
1095.89, 1101.24, 1103.10, 1105.36, 1117.01, 1119.36, 1119.12,
1125.12, 1138.40, 1137.56, 1140.22, 1143.96, 1151.71, 1148.53,
1150.83, 1159.94, 1166.13, 1166.68, 1157.25, 1166.47, 1172.70,
1170.03, 1167.58, 1167.71, 1173.75, 1171.75, 1171.23, 1178.71,
1186.01, 1188.23, 1181.75, 1187.47, 1194.94, 1195.94, 1198.69,
1210.77, 1210.17, 1192.06, 1199.04, 1207.16, 1202.52, 1207.87,
1217.07, 1209.92, 1184.59, 1193.30, 1206.77, 1188.58, 1197.50,
1169.24, 1164.38, 1127.04, 1122.27, 1156.39, 1155.43, 1170.04,
1157.19, 1136.52, 1138.78, 1119.57, 1107.34, 1067.26, 1084.78,
1067.42, 1075.51, 1074.27, 1102.59, 1087.30, 1073.01, 1098.82,
1098.43, 1065.84, 1050.81, 1062.75, 1058.77, 1082.65, 1095.00,
1091.21, 1114.02, 1115.98, 1116.16, 1122.79, 1113.90, 1095.57,
1090.93, 1075.10, 1077.50, 1071.10, 1040.56, 1031.10, 1027.65,
1028.09, 1028.54, 1062.92, 1070.50, 1077.23, 1080.65, 1095.61,
1094.46, 1093.85, 1066.85, 1064.53, 1086.67, 1072.14, 1092.17,
1102.89, 1117.36, 1112.84, 1108.07, 1098.44, 1107.53, 1125.34,
1121.06, 1125.78, 1122.07, 1122.80, 1122.92, 1116.89, 1081.48,
1082.22, 1077.49, 1081.16, 1092.08, 1092.44, 1075.63, 1073.36,
1063.20, 1048.98, 1056.28, 1049.27, 1062.90, 1046.88, 1049.72,
1080.66, 1093.61, 1102.60, 1092.36, 1101.15, 1104.57, 1113.38,
1121.16, 1119.43, 1123.89, 1126.39, 1126.57, 1142.82, 1139.49,
1131.10, 1131.69, 1148.64, 1142.31, 1146.75, 1145.97, 1143.49,
1144.96, 1140.68, 1159.81, 1161.57, 1158.36, 1165.32, 1164.28,
1171.32, 1177.82, 1177.47, 1176.83, 1178.64, 1166.74, 1179.82,
1180.52, 1184.74, 1184.88, 1183.84, 1184.47, 1183.87, 1185.71,
1187.86, 1193.79, 1198.34, 1221.20, 1223.24, 1223.59, 1213.14,
1213.04, 1209.07, 1200.44, 1194.79, 1178.33, 1183.75, 1196.12,
1198.07, 1192.51, 1183.70, 1194.16, 1189.08, 1182.96, 1186.60,
1206.81, 1219.93, 1223.87, 1227.25, 1225.02, 1230.14, 1233.85,
1242.52, 1241.84, 1241.58, 1236.34
};
var haar = new HaarWavelet();
haar.transform(sp500);
</code></pre>
</div>
</div>
<div class="tab-pane" id="kotlin_2">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-kotlin"><code>
import smile.wavelet.*;
val sp500 = doubleArrayOf(
1103.96, 1107.84, 1114.11, 1108.61, 1106.36, 1097.86, 1105.31,
1114.51, 1118.84, 1121.08, 1127.53, 1128.55, 1125.53, 1126.60,
1116.56, 1132.66, 1135.71, 1136.27, 1140.52, 1145.96, 1143.81,
1137.31, 1145.68, 1147.72, 1136.03, 1147.95, 1138.68, 1115.49,
1092.40, 1095.80, 1091.94, 1096.93, 1087.61, 1073.89, 1090.05,
1100.67, 1097.25, 1064.12, 1065.51, 1060.06, 1069.68, 1067.10,
1075.95, 1079.13, 1096.14, 1099.03, 1105.49, 1110.00, 1107.49,
1095.89, 1101.24, 1103.10, 1105.36, 1117.01, 1119.36, 1119.12,
1125.12, 1138.40, 1137.56, 1140.22, 1143.96, 1151.71, 1148.53,
1150.83, 1159.94, 1166.13, 1166.68, 1157.25, 1166.47, 1172.70,
1170.03, 1167.58, 1167.71, 1173.75, 1171.75, 1171.23, 1178.71,
1186.01, 1188.23, 1181.75, 1187.47, 1194.94, 1195.94, 1198.69,
1210.77, 1210.17, 1192.06, 1199.04, 1207.16, 1202.52, 1207.87,
1217.07, 1209.92, 1184.59, 1193.30, 1206.77, 1188.58, 1197.50,
1169.24, 1164.38, 1127.04, 1122.27, 1156.39, 1155.43, 1170.04,
1157.19, 1136.52, 1138.78, 1119.57, 1107.34, 1067.26, 1084.78,
1067.42, 1075.51, 1074.27, 1102.59, 1087.30, 1073.01, 1098.82,
1098.43, 1065.84, 1050.81, 1062.75, 1058.77, 1082.65, 1095.00,
1091.21, 1114.02, 1115.98, 1116.16, 1122.79, 1113.90, 1095.57,
1090.93, 1075.10, 1077.50, 1071.10, 1040.56, 1031.10, 1027.65,
1028.09, 1028.54, 1062.92, 1070.50, 1077.23, 1080.65, 1095.61,
1094.46, 1093.85, 1066.85, 1064.53, 1086.67, 1072.14, 1092.17,
1102.89, 1117.36, 1112.84, 1108.07, 1098.44, 1107.53, 1125.34,
1121.06, 1125.78, 1122.07, 1122.80, 1122.92, 1116.89, 1081.48,
1082.22, 1077.49, 1081.16, 1092.08, 1092.44, 1075.63, 1073.36,
1063.20, 1048.98, 1056.28, 1049.27, 1062.90, 1046.88, 1049.72,
1080.66, 1093.61, 1102.60, 1092.36, 1101.15, 1104.57, 1113.38,
1121.16, 1119.43, 1123.89, 1126.39, 1126.57, 1142.82, 1139.49,
1131.10, 1131.69, 1148.64, 1142.31, 1146.75, 1145.97, 1143.49,
1144.96, 1140.68, 1159.81, 1161.57, 1158.36, 1165.32, 1164.28,
1171.32, 1177.82, 1177.47, 1176.83, 1178.64, 1166.74, 1179.82,
1180.52, 1184.74, 1184.88, 1183.84, 1184.47, 1183.87, 1185.71,
1187.86, 1193.79, 1198.34, 1221.20, 1223.24, 1223.59, 1213.14,
1213.04, 1209.07, 1200.44, 1194.79, 1178.33, 1183.75, 1196.12,
1198.07, 1192.51, 1183.70, 1194.16, 1189.08, 1182.96, 1186.60,
1206.81, 1219.93, 1223.87, 1227.25, 1225.02, 1230.14, 1233.85,
1242.52, 1241.84, 1241.58, 1236.34
)
val haar = wavelet("haar")
haar.transform(sp500)
</code></pre>
</div>
</div>
</div>
<p>The above example transform a S&P 500 time series with Haar
wavelet. The result is stored in the input array. To transform
it back, the method <code>inverse</code> can be applied.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_3" data-toggle="tab">Java</a></li>
<li><a href="#scala_3" data-toggle="tab">Scala</a></li>
<li><a href="#kotlin_3" data-toggle="tab">Kotlin</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_3">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
smile> haar.inverse(sp500)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_3">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
smile> haar.inverse(sp500)
</code></pre>
</div>
</div>
<div class="tab-pane" id="kotlin_3">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-kotlin"><code>
>>> haar.inverse(sp500)
</code></pre>
</div>
</div>
</div>
<p>In case that you don't want to create the wavelet object
explicitly or prefer functional APIs, the methods <code>dwt</code> and <code>idwt</code>
can be used in Scala and Kotlin.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#scala_4" data-toggle="tab">Scala</a></li>
<li><a href="#kotlin_4" data-toggle="tab">Kotlin</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="scala_4">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
// Discrete wavelet transform.
def dwt(t: Array[Double], filter: String): Unit
// Inverse discrete wavelet transform.
def idwt(wt: Array[Double], filter: String): Unit
</code></pre>
</div>
</div>
<div class="tab-pane" id="kotlin_4">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-kotlin"><code>
// Discrete wavelet transform.
fun dwt(t: DoubleArray, filter: String): Unit
// Inverse discrete wavelet transform.
fun idwt(wt: DoubleArray, filter: String): Unit
</code></pre>
</div>
</div>
</div>
<h2 id="shrinkage" class="title">Wavelet Shrinkage</h2>
<p>The wavelet shrinkage is a signal denoising technique based on the idea of
thresholding the wavelet coefficients. Wavelet coefficients having small
absolute value are considered to encode mostly noise and very fine details
of the signal. In contrast, the important information is encoded by the
coefficients having large absolute value. Removing the small absolute value
coefficients and then reconstructing the signal should produce signal with
lesser amount of noise. The wavelet shrinkage approach can be summarized as
follows:</p>
<ul>
<li>Apply the wavelet transform to the signal.</li>
<li>Estimate a threshold value.</li>
<li>The so-called hard thresholding method zeros the coefficients that are
smaller than the threshold and leaves the other ones unchanged. In contrast,
the soft thresholding scales the remaining coefficients in order to form a
continuous distribution of the coefficients centered on zero.</li>
<li>Reconstruct the signal (apply the inverse wavelet transform).</li>
</ul>
<p>The biggest challenge in the wavelet shrinkage approach is finding an
appropriate threshold value. In this method, we use the universal threshold
<code>T = σ sqrt(2*log(N))</code>, where <code>N</code> is the length
of time series and σ is the estimate of standard deviation of the noise by the
so-called scaled median absolute deviation (MAD) computed from the high-pass
wavelet coefficients of the first level of the transform.</p>
<p>The class <code>WaveletShrinkage</code> implements this process.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_5" data-toggle="tab">Java</a></li>
<li><a href="#scala_5" data-toggle="tab">Scala</a></li>
<li><a href="#kotlin_5" data-toggle="tab">Kotlin</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_5">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val d4 = wavelet("d4")
val smooth = sp500.clone()
WaveletShrinkage.denoise(smooth, d4)
val canvas = plot(sp500)
canvas.line(smooth, BLUE)
show(canvas)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_5">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
var d4 = new D4Wavelet();
var smooth = sp500.clone();
WaveletShrinkage.denoise(smooth, d4);
var canvas = LinePlot.of(sp500).canvas();
canvas.add(LinePlot.of(smooth, Color.BLUE));
canvas.window();
</code></pre>
</div>
</div>
<div class="tab-pane" id="kotlin_5">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-kotlin"><code>
import java.awt.Color;
import smile.plot.swing.*;
val d4 = wavelet("d4");
val smooth = sp500.clone();
WaveletShrinkage.denoise(smooth, d4);
val canvas = LinePlot.of(sp500).canvas();
canvas.add(LinePlot.of(smooth, Color.BLUE));
canvas.window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/wavelet-denoise.png" class="enlarge" style="width: 480px;" />
</div>
<p>Similar to <code>dwt</code> method, <code>wsdenoise</code> is
a function for wavelet shrinkage if you don't want to
create the wavelet object explicitly.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#scala_6" data-toggle="tab">Scala</a></li>
<li><a href="#kotlin_6" data-toggle="tab">Kotlin</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="scala_6">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
// If the parameter soft is true, apply soft thresholding.
def wsdenoise(t: Array[Double], filter: String, soft: Boolean = false): Unit
</code></pre>
</div>
</div>
<div class="tab-pane" id="kotlin_6">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-kotlin"><code>
// If the parameter soft is true, apply soft thresholding.
fun wsdenoise(t: DoubleArray, filter: String, soft: Boolean = false): Unit
</code></pre>
</div>
</div>
</div>
<div id="btnv">
<span class="btn-arrow-left">← </span>
<a class="btn-prev-text" href="statistics.html" title="Previous Section: Statistics"><span>Statistics</span></a>
<a class="btn-next-text" href="interpolation.html" title="Next Section: Interpolation"><span>Interpolation</span></a>
<span class="btn-arrow-right"> →</span>
</div>
</div>
<script type="text/javascript">
$('#toc').toc({exclude: 'h1, h5, h6', context: '', autoId: true, numerate: false});
</script>
</div>
</div>
<a href=https://github.com/haifengl/smile><img style="position: fixed; top: 0; right: 0; border: 0" src=/images/forkme_right_orange.png alt="Fork me on GitHub"></a>
<!-- Place this tag right after the last button or just before your close body tag. -->
<script async defer id="github-bjs" src="https://buttons.github.io/buttons.js"></script>
</body>
</html>