Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // len(f) == len(g) == len(p)
- // f is frequency, exponentially spaced
- // g is gain, in dB
- // p is phase, between -1 and +1
- func createImpulseResponse(f []float64, g []float64, p []float64) {
- sampleRate := 48000
- filterLength := 16384
- filterLength2 := filterLength * 2
- iScalar := float64(sampleRate) / float64(filterLength2)
- freqDomain := make([]complex128, filterLength2)
- magnitudeScalar := 1.0
- for i := 1; i < filterLength; i++ {
- freq := float64(i) * iScalar
- if freq < 20 || freq > 22000 {
- freqDomain[i] = 0 + 0i
- continue
- }
- phase := logInterp(f, p, freq)
- gainDB := logInterp(f, g, freq)
- freqDomain[i] = complex(math.Pow(10.0, gainDB/20.0)*magnitudeScalar, phase)
- freqDomain[filterLength2-i-1] = complex(math.Pow(10.0, gainDB/20.0)*magnitudeScalar, -phase)
- }
- freqDomain[0] = complex(0*magnitudeScalar, 0)
- timeDomain := fft.IFFT(freqDomain)
- }
- func logInterp(f []float64, g []float64, freq float64) float64 {
- if freq < f[0] {
- return 0
- }
- i := 1
- for ; i < len(f); i++ {
- if freq >= f[i-1] && freq < f[i] {
- break
- }
- }
- logLeft := math.Log(f[i-1])
- if i == len(f) {
- return 0
- }
- logRightMinusLeft := math.Log(f[i]) - logLeft
- gainRight := g[i]
- t := (math.Log(freq) - logLeft) / logRightMinusLeft
- return g[i-1] + t*(gainRight-g[i-1])
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement