0.1.2 WaveMix

This commit is contained in:
rob tillaart 2022-03-27 10:26:59 +02:00
parent e8cfa49171
commit 16918bada3
8 changed files with 98 additions and 27 deletions

View File

@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.2] - 2022-03-26
### Added
- add **setOffset(offset)** and **getOffset()**
- add example
### Changed
- updated **mix()** code
### Fixed
- set gain explicit in constructor.
## [0.1.1] - 2022-03-26 ## [0.1.1] - 2022-03-26
### Added ### Added

View File

@ -41,6 +41,9 @@ Channel B will have 100 - percentage.
An important use of gain is to amplify weak signals but one can also use it as a modulator of a signal. An important use of gain is to amplify weak signals but one can also use it as a modulator of a signal.
See examples. See examples.
- **float getGain()** return the gain set. - **float getGain()** return the gain set.
- **void setOffset(float offset)** sets the offset for the output signal.
Typical used to align the zero level.
- **float getOffset()** return the current offset used.
- **float mix(float s1, float s2 = 0)** returns the weighted average of signal1 and signal2. - **float mix(float s1, float s2 = 0)** returns the weighted average of signal1 and signal2.
Signal2 is made optional to allow single signal processes e.g. modulation by **setGain()**. Signal2 is made optional to allow single signal processes e.g. modulation by **setGain()**.
@ -71,32 +74,28 @@ When the gain is negative, the output is effectively inverted.
## Future ideas ## Future ideas
#### 0.2.0 #### N channel variant.
- make a N channel variant. - add **setValue(uint8_t channel, float value)** allow update of channels at a different frequency.
- add **setValue(uint8_t channel, float value)** allow update of channels at a different frequency. - add **getValue()**, read the current output given the value of the channels. OR
- add **getValue()**, read the current output given the value of the channels. OR - add **getValue(uint8_t mask = 0xFF)**, read the current output given the value of selected channels.
- add **getValue(uint8_t mask = 0xFF)**, read the current output given the value of selected channels. - add **setMask(uint8_t mask = 0xFF)**, select channels. ease of use? **getValue(mask)** still needed?
- add **setMask(uint8_t mask = 0xFF)**, select channels. ease of use? **getValue(mask)** still needed? - add **getMask()**, read back \_mask;
- add **getMask()**, read back \_mask; - note that **mix()** can be implemented with the above functions.
- note that **mix()** can be implemented with the above functions. - add **setWeight(uint8_t channel, float weight)** need internal array of weights and \_sum
- add **setWeight(uint8_t channel, float weight)** need internal array of weights and \_sum - add **float getWeight(uint8_t channel)** Normalized or not?
- add **float getWeight(uint8_t channel)** Not normalized allows easier increment per channel. Needs a **float getTotalWeight()**.
- add constructor **WaveMix(uint8_t channels = 8)** with parameter to set the nr of channels? - add constructor **WaveMix(uint8_t channels = 8)** with parameter to set the number of channels? \[NO\]
- or do we need **WaveMix2()**, **WaveMix4()**, **WaveMix8()**, or even **WaveMix16()**, **WaveMix24()**, **WaveMix32()** class? - or do we need **WaveMix2()**, **WaveMix4()**, **WaveMix8()**, or even **WaveMix16()**, **WaveMix24()**, **WaveMix32()** class?
**WaveMix4()** and **WaveMix8()** seems to be realistic in terms of performance. **WaveMix4()** and **WaveMix8()** seems to be realistic in terms of performance.
**WaveMix8()** can be used for 2-8 channels, using a uint8_t mask. **WaveMix8()** can be used for 2-8 channels, using a uint8_t mask.
More channels will be much slower, so upon request the 16 and 32 variant? More channels will be much slower, so upon request the 16 and 32 variant? other variants can be obtained by masking.
#### Medium #### Medium
- add **void setOffset(float)**
- add **float getOffset()**
- add top clipping
- add **setMaximum(float)**
- add **setMinimum(float)**
- performance test. - performance test.
@ -113,5 +112,16 @@ More channels will be much slower, so upon request the 16 and 32 variant?
- add **increment(float)** - add **increment(float)**
- add **decrement(float)** - add **decrement(float)**
- percentages? hard for multichannel? - percentages? hard for multichannel?
- default parameters
- gain = 1.0
- percentage 50%
- offset = 0.0
- **reset()** needed?
#### wont
- add top clipping
- add **setMaximum(float)**
- add **setMinimum(float)**
- needs an enable/disable per limit.
becomes more complex than let the user constrain the output.

