hello
I have created a class that allows me to create wave (sine, square, saw,etc). My intention is to modulate a carrier wave with other waves (modulators), my program works fine and I have been able to achive this. I am also using potentiometers to change the frequency, offset and amplitude of the modulating waves.
I am planning to use the modulated wave to control the parameters of an LED object.
Now in order do this I need to ensure that the modulated carrier values are:
1 data type int
2 within 0 to 255
3 positive values.
clearly this can be easily achive with restrain function, absolute, etc, but the result would not be what I want. ie any value above 255 would be 255 . What I would really like is to be able to compress the values to limit them to the range required and enure that they are positive. then cast the result to int.
the difficulty is that my modulated wave would vary when I use pot to affect the modulating waves values so maximum and minimum would be variable. I am thinking that I need a way to track max and min and then go from there (using map()). But I am sort of a bit stuck... I am pasting a picture to show the modulated wave (but this is fixed, not moving potentiometers, so the real modulated wave couuld end up being much wilder).
map() does not constrain (there is the constrain function for that) but here because the input value is guaranteed to be in between minValue and maxValue, the result is guaranteed to be between 0 and 255 so it will fit a uin8_t
But, are you adjusting minValue and maxValue for each point of the wave? They should be the same for all the wave, the absulute max and min. Otherwise you are applying different scaling factors all the time, until you reach the absolute max and min.
You have point... I have not had the chance to see it working to see the side effects of this method, but I see a possible issue there. yes there would be a different scaling applied as the max value increase and the minimum decreases. And that should not a problem for me. In fact the problem will be the opposite. You made me think that once I have increased the max and min, if i then decrease the modulations again, the scaling applied will be the same as when the max and min were at extreme values....it may not be a huge problem for this project, but it would be interesting to find a way to avoid this.....
You can keep absolute max/min and also the max/min of the last N samples, by storing also the timestamp of when they occurred.
But it means that when the signal changes the amplitude overtime, the amplitude will adjust itself up and down. You could smooth the changes in max/min to avoid jumps.
I suppose that it depends on what you want to achieve.
Another option, if you know the carrier and modulators amplitudes, or can estimate them, would be to calculate the total max and min at any time when you change the signals.
That's probably the best way to approach this. I will need to see how this works in practice and whether it is a noticeable problem. If it is I will try this way.
Actually it is not for audio, it is for LEDs. But I had in mind audio compression when approaching the problem anyway, and I think it would work fine in this scenario as well.