diff --git a/cmd/audiofiltering/main.go b/cmd/audiofiltering/main.go index 8a66ed22..c4d99939 100644 --- a/cmd/audiofiltering/main.go +++ b/cmd/audiofiltering/main.go @@ -36,10 +36,10 @@ func main() { } // create filter - filterLen := 500 + filterLen := 25 wg2.Add(1) ch := make(chan []float64, 1) - go LowPass(filterLen, 5000, &wg2, ch) + go HighPass(filterLen, 2000, &wg2, ch) wg1.Wait() // combine audio @@ -60,7 +60,7 @@ func main() { wg2.Add(1) filteredAudio := Convolve(combinedAudio, filter, &wg2) wg2.Wait() - go SaveAudioData(filteredAudio, "lowpass", &wg1) + go SaveAudioData(filteredAudio, "highpass", &wg1) wg1.Wait() 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) { // 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 { convLen := len(x)+len(h) @@ -141,7 +171,7 @@ func SaveAudioData (signal []float64, fileName string, 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.Fprintf(f, "%v\n", /*20*math.Log10*/(spectrum[i])) } fmt.Printf("Saved spectrum values to: %s\n", fileName) wg1.Done()