Skip to content

Instantly share code, notes, and snippets.

Created February 12, 2016 18:09
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 anonymous/2f5f7d1824c11d2698b1 to your computer and use it in GitHub Desktop.
Save anonymous/2f5f7d1824c11d2698b1 to your computer and use it in GitHub Desktop.
#!/python3
import urllib.request, numpy,io, scipy, scipy.io.wavfile
#info
url='https://losc.ligo.org/s/events/GW150914/P150914/fig1-observed-H.txt'
output='output.wav'
wav_freq=44000
max_wav_volume=32000
new_time_factor=3. #the audio will be stretched of a factor of new_time_factor
# read the data
print('downloading ',url)
with urllib.request.urlopen(url) as response:
file_content=response.read().decode()
table = numpy.loadtxt(io.StringIO(file_content))
#get first and second columns:
timing=table.T[0]
measure=table.T[1]
duration=timing[-1]-timing[0]
len_dataset=len(timing)
print(' dataset len=',len_dataset, ' duration=',duration,'[s], max,min values=',numpy.max(measure),numpy.min(measure))
#normalize the measure from -max_wav_volume to +max_wav_volume, and make it an array of integers
wav_data=numpy.array((max_wav_volume*measure/numpy.max(numpy.abs(measure))).astype(int))
#stretch the data with numpy linear interpolation
N=new_time_factor*duration*wav_freq/len_dataset
new_len=int(len_dataset*N)
x=numpy.arange(len_dataset)
xs=numpy.arange(new_len)/N
wav_data_interpol = numpy.interp(xs,x,wav_data)
print(' new dataset len=',new_len,', new duration=',new_time_factor*duration,'[s], max,min values=',numpy.max(wav_data_interpol ),numpy.min(wav_data_interpol ))
#write it
scipy.io.wavfile.write(output,wav_freq,numpy.array(wav_data_interpol,dtype=numpy.int16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment