Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
The-Algorithms-Python/physics/doppler_frequency.py at master · foo123/The-Algorithms-Python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
foo123
/
The-Algorithms-Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
The-Algorithms-Python
/
physics
/
doppler_frequency.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
104 lines (87 loc) · 4.37 KB
master
Breadcrumbs
The-Algorithms-Python
/
physics
/
doppler_frequency.py
Copy path
Top
File metadata and controls
Code
Blame
104 lines (87 loc) · 4.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""
Doppler's effect
The Doppler effect (also Doppler shift) is the change in the frequency of a wave in
relation to an observer who is moving relative to the source of the wave. The Doppler
effect is named after the physicist Christian Doppler. A common example of Doppler
shift is the change of pitch heard when a vehicle sounding a horn approaches and
recedes from an observer.
The reason for the Doppler effect is that when the source of the waves is moving
towards the observer, each successive wave crest is emitted from a position closer to
the observer than the crest of the previous wave. Therefore, each wave takes slightly
less time to reach the observer than the previous wave. Hence, the time between the
arrivals of successive wave crests at the observer is reduced, causing an increase in
the frequency. Similarly, if the source of waves is moving away from the observer,
each wave is emitted from a position farther from the observer than the previous wave,
so the arrival time between successive waves is increased, reducing the frequency.
If the source of waves is stationary but the observer is moving with respect to the
source, the transmission velocity of the waves changes (ie the rate at which the
observer receives waves) even if the wavelength and frequency emitted from the source
remain constant.
These results are all summarized by the Doppler formula:
f = (f0 * (v + v0)) / (v - vs)
where:
f: frequency of the wave
f0: frequency of the wave when the source is stationary
v: velocity of the wave in the medium
v0: velocity of the observer, positive if the observer is moving towards the source
vs: velocity of the source, positive if the source is moving towards the observer
Doppler's effect has many applications in physics and engineering, such as radar,
astronomy, medical imaging, and seismology.
References:
https://en.wikipedia.org/wiki/Doppler_effect
Now, we will implement a function that calculates the frequency of a wave as a function
of the frequency of the wave when the source is stationary, the velocity of the wave
in the medium, the velocity of the observer and the velocity of the source.
"""
def
doppler_effect
(
org_freq
:
float
,
wave_vel
:
float
,
obs_vel
:
float
,
src_vel
:
float
)
->
float
:
"""
Input Parameters:
-----------------
org_freq: frequency of the wave when the source is stationary
wave_vel: velocity of the wave in the medium
obs_vel: velocity of the observer, +ve if the observer is moving towards the source
src_vel: velocity of the source, +ve if the source is moving towards the observer
Returns:
--------
f: frequency of the wave as perceived by the observer
Docstring Tests:
>>> doppler_effect(100, 330, 10, 0) # observer moving towards the source
103.03030303030303
>>> doppler_effect(100, 330, -10, 0) # observer moving away from the source
96.96969696969697
>>> doppler_effect(100, 330, 0, 10) # source moving towards the observer
103.125
>>> doppler_effect(100, 330, 0, -10) # source moving away from the observer
97.05882352941177
>>> doppler_effect(100, 330, 10, 10) # source & observer moving towards each other
106.25
>>> doppler_effect(100, 330, -10, -10) # source and observer moving away
94.11764705882354
>>> doppler_effect(100, 330, 10, 330) # source moving at same speed as the wave
Traceback (most recent call last):
...
ZeroDivisionError: Division by zero implies vs=v and observer in front of the source
>>> doppler_effect(100, 330, 10, 340) # source moving faster than the wave
Traceback (most recent call last):
...
ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction)
>>> doppler_effect(100, 330, -340, 10) # observer moving faster than the wave
Traceback (most recent call last):
...
ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction)
"""
if
wave_vel
==
src_vel
:
raise
ZeroDivisionError
(
"Division by zero implies vs=v and observer in front of the source"
)
doppler_freq
=
(
org_freq
*
(
wave_vel
+
obs_vel
))
/
(
wave_vel
-
src_vel
)
if
doppler_freq
<=
0
:
raise
ValueError
(
"Non-positive frequency implies vs>v or v0>v (in the opposite direction)"
)
return
doppler_freq
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
You can’t perform that action at this time.