Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

73 lignes
1.7KB

  1. package dsp
  2. import (
  3. "math"
  4. "testing"
  5. )
  6. func TestPreEmphasisBoostsHighFrequency(t *testing.T) {
  7. fs := 48000.0
  8. pe := NewPreEmphasis(50, fs)
  9. // Generate a low-frequency tone and a high-frequency tone,
  10. // measure the energy ratio after pre-emphasis.
  11. n := 4800 // 100ms
  12. lowFreq := 100.0
  13. highFreq := 10000.0
  14. var lowEnergy, highEnergy float64
  15. for i := 0; i < n; i++ {
  16. s := math.Sin(2 * math.Pi * lowFreq * float64(i) / fs)
  17. out := pe.Process(s)
  18. lowEnergy += out * out
  19. }
  20. pe.Reset()
  21. for i := 0; i < n; i++ {
  22. s := math.Sin(2 * math.Pi * highFreq * float64(i) / fs)
  23. out := pe.Process(s)
  24. highEnergy += out * out
  25. }
  26. // High frequency should have more energy than low frequency
  27. // after pre-emphasis. The input energy is the same for both.
  28. ratio := highEnergy / lowEnergy
  29. if ratio < 2.0 {
  30. t.Fatalf("expected high-freq boost, energy ratio=%.2f", ratio)
  31. }
  32. }
  33. func TestPreEmphasisDeEmphasisRoundtrip(t *testing.T) {
  34. fs := 48000.0
  35. pe := NewPreEmphasis(50, fs)
  36. de := NewDeEmphasis(50, fs)
  37. // Run a 1kHz tone through pre+de, should approximately recover
  38. n := 4800
  39. freq := 1000.0
  40. var maxErr float64
  41. // Let filters settle for 200 samples
  42. for i := 0; i < 200; i++ {
  43. s := math.Sin(2 * math.Pi * freq * float64(i) / fs)
  44. de.Process(pe.Process(s))
  45. }
  46. for i := 200; i < n; i++ {
  47. s := math.Sin(2 * math.Pi * freq * float64(i) / fs)
  48. recovered := de.Process(pe.Process(s))
  49. err := math.Abs(recovered - s)
  50. if err > maxErr {
  51. maxErr = err
  52. }
  53. }
  54. if maxErr > 0.05 {
  55. t.Fatalf("roundtrip error too large: %.4f", maxErr)
  56. }
  57. }
  58. func TestDisabledPreEmphasis(t *testing.T) {
  59. pe := NewPreEmphasis(0, 48000)
  60. if pe.Process(0.5) != 0.5 {
  61. t.Fatal("disabled filter should pass through")
  62. }
  63. }