|
|
@@ -1,13 +1,13 @@
|
|
|
-<!DOCTYPE html>
|
|
|
+<!DOCTYPE html>
|
|
|
<html lang="ru">
|
|
|
<head>
|
|
|
<meta charset="UTF-8">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
- <title>Механические волны и Фурье (Точно по конспекту)</title>
|
|
|
+ <title>Механические волны и Фурье v1.1</title>
|
|
|
<style>
|
|
|
body {
|
|
|
font-family: Arial, sans-serif;
|
|
|
- background: #fdfaf6; /* Светлый/бежевый фон */
|
|
|
+ background: #fdfaf6;
|
|
|
margin: 0;
|
|
|
padding: 10px;
|
|
|
color: #333;
|
|
|
@@ -88,9 +88,12 @@
|
|
|
font-weight: bold;
|
|
|
color: white;
|
|
|
margin-right: 10px;
|
|
|
+ margin-bottom: 10px;
|
|
|
+ display: inline-block;
|
|
|
}
|
|
|
.btn-audio { background: #2196F3; }
|
|
|
.btn-dist { background: #9C27B0; }
|
|
|
+ .btn-reset { background: #607D8B; } /* Новая кнопка */
|
|
|
.btn-audio.active { background: #f44336; }
|
|
|
.btn-dist.active { background: #d32f2f; }
|
|
|
</style>
|
|
|
@@ -117,12 +120,10 @@
|
|
|
<div class="panel">
|
|
|
<h3 style="margin:0 0 10px 0;">2. Элементы управления</h3>
|
|
|
|
|
|
- <!-- Формулы из тетради -->
|
|
|
<div class="formulas">
|
|
|
y = A · cos(wt + θ) | V = 1/T | w = 2πV
|
|
|
</div>
|
|
|
|
|
|
- <!-- Вкладки (несколько колебаний) -->
|
|
|
<div class="tabs">
|
|
|
<div class="tab active" onclick="switchTab(0)">V1 (Волна 1)</div>
|
|
|
<div class="tab" onclick="switchTab(1)">V2 (Волна 2)</div>
|
|
|
@@ -150,6 +151,7 @@
|
|
|
<div style="margin-top: 10px;">
|
|
|
<button class="btn btn-audio" onclick="toggleAudio()" id="btnAudio">🔊 Вывод на динамике</button>
|
|
|
<button class="btn btn-dist" onclick="toggleClip()" id="btnClip">Добавить нелинейное преобразование</button>
|
|
|
+ <button class="btn btn-reset" onclick="resetAll()">🔄 Сбросить все</button> <!-- Новая фича -->
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
@@ -162,7 +164,6 @@
|
|
|
</div>
|
|
|
|
|
|
<script>
|
|
|
- // Состояние волн (свои настройки для каждой)
|
|
|
let waves = [
|
|
|
{ f: 440, a: 80 },
|
|
|
{ f: 880, a: 0 },
|
|
|
@@ -177,7 +178,6 @@
|
|
|
let freqStep = 100;
|
|
|
let time = 0;
|
|
|
|
|
|
- // Переключение вкладок
|
|
|
function switchTab(index) {
|
|
|
currentWave = index;
|
|
|
document.querySelectorAll('.tab').forEach((el, i) => {
|
|
|
@@ -198,7 +198,6 @@
|
|
|
document.getElementById('valA').innerText = waves[currentWave].a;
|
|
|
}
|
|
|
|
|
|
- // Обновление параметров волны
|
|
|
function updateWave() {
|
|
|
waves[currentWave].f = parseInt(document.getElementById('freq').value);
|
|
|
waves[currentWave].a = parseInt(document.getElementById('amp').value);
|
|
|
@@ -206,7 +205,6 @@
|
|
|
updateAudio();
|
|
|
}
|
|
|
|
|
|
- // Включение/выключение искажения (клиппинга)
|
|
|
function toggleClip() {
|
|
|
isClipped = !isClipped;
|
|
|
let btn = document.getElementById('btnClip');
|
|
|
@@ -217,10 +215,24 @@
|
|
|
btn.classList.remove('active');
|
|
|
btn.innerText = "Добавить нелинейное преобразование";
|
|
|
}
|
|
|
- updateAudio(); // Применяем искажение к звуку
|
|
|
+ updateAudio();
|
|
|
+ }
|
|
|
+
|
|
|
+ // --- ИСПРАВЛЕНИЕ: Функция сброса ---
|
|
|
+ function resetAll() {
|
|
|
+ waves = [
|
|
|
+ { f: 440, a: 80 },
|
|
|
+ { f: 880, a: 0 },
|
|
|
+ { f: 1320, a: 0 }
|
|
|
+ ];
|
|
|
+ isClipped = false;
|
|
|
+ let btnClip = document.getElementById('btnClip');
|
|
|
+ btnClip.classList.remove('active');
|
|
|
+ btnClip.innerText = "Добавить нелинейное преобразование";
|
|
|
+ switchTab(0);
|
|
|
+ updateAudio();
|
|
|
}
|
|
|
|
|
|
- // WEB AUDIO API
|
|
|
function toggleAudio() {
|
|
|
if (!audioCtx) {
|
|
|
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
@@ -253,27 +265,22 @@
|
|
|
if (!audioCtx) return;
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
oscillators[i].osc.frequency.setValueAtTime(waves[i].f, audioCtx.currentTime);
|
|
|
- // Громкость (0.01 чтобы не было слишком громко)
|
|
|
let vol = isAudioOn ? (waves[i].a * 0.005) : 0;
|
|
|
- // Имитация звукового искажения при клиппинге
|
|
|
if (isClipped && isAudioOn && waves[i].a > 0) vol *= 2.5;
|
|
|
- oscillators[i].gain.gain.setValueAtTime(vol, audioCtx.currentTime);
|
|
|
|
|
|
- // Меняем тип волны для эффекта искажения
|
|
|
+ // --- ИСПРАВЛЕНИЕ: Плавное изменение звука (без щелчка) ---
|
|
|
+ oscillators[i].gain.gain.setTargetAtTime(vol, audioCtx.currentTime, 0.05);
|
|
|
+
|
|
|
oscillators[i].osc.type = isClipped ? 'square' : 'sine';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // ОТРИСОВКА ГРАФИКОВ
|
|
|
function draw() {
|
|
|
time += 0.08;
|
|
|
|
|
|
- // 1. График одного колебания (Левый верхний)
|
|
|
let c1 = document.getElementById('canvas-single');
|
|
|
let ctx1 = c1.getContext('2d');
|
|
|
ctx1.clearRect(0, 0, c1.width, c1.height);
|
|
|
-
|
|
|
- // Ось X
|
|
|
ctx1.beginPath(); ctx1.strokeStyle = "#ccc";
|
|
|
ctx1.moveTo(0, c1.height/2); ctx1.lineTo(c1.width, c1.height/2); ctx1.stroke();
|
|
|
|
|
|
@@ -281,7 +288,6 @@
|
|
|
ctx1.strokeStyle = "#2196F3";
|
|
|
ctx1.lineWidth = 2;
|
|
|
for (let x = 0; x < c1.width; x++) {
|
|
|
- // Масштабируем частоту визуально
|
|
|
let visualF = (waves[currentWave].f / 200) || 1;
|
|
|
let y = Math.cos(x * 0.02 * visualF - time) * (waves[currentWave].a / 100) * (c1.height/2.5);
|
|
|
if(x===0) ctx1.moveTo(x, c1.height/2 - y);
|
|
|
@@ -289,11 +295,9 @@
|
|
|
}
|
|
|
ctx1.stroke();
|
|
|
|
|
|
- // 2. График суммы Фурье (Правый верхний)
|
|
|
let c2 = document.getElementById('canvas-sum');
|
|
|
let ctx2 = c2.getContext('2d');
|
|
|
ctx2.clearRect(0, 0, c2.width, c2.height);
|
|
|
-
|
|
|
ctx2.beginPath(); ctx2.strokeStyle = "#ccc";
|
|
|
ctx2.moveTo(0, c2.height/2); ctx2.lineTo(c2.width, c2.height/2); ctx2.stroke();
|
|
|
|
|
|
@@ -307,7 +311,6 @@
|
|
|
sumY += Math.cos(x * 0.02 * visualF - time) * (waves[i].a / 100) * (c2.height/2.5);
|
|
|
}
|
|
|
|
|
|
- // Применение нелинейного преобразования (Клиппинг) к графику
|
|
|
if (isClipped) {
|
|
|
let limit = (c2.height/4);
|
|
|
if (sumY > limit) sumY = limit;
|
|
|
@@ -319,15 +322,21 @@
|
|
|
}
|
|
|
ctx2.stroke();
|
|
|
|
|
|
- // 3. Спектр (Правый нижний)
|
|
|
let c3 = document.getElementById('canvas-spectrum');
|
|
|
let ctx3 = c3.getContext('2d');
|
|
|
ctx3.clearRect(0, 0, c3.width, c3.height);
|
|
|
|
|
|
- // Ось X для спектра
|
|
|
ctx3.beginPath(); ctx3.strokeStyle = "#ccc";
|
|
|
ctx3.moveTo(0, c3.height - 15); ctx3.lineTo(c3.width, c3.height - 15); ctx3.stroke();
|
|
|
|
|
|
+ // --- ИСПРАВЛЕНИЕ: Разметка частот спектра ---
|
|
|
+ ctx3.fillStyle = "#999";
|
|
|
+ ctx3.font = "10px Arial";
|
|
|
+ ctx3.fillText("0", 5, c3.height - 2);
|
|
|
+ ctx3.fillText("5кГц", (5000 / 20000) * c3.width, c3.height - 2);
|
|
|
+ ctx3.fillText("10кГц", (10000 / 20000) * c3.width, c3.height - 2);
|
|
|
+ ctx3.fillText("15кГц", (15000 / 20000) * c3.width, c3.height - 2);
|
|
|
+
|
|
|
ctx3.fillStyle = "#4CAF50";
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
if (waves[i].a > 0) {
|
|
|
@@ -337,12 +346,11 @@
|
|
|
|
|
|
ctx3.fillStyle = "#333";
|
|
|
ctx3.font = "10px Arial";
|
|
|
- ctx3.fillText(waves[i].f, Math.max(xPos-5, 0), c3.height - 5);
|
|
|
+ ctx3.fillText(waves[i].f, Math.max(xPos-5, 0), c3.height - 25 - h);
|
|
|
ctx3.fillStyle = "#4CAF50";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Отрисовка паразитных гармоник при искажении (клиппинге)
|
|
|
if (isClipped && waves[0].a > 0) {
|
|
|
ctx3.fillStyle = "rgba(156, 39, 176, 0.6)";
|
|
|
for (let i = 3; i <= 9; i += 2) {
|
|
|
@@ -357,7 +365,6 @@
|
|
|
requestAnimationFrame(draw);
|
|
|
}
|
|
|
|
|
|
- // Инициализация размеров холста
|
|
|
function resizeCanvases() {
|
|
|
document.querySelectorAll('canvas').forEach(c => {
|
|
|
c.width = c.parentElement.clientWidth - 30;
|