Audiofiltering:

Add highpass filter functionality.
This commit is contained in:
ausocean-david 2022-12-15 03:04:26 +10:30
parent 8231379a51
commit fcc7d72b0b
1 changed files with 34 additions and 4 deletions

View File

@ -36,10 +36,10 @@ func main() {
} }
// create filter // create filter
filterLen := 500 filterLen := 25
wg2.Add(1) wg2.Add(1)
ch := make(chan []float64, 1) ch := make(chan []float64, 1)
go LowPass(filterLen, 5000, &wg2, ch) go HighPass(filterLen, 2000, &wg2, ch)
wg1.Wait() wg1.Wait()
// combine audio // combine audio
@ -60,7 +60,7 @@ func main() {
wg2.Add(1) wg2.Add(1)
filteredAudio := Convolve(combinedAudio, filter, &wg2) filteredAudio := Convolve(combinedAudio, filter, &wg2)
wg2.Wait() wg2.Wait()
go SaveAudioData(filteredAudio, "lowpass", &wg1) go SaveAudioData(filteredAudio, "highpass", &wg1)
wg1.Wait() wg1.Wait()
fmt.Println(time.Since(start)) fmt.Println(time.Since(start))
@ -87,6 +87,19 @@ func generate(Frequency float64) []float64 {
} }
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, wg *sync.WaitGroup, ch chan []float64) { func LowPass (n int, fc float64, wg *sync.WaitGroup, ch chan []float64) {
// n is number of points on either side of 0 // n is number of points on either side of 0
@ -98,6 +111,23 @@ func LowPass (n int, fc float64, wg *sync.WaitGroup, ch chan []float64) {
} }
func HighPass (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 := 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))
}
ch <- filter
wg.Done()
}
func Convolve (x, h []float64, wg *sync.WaitGroup) []float64 { func Convolve (x, h []float64, wg *sync.WaitGroup) []float64 {
convLen := len(x)+len(h) convLen := len(x)+len(h)
@ -141,7 +171,7 @@ func SaveAudioData (signal []float64, fileName string, wg1 *sync.WaitGroup) {
file := fileName + ".txt" file := fileName + ".txt"
f, _ := os.Create(file) f, _ := os.Create(file)
for i:=0; i<20000; i++ { for i:=0; i<20000; i++ {
fmt.Fprintf(f, "%v\n", /*10*math.Log10*/(spectrum[i])) fmt.Fprintf(f, "%v\n", /*20*math.Log10*/(spectrum[i]))
} }
fmt.Printf("Saved spectrum values to: %s\n", fileName) fmt.Printf("Saved spectrum values to: %s\n", fileName)
wg1.Done() wg1.Done()