-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavelet.html
More file actions
1775 lines (1532 loc) · 67.1 KB
/
wavelet.html
File metadata and controls
1775 lines (1532 loc) · 67.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced Time Series Analysis: Spectral & Wavelet</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/plotly.js-dist@2.26.0/plotly.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
min-height: 100vh;
padding: 20px;
color: #333;
}
.container {
max-width: 1800px;
margin: 0 auto;
background: white;
border-radius: 20px;
box-shadow: 0 25px 50px rgba(0,0,0,0.3);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 30%, #f093fb 70%, #ff9a9e 100%);
color: white;
padding: 40px;
text-align: center;
position: relative;
}
.header::before {
content: '🌊';
position: absolute;
top: 30px;
right: 40px;
font-size: 3rem;
animation: wave 3s ease-in-out infinite;
}
@keyframes wave {
0%, 100% { transform: rotate(0deg) scale(1); }
25% { transform: rotate(5deg) scale(1.1); }
75% { transform: rotate(-5deg) scale(0.9); }
}
.header h1 {
font-size: 3.5rem;
margin-bottom: 15px;
font-weight: 300;
text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
.header p {
font-size: 1.4rem;
opacity: 0.9;
margin-bottom: 8px;
}
.header .subtitle {
font-size: 1.1rem;
opacity: 0.75;
}
.main-layout {
display: grid;
grid-template-columns: 450px 1fr;
gap: 0;
min-height: 1200px;
}
.input-panel {
background: #f8fafc;
padding: 30px;
border-right: 1px solid #e2e8f0;
overflow-y: auto;
max-height: 1200px;
}
.input-section {
margin-bottom: 25px;
padding: 25px;
background: white;
border-radius: 15px;
box-shadow: 0 4px 15px rgba(0,0,0,0.08);
border: 1px solid #e2e8f0;
}
.section-title {
font-size: 1.2rem;
font-weight: 600;
color: #667eea;
margin-bottom: 18px;
padding-bottom: 12px;
border-bottom: 2px solid #e2e8f0;
display: flex;
align-items: center;
gap: 10px;
}
.data-input {
width: 100%;
height: 180px;
padding: 15px;
border: 2px solid #d1d5db;
border-radius: 10px;
font-family: 'Courier New', monospace;
font-size: 13px;
resize: vertical;
transition: all 0.3s ease;
}
.data-input:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.12);
}
.input-help {
font-size: 12px;
color: #6b7280;
margin-top: 8px;
line-height: 1.5;
}
.param-group {
margin-bottom: 18px;
}
.param-group label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: #374151;
font-size: 14px;
}
.param-group input, .param-group select {
width: 100%;
padding: 12px;
border: 2px solid #d1d5db;
border-radius: 8px;
font-size: 14px;
transition: all 0.3s ease;
}
.param-group input:focus, .param-group select:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.12);
}
.param-unit {
font-size: 11px;
color: #6b7280;
margin-top: 5px;
font-style: italic;
}
.preset-section {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 15px;
}
.preset-btn {
padding: 12px 16px;
background: #f1f5f9;
border: 1px solid #cbd5e1;
border-radius: 8px;
font-size: 13px;
cursor: pointer;
transition: all 0.25s ease;
font-weight: 500;
text-align: center;
}
.preset-btn:hover {
background: #e2e8f0;
border-color: #94a3b8;
transform: translateY(-2px);
}
.analysis-tabs {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.tab-btn {
flex: 1;
padding: 15px;
background: #f1f5f9;
border: 2px solid #cbd5e1;
border-radius: 10px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
text-align: center;
}
.tab-btn.active {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-color: #667eea;
}
.tab-btn:hover:not(.active) {
background: #e2e8f0;
border-color: #94a3b8;
}
.analyze-btn {
width: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 20px;
border-radius: 12px;
font-size: 18px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
margin-top: 25px;
text-transform: uppercase;
letter-spacing: 0.8px;
}
.analyze-btn:hover {
transform: translateY(-3px);
box-shadow: 0 15px 30px rgba(102, 126, 234, 0.4);
}
.analyze-btn:disabled {
background: #9ca3af;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
.results-area {
display: flex;
flex-direction: column;
background: white;
}
.stats-dashboard {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 20px;
padding: 30px;
background: #f0f4ff;
border-bottom: 1px solid #e2e8f0;
}
.stat-card {
text-align: center;
padding: 20px;
background: white;
border-radius: 15px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
border: 1px solid #e2e8f0;
transition: transform 0.2s ease;
}
.stat-card:hover {
transform: translateY(-3px);
}
.stat-card.primary { border-left: 4px solid #667eea; }
.stat-card.secondary { border-left: 4px solid #10b981; }
.stat-card.tertiary { border-left: 4px solid #f59e0b; }
.stat-card.quaternary { border-left: 4px solid #ef4444; }
.stat-card.wavelet { border-left: 4px solid #8b5cf6; }
.stat-label {
font-size: 11px;
color: #6b7280;
margin-bottom: 8px;
text-transform: uppercase;
letter-spacing: 0.8px;
font-weight: 500;
}
.stat-value {
font-size: 22px;
font-weight: 700;
color: #667eea;
margin-bottom: 5px;
}
.stat-unit {
font-size: 12px;
color: #6b7280;
font-weight: 400;
}
.charts-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 400px 500px 400px;
gap: 25px;
padding: 30px;
}
.chart-box {
background: #fafafa;
border-radius: 15px;
padding: 25px;
border: 1px solid #e2e8f0;
display: flex;
flex-direction: column;
box-shadow: 0 3px 10px rgba(0,0,0,0.06);
}
.chart-box.full-width {
grid-column: 1 / -1;
}
.chart-box.wavelet-plot {
grid-column: 1 / -1;
grid-row: 2;
}
.chart-header {
font-size: 1.4rem;
font-weight: 600;
color: #374151;
margin-bottom: 20px;
text-align: center;
padding-bottom: 10px;
border-bottom: 1px solid #e5e7eb;
}
.chart-content {
flex: 1;
position: relative;
}
.export-controls {
display: flex;
justify-content: center;
gap: 12px;
margin-top: 18px;
}
.export-btn {
padding: 10px 18px;
background: #667eea;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 13px;
transition: all 0.2s ease;
font-weight: 500;
}
.export-btn:hover {
background: #5a67d8;
transform: translateY(-1px);
}
.peak-table {
max-height: 300px;
overflow-y: auto;
margin-top: 15px;
border: 1px solid #e2e8f0;
border-radius: 8px;
}
.peak-table table {
width: 100%;
border-collapse: collapse;
font-size: 13px;
}
.peak-table th {
background: #f8fafc;
padding: 10px;
text-align: left;
font-weight: 600;
color: #374151;
border-bottom: 1px solid #e2e8f0;
position: sticky;
top: 0;
}
.peak-table td {
padding: 8px 10px;
border-bottom: 1px solid #f1f5f9;
}
.peak-table tr:hover {
background: #f8fafc;
}
.error-message {
background: #fef2f2;
border: 1px solid #fecaca;
color: #dc2626;
padding: 15px;
border-radius: 8px;
margin-top: 15px;
font-size: 14px;
}
.success-message {
background: #f0fdf4;
border: 1px solid #bbf7d0;
color: #166534;
padding: 15px;
border-radius: 8px;
margin-top: 15px;
font-size: 14px;
}
.wavelet-controls {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
margin-bottom: 20px;
}
.colorbar-info {
font-size: 12px;
color: #6b7280;
text-align: center;
margin-top: 10px;
font-style: italic;
}
@media (max-width: 1400px) {
.main-layout {
grid-template-columns: 1fr;
}
.input-panel {
border-right: none;
border-bottom: 1px solid #e2e8f0;
max-height: none;
}
.charts-container {
grid-template-columns: 1fr;
grid-template-rows: repeat(6, 400px);
}
.chart-box.full-width, .chart-box.wavelet-plot {
grid-column: 1;
}
}
@media (max-width: 768px) {
.header h1 {
font-size: 2.5rem;
}
.stats-dashboard {
grid-template-columns: repeat(2, 1fr);
}
.analysis-tabs {
flex-direction: column;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🌊 Advanced Time Series Analysis</h1>
<p>Spectral Analysis & Continuous Wavelet Transform by Claudio Iturra</p>
<div class="subtitle">FFT • CWT • Power Spectral Density • Time-Frequency Analysis • Peak Detection</div>
</div>
<div class="main-layout">
<div class="input-panel">
<div class="input-section">
<div class="section-title">
📈 Time Series Data
</div>
<textarea
class="data-input"
id="timeSeriesData"
placeholder="Paste your time series data here...
Examples:
1.2, 1.5, 1.8, 2.1, 1.9, 1.6, 1.3, 1.0
OR
1.2
1.5
1.8
OR
2023-01-01 00:00, 1.2
2023-01-01 01:00, 1.5"></textarea>
<div class="input-help">
Supported formats: Comma-separated, line-separated, or timestamp,value pairs.
Handles missing values automatically.
</div>
<div class="preset-section">
<button class="preset-btn" onclick="loadSampleData('tidal')">🌊 Tidal</button>
<button class="preset-btn" onclick="loadSampleData('temperature')">🌡️ Temperature</button>
<button class="preset-btn" onclick="loadSampleData('seismic')">🌍 Seismic</button>
<button class="preset-btn" onclick="loadSampleData('chirp')">📡 Chirp Signal</button>
</div>
</div>
<div class="input-section">
<div class="section-title">
⏱️ Temporal Parameters
</div>
<div class="param-group">
<label for="temporalResolution">Temporal Resolution</label>
<input type="number" id="temporalResolution" value="60" step="1" min="1" max="1440">
<div class="param-unit">minutes between data points</div>
</div>
<div class="param-group">
<label for="dataUnits">Data Units</label>
<input type="text" id="dataUnits" value="m/s" placeholder="e.g., m/s, °C, m">
<div class="param-unit">for axis labeling</div>
</div>
</div>
<div class="input-section">
<div class="section-title">
🔧 Analysis Type
</div>
<div class="analysis-tabs">
<button class="tab-btn active" id="spectralTab" onclick="switchAnalysisType('spectral')">
📊 Spectral
</button>
<button class="tab-btn" id="waveletTab" onclick="switchAnalysisType('wavelet')">
🌊 Wavelet
</button>
</div>
</div>
<div class="input-section" id="spectralParams">
<div class="section-title">
📊 Spectral Parameters
</div>
<div class="param-group">
<label for="windowFunction">Window Function</label>
<select id="windowFunction">
<option value="hanning">Hanning</option>
<option value="hamming">Hamming</option>
<option value="blackman">Blackman</option>
<option value="bartlett">Bartlett</option>
<option value="rectangular">Rectangular</option>
</select>
</div>
<div class="param-group">
<label for="detrend">Detrend Method</label>
<select id="detrend">
<option value="linear">Linear</option>
<option value="constant">Remove Mean</option>
<option value="none">None</option>
</select>
</div>
<div class="param-group">
<label for="zeroPadding">Zero Padding Factor</label>
<select id="zeroPadding">
<option value="1">None (1x)</option>
<option value="2" selected>2x</option>
<option value="4">4x</option>
<option value="8">8x</option>
</select>
</div>
<div class="param-group">
<label for="peakThreshold">Peak Detection Threshold</label>
<input type="number" id="peakThreshold" value="0.1" step="0.01" min="0.01" max="1">
<div class="param-unit">relative to maximum power</div>
</div>
</div>
<div class="input-section" id="waveletParams" style="display: none;">
<div class="section-title">
🌊 Wavelet Parameters
</div>
<div class="param-group">
<label for="waveletType">Wavelet Type</label>
<select id="waveletType">
<option value="morlet" selected>Morlet</option>
<option value="paul">Paul</option>
<option value="dog">DOG (Mexican Hat)</option>
</select>
</div>
<div class="param-group">
<label for="waveletParam">Wavelet Parameter</label>
<input type="number" id="waveletParam" value="6" step="0.5" min="1" max="20">
<div class="param-unit">ω₀ for Morlet, m for Paul/DOG</div>
</div>
<div class="param-group">
<label for="scaleMin">Minimum Scale</label>
<input type="number" id="scaleMin" value="2" step="0.5" min="1" max="100">
<div class="param-unit">minimum period in data points</div>
</div>
<div class="param-group">
<label for="scaleMax">Maximum Scale</label>
<input type="number" id="scaleMax" value="100" step="1" min="10" max="1000">
<div class="param-unit">maximum period in data points</div>
</div>
<div class="param-group">
<label for="scaleSteps">Scale Steps</label>
<input type="number" id="scaleSteps" value="50" step="1" min="20" max="200">
<div class="param-unit">number of frequency bands</div>
</div>
</div>
<div class="input-section">
<div class="section-title">
📊 Display Options
</div>
<div class="param-group">
<label for="frequencyUnits">Frequency Units</label>
<select id="frequencyUnits">
<option value="cpd" selected>Cycles per Day</option>
<option value="cph">Cycles per Hour</option>
<option value="hz">Hz (cycles/second)</option>
<option value="period_hours">Period (hours)</option>
<option value="period_days">Period (days)</option>
</select>
</div>
<div class="param-group">
<label for="powerScale">Power Scale</label>
<select id="powerScale">
<option value="linear">Linear</option>
<option value="log" selected>Logarithmic</option>
</select>
</div>
<div class="param-group">
<label for="maxFrequency">Maximum Frequency</label>
<input type="number" id="maxFrequency" value="10" step="0.5" min="0.1" max="100">
<div class="param-unit">cycles per day</div>
</div>
</div>
<button class="analyze-btn" id="analyzeBtn" onclick="performAnalysis()">
🔬 Analyze Time Series
</button>
<div id="statusMessage"></div>
</div>
<div class="results-area">
<div class="stats-dashboard" id="statsDashboard">
<!-- Statistics will be populated here -->
</div>
<div class="charts-container">
<div class="chart-box">
<div class="chart-header">Time Series Data</div>
<div class="chart-content">
<canvas id="timeSeriesChart"></canvas>
</div>
</div>
<div class="chart-box">
<div class="chart-header" id="analysisChartHeader">Power Spectral Density</div>
<div class="chart-content">
<canvas id="analysisChart"></canvas>
</div>
<div class="export-controls">
<button class="export-btn" onclick="exportResults()">📊 Export Data</button>
<button class="export-btn" onclick="exportPeaks()">🎯 Export Peaks</button>
</div>
</div>
<div class="chart-box wavelet-plot">
<div class="chart-header">Wavelet Transform / Scalogram</div>
<div class="chart-content">
<div id="waveletPlot" style="width: 100%; height: 100%;"></div>
</div>
<div class="colorbar-info">
Color intensity represents wavelet power magnitude
</div>
</div>
<div class="chart-box">
<div class="chart-header">Global Wavelet Spectrum</div>
<div class="chart-content">
<canvas id="globalWaveletChart"></canvas>
</div>
</div>
<div class="chart-box full-width">
<div class="chart-header">Detected Peaks & Significant Frequencies</div>
<div class="chart-content">
<div class="peak-table" id="peakTable">
<!-- Peak table will be populated here -->
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
let timeSeriesChart = null;
let analysisChart = null;
let globalWaveletChart = null;
let analysisResults = null;
let currentAnalysisType = 'spectral';
// Complex number operations
class Complex {
constructor(real, imag = 0) {
this.real = real;
this.imag = imag;
}
add(other) {
return new Complex(this.real + other.real, this.imag + other.imag);
}
multiply(other) {
return new Complex(
this.real * other.real - this.imag * other.imag,
this.real * other.imag + this.imag * other.real
);
}
magnitude() {
return Math.sqrt(this.real * this.real + this.imag * this.imag);
}
phase() {
return Math.atan2(this.imag, this.real);
}
}
// FFT implementation
function fft(signal) {
const N = signal.length;
if (N <= 1) return signal.map(x => new Complex(x));
const nextPow2 = Math.pow(2, Math.ceil(Math.log2(N)));
const paddedSignal = [...signal];
while (paddedSignal.length < nextPow2) {
paddedSignal.push(0);
}
return fftRecursive(paddedSignal.map(x => new Complex(x)));
}
function fftRecursive(x) {
const N = x.length;
if (N <= 1) return x;
const even = fftRecursive(x.filter((_, i) => i % 2 === 0));
const odd = fftRecursive(x.filter((_, i) => i % 2 === 1));
const result = new Array(N);
for (let k = 0; k < N/2; k++) {
const angle = -2 * Math.PI * k / N;
const t = new Complex(Math.cos(angle), Math.sin(angle)).multiply(odd[k]);
result[k] = even[k].add(t);
result[k + N/2] = even[k].add(t.multiply(new Complex(-1, 0)));
}
return result;
}
// Wavelet functions
function morletWavelet(t, s, w0 = 6) {
const norm = Math.pow(Math.PI, -0.25) * Math.sqrt(2 / s);
const exp1 = Math.exp(-0.5 * Math.pow(t / s, 2));
const exp2 = Math.exp(new Complex(0, w0 * t / s));
return norm * exp1 * Math.cos(w0 * t / s);
}
function paulWavelet(t, s, m = 4) {
const norm = Math.sqrt(2 * Math.PI * s / m) * Math.pow(2, m) / Math.sqrt(Math.PI * factorial(2 * m - 1));
const factor = Math.pow(1 + new Complex(0, t / s), -m - 1);
return norm * factor;
}
function dogWavelet(t, s, m = 2) {
const norm = Math.pow(-1, m + 1) / Math.sqrt(gamma(m + 0.5)) * Math.sqrt(s);
const factor = Math.pow(t / s, m) * Math.exp(-0.5 * Math.pow(t / s, 2));
return norm * factor;
}
function factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
function gamma(z) {
// Approximation for gamma function
if (z === 0.5) return Math.sqrt(Math.PI);
if (z === 1.5) return 0.5 * Math.sqrt(Math.PI);
if (z === 2.5) return 0.75 * Math.sqrt(Math.PI);
return factorial(Math.floor(z) - 1);
}
// Continuous Wavelet Transform
function cwt(signal, scales, waveletType = 'morlet', param = 6) {
const N = signal.length;
const result = [];
for (let scaleIdx = 0; scaleIdx < scales.length; scaleIdx++) {
const scale = scales[scaleIdx];
const convolution = new Array(N);
for (let n = 0; n < N; n++) {
let sum = 0;
for (let k = 0; k < N; k++) {
const t = k - n;
let waveletValue = 0;
switch (waveletType) {
case 'morlet':
waveletValue = morletWavelet(t, scale, param);
break;
case 'paul':
waveletValue = paulWavelet(t, scale, param);
break;
case 'dog':
waveletValue = dogWavelet(t, scale, param);
break;
}
sum += signal[k] * waveletValue;
}
convolution[n] = Math.abs(sum);
}
result.push(convolution);
}
return result;
}
function switchAnalysisType(type) {
currentAnalysisType = type;
// Update tab appearance
document.getElementById('spectralTab').classList.toggle('active', type === 'spectral');
document.getElementById('waveletTab').classList.toggle('active', type === 'wavelet');
// Show/hide parameter sections
document.getElementById('spectralParams').style.display = type === 'spectral' ? 'block' : 'none';
document.getElementById('waveletParams').style.display = type === 'wavelet' ? 'block' : 'none';
// Update button text
const btn = document.getElementById('analyzeBtn');
btn.innerHTML = type === 'spectral' ? '🔬 Analyze Spectrum' : '🌊 Analyze Wavelets';
}
function loadSampleData(type) {
const samples = {
'tidal': generateTidalData(),
'temperature': generateTemperatureData(),
'seismic': generateSeismicData(),
'chirp': generateChirpSignal()
};
document.getElementById('timeSeriesData').value = samples[type];
const params = {
'tidal': { resolution: 60, units: 'm/s' },
'temperature': { resolution: 30, units: '°C' },
'seismic': { resolution: 1, units: 'm/s²' },
'chirp': { resolution: 10, units: 'amplitude' }
};
document.getElementById('temporalResolution').value = params[type].resolution;
document.getElementById('dataUnits').value = params[type].units;
}
function generateTidalData() {
const data = [];
const hours = 336; // 14 days
for (let i = 0; i < hours; i++) {
const t = i;
// Multiple tidal components
const m2 = 1.5 * Math.cos(2 * Math.PI * t / 12.42); // M2 tide
const s2 = 0.4 * Math.cos(2 * Math.PI * t / 12 + Math.PI/3); // S2 tide
const m4 = 0.3 * Math.cos(2 * Math.PI * t / 6.21 + Math.PI/4); // M4 tide
const o1 = 0.2 * Math.cos(2 * Math.PI * t / 25.82 + Math.PI/6); // O1 tide
const noise = 0.1 * (Math.random() - 0.5);
data.push((m2 + s2 + m4 + o1 + noise).toFixed(3));
}
return data.join('\n');
}
function generateTemperatureData() {
const data = [];
const hours = 720; // 30 days
for (let i = 0; i < hours; i++) {
const t = i * 0.5; // 30-minute intervals
const daily = 8 * Math.cos(2 * Math.PI * t / 24 - Math.PI/2);
const weekly = 3 * Math.cos(2 * Math.PI * t / (24 * 7));
const seasonal = 1 * Math.cos(2 * Math.PI * t / (24 * 30));
const trend = 0.005 * t;
const noise = 1 * (Math.random() - 0.5);
const temp = 15 + daily + weekly + seasonal + trend + noise;
data.push(temp.toFixed(2));
}
return data.join('\n');
}
function generateSeismicData() {
const data = [];
const points = 1000;
for (let i = 0; i < points; i++) {
const t = i;
let signal = 0;
// Background noise
signal += 0.1 * (Math.random() - 0.5);
// Seismic events at different times
if (t > 200 && t < 250) {
signal += 2 * Math.exp(-(t - 225) * (t - 225) / 100) * Math.cos(2 * Math.PI * t / 8);
}
if (t > 600 && t < 700) {
signal += 1.5 * Math.exp(-(t - 650) * (t - 650) / 200) * Math.cos(2 * Math.PI * t / 12);
}
data.push(signal.toFixed(4));
}
return data.join('\n');
}
function generateChirpSignal() {
const data = [];
const points = 500;
for (let i = 0; i < points; i++) {
const t = i / points;
// Frequency increases linearly from 1 to 20 cycles per unit time
const freq = 1 + 19 * t;
const phase = 2 * Math.PI * (t + 9.5 * t * t);
const signal = Math.cos(phase) + 0.1 * (Math.random() - 0.5);
data.push(signal.toFixed(4));
}
return data.join('\n');
}
function parseTimeSeriesData(dataText) {
const lines = dataText.trim().split('\n');
const values = [];
const timestamps = [];
for (let line of lines) {
line = line.trim();
if (!line) continue;
if (line.includes(',')) {
const parts = line.split(',');
if (parts.length === 2 && isNaN(parts[0])) {
timestamps.push(parts[0].trim());
values.push(parseFloat(parts[1].trim()));
} else {
for (let part of parts) {
const val = parseFloat(part.trim());
if (!isNaN(val)) values.push(val);
}
}
} else {
const val = parseFloat(line);
if (!isNaN(val)) values.push(val);
}
}
return { values, timestamps };
}
function applyWindow(data, windowType) {
const N = data.length;
const windowed = new Array(N);
for (let i = 0; i < N; i++) {
let w = 1;
const n = i / (N - 1);