From 8231379a5148989f26b8c5fb8e8f43610557af4b Mon Sep 17 00:00:00 2001 From: ausocean-david Date: Thu, 15 Dec 2022 00:42:44 +1030 Subject: [PATCH] Audiofiltering: Increase the efficieny and stability of algorithms by making use of waitgroups and goroutines. --- cmd/audiofiltering/main.go | 185 ++++++++++++++----------------------- 1 file changed, 70 insertions(+), 115 deletions(-) diff --git a/cmd/audiofiltering/main.go b/cmd/audiofiltering/main.go index 5fec888e..8a66ed22 100644 --- a/cmd/audiofiltering/main.go +++ b/cmd/audiofiltering/main.go @@ -6,9 +6,9 @@ import( "os" "encoding/binary" "github.com/mjibson/go-dsp/fft" - // "github.com/mjibson/go-dsp/window" "math/cmplx" "time" + "sync" ) // define constants used in the generation of the sound waves @@ -21,18 +21,27 @@ const( func main() { + var wg1, wg2 sync.WaitGroup start := time.Now() - // generate two sine waves with different frequencies to test frequency response - n := 2 + // generate sine waves with different frequencies to test frequency response + n := 100 + wg1.Add(n) audio := make([][]float64, n) - // for i:=0; i runMax[0]: - for i:=0; i<4; i++ { - runMax[4-i] = runMax[4-(1+i)] - indices[4-i] = indices[4-(1+i)] - } - runMax[0] = a[i] - indices[0] = i - case a[i] > runMax[1]: - for i:=0; i<3; i++ { - runMax[4-i] = runMax[4-(1+i)] - indices[4-i] = indices[4-(1+i)] - } - runMax[1] = a[i] - indices[1] = i - case a[i] > runMax[2]: - for i:=0; i<2; i++ { - runMax[4-i] = runMax[4-(1+i)] - indices[4-i] = indices[4-(1+i)] - } - runMax[2] = a[i] - indices[2] = i - case a[i] > runMax[3]: - for i:=0; i<1; i++ { - runMax[4-i] = runMax[4-(1+i)] - indices[4-i] = indices[4-(1+i)] - } - runMax[3] = a[i] - indices[3] = i - } - } - - return runMax, indices - -} - -func Max (a []float64) float64 { - - var runMax float64 = -1 - for i:= range a { - if math.Abs(a[i]) > runMax { - runMax = math.Abs(a[i]) - } - } - - return runMax - -} - -func LowPass (n int, fc float64) (filter []float64) { +func LowPass (n 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/(2*SampleRate) + fd := (fc*4)/(2*SampleRate) // create sinc function - return Sinc(n, fd) - + ch <- Sinc(n, fd) + wg.Done() } -func Convolve (x, h []float64) []float64 { +func Convolve (x, h []float64, wg *sync.WaitGroup) []float64 { convLen := len(x)+len(h) y := make([]float64, convLen) for n:=0; n= 0 && n-k < len(h) { + sum += x[k]*h[n-k] + } + } + y[n] = sum + wg.Done() + }(n, x, h, y, wg) } - + wg.Done() return y } -func SubConvolve (n int, x, h, y []float64) { - var sum float64 = 0 - for k:=0; k= 0 && n-k < len(h) { - sum += x[k]*h[n-k] - } - } - y[n] = sum -} +func SaveAudioData (signal []float64, fileName string, wg1 *sync.WaitGroup) { - -func SaveAudioData (signal []float64, fileName string) { - // compute fft of signal FFTaudio := fft.FFTReal(signal) @@ -188,23 +134,32 @@ func SaveAudioData (signal []float64, fileName string) { for i := range spectrum { spectrum[i] = spectrum[i]/maximum } - // spectrumAlt := spectrum[0:length/2 + 1] // SAVE - file := fileName + ".txt" - f, _ := os.Create(file) - for i:=0; i<20000; i++ { - fmt.Fprintf(f, "%v\n", /*10*math.Log10*/(spectrum[i])) - } - fmt.Printf("Saved spectrum values to: %s\n", fileName) + wg1.Add(2) + 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", /*10*math.Log10*/(spectrum[i])) + } + fmt.Printf("Saved spectrum values to: %s\n", fileName) + wg1.Done() + }(fileName, 44100, spectrum, wg1) + + go func (fileName string, signal []float64, wg1 *sync.WaitGroup) { + file := fileName + ".bin" + f, _ := os.Create(file) + var buf [8]byte + for i:=0; i