-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgeq7.cpp
210 lines (183 loc) · 4.57 KB
/
msgeq7.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "msgeq7.h"
msgeq7::msgeq7(byte strobePort ,byte resetPort,byte inputPort)
{
this->msg7StrobePort = strobePort;
this->msg7ResetPort = resetPort;
this->msg7AnalogInputPort = inputPort;
this->isInitialized = false;
this->isFiltered = false;
this->numberOfFrequencies = 7;
this->currentlyFilterProgramm = 0;
this->diffValueForExpFloatAvg = 0;
this->index = 0;
this->intervalVerlaufNonlinearLED = (minFrequencyValue * log10(2)) / (log10(minFrequencyValue));
pinMode(msg7ResetPort, OUTPUT);
pinMode(msg7StrobePort, OUTPUT);
pinMode(msg7AnalogInputPort, INPUT);
for (size_t j = 0; j < floatingAverageLenght; j++)
{
for (size_t i = 0; i < numberOfFrequencies; i++)
{
frequencyValue[j][i] = 0;
}
this->diffValueForExpFloatAvg += floatingAverageLenght-j;
}
}
int msgeq7::getMaxFrequency()
{
int tmp =0;
for (size_t i = 0; i < numberOfFrequencies; i++)
{
tmp = max(frequencyValue[index][i],tmp);
}
return tmp;
}
int msgeq7::getMinFrequency()
{
int tmp=maxFrequencyValue;
for (size_t i = 0; i < numberOfFrequencies; i++)
{
tmp = min(frequencyValue[index][i],tmp);
}
return tmp;
}
byte msgeq7::getNumberOfFrequencies()
{
return numberOfFrequencies;
}
void msgeq7::setFilterProgramm(byte number)
{
currentlyFilterProgramm = number;
}
void msgeq7::setNumberOfFrequencies(byte newCount)
{
if(newCount <= maxNumberOfFrequencies && newCount >= minNumberOfFrequencies)
numberOfFrequencies = newCount;
}
void msgeq7::readFrequencies()
{
isFiltered = false;
if(currentlyFilterProgramm != 0)
index = (index+1) % floatingAverageLenght;
reset();
for (int i = 0; i < numberOfFrequencies; i++)
{
measureFrequency(i);
chanceFrequency();
}
filter();
if(!isInitialized)
isInitialized = true;
}
int msgeq7::getFrequency(byte frequencyNumber)
{
if(isInitialized)
{
return frequencyValue[index][frequencyNumber];
}
else
{
return 0;
}
}
double msgeq7::getRelativeFrequency(byte frequencyNumber)
{
if(isInitialized)
{
return (double)frequencyValue[index][frequencyNumber]/getVolume();
}
return 0;
}
double msgeq7::getFrequencyVoltage(byte frequencyNumber)
{
return (double)frequencyValue[index][frequencyNumber] * 5 / 1023;
}
int msgeq7::getVolume()
{
int vol = 1;
byte freqWithVolume = 1;
for(int i = 0; i< numberOfFrequencies;i++)
{
if(frequencyValue[index][i] > minFrequencyValue/2)
{
vol += frequencyValue[index][i];
freqWithVolume++;
}
}
int avg = vol/freqWithVolume;
return (avg+getMaxFrequency())/2;
}
void msgeq7::exponentialFloatingAverageFilter()
{
for(int i = 0; i< numberOfFrequencies;i++)
{
int tmp =0;
for (size_t j = 1; j < floatingAverageLenght; j++)
{
tmp += frequencyValue[(index-j)%floatingAverageLenght][i]*(floatingAverageLenght-j);
}
frequencyValue[index][i] = (tmp+ (frequencyValue[index][i]*floatingAverageLenght))/diffValueForExpFloatAvg;
}
}
void msgeq7::lowValueReductionFilter()
{
for(int i = 0; i< numberOfFrequencies;i++)
{
if(frequencyValue[index][i] <= minFrequencyValue)
{
int tmp = frequencyValue[index][i] - abs(frequencyValue[index][i]-minFrequencyValue);
if(tmp > 0)
frequencyValue[index][i] = tmp;
else
frequencyValue[index][i] = 0;
}
}
}
void msgeq7::removeLowValuesAfterFilter()
{
for (int i = 0; i < numberOfFrequencies; i++)
{
if (frequencyValue[index][i] <= minFrequencyValue/2 )
frequencyValue[index][i] = 0;
}
}
void msgeq7::filter()
{
switch(currentlyFilterProgramm)
{
case 1:
exponentialFloatingAverageFilter();
break;
case 2:
lowValueReductionFilter();
exponentialFloatingAverageFilter();
removeLowValuesAfterFilter();
break;
default: //Programm Zero, don't manipulate the values
break;
}
isFiltered = true;
}
void msgeq7::reset()
{
digitalWrite (msg7ResetPort, HIGH);
digitalWrite (msg7StrobePort, HIGH);
delayMicroseconds(resetPulseWidth / 2);
digitalWrite (msg7StrobePort, LOW);
delayMicroseconds(resetPulseWidth / 2);
digitalWrite (msg7ResetPort, LOW);
digitalWrite (msg7StrobePort, HIGH);
delayMicroseconds(resetToStrobeDelay);
}
void msgeq7::measureFrequency(byte frequencyNumber)
{
digitalWrite (msg7StrobePort, LOW);
delayMicroseconds(outputSettlingTime * 2);
frequencyValue[index][frequencyNumber] = analogRead(msg7AnalogInputPort);;
}
void msgeq7::chanceFrequency()
{
digitalWrite (msg7StrobePort, HIGH);
delayMicroseconds(resetToStrobeDelay);
digitalWrite (msg7StrobePort, LOW);
}