-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
added a function to calculate perceived frequency by observer using Doppler Effect #10776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 40 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
1427fe3
avg and mps speed formulae added
Baron105 0608c40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7457b25
avg and mps speed formulae added
Baron105 b1ef5a3
fixed_spacing
Baron105 c957bd7
.
Baron105 07502c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ead4f47
spacing
Baron105 c18b39d
.
Baron105 cb006c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ab04734
ws
Baron105 69487f3
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 6a741dc
added amicable numbers
Baron105 d13c09d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 18df9ba
Merge branch 'TheAlgorithms:master' into master
Baron105 63734b9
added amicable numbers
Baron105 aa5f176
descriptive
Baron105 9779c1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aa4d4e8
spacing
Baron105 7bb663f
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 52c2cb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 643958b
removed
Baron105 65f5851
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 7f14ab9
Merge branch 'TheAlgorithms:master' into master
Baron105 c05ed7c
changed name of file and added code improvements
Baron105 886fb5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 97b89cd
issues fixed due to pi
Baron105 a1c783a
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 1c2b7b2
requested changes added
Baron105 5aa65b8
Merge branch 'TheAlgorithms:master' into master
Baron105 219d973
Merge branch 'TheAlgorithms:master' into doppler
Baron105 2052af0
Created doppler_effect_of_sound.py
Baron105 efe1055
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 03e3c0c
Updated doppler_effect_of_sound.py
Baron105 664095f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 728a378
added desc names
Baron105 a56688b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 006761f
fixed spacing
Baron105 ba5a96b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2a01a65
Merge branch 'TheAlgorithms:master' into doppler
Baron105 620b03f
renamed doppler_effect_of_sound.py to doppler_frequency.py
Baron105 8947a2a
used expection handling rather than print statements
Baron105 72988ae
fixed spacing for ruff
Baron105 36909a1
Update doppler_frequency.py
cclauss 7a01a8a
Update perfect_number.py
cclauss f29ded9
Rename perceptron.py to perceptron.py.DISABLED
cclauss 3e5172e
Merge branch 'master' into doppler
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
""" | ||
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. | ||
|
||
Now, 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. | ||
|
||
All these results are 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) #observer and source moving towards each other | ||
106.25 | ||
>>> doppler_effect(100, 330, -10, -10) #observer and source moving away | ||
94.11764705882354 | ||
>>> doppler_effect(100, 330, 10, 330) #source moving at the same speed as the wave | ||
Error: Division by zero | ||
Infinite frequency implies vs = v and observer infront of the source | ||
0.0 | ||
>>> doppler_effect(100, 330, 10, 340) #source moving faster than the wave | ||
Error: Non-positive frequency | ||
Non-positive frequency implies vs > v or v0 > v(in opposite direction) | ||
0.0 | ||
>>> doppler_effect(100, 330, -340, 10) #observer moving faster than the wave | ||
Error: Non-positive frequency | ||
Non-positive frequency implies vs > v or v0 > v(in opposite direction) | ||
0.0 | ||
""" | ||
|
||
try: | ||
doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel) | ||
except ZeroDivisionError: | ||
print("Error: Division by zero") | ||
print("Infinite frequency implies vs = v and observer infront of the source") | ||
return 0.0 | ||
if doppler_freq <= 0: | ||
print("Error: Non-positive frequency") | ||
print("Non-positive frequency implies vs > v or v0 > v(in opposite direction)") | ||
return 0.0 | ||
return doppler_freq | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.