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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ============================================================
// fourier_transform.js — Fourier Transform Visualizer
// ============================================================

// --- GLOBAL VARIABLES ---
let timeDomainChart, magnitudeChart, phaseChart;

// Wave colors matching the HTML
const WAVE_COLORS = ['#1976d2', '#d32f2f', '#388e3c', '#f57c00'];

// Sampling parameters
const SAMPLING_RATE = 256; // Hz - Fixed sampling rate for display
const MAX_SAMPLES_FOR_DFT = 512; // Maximum samples for DFT to maintain performance

// --- DFT IMPLEMENTATION ---

/**
 * Cooley-Tukey FFT algorithm for power-of-2 sizes
 * Much faster than DFT for large N
 * 
 * @param {number[]} signal - Real-valued input signal
 * @returns {Object} - Object with real (re) and imaginary (im) parts arrays
 */
function fft(signal) {
    const N = signal.length;
    
    // Check if N is a power of 2
    if (N & (N - 1)) {
        // Not a power of 2, fall back to DFT
        return dft(signal);
    }
    
    // Bit-reverse copy
    const re = new Array(N);
    const im = new Array(N).fill(0);
    
    for (let i = 0; i < N; i++) {
        let j = 0;
        let bit = N >> 1;
        let ii = i;
        while (bit > 0) {
            if (ii & 1) j |= bit;
            ii >>= 1;
            bit >>= 1;
        }
        re[j] = signal[i];
    }
    
    // Butterfly operations
    for (let step = 2; step <= N; step <<= 1) {
        const halfStep = step >> 1;
        const angleInc = -2 * Math.PI / step;
        
        for (let group = 0; group < N; group += step) {
            for (let i = 0; i < halfStep; i++) {
                const angle = angleInc * i;
                const cos = Math.cos(angle);
                const sin = Math.sin(angle);
                
                const evenRe = re[group + i];
                const evenIm = im[group + i];
                const oddRe = re[group + i + halfStep] * cos - im[group + i + halfStep] * sin;
                const oddIm = re[group + i + halfStep] * sin + im[group + i + halfStep] * cos;
                
                re[group + i] = evenRe + oddRe;
                im[group + i] = evenIm + oddIm;
                re[group + i + halfStep] = evenRe - oddRe;
                im[group + i + halfStep] = evenIm - oddIm;
            }
        }
    }
    
    return { re, im };
}

/**
 * Discrete Fourier Transform (DFT) - O(N²) implementation
 * Input: real-valued time domain signal
 * Output: complex frequency domain values
 * 
 * @param {number[]} signal - Real-valued input signal
 * @returns {Object} - Object with real (re) and imaginary (im) parts arrays
 */
function dft(signal) {
    const N = signal.length;
    const re = new Array(N).fill(0);
    const im = new Array(N).fill(0);
    
    for (let k = 0; k < N; k++) {
        let sumRe = 0;
        let sumIm = 0;
        for (let n = 0; n < N; n++) {
            const angle = -2 * Math.PI * k * n / N;
            sumRe += signal[n] * Math.cos(angle);
            sumIm += signal[n] * Math.sin(angle);
        }
        re[k] = sumRe;
        im[k] = sumIm;
    }
    
    return { re, im };
}

/**
 * Compute magnitude spectrum from complex DFT output
 * Returns single-sided spectrum (positive frequencies only)
 * 
 * @param {Object} dftResult - Object with re and im arrays
 * @returns {number[]} - Magnitude values for positive frequencies
 */
function computeMagnitudeSpectrum(dftResult) {
    const N = dftResult.re.length;
    const magnitudes = [];
    
    // Only return positive frequencies (0 to N/2)
    for (let k = 0; k <= N / 2; k++) {
        const mag = Math.sqrt(dftResult.re[k] ** 2 + dftResult.im[k] ** 2);
        // Normalize by N/2 for single-sided spectrum
        magnitudes.push(2 * mag / N);
    }
    
    // DC component should not be doubled
    magnitudes[0] = magnitudes[0] / 2;
    if (N % 2 === 0) {
        magnitudes[magnitudes.length - 1] = magnitudes[magnitudes.length - 1] / 2;
    }
    
    return magnitudes;
}

/**
 * Compute phase spectrum from complex DFT output
 * Returns single-sided spectrum (positive frequencies only)
 * 
 * @param {Object} dftResult - Object with re and im arrays
 * @returns {number[]} - Phase values in degrees for positive frequencies
 */
