Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.6KB

  1. package dsp
  2. import "math"
  3. // Biquad is a generic second-order IIR filter (direct form II transposed).
  4. type Biquad struct {
  5. b0, b1, b2 float64
  6. a1, a2 float64
  7. z1, z2 float64
  8. }
  9. // Process filters one sample.
  10. func (f *Biquad) Process(in float64) float64 {
  11. out := f.b0*in + f.z1
  12. f.z1 = f.b1*in - f.a1*out + f.z2
  13. f.z2 = f.b2*in - f.a2*out
  14. return out
  15. }
  16. // Reset clears state.
  17. func (f *Biquad) Reset() { f.z1 = 0; f.z2 = 0 }
  18. // FilterChain cascades multiple biquad sections in series.
  19. // Used for higher-order filters (e.g. 4th-order = 2 biquads).
  20. type FilterChain struct {
  21. Stages []Biquad
  22. }
  23. // Process runs input through all stages in series.
  24. func (c *FilterChain) Process(in float64) float64 {
  25. x := in
  26. for i := range c.Stages {
  27. x = c.Stages[i].Process(x)
  28. }
  29. return x
  30. }
  31. // Reset clears all filter state.
  32. func (c *FilterChain) Reset() {
  33. for i := range c.Stages {
  34. c.Stages[i].Reset()
  35. }
  36. }
  37. // --- Factory functions ---
  38. // NewBiquadLPF creates a 2nd-order Butterworth lowpass (Q = 1/√2).
  39. func NewBiquadLPF(cutoffHz, sampleRate float64) *Biquad {
  40. return newBiquadLPFWithQ(cutoffHz, sampleRate, math.Sqrt2/2)
  41. }
  42. func newBiquadLPFWithQ(cutoffHz, sampleRate, q float64) *Biquad {
  43. if cutoffHz <= 0 || sampleRate <= 0 || cutoffHz >= sampleRate/2 {
  44. return &Biquad{b0: 1} // passthrough
  45. }
  46. omega := 2 * math.Pi * cutoffHz / sampleRate
  47. cosW := math.Cos(omega)
  48. sinW := math.Sin(omega)
  49. alpha := sinW / (2 * q)
  50. a0 := 1 + alpha
  51. return &Biquad{
  52. b0: (1 - cosW) / 2 / a0,
  53. b1: (1 - cosW) / a0,
  54. b2: (1 - cosW) / 2 / a0,
  55. a1: (-2 * cosW) / a0,
  56. a2: (1 - alpha) / a0,
  57. }
  58. }
  59. // NewLPF4 creates a 4th-order Butterworth lowpass (two cascaded biquads).
  60. func NewLPF4(cutoffHz, sampleRate float64) *FilterChain {
  61. q1 := 1.0 / (2 * math.Cos(math.Pi/8)) // ≈ 0.5412
  62. q2 := 1.0 / (2 * math.Cos(3*math.Pi/8)) // ≈ 1.3066
  63. return &FilterChain{
  64. Stages: []Biquad{
  65. *newBiquadLPFWithQ(cutoffHz, sampleRate, q1),
  66. *newBiquadLPFWithQ(cutoffHz, sampleRate, q2),
  67. },
  68. }
  69. }
  70. // NewLPF8 creates an 8th-order Butterworth lowpass (four cascaded biquads).
  71. // Provides -48dB/octave rolloff. At 228kHz with fc=15kHz:
  72. //
  73. // 15kHz: -6dB, 19kHz: -28dB, 38kHz: -72dB, 57kHz: -108dB
  74. func NewLPF8(cutoffHz, sampleRate float64) *FilterChain {
  75. // 8th-order Butterworth pole angles: π/16, 3π/16, 5π/16, 7π/16
  76. q1 := 1.0 / (2 * math.Cos(math.Pi/16)) // ≈ 0.5098
  77. q2 := 1.0 / (2 * math.Cos(3*math.Pi/16)) // ≈ 0.6013
  78. q3 := 1.0 / (2 * math.Cos(5*math.Pi/16)) // ≈ 0.8999
  79. q4 := 1.0 / (2 * math.Cos(7*math.Pi/16)) // ≈ 2.5629
  80. return &FilterChain{
  81. Stages: []Biquad{
  82. *newBiquadLPFWithQ(cutoffHz, sampleRate, q1),
  83. *newBiquadLPFWithQ(cutoffHz, sampleRate, q2),
  84. *newBiquadLPFWithQ(cutoffHz, sampleRate, q3),
  85. *newBiquadLPFWithQ(cutoffHz, sampleRate, q4),
  86. },
  87. }
  88. }
  89. // NewNotch creates a 2nd-order IIR notch (bandstop) filter.
  90. // Q controls width: higher Q = narrower notch.
  91. // Typical: Q=5 → ~4kHz wide at -3dB, Q=10 → ~2kHz wide.
  92. func NewNotch(centerHz, sampleRate, q float64) *Biquad {
  93. if centerHz <= 0 || sampleRate <= 0 || centerHz >= sampleRate/2 {
  94. return &Biquad{b0: 1}
  95. }
  96. omega := 2 * math.Pi * centerHz / sampleRate
  97. cosW := math.Cos(omega)
  98. alpha := math.Sin(omega) / (2 * q)
  99. a0 := 1 + alpha
  100. return &Biquad{
  101. b0: 1 / a0,
  102. b1: -2 * cosW / a0,
  103. b2: 1 / a0,
  104. a1: -2 * cosW / a0,
  105. a2: (1 - alpha) / a0,
  106. }
  107. }
  108. // --- Broadcast-specific filter factories ---
  109. // NewAudioLPF creates the broadcast-standard audio lowpass at 14kHz.
  110. // 8th-order Butterworth: -21dB@19kHz per pass. Two passes through the
  111. // clip-filter-clip loop give -42dB broadband floor at 19kHz.
  112. func NewAudioLPF(sampleRate float64) *FilterChain {
  113. return NewLPF8(14000, sampleRate)
  114. }
  115. // NewPilotNotch creates a double-cascade 19kHz notch for maximum
  116. // rejection at the pilot frequency. Two stages give >60dB rejection.
  117. // Applied BEFORE stereo encoding to kill audio energy at 19kHz.
  118. func NewPilotNotch(sampleRate float64) *FilterChain {
  119. return &FilterChain{
  120. Stages: []Biquad{
  121. *NewNotch(19000, sampleRate, 5),
  122. *NewNotch(19000, sampleRate, 5),
  123. },
  124. }
  125. }
  126. // NewCompositeProtection creates double-cascade notch filters for the
  127. // composite clipper. Each band gets two notch stages for >60dB rejection.
  128. // Applied to clipped audio composite to remove clip harmonics from the
  129. // pilot (19kHz) and RDS (57kHz) bands.
  130. func NewCompositeProtection(sampleRate float64) (notch19, notch57 *FilterChain) {
  131. notch19 = &FilterChain{
  132. Stages: []Biquad{
  133. *NewNotch(19000, sampleRate, 3),
  134. *NewNotch(19000, sampleRate, 3),
  135. },
  136. }
  137. notch57 = &FilterChain{
  138. Stages: []Biquad{
  139. *NewNotch(57000, sampleRate, 3),
  140. *NewNotch(57000, sampleRate, 3),
  141. },
  142. }
  143. return
  144. }