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 sour
(
org_freq: float, wave_vel: float, obs_vel: float, src_vel: float
)
| 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__": |
nothing calls this directly
no outgoing calls
no test coverage detected