Advertisement
Guest User

IFFT

a guest
Jul 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.31 KB | None | 0 0
  1. // len(f) == len(g) == len(p)
  2. // f is frequency, exponentially spaced
  3. // g is gain, in dB
  4. // p is phase, between -1 and +1
  5. func createImpulseResponse(f []float64, g []float64, p []float64) {
  6.     sampleRate := 48000
  7.     filterLength := 16384
  8.     filterLength2 := filterLength * 2
  9.  
  10.     iScalar := float64(sampleRate) / float64(filterLength2)
  11.  
  12.     freqDomain := make([]complex128, filterLength2)
  13.  
  14.     magnitudeScalar := 1.0
  15.  
  16.     for i := 1; i < filterLength; i++ {
  17.         freq := float64(i) * iScalar
  18.         if freq < 20 || freq > 22000 {
  19.             freqDomain[i] = 0 + 0i
  20.             continue
  21.         }
  22.  
  23.         phase := logInterp(f, p, freq)
  24.         gainDB := logInterp(f, g, freq)
  25.  
  26.         freqDomain[i] = complex(math.Pow(10.0, gainDB/20.0)*magnitudeScalar, phase)
  27.         freqDomain[filterLength2-i-1] = complex(math.Pow(10.0, gainDB/20.0)*magnitudeScalar, -phase)
  28.     }
  29.     freqDomain[0] = complex(0*magnitudeScalar, 0)
  30.  
  31.     timeDomain := fft.IFFT(freqDomain)
  32. }
  33.  
  34. func logInterp(f []float64, g []float64, freq float64) float64 {
  35.     if freq < f[0] {
  36.         return 0
  37.     }
  38.  
  39.     i := 1
  40.     for ; i < len(f); i++ {
  41.         if freq >= f[i-1] && freq < f[i] {
  42.             break
  43.         }
  44.     }
  45.     logLeft := math.Log(f[i-1])
  46.  
  47.     if i == len(f) {
  48.         return 0
  49.     }
  50.  
  51.     logRightMinusLeft := math.Log(f[i]) - logLeft
  52.     gainRight := g[i]
  53.  
  54.     t := (math.Log(freq) - logLeft) / logRightMinusLeft
  55.     return g[i-1] + t*(gainRight-g[i-1])
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement