def main() : import numpy import math import sys, os import RandomArray sys.path.append(os.path.join(os.path.dirname(__file__),"../../../src/libs/python")) import bmaudio # Simulation of random error speakerDistances = numpy.random.normal(loc=0,scale=standardDelay,size=nSpeakers) simulatedSpectrum = numpy.array( [ sum( complex(math.cos(w*delay), math.sin(w*delay)) for delay in speakerDistances ) for w in ( math.pi*2*wavenumber*spectralRange/nBins for wavenumber in xrange(nBins) ) ]) simulatedSpectrum/=nSpeakers simulatedInverse = 1/simulatedSpectrum simulatedInverseAudio = numpy.fft.irfft(simulatedInverse) # Analytical computation computedSpectrum = numpy.array([ math.exp(-(standardDelay*w)**2/2) for w in (2*math.pi*wavenumber*spectralRange/nBins for wavenumber in xrange(nBins) ) ]) #computedSpectrum[abs(computedSpectrum)<.5e-2]=.5e-2 computedInverseAudio = numpy.fft.irfft(1/computedSpectrum) bmaudio.saveWave("lala.wav",computedInverseAudio,44100) fixedSpectrum = numpy.array([ math.sqrt(w)/10000+math.exp(-(standardDelay*w)**2/2) for w in (2*math.pi*wavenumber*spectralRange/nBins for wavenumber in xrange(nBins) ) ]) import Gnuplot gp=Gnuplot.Gnuplot(persist=1) gp('set data style lines') gp("set log y ") gp.plot( numpy.zeros(nBins), abs(simulatedSpectrum), abs(computedSpectrum)[abs(computedSpectrum)>1e-4], abs(fixedSpectrum), abs(1/simulatedSpectrum), abs(1/computedSpectrum)[abs(computedSpectrum)>1e-4], abs(1/fixedSpectrum), # [bin.real for bin in spectrum], # [bin.imag for bin in spectrum], ) gp.hardcopy(filename="IncoherenceSimulation.png",terminal="png")
gp = bmaudio.SpectrumDisplay() filter = abs(numpy.fft.irfft(1/exponential))[:nBins] gp.inDb() for spectrum, name in [ (numpy.zeros(nBins), ""), (abs(Ew), "Ew"), (abs(Ex), "Ex"), (abs(Ey), "Ey"), (abs(Ez), "Ez"), (abs(Ew)/exponential, "Fixed Ew"), (abs(Ex)/exponential, "Fixed Ex"), (abs(Ey)/exponential, "Fixed Ey"), (abs(Ez)/exponential, "Fixed Ez"), (Iwsum, "Iwsum"), (abs(Ew)/Iwsum, "Ew/Iwsum"), # The ratio # [bin.real for bin in spectrum], # [bin.imag for bin in spectrum], (exponential, "Fix filter"), ] : gp.addSpectrumData(spectrum, 256, name) gp.show() gp.hardcopy("IncoherenceSimulation.png","png") print filter/max(filter) bmaudio.saveWave("coherenceFilter.wav", filter/max(filter), 44100) #bmaudio.saveWave("lala.wav",computedInverseAudio,44100)
print len(azimuths) print elevations print azimuths for matfile in files : matdata = scipy.io.loadmat(matfile) subject = re.findall('[0-9]+',matdata['name'][0])[0] # has the form subject_000 os.system('mkdir -p "CIPIC/subject_%s"' % subject) for channel in ['l','r'] : database=open("cipic%s%s.hrtfs"%(subject,channel.upper()),'w') hrirs = matdata['hrir_'+channel] normFactor = max(abs(hrirs.reshape(hrirs.size))) hrirs /= normFactor for azimuthIndex, azimuthData in enumerate(hrirs) : for elevationIndex, hrir in enumerate(azimuthData) : azimuth = azimuths[azimuthIndex] elevation = elevations[elevationIndex] print "Subject", subject, "original", elevation, azimuth, "->", if elevation > 90 : azimuth = 180 - azimuth elevation = 180 - elevation if channel == 'l' : azimuth = (360 - azimuth)%360 print elevation, azimuth wavefile = 'CIPIC/subject_%s/%s_e%+03.1f_a%+03i.wav'%(subject,channel.upper(),elevation,azimuth) print >> database, elevation, azimuth, wavefile bmaudio.saveWave(wavefile, hrir, 44100)