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
/
mono_spectrum.go
78 lines (67 loc) · 1.72 KB
/
mono_spectrum.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
package hugipipes_sample
import (
"fmt"
)
const minSpectrumFreq = 15.0
type MonoSpectrum struct {
Points []MonoSpectrumPoint
MaxAmpl float64
EstimatedBaseFreq MonoSpectrumPoint
}
func newMonoSpectrum(Points []MonoSpectrumPoint, MaxAmpl float64) *MonoSpectrum {
return &MonoSpectrum{
Points: Points,
MaxAmpl: MaxAmpl,
EstimatedBaseFreq: calcEstimatedBaseFreq(Points, MaxAmpl),
}
}
func calcEstimatedBaseFreq(Points []MonoSpectrumPoint, MaxAmpl float64) MonoSpectrumPoint {
minAmplitudeToCalcEstimatedFreq := MaxAmpl * 0.1
rising := false
minAmplReached := false
for i, curr := range Points[1:] {
if curr.Frequency > minSpectrumFreq {
if curr.Amplitude >= minAmplitudeToCalcEstimatedFreq {
minAmplReached = true
}
if minAmplReached {
prev := Points[i-1]
if rising {
if curr.Amplitude < prev.Amplitude {
return prev
}
}
rising = curr.Amplitude > prev.Amplitude
}
}
}
panic(fmt.Sprintf("could not find out estimated base frequency! %v", minAmplReached))
}
func (s *MonoSpectrum) getFrequencies() []float64 {
frequencies := make([]float64, len(s.Points))
for i, p := range s.Points {
frequencies[i] = p.Frequency
}
return frequencies
}
func (s *MonoSpectrum) getPhases() []float64 {
phases := make([]float64, len(s.Points))
for i, p := range s.Points {
phases[i] = p.Phase
}
return phases
}
func (s *MonoSpectrum) getAmplitudes() []float64 {
abs := make([]float64, len(s.Points))
for i, p := range s.Points {
abs[i] = p.Amplitude
}
return abs
}
func (s *MonoSpectrum) getPowers() []float64 {
frequencies := make([]float64, len(s.Points))
for i, p := range s.Points {
frequencies[i] = p.Frequency
}
return frequencies
}