Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. def generate_directional_noise(audio,Fs,SNR,H,indicesListForMics):
  2.  
  3. #variables for impulse response
  4. winlen=2048; #window length fo STFT
  5. hoplen=winlen/2; #hop size fo STFT
  6.  
  7. [I,L,C]=H.shape #1024 frequencies,4 blocks,32 channels
  8. [samples,ch]=audio.shape
  9.  
  10. #extract noise power from SNR
  11. p_audio=np.mean(np.sum(audio**2,axis=0)/samples) #Average power for audio
  12. sigma_db=10*np.log10(p_audio)-SNR #Average power of noise with respec to requiered SNR
  13. sigma=np.sqrt(10**(sigma_db/10))
  14. noise=np.random.normal(0,sigma,(samples)) #noise vector
  15.  
  16. #convolve the spectrogram with the impulse response
  17. X_conv = librosa.core.stft(noise, n_fft=winlen, hop_length=hoplen,win_length=winlen, window='hann')
  18.  
  19. frame_len=X_conv.shape[1]#-2
  20. XestLSsyn = np.zeros((I,frame_len,C),dtype=np.complex64)
  21.  
  22. nframe = 0
  23. sta=1
  24. sto=sta + winlen -1
  25. while sto <=samples:
  26. Lind = np.arange(nframe,np.minimum(nframe+L,frame_len))
  27.  
  28. # Apply estimate RIR to close field signal
  29. spatial_audio = np.squeeze(H*np.expand_dims(np.expand_dims(X_conv[:,nframe],axis=1),axis=2))
  30. XestLSsyn[:,Lind,:] = XestLSsyn[:,Lind,:] + spatial_audio[:,0:len(Lind),:]
  31.  
  32. sta = sta + hoplen
  33. sto = sto + hoplen
  34. nframe = nframe + 1
  35.  
  36.  
  37. y=[]
  38.  
  39. #convert back to time domain
  40. for i in range(len(indicesListForMics)):
  41. y.append(librosa.core.istft(XestLSsyn[:,:,indicesListForMics[i]],hop_length=hoplen,win_length=winlen,window='hann'))
  42.  
  43. #set up the format of returned value
  44. y=np.asarray(y).T
  45. tmp=y
  46. y=np.zeros((samples,ch))
  47. length=tmp.shape[0]
  48. y[0:length,:]=tmp
  49. return y
  50.  
  51. fs = 48000
  52. nfft=2048
  53. hop_len_s=0.02
  54. win_len=2*int(hop_len_s*fs)
  55. r_mic = 0.042
  56. mic_number=4
  57. mics=np.array([ [r_mic*np.cos(np.deg2rad(35))*np.cos(np.deg2rad(45)),r_mic*np.cos(np.deg2rad(35))*np.sin(np.deg2rad(45)),r_mic*np.sin(np.deg2rad(35))],
  58. [r_mic*np.cos(np.deg2rad(-35))*np.cos(np.deg2rad(-45)),r_mic*np.cos(np.deg2rad(-35))*np.sin(np.deg2rad(-45)),r_mic*np.sin(np.deg2rad(-35))],
  59. [r_mic*np.cos(np.deg2rad(-35))*np.cos(np.deg2rad(135)),r_mic*np.cos(np.deg2rad(-35))*np.sin(np.deg2rad(135)),r_mic*np.sin(np.deg2rad(-35))],
  60. [r_mic*np.cos(np.deg2rad(35))*np.cos(np.deg2rad(-135)),r_mic*np.cos(np.deg2rad(35))*np.sin(np.deg2rad(-135)),r_mic*np.sin(np.deg2rad(35))]])
  61.  
  62.  
  63. def beamformer(spectrogram,csv_file):
  64. [time_bins,frequencies,nb_chnnels] = spectrogram.shape #input - spectrogram of the noisy audio
  65.  
  66. #generate a vector of frequencies
  67. f_hop=float(fs)/float(nfft)
  68. f=np.expand_dims(np.arange(f_hop/2,fs/2,f_hop),axis=0)
  69. #place holder for output
  70. beamformed_signal=np.zeros((time_bins,frequencies),dtype=np.complex)
  71. #read ground truth
  72. csv=pd.read_csv(csv_file)
  73.  
  74. #for each time frame
  75. for time in range(time_bins):
  76. #find the direction of the source in the current time frame
  77. start_t=time*hop_len_s
  78. end_t=(time*hop_len_s)+(float(win_len)/fs)
  79. row=csv[(csv['start_time']<start_t)&(csv['end_time']>end_t)].to_numpy()
  80. if row.size!=0:
  81. ele=row[0,3]#elevation for current source
  82. azi=row[0,4]#azimuth for current source
  83. else:
  84. ele=0#elevation for current source
  85. azi=0#azimuth for current source
  86.  
  87. # x,y,z coordinates of source directional vector of magnitude 1
  88. x=np.cos(np.deg2rad(ele))*np.cos(np.deg2rad(azi))
  89. y=np.cos(np.deg2rad(ele))*np.sin(np.deg2rad(azi))
  90. z=np.sin(np.deg2rad(ele))
  91. directoion=np.expand_dims(np.array([x,y,z]),axis=1)
  92.  
  93. #project microphone onto the source direction and convert to time domain by deviding by the speed of sound c=343m/s
  94. delays=np.matmul(mics , directoion)/343
  95. #referrance to the first microphone
  96. normalized_delays=delays-delays[0]
  97. #steering vector
  98. steering_vector=np.exp(-2*(1j)*np.pi*f*normalized_delays)/mic_number
  99. #beamformed signal
  100. beamformed_signal[time,:]=np.sum(spectrogram[time,:,:]*steering_vector.T,axis=1)
  101. return beamformed_signal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement