Audiofiltering:

Increase the efficieny and stability of algorithms by making use of waitgroups and goroutines.
This commit is contained in:
ausocean-david 2022-12-15 00:42:44 +10:30
parent 80ab5e4768
commit 8231379a51
1 changed files with 70 additions and 115 deletions

View File

@ -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<n; i++ {
// audio[i] = generate(float64((i)*(22000/n)))
// }
audio[0] = generate(2000)
audio[1] = generate(10000)
for i:=0; i<n; i++ {
go func(i int, audio [][]float64, wg *sync.WaitGroup) {
audio[i] = generate(float64((i)*(22000/n)))
wg.Done()
} (i, audio, &wg1)
}
// create filter
filterLen := 500
wg2.Add(1)
ch := make(chan []float64, 1)
go LowPass(filterLen, 5000, &wg2, ch)
wg1.Wait()
// combine audio
combinedAudio := make([]float64, length)
for i := range audio[0] {
@ -41,25 +50,19 @@ func main() {
combinedAudio[i] += audio[j][i]
}
}
fmt.Println("audio generation:", time.Since(start))
start = time.Now()
filterLen := 500
filter := LowPass(filterLen, 20000)
fmt.Println("audio filter generation:", time.Since(start))
start = time.Now()
wg2.Wait()
filter := <- ch
// convolve sinc with audio
filteredAudio := Convolve(combinedAudio, filter)
fmt.Println("convolution:", time.Since(start))
start = time.Now()
SaveAudioData(filteredAudio, "lowpass")
SaveAudioData(combinedAudio, "unfiltered")
fmt.Println("audio saving:", time.Since(start))
start = time.Now()
// SaveAudioData(filter, "test")
wg1.Add(2)
go SaveAudioData(combinedAudio, "unfiltered", &wg1)
wg2.Add(1)
filteredAudio := Convolve(combinedAudio, filter, &wg2)
wg2.Wait()
go SaveAudioData(filteredAudio, "lowpass", &wg1)
wg1.Wait()
fmt.Println(time.Since(start))
}
@ -77,93 +80,31 @@ func generate(Frequency float64) []float64 {
// generate samples and write to file
for i := 0; i < int(nsamps); i++ {
samp[i] = math.Sin(angle * Frequency * float64(i))
samp[i] = math.Sin(angle * Frequency * float64(4*i))
}
return samp
}
func TopFive (a []float64) (topVal []float64, topI []int) {
length := 5
runMax := make([]float64, length, length)
indices := make([]int, length, length)
for i:=range a {
switch {
case a[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<convLen; n++ {
go SubConvolve(n, x, h, y)
}
return y
}
func SubConvolve (n int, x, h, y []float64) {
wg.Add(1)
go func (n int, x, h, y []float64, wg *sync.WaitGroup) {
var sum float64 = 0
for k:=0; k<len(x); k++ {
if n-k >= 0 && n-k < len(h) {
@ -171,10 +112,15 @@ func SubConvolve (n int, x, h, y []float64) {
}
}
y[n] = sum
wg.Done()
}(n, x, h, y, wg)
}
wg.Done()
return y
}
func SaveAudioData (signal []float64, fileName string) {
func SaveAudioData (signal []float64, fileName string, wg1 *sync.WaitGroup) {
// 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
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)
file = fileName + ".bin"
f, _ = os.Create(file)
go func (fileName string, signal []float64, wg1 *sync.WaitGroup) {
file := fileName + ".bin"
f, _ := os.Create(file)
var buf [8]byte
for i:=0; i<length; i++ {
binary.LittleEndian.PutUint32(buf[:], math.Float32bits(float32(signal[i])))
_, _ = f.Write(buf[:])
}
fmt.Printf("Saved binary values to: %s\n", fileName)
wg1.Done()
}(fileName, signal, wg1)
wg1.Done()
}