View File

@ -1,7 +1,7 @@
// //
// FILE: WaveMix.cpp // FILE: WaveMix.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.1 // VERSION: 0.1.2
// PURPOSE: Arduino library to mix two signals (A and B) with an adaptive weight. // PURPOSE: Arduino library to mix two signals (A and B) with an adaptive weight.
// URL: https://github.com/RobTillaart/WaveMix // URL: https://github.com/RobTillaart/WaveMix
// //
@ -14,6 +14,8 @@ WaveMix::WaveMix()
{ {
_weight[0] = 0.5; _weight[0] = 0.5;
_weight[1] = 0.5; _weight[1] = 0.5;
_gain = 1.0;
_offset = 0.0;
} }
@ -34,10 +36,13 @@ void WaveMix::setPercentage(float percentage)
float WaveMix::mix(float s1, float s2) float WaveMix::mix(float s1, float s2)
{ {
float total = 0;
if (_gain == 0) return 0; if (_gain == 0) return 0;
if (_weight[0] == 0) return s2 * _gain; if (_weight[0] != 0) total += (_weight[0] * s1);
if (_weight[1] == 0) return s1 * _gain; if (_weight[1] != 0) total += (_weight[1] * s2);
return ((_weight[0] * s1) + (_weight[1] * s2)) * _gain; total *= _gain;
if (_offset != 0) total += _offset;
return total;
} }

View File

@ -2,7 +2,7 @@
// //
// FILE: WaveMix.h // FILE: WaveMix.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.1 // VERSION: 0.1.2
// PURPOSE: Arduino library to mix two signals (A and B) with an adaptive weight. // PURPOSE: Arduino library to mix two signals (A and B) with an adaptive weight.
// URL: https://github.com/RobTillaart/WaveMix // URL: https://github.com/RobTillaart/WaveMix
// //
@ -10,7 +10,7 @@
#include "Arduino.h" #include "Arduino.h"
#define WAVEMIX_LIB_VERSION (F("0.1.1")) #define WAVEMIX_LIB_VERSION (F("0.1.2"))
class WaveMix class WaveMix
@ -28,12 +28,16 @@ public:
void setGain(float gain) { _gain = gain; }; void setGain(float gain) { _gain = gain; };
float getGain() { return _gain; }; float getGain() { return _gain; };
void setOffset(float offset) { _offset = offset; };
float getOffset() { return _offset; };
float mix(float s1, float s2 = 0); float mix(float s1, float s2 = 0);
private: private:
float _weight[2] = { 0.5, 0.5 }; float _weight[2] = { 0.5, 0.5 };
float _gain = 1.0; float _gain = 1.0;
float _offset = 0.0;
}; };

View File

@ -0,0 +1,36 @@
//
// FILE: waveMix_demo_noise_offset.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/WaveMix
//
#include "WaveMix.h"
WaveMix wm;
float n = 0;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("WAVEMIX_LIB_VERSION: ");
Serial.println(WAVEMIX_LIB_VERSION);
wm.setPercentage(95);
}
void loop()
{
Serial.println(wm.mix(sin(n), random(1000) * 0.001) * 100);
n = n + 0.05;
delay(10);
wm.setOffset(int(n/20)); // just a demo
}
// -- END OF FILE --

View File

@ -13,6 +13,9 @@ setPercentage KEYWORD2
setGain KEYWORD2 setGain KEYWORD2
getGain KEYWORD2 getGain KEYWORD2
setOffset KEYWORD2
getOffset KEYWORD2
mix KEYWORD2 mix KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/WaveMix.git" "url": "https://github.com/RobTillaart/WaveMix.git"
}, },
"version": "0.1.1", "version": "0.1.2",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=WaveMix name=WaveMix
version=0.1.1 version=0.1.2
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library to mix two signals (A and B) with an adaptive weight. sentence=Arduino library to mix two signals (A and B) with an adaptive weight.