This repository was archived by the owner on Nov 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaves.py
More file actions
73 lines (53 loc) · 2.31 KB
/
waves.py
File metadata and controls
73 lines (53 loc) · 2.31 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
# Physics/waves.py
"""
General Wave Physics Module
Covers the base properties of waves: harmonic motion, interference, and wave behavior.
"""
from decimal import Decimal, getcontext
from math import sin, pi
getcontext().prec = 50
# Wave equation: v = f * λ (reused from sound.py if needed)
def wave_function(amplitude: Decimal, frequency: Decimal, time: Decimal, phase: Decimal = Decimal("0")) -> Decimal:
"""
Basic wave function: y(t) = A * sin(2πft + φ)
"""
return amplitude * Decimal(sin(float(2 * pi * frequency * time + phase)))
def angular_frequency(frequency: Decimal) -> Decimal:
"""ω = 2πf"""
return Decimal("2") * Decimal(pi) * frequency
def harmonic_frequency(fundamental_freq: Decimal, harmonic_number: int) -> Decimal:
"""Calculate frequency of nth harmonic."""
return fundamental_freq * Decimal(harmonic_number)
def standing_wave_length(length: Decimal, harmonic_number: int, fixed_ends: bool = True) -> Decimal:
"""
Standing wave wavelength based on harmonic number.
For string with both ends fixed: λ = 2L / n
"""
if fixed_ends:
return Decimal("2") * length / Decimal(harmonic_number)
else:
# One end fixed (like open pipe): λ = 4L / (2n - 1)
return Decimal("4") * length / Decimal(2 * harmonic_number - 1)
def superposition(wave1: Decimal, wave2: Decimal) -> Decimal:
"""Add two wave amplitudes (constructive/destructive interference)."""
return wave1 + wave2
def beat_frequency(f1: Decimal, f2: Decimal) -> Decimal:
"""Calculate beat frequency: |f1 - f2|"""
return abs(f1 - f2)
def reflection_phase_change(fixed_end: bool) -> str:
"""
Describes phase change due to reflection.
Fixed end → 180° (π) phase shift.
Free end → no phase shift.
"""
return "π (180°)" if fixed_end else "0 (no phase shift)"
# Example usage
if __name__ == "__main__":
A = Decimal("1.0")
f = Decimal("10")
t = Decimal("0.1")
y = wave_function(A, f, t)
print(f"Wave displacement at t={t}s: {y} units")
print("2nd harmonic frequency of 100Hz fundamental:", harmonic_frequency(Decimal("100"), 2), "Hz")
print("Standing wavelength for L=1m, n=2:", standing_wave_length(Decimal("1"), 2), "m")
print("Beat frequency between 440Hz and 445Hz:", beat_frequency(Decimal("440"), Decimal("445")), "Hz")