|
| 1 | +""" |
| 2 | +Doppler's effect |
| 3 | +
|
| 4 | +The Doppler effect (also Doppler shift) is the change in the frequency of a wave in |
| 5 | +relation to an observer who is moving relative to the source of the wave. The Doppler |
| 6 | +effect is named after the physicist Christian Doppler. A common example of Doppler |
| 7 | +shift is the change of pitch heard when a vehicle sounding a horn approaches and |
| 8 | +recedes from an observer. |
| 9 | +
|
| 10 | +The reason for the Doppler effect is that when the source of the waves is moving |
| 11 | +towards the observer, each successive wave crest is emitted from a position closer to |
| 12 | +the observer than the crest of the previous wave. Therefore, each wave takes slightly |
| 13 | +less time to reach the observer than the previous wave. Hence, the time between the |
| 14 | +arrivals of successive wave crests at the observer is reduced, causing an increase in |
| 15 | +the frequency. Similarly, if the source of waves is moving away from the observer, |
| 16 | +each wave is emitted from a position farther from the observer than the previous wave, |
| 17 | +so the arrival time between successive waves is increased, reducing the frequency. |
| 18 | +
|
| 19 | +If the source of waves is stationary but the observer is moving with respect to the |
| 20 | +source, the transmission velocity of the waves changes (ie the rate at which the |
| 21 | +observer receives waves) even if the wavelength and frequency emitted from the source |
| 22 | +remain constant. |
| 23 | +
|
| 24 | +These results are all summarized by the Doppler formula: |
| 25 | +
|
| 26 | + f = (f0 * (v + v0)) / (v - vs) |
| 27 | +
|
| 28 | +where: |
| 29 | + f: frequency of the wave |
| 30 | + f0: frequency of the wave when the source is stationary |
| 31 | + v: velocity of the wave in the medium |
| 32 | + v0: velocity of the observer, positive if the observer is moving towards the source |
| 33 | + vs: velocity of the source, positive if the source is moving towards the observer |
| 34 | +
|
| 35 | +Doppler's effect has many applications in physics and engineering, such as radar, |
| 36 | +astronomy, medical imaging, and seismology. |
| 37 | +
|
| 38 | +References: |
| 39 | +https://en.wikipedia.org/wiki/Doppler_effect |
| 40 | +
|
| 41 | +Now, we will implement a function that calculates the frequency of a wave as a function |
| 42 | +of the frequency of the wave when the source is stationary, the velocity of the wave |
| 43 | +in the medium, the velocity of the observer and the velocity of the source. |
| 44 | +""" |
| 45 | + |
| 46 | + |
| 47 | +def doppler_effect( |
| 48 | + org_freq: float, wave_vel: float, obs_vel: float, src_vel: float |
| 49 | +) -> float: |
| 50 | + """ |
| 51 | + Input Parameters: |
| 52 | + ----------------- |
| 53 | + org_freq: frequency of the wave when the source is stationary |
| 54 | + wave_vel: velocity of the wave in the medium |
| 55 | + obs_vel: velocity of the observer, +ve if the observer is moving towards the source |
| 56 | + src_vel: velocity of the source, +ve if the source is moving towards the observer |
| 57 | +
|
| 58 | + Returns: |
| 59 | + -------- |
| 60 | + f: frequency of the wave as perceived by the observer |
| 61 | +
|
| 62 | + Docstring Tests: |
| 63 | + >>> doppler_effect(100, 330, 10, 0) # observer moving towards the source |
| 64 | + 103.03030303030303 |
| 65 | + >>> doppler_effect(100, 330, -10, 0) # observer moving away from the source |
| 66 | + 96.96969696969697 |
| 67 | + >>> doppler_effect(100, 330, 0, 10) # source moving towards the observer |
| 68 | + 103.125 |
| 69 | + >>> doppler_effect(100, 330, 0, -10) # source moving away from the observer |
| 70 | + 97.05882352941177 |
| 71 | + >>> doppler_effect(100, 330, 10, 10) # source & observer moving towards each other |
| 72 | + 106.25 |
| 73 | + >>> doppler_effect(100, 330, -10, -10) # source and observer moving away |
| 74 | + 94.11764705882354 |
| 75 | + >>> doppler_effect(100, 330, 10, 330) # source moving at same speed as the wave |
| 76 | + Traceback (most recent call last): |
| 77 | + ... |
| 78 | + ZeroDivisionError: Division by zero implies vs=v and observer in front of the source |
| 79 | + >>> doppler_effect(100, 330, 10, 340) # source moving faster than the wave |
| 80 | + Traceback (most recent call last): |
| 81 | + ... |
| 82 | + ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction) |
| 83 | + >>> doppler_effect(100, 330, -340, 10) # observer moving faster than the wave |
| 84 | + Traceback (most recent call last): |
| 85 | + ... |
| 86 | + ValueError: Non-positive frequency implies vs>v or v0>v (in the opposite direction) |
| 87 | + """ |
| 88 | + |
| 89 | + if wave_vel == src_vel: |
| 90 | + raise ZeroDivisionError( |
| 91 | + "Division by zero implies vs=v and observer in front of the source" |
| 92 | + ) |
| 93 | + doppler_freq = (org_freq * (wave_vel + obs_vel)) / (wave_vel - src_vel) |
| 94 | + if doppler_freq <= 0: |
| 95 | + raise ValueError( |
| 96 | + "Non-positive frequency implies vs>v or v0>v (in the opposite direction)" |
| 97 | + ) |
| 98 | + return doppler_freq |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + import doctest |
| 103 | + |
| 104 | + doctest.testmod() |
0 commit comments