Skip to content

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 46 commits into from
Oct 21, 2023
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 Oct 10, 2023
0608c40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
7457b25
avg and mps speed formulae added
Baron105 Oct 10, 2023
b1ef5a3
fixed_spacing
Baron105 Oct 10, 2023
c957bd7
.
Baron105 Oct 10, 2023
07502c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ead4f47
spacing
Baron105 Oct 10, 2023
c18b39d
.
Baron105 Oct 10, 2023
cb006c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ab04734
ws
Baron105 Oct 10, 2023
69487f3
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
6a741dc
added amicable numbers
Baron105 Oct 10, 2023
d13c09d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
18df9ba
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 10, 2023
63734b9
added amicable numbers
Baron105 Oct 10, 2023
aa5f176
descriptive
Baron105 Oct 10, 2023
9779c1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
aa4d4e8
spacing
Baron105 Oct 10, 2023
7bb663f
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
52c2cb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
643958b
removed
Baron105 Oct 10, 2023
65f5851
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
7f14ab9
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 12, 2023
c05ed7c
changed name of file and added code improvements
Baron105 Oct 12, 2023
886fb5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 12, 2023
97b89cd
issues fixed due to pi
Baron105 Oct 12, 2023
a1c783a
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 12, 2023
1c2b7b2
requested changes added
Baron105 Oct 12, 2023
5aa65b8
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 13, 2023
219d973
Merge branch 'TheAlgorithms:master' into doppler
Baron105 Oct 14, 2023
2052af0
Created doppler_effect_of_sound.py
Baron105 Oct 14, 2023
efe1055
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2023
03e3c0c
Updated doppler_effect_of_sound.py
Baron105 Oct 14, 2023
664095f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2023
728a378
added desc names
Baron105 Oct 14, 2023
a56688b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2023
006761f
fixed spacing
Baron105 Oct 14, 2023
ba5a96b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2023
2a01a65
Merge branch 'TheAlgorithms:master' into doppler
Baron105 Oct 21, 2023
620b03f
renamed doppler_effect_of_sound.py to doppler_frequency.py
Baron105 Oct 21, 2023
8947a2a
used expection handling rather than print statements
Baron105 Oct 21, 2023
72988ae
fixed spacing for ruff
Baron105 Oct 21, 2023
36909a1
Update doppler_frequency.py
cclauss Oct 21, 2023
7a01a8a
Update perfect_number.py
cclauss Oct 21, 2023
f29ded9
Rename perceptron.py to perceptron.py.DISABLED
cclauss Oct 21, 2023
3e5172e
Merge branch 'master' into doppler
cclauss Oct 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions physics/doppler_frequency.py
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()