Skip to content

Instantly share code, notes, and snippets.

@unalfaruk
Created September 14, 2022 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unalfaruk/457c49f61bff4855e2df793f051391d8 to your computer and use it in GitHub Desktop.
Save unalfaruk/457c49f61bff4855e2df793f051391d8 to your computer and use it in GitHub Desktop.
It's a simple python script that calculates the doppler shift from the velocity, and generates a sound wave for the observer...
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 9 13:43:13 2022
@author: faruk.unal
"""
import scipy.constants as sc
import numpy as np
from scipy import signal
from scipy.fft import fft,fftshift,fftfreq
from matplotlib import mlab
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from scipy.io.wavfile import write
def generate_signal(freq_signal,duration_signal,freq_sample):
timeline = np.linspace(0,duration_signal,freq_sample*duration_signal,endpoint=False)
signal = np.sin(2*np.pi*freq_signal*timeline)
return timeline,signal
class Car:
def __init__(self, speed, beacon_freq):
self.speed = speed
self.beacon_freq = beacon_freq
self.freq_sample = beacon_freq*16
def beaconOn(self,duration):
self.duration = duration
print("Beacon freq= %f\n" % self.beacon_freq)
self.timeline, self.signal = generate_signal(self.beacon_freq,self.duration,self.freq_sample)
def howDoesHear(self,name):
normalized_tone = np.int16((self.signal / self.signal.max()) * 32767)
write(name+".wav", self.freq_sample, normalized_tone)
class Observer:
def __init__(self, speed, observedObject):
self.observedObject = observedObject
self.speed = speed
def calculateObservedSignal(self,source):
freq_observed = source.beacon_freq*(sc.speed_of_sound+self.speed)/(sc.speed_of_sound-source.speed)
return freq_observed
def howDoesHear(self,name):
self.freqObservedSignal = self.calculateObservedSignal(self.observedObject)
print("Observer freq= %f\n" % self.freqObservedSignal)
self.timeline, self.signal = generate_signal(self.freqObservedSignal,self.observedObject.duration,self.observedObject.freq_sample)
normalized_tone = np.int16((self.signal / self.signal.max()) * 32767)
write(name+".wav", self.observedObject.freq_sample, normalized_tone)
beacon_freq = 500 #Hz
speed = 35 #m/s
police_car = Car(speed, beacon_freq)
police_car.beaconOn(5)
police_car.howDoesHear("policeCar_sound")
observer = Observer(0,police_car)
observer.howDoesHear("observer_sound")
dopp = police_car.beacon_freq-observer.freqObservedSignal
N = police_car.freq_sample * police_car.duration
yf_police = fft(police_car.signal)
xf = fftfreq(N, 1 / police_car.freq_sample)
plt.plot(xf, np.abs(yf_police))
yf_observer = fft(observer.signal)
plt.plot(xf, np.abs(yf_observer))
plt.legend(["Emmited","Observed"])
plt.xlabel("Frequency (Hz)")
maxLim=max(plt.xlim())
plt.xlim([0,police_car.beacon_freq+abs(dopp)*2])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment