diff --git a/cmd/audiofiltering/main.go b/cmd/audiofiltering/main.go index c4d99939..c8a17d28 100644 --- a/cmd/audiofiltering/main.go +++ b/cmd/audiofiltering/main.go @@ -6,6 +6,7 @@ import( "os" "encoding/binary" "github.com/mjibson/go-dsp/fft" + "github.com/mjibson/go-dsp/window" "math/cmplx" "time" "sync" @@ -36,7 +37,7 @@ func main() { } // create filter - filterLen := 25 + filterLen := 500 wg2.Add(1) ch := make(chan []float64, 1) go HighPass(filterLen, 2000, &wg2, ch) @@ -55,12 +56,12 @@ func main() { filter := <- ch // convolve sinc with audio - wg1.Add(2) - go SaveAudioData(combinedAudio, "unfiltered", &wg1) + wg1.Add(1) + // go SaveAudioData(combinedAudio, "unfiltered", &wg1) wg2.Add(1) filteredAudio := Convolve(combinedAudio, filter, &wg2) wg2.Wait() - go SaveAudioData(filteredAudio, "highpass", &wg1) + go SaveAudioData(filteredAudio, "highpass-2KHz", &wg1) wg1.Wait() fmt.Println(time.Since(start)) @@ -100,29 +101,41 @@ func Max (a []float64) float64 { } -func LowPass (n int, fc float64, wg *sync.WaitGroup, ch chan []float64) { +func LowPass (taps int, fc float64, wg *sync.WaitGroup, ch chan []float64) { - // n is number of points on either side of 0 - // determine digital frequency equivalent for fc - fd := (fc*4)/(2*SampleRate) // create sinc function - ch <- Sinc(n, fd) + size := taps + 1 + fd := fc/SampleRate + b := (2*math.Pi) * fd + filter := make([]float64, size) + winData := window.FlatTop(size) + for n:=0; n<(taps/2); n++ { + c := float64(n) - float64(taps)/2 + y := math.Sin(c*b) / (math.Pi * c) + filter[n] = y * winData[n] + filter[size-1-n] = filter[n] + } + filter[taps/2] = 2*fd*winData[taps/2] + ch <- filter wg.Done() } -func HighPass (n int, fc float64, wg *sync.WaitGroup, ch chan []float64) { +func HighPass (taps int, fc float64, wg *sync.WaitGroup, ch chan []float64) { - // n is number of points on either side of 0 - // determine digital frequency equivalent for fc - fd := 0.5-((2*fc)/(SampleRate)) - fmt.Println(fd) // create sinc function - filter := Sinc(n, fd) - // convert lowpass to highpass filter - for i:=-n; i<=n; i++ { - filter[i+n] = filter[i+n] * math.Pow(-1, float64(i)) + size := taps + 1 + fd := fc/SampleRate + b := (2*math.Pi) * fd + filter := make([]float64, size) + winData := window.FlatTop(size) + for n:=0; n<(taps/2); n++ { + c := float64(n) - float64(taps)/2 + y := math.Sin(c*b) / (math.Pi * c) + filter[n] = -y * winData[n] + filter[size-1-n] = filter[n] } + filter[taps/2] = (1 - 2*fd) * winData[taps/2] ch <- filter wg.Done() @@ -130,20 +143,21 @@ func HighPass (n int, fc float64, wg *sync.WaitGroup, ch chan []float64) { func Convolve (x, h []float64, wg *sync.WaitGroup) []float64 { - convLen := len(x)+len(h) + // conv waitgroup + // var convwg sync.WaitGroup + convLen := len(x)+len(h)-1 y := make([]float64, convLen) for n:=0; n= 0 && n-k < len(h) { - sum += x[k]*h[n-k] + go func(n int, y []float64) { + var sum float64 = 0 + for k:=0; k= 0 && n-k < len(h) { + sum += x[k]*h[n-k] + } } - } y[n] = sum - wg.Done() - }(n, x, h, y, wg) + } (n, y) + } wg.Done() return y @@ -160,6 +174,10 @@ func SaveAudioData (signal []float64, fileName string, wg1 *sync.WaitGroup) { for i := range FFTaudio { spectrum[i] = cmplx.Abs(FFTaudio[i])/float64(length) } + // maximum := Max(signal) + // for i := range signal { + // signal[i] = signal[i]/maximum + // } maximum := Max(spectrum) for i := range spectrum { spectrum[i] = spectrum[i]/maximum @@ -170,8 +188,8 @@ func SaveAudioData (signal []float64, fileName string, wg1 *sync.WaitGroup) { go func (fileName string, length int, spectrum []float64, wg1 *sync.WaitGroup) { file := fileName + ".txt" f, _ := os.Create(file) - for i:=0; i<20000; i++ { - fmt.Fprintf(f, "%v\n", /*20*math.Log10*/(spectrum[i])) + for i:=0; i