function computePhaseSpectrum(dftResult) {
    const N = dftResult.re.length;
    const phases = [];
    
    for (let k = 0; k <= N / 2; k++) {
        const phaseRad = Math.atan2(dftResult.im[k], dftResult.re[k]);
        const phaseDeg = phaseRad * 180 / Math.PI;
        phases.push(phaseDeg);
    }
    
    return phases;
}

// --- SIGNAL GENERATION ---

/**
 * Generate a sine wave
 * 
 * @param {number} frequency - Frequency in Hz
 * @param {number} amplitude - Peak amplitude
 * @param {number} phaseDeg - Phase in degrees
 * @param {number} samplingRate - Sampling rate in Hz
 * @param {number} numSamples - Number of samples
 * @returns {number[]} - Generated signal
 */
function generateSineWave(frequency, amplitude, phaseDeg, samplingRate, numSamples) {
    const signal = [];
    const phaseRad = phaseDeg * Math.PI / 180;
    
    for (let n = 0; n < numSamples; n++) {
        const t = n / samplingRate;
        const value = amplitude * Math.sin(2 * Math.PI * frequency * t + phaseRad);
        signal.push(value);
    }
    
    return signal;
}

/**
 * Generate the composite signal from all enabled waves
 * 
 * @returns {Object} - Object with time array, signal array, and individual waves
 */
function generateCompositeSignal() {
    const numSamples = parseInt(document.getElementById('numSamples').value);
    const addNoise = document.getElementById('addNoise').checked;
    const noiseLevel = parseFloat(document.getElementById('noiseLevel').value);
    
    // Initialize signal and time arrays
    const time = [];
    const signal = new Array(numSamples).fill(0);
    const individualWaves = [];
    
    // Generate time array
    for (let n = 0; n < numSamples; n++) {
        time.push(n / SAMPLING_RATE);
    }
    
    // Add each enabled wave
    for (let i = 1; i <= 4; i++) {
        const enabled = document.getElementById(`enableWave${i}`).checked;
        if (!enabled) continue;
        
        const frequency = parseInt(document.getElementById(`freq${i}`).value);
        const amplitude = parseFloat(document.getElementById(`amp${i}`).value);
        const phase = parseInt(document.getElementById(`phase${i}`).value);
        
        // Generate wave even if amplitude is 0, as long as the wave is enabled
        const wave = generateSineWave(frequency, amplitude, phase, SAMPLING_RATE, numSamples);
        individualWaves.push({
            frequency,
            amplitude,
            phase,
            color: WAVE_COLORS[i - 1],
            data: wave
        });
        
        // Add to composite signal
        for (let n = 0; n < numSamples; n++) {
            signal[n] += wave[n];
        }
    }
    
    // Add noise if enabled
    if (addNoise) {
        for (let n = 0; n < numSamples; n++) {
            signal[n] += randomGaussian(0, noiseLevel);
        }
    }
    
    return { time, signal, individualWaves, numSamples };
}

// --- CHART INITIALIZATION ---

