This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrum_point.go
93 lines (79 loc) · 2.46 KB
/
spectrum_point.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package hugipipes_sample
import (
"fmt"
"github.com/mjibson/go-dsp/fft"
wv "github.com/mjibson/go-dsp/wav"
"math"
"math/cmplx"
)
type SpectrumPoint struct {
Point complex128
LowFrequency float64
HighFrequency float64
CenterFrequency float64
Power float64
Ampl float64
Phase float64
}
func newSpectrumPoint(point complex128, powFactor float64, deltaF *float64, deltaFHalf *float64, index *int, maxFrequency *float64) *SpectrumPoint {
ampl := cmplx.Abs(point)
phase := cmplx.Phase(point)
pwr := powFactor * math.Pow(ampl, float64(2))
CenterFrequency := float64(*index) * *deltaF
LowFrequency := math.Max(CenterFrequency-*deltaFHalf, 0)
HighFrequency := math.Min(CenterFrequency+*deltaFHalf, *maxFrequency)
return &SpectrumPoint{
Point: point,
CenterFrequency: CenterFrequency,
LowFrequency: LowFrequency,
HighFrequency: HighFrequency,
Power: pwr,
Ampl: ampl,
Phase: phase,
}
}
func (s *SpectrumPoint) String() string {
return fmt.Sprintf("------------------------------\n"+
"complex: %v\n"+
"low frequency: %f Hz\n"+
"center frequency: %f Hz\n"+
"high frequency: %f Hz\n"+
"ampl: %f\n"+
"phase: %f Rad\n"+
"power: %f dB\n------------------------------\n",
s.Point,
s.LowFrequency,
s.CenterFrequency,
s.HighFrequency,
s.Ampl,
s.Phase,
s.Power)
}
func spectrum(wav *wv.Wav) ([]SpectrumPoint, error) {
realSamples, err := wav.ReadFloats(wav.Samples)
if err != nil {
return nil, err
}
realSamples64 := make([]float64, len(realSamples))
for i, realSample := range realSamples {
realSamples64[i] = float64(realSample)
}
complexSpectrumTwoSided := fft.FFTReal(realSamples64)
maxFrequency := float64(wav.SampleRate) / 2
startIndex := len(complexSpectrumTwoSided) / 2
validSamplePointsN := len(complexSpectrumTwoSided) - startIndex
complexSpectrumOneSided := make([]complex128, validSamplePointsN)
for i := range complexSpectrumOneSided {
complexSpectrumOneSided[i] = complexSpectrumTwoSided[i+startIndex]
}
deltaF := maxFrequency / float64(validSamplePointsN-1)
deltaFHalf := deltaF / 2
spec := make([]SpectrumPoint, validSamplePointsN)
N := float64(len(realSamples))
Fs := float64(wav.SampleRate)
powFactor := 1 / (Fs * N)
for i := range spec {
spec[i] = *newSpectrumPoint(complexSpectrumOneSided[i], powFactor, &deltaF, &deltaFHalf, &i, &maxFrequency)
}
return spec, nil
}