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.

59 lignes
1.6KB

  1. package dsp
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. "github.com/jan/fm-rds-tx/internal/output"
  7. )
  8. func TestResampleIQIdentity(t *testing.T) {
  9. frame := &output.CompositeFrame{
  10. Samples: make([]output.IQSample, 100),
  11. SampleRateHz: 228000,
  12. Timestamp: time.Now(),
  13. }
  14. for i := range frame.Samples {
  15. frame.Samples[i] = output.IQSample{I: float32(i) / 100, Q: 0}
  16. }
  17. result := ResampleIQ(frame, 228000)
  18. if len(result.Samples) != 100 {
  19. t.Fatalf("expected 100, got %d", len(result.Samples))
  20. }
  21. }
  22. func TestResampleIQUpsample(t *testing.T) {
  23. frame := &output.CompositeFrame{
  24. Samples: make([]output.IQSample, 228),
  25. SampleRateHz: 228000,
  26. Timestamp: time.Now(),
  27. }
  28. for i := range frame.Samples {
  29. phase := 2 * math.Pi * float64(i) / float64(len(frame.Samples))
  30. frame.Samples[i] = output.IQSample{I: float32(math.Cos(phase)), Q: float32(math.Sin(phase))}
  31. }
  32. result := ResampleIQ(frame, 528000)
  33. expectedLen := int(float64(228) * 528000 / 228000)
  34. if math.Abs(float64(len(result.Samples)-expectedLen)) > 2 {
  35. t.Fatalf("expected ~%d samples, got %d", expectedLen, len(result.Samples))
  36. }
  37. if result.SampleRateHz != 528000 {
  38. t.Fatalf("expected rate 528000, got %.0f", result.SampleRateHz)
  39. }
  40. // Verify magnitude preserved (should be ~1.0 for unit circle)
  41. for i := 10; i < len(result.Samples)-10; i++ {
  42. s := result.Samples[i]
  43. mag := math.Sqrt(float64(s.I)*float64(s.I) + float64(s.Q)*float64(s.Q))
  44. if mag < 0.9 || mag > 1.1 {
  45. t.Fatalf("sample %d: magnitude=%.4f", i, mag)
  46. }
  47. }
  48. }
  49. func TestResampleIQNil(t *testing.T) {
  50. if ResampleIQ(nil, 528000) != nil {
  51. t.Fatal("expected nil")
  52. }
  53. }