Skip to content

Commit e5517fe

Browse files
author
bartlettmic
committed
Added Averaging explanation to code math
1 parent b5f7c7b commit e5517fe

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

code math.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
The mathematics of the code are explained here for those of you who are curious or want clarification. Some of this is mentioned in the code itself, but without visual aid.
1+
### The mathematics of the code are explained here for those of you who are curious or want clarification. Some of this is mentioned in the code itself, but without visual aid.
22

33
---
4+
### Loudness and Brightness
45

5-
Having brightness correspond to loudness is an obvious enough feature, however its implementation was a bit of a design choice. Initially, I started with straight proportionality:
6+
Having brightness correspond to volume is an obvious enough feature, however its implementation was a bit of a design choice. Initially, I started with straight proportionality:
67
<center>![linear proportionality](http://i.imgur.com/bAM95uO.gif) &nbsp;&nbsp;&nbsp;&nbsp;in the code as: `(volume / maxVol) * 255`</center>
78

89
I found this method to be a little underwhelming, so I tried a more exponential approach to make lower ratios darker and higher ratios brighter:
@@ -18,3 +19,45 @@ This was used because it is a balance between the linear and exponential approac
1819
<center>![bright graph](http://imgur.com/DPynqok.gif)</center>
1920

2021
I encourage you to to try all methods to see which one you find to be the most pleasing. One other alternative is to raise the volume ratio to the power of 1.5 to get a slightly more linear output.
22+
23+
---
24+
### "Averaging"
25+
This program uses what is called "sequenced averages." The difference between them and a regular average is that rather than averaging all available values at once, the averages between a current value and the last computed average are computed in a sequence (this sequence influences the final result).
26+
27+
**Examples:**
28+
29+
True average of the list of integers from 0&ndash;10 inclusive:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![true avg](http://i.imgur.com/mTfEUwg.gif)
30+
31+
Sequenced average in numerical order:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;![sequenced avg](http://i.imgur.com/sLXRbIf.gif)
32+
33+
_Interestingly&mdash;although useless&mdash;if the sequence is in numerical order it follows this formula:_
34+
![seq numerical](http://i.imgur.com/NyLx6lo.gif)
35+
36+
&nbsp;
37+
38+
Rarely will you get an ordered sequence from the sound detector, and that's where the importance of this model becomes evident:![seq arb](http://i.imgur.com/3sMsmzr.gif)
39+
40+
In this sequence, there is an trend toward lower inputs (i.e. volume readings, so in the program this would reflect a decreasing noise level). The sequenced average properly reflects this trend, whereas if we did a proper mathematical average it would still read as 5. The difference here is a little too nuanced to be of importance, so I'll demonstrate with some actual data:
41+
42+
<table>
43+
<tr><td><center>All volumes were read out simultaneously during the same song. Some quiet was left at the end to demonstrate the difference between true and sequenced averages.</td></tr>
44+
<tr><th><center>Volume Readings, Straight from Sound Detector</center></th></tr>
45+
<tr><td><img src="http://i.imgur.com/bYXGvKV.png" alt="vols"></td></tr>
46+
<tr><th><center>Volume Readings + Sequenced Average of Volumes<sub>(orange)</sub></center></th></tr>
47+
<tr><td><img src="http://i.imgur.com/CLJKTkV.png" alt="vol+sa"></td></tr>
48+
<tr><th><center>Volume Readings + True Average of Volumes<sub>(green)</sub></center></th></tr>
49+
<tr><td><img src="http://i.imgur.com/3CgFLbo.png" alt="vol+av"></td></tr>
50+
<tr><th><center>Sequenced Average of Volumes<sub>(orange)</sub> + True Average of Volumes<sub>(green)</sub></center></th></tr>
51+
<tr><td><img src="http://i.imgur.com/MFDu64M.png" alt="av+sa"></td></tr>
52+
</table>
53+
54+
It's pretty evident which method is more responsive to the sound level. The most notable failure of the true average is when the song ends and it's silent. If we relied on the true average to to tell us what was going on in the environment, we'd still think it was plenty noisy, whereas the sequenced average reflects the silence almost immediately&mdash;which is the point.
55+
56+
The reason the true average fails is because of the culmination of data. It is an average after all, so after a long time reading values even large fluctuations in volume have little impact. This problem would only exaggerate the longer you left the visualizer on, which is obviously not in our best interest since we want to be able to leave the visualizer unattended.
57+
58+
Case in point, the sequenced average is the objectively superior method to get an "average" idea of the sound level (unless you like vapid, unresponsive visualization.)
59+
60+
---
61+
62+
63+

0 commit comments

Comments
 (0)