function initCharts() {
    // Time Domain Chart
    const timeCtx = document.getElementById('timeDomainChart').getContext('2d');
    timeDomainChart = new Chart(timeCtx, {
        type: 'line',
        data: {
            datasets: [
                {
                    label: 'Composite Signal',
                    data: [],
                    borderColor: '#1976d2',
                    backgroundColor: 'rgba(25, 118, 210, 0.1)',
                    borderWidth: 2,
                    pointRadius: 0,
                    fill: true,
                    tension: 0.1
                },
                {
                    label: 'Wave 1',
                    data: [],
                    borderColor: WAVE_COLORS[0],
                    borderWidth: 1,
                    pointRadius: 0,
                    borderDash: [5, 5],
                    fill: false,
                    tension: 0.1,
                    hidden: false
                },
                {
                    label: 'Wave 2',
                    data: [],
                    borderColor: WAVE_COLORS[1],
                    borderWidth: 1,
                    pointRadius: 0,
                    borderDash: [5, 5],
                    fill: false,
                    tension: 0.1,
                    hidden: false
                },
                {
                    label: 'Wave 3',
                    data: [],
                    borderColor: WAVE_COLORS[2],
                    borderWidth: 1,
                    pointRadius: 0,
                    borderDash: [5, 5],
                    fill: false,
                    tension: 0.1,
                    hidden: true
                },
                {
                    label: 'Wave 4',
                    data: [],
                    borderColor: WAVE_COLORS[3],
                    borderWidth: 1,
                    pointRadius: 0,
                    borderDash: [5, 5],
                    fill: false,
                    tension: 0.1,
                    hidden: true
                }
            ]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            animation: { duration: 0 },
            interaction: {
                mode: 'index',
                intersect: false
            },
            scales: {
                x: {
                    type: 'linear',
                    title: { display: true, text: 'Time (s)' }
                },
                y: {
                    title: { display: true, text: 'Amplitude' },
                    min: -25,
                    max: 25
                }
            },
            plugins: {
                legend: {
                    display: true,
                    labels: {
                        filter: function(item, data) {
                            // Only show legend for visible waves
                            const dataset = data.datasets[item.datasetIndex];
                            return !dataset.hidden;
                        }
                    }
                },
                tooltip: {
                    callbacks: {
                        title: function(context) {
                            return `t = ${context[0].parsed.x.toFixed(4)} s`;
                        }
                    }
                }
            }
        }
    });

    // Magnitude Spectrum Chart
    const magCtx = document.getElementById('magnitudeChart').getContext('2d');
    magnitudeChart = new Chart(magCtx, {
        type: 'bar',
        data: {
            datasets: [{
                label: 'Magnitude',
                data: [],
                backgroundColor: 'rgba(25, 118, 210, 0.7)',
                borderColor: '#1976d2',
                borderWidth: 1
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            animation: { duration: 0 },
            scales: {
                x: {
                    type: 'linear',
                    title: { display: true, text: 'Frequency (Hz)' },
                    min: 0
                },
                y: {
                    title: { display: true, text: '|X(f)|' },
                    beginAtZero: true
                }
            },
            plugins: {
                legend: { display: false },
                tooltip: {
                    callbacks: {
                        title: function(context) {
                            return `f = ${context[0].parsed.x.toFixed(1)} Hz`;
                        },
                        label: function(context) {
                            return `Magnitude: ${context.parsed.y.toFixed(3)}`;
                        }
                    }
                }
            }
        }
    });

    // Phase Spectrum Chart
    const phaseCtx = document.getElementById('phaseChart').getContext('2d');
    phaseChart = new Chart(phaseCtx, {
        type: 'scatter',
        data: {
            datasets: [{
                label: 'Phase',
                data: [],
                backgroundColor: 'rgba(46, 125, 50, 0.7)',
                borderColor: '#2e7d32',
                borderWidth: 1,
                pointRadius: 4
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            animation: { duration: 0 },
            scales: {
                x: {
                    type: 'linear',
                    title: { display: true, text: 'Frequency (Hz)' },
                    min: 0
                },
                y: {
                    title: { display: true, text: 'Phase (degrees)' },
                    min: -180,
                    max: 180,
                    ticks: {
                        stepSize: 45
                    }
                }
            },
            plugins: {
                legend: { display: false },
                tooltip: {
                    callbacks: {
                        title: function(context) {
                            return `f = ${context[0].parsed.x.toFixed(1)} Hz`;
                        },
                        label: function(context) {
                            return `Phase: ${context.parsed.y.toFixed(1)}°`;
                        }
                    }
                }
            }
        }
    });
}

// --- UPDATE FUNCTIONS ---

function updateVisualizations() {
    const { time, signal, individualWaves, numSamples } = generateCompositeSignal();
    
    // Update Time Domain Chart
    timeDomainChart.data.datasets[0].data = signal.map((y, i) => ({ x: time[i], y }));
    
    // Update individual wave datasets
    for (let i = 0; i < 4; i++) {
        const waveData = individualWaves.find(w => w.color === WAVE_COLORS[i]);
        if (waveData) {
            timeDomainChart.data.datasets[i + 1].data = waveData.data.map((y, j) => ({ x: time[j], y }));
            timeDomainChart.data.datasets[i + 1].hidden = false;
        } else {
            timeDomainChart.data.datasets[i + 1].data = [];
            timeDomainChart.data.datasets[i + 1].hidden = true;
        }
    }
    
    // Compute FFT (falls back to DFT if not power of 2)
    const dftResult = fft(signal);
    const magnitudes = computeMagnitudeSpectrum(dftResult);
    const phases = computePhaseSpectrum(dftResult);
    
    // Frequency bins
    const freqResolution = SAMPLING_RATE / numSamples;
    const frequencies = magnitudes.map((_, k) => k * freqResolution);
    const nyquistFreq = SAMPLING_RATE / 2;
    
    // Update Magnitude Spectrum
    // Only show frequencies up to Nyquist, and only show significant peaks
    const maxFreq = Math.min(50, nyquistFreq); // Limit display to 50 Hz for better visualization
    const filteredFreqIndices = frequencies
        .map((f, i) => ({ f, i }))
        .filter(item => item.f <= maxFreq);
    
    magnitudeChart.data.datasets[0].data = filteredFreqIndices.map(item => ({
        x: item.f,
        y: magnitudes[item.i]
    }));
    magnitudeChart.options.scales.x.max = maxFreq;
    
    // Update Phase Spectrum
    // Use dynamic threshold based on max magnitude to avoid noise in phase spectrum
    const maxMagnitude = Math.max(...magnitudes);
    const threshold = Math.max(maxMagnitude * 0.01, 0.001); // At least 1% of max or 0.001
    
    const significantPhases = filteredFreqIndices
        .filter(item => magnitudes[item.i] > threshold) // Only show phase for significant frequencies
        .map(item => ({
            x: item.f,
            y: phases[item.i]
        }));
    
    phaseChart.data.datasets[0].data = significantPhases;
    phaseChart.options.scales.x.max = maxFreq;
    
    // Update charts
    timeDomainChart.update('none');
    magnitudeChart.update('none');
    phaseChart.update('none');
    
    // Update signal information
    updateSignalInfo(numSamples, freqResolution, signal, magnitudes);
}

function updateSignalInfo(numSamples, freqResolution, signal, magnitudes) {
    const nyquistFreq = SAMPLING_RATE / 2;
    const duration = numSamples / SAMPLING_RATE;
    
    // Calculate total power (Parseval's theorem)
    const totalPower = magnitudes.reduce((sum, mag, i) => {
        // DC and Nyquist components are not doubled
        const factor = (i === 0 || (i === magnitudes.length - 1 && numSamples % 2 === 0)) ? 1 : 0.5;
        return sum + factor * mag * mag;
    }, 0);
    
    // Calculate RMS amplitude
    const rmsAmplitude = Math.sqrt(signal.reduce((sum, val) => sum + val * val, 0) / signal.length);
    
    document.getElementById('samplingRate').textContent = `${SAMPLING_RATE} Hz`;
    document.getElementById('nyquistFreq').textContent = `${nyquistFreq} Hz`;
    document.getElementById('freqResolution').textContent = `${freqResolution.toFixed(3)} Hz`;
    document.getElementById('signalDuration').textContent = `${duration.toFixed(3)} s`;
    document.getElementById('totalPower').textContent = totalPower.toFixed(3);
    document.getElementById('rmsAmplitude').textContent = rmsAmplitude.toFixed(3);
}

// --- EVENT LISTENERS ---

function setupEventListeners() {
    // Wave enable checkboxes
    for (let i = 1; i <= 4; i++) {
        document.getElementById(`enableWave${i}`).addEventListener('change', function() {
            const waveSection = this.closest('.wave-section');
            if (this.checked) {
                waveSection.classList.remove('disabled');
            } else {
                waveSection.classList.add('disabled');
            }
            updateVisualizations();
        });
    }
    
    // Wave parameter sliders
    for (let i = 1; i <= 4; i++) {
        document.getElementById(`freq${i}`).addEventListener('input', function() {
            document.getElementById(`freq${i}Value`).textContent = `${this.value} Hz`;
            updateVisualizations();
        });
        
        document.getElementById(`amp${i}`).addEventListener('input', function() {
            document.getElementById(`amp${i}Value`).textContent = parseFloat(this.value).toFixed(1);
            updateVisualizations();
        });
        
        document.getElementById(`phase${i}`).addEventListener('input', function() {
            document.getElementById(`phase${i}Value`).textContent = `${this.value}°`;
            updateVisualizations();
        });
    }
    
    // Number of samples slider
    document.getElementById('numSamples').addEventListener('input', function() {
        document.getElementById('numSamplesValue').textContent = this.value;
        updateVisualizations();
    });
    
    // Noise toggle
    document.getElementById('addNoise').addEventListener('change', function() {
        const noiseLevelGroup = document.getElementById('noiseLevelGroup');
        const noiseLevelSlider = document.getElementById('noiseLevel');
        
        if (this.checked) {
            noiseLevelGroup.style.opacity = '1';
            noiseLevelSlider.disabled = false;
        } else {
            noiseLevelGroup.style.opacity = '0.4';
            noiseLevelSlider.disabled = true;
        }
        updateVisualizations();
    });
    
    // Noise level slider
    document.getElementById('noiseLevel').addEventListener('input', function() {
        document.getElementById('noiseLevelValue').textContent = parseFloat(this.value).toFixed(1);
        updateVisualizations();
    });
    
}

// --- MAIN INITIALIZATION ---

function main() {
    initCharts();
    setupEventListeners();
    
    // Make controls draggable on desktop
    if (window.innerWidth > 1200) {
        makeDraggable(document.getElementById('floatingControls'), document.getElementById('controlsTitle'));
    }
    
    // Initial visualization
    updateVisualizations();
}

window.addEventListener('load', main);