| Author |
Message |
kijjaz

Joined: Sep 20, 2004 Posts: 452 Location: bangkok, thailand
Audio files: 2
|
Posted: Thu Nov 01, 2007 5:19 am Post subject:
Energy Integrator Subject description: a tool for measuring energy produced from a patch |
 |
|
From Inventor's Impulse i => BPF f1 => ... sound issue,
I started to want to make a tool to easily calculate overall power of some sound sources
with this example, I tried to show how we can use it to integrate the energy of Impulse through BPF and Impulse through LPF
it also show that when a signal slowly decays, we'll see an increasing energy value and then it reaches a static value when the decay stops.
| Code: | class kjzRMS101
{
// calculate square of the input signal
Gain input => Gain square;
input => Gain input_dup => square;
3 => square.op;
// calculate summation
square => Gain integrator => integrator => blackhole;
0 => float lastEnergy;
fun float energy()
{
return Math.sqrt(integrator.last()) - lastEnergy;
}
fun void reset()
{
Math.sqrt(integrator.last()) => lastEnergy;
}
}
kjzRMS101 power1;
Impulse i => power1.input;
i.next(1.0);
.5::second => now;
<<< power1.energy() >>>;
.5::second => now;
<<< power1.energy() >>>;
<<< "RESET!" >>>;
i =< power1.input;
power1.reset();
.5::second => now;
<<< power1.energy() >>>;
<<< "Calculate power of impulse through BPF that's set to (1000, 1)","" >>>;
i => BPF f => power1.input;
f.set(1000, 1);
i.next(1);
.5::second => now;
<<< power1.energy() >>>;
<<< "RESET!" >>>;
power1.reset();
<<< "Calculate power of impulse through LPF that's set to (1000, 500)","" >>>;
i => LPF f2 => power1.input;
f2.set(1000, 500);
i.next(1);
for(int j; j < 10; j++)
{
.1::second => now;
<<< power1.energy() >>>;
} |
result:
| Code: | 1.000000 :(float)
1.000000 :(float)
"RESET!" : (string)
0.000000 :(float)
Calculate power of impulse through BPF that's set to (1000, 1)
0.030297 :(float)
"RESET!" : (string)
Calculate power of impulse through LPF that's set to (1000, 500)
3.909407 :(float)
4.539713 :(float)
4.706453 :(float)
4.753040 :(float)
4.766196 :(float)
4.769912 :(float)
4.770947 :(float)
4.771033 :(float)
4.771033 :(float)
4.771033 :(float) |
|
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 452 Location: bangkok, thailand
Audio files: 2
|
Posted: Thu Nov 01, 2007 5:38 am Post subject:
|
 |
|
This is the update:
I thought about it and see that the output value can't be compared while running at various sample rate,
so here this one divide the output by sample rate to be able to compare that while running the same patch at different sample rate.
After dividing with sample rate, the value'd be quite small,
so I multiply by 1000.
The unit of the energy value is gonna be milli unit per sample
| Code: | class kjzRMS102
{
second / samp => float sampleRate;
// calculate square of the input signal
Gain input => Gain square;
input => Gain input_dup => square;
3 => square.op;
// calculate summation
square => Gain integrator => integrator => blackhole;
0 => float lastEnergy;
fun float energy()
{
return (Math.sqrt(integrator.last()) - lastEnergy) / sampleRate * 1000;
}
fun void reset()
{
Math.sqrt(integrator.last()) => lastEnergy;
}
}
kjzRMS102 power1;
Impulse i => power1.input;
i.next(1.0);
.5::second => now;
<<< power1.energy() >>>;
.5::second => now;
<<< power1.energy() >>>;
<<< "RESET!" >>>;
i =< power1.input;
power1.reset();
.5::second => now;
<<< power1.energy() >>>;
<<< "Calculate power of impulse through BPF that's set to (1000, 1)","" >>>;
i => BPF f => power1.input;
f.set(1000, 1);
i.next(1);
.5::second => now;
<<< power1.energy() >>>;
<<< "RESET!" >>>;
power1.reset();
<<< "Calculate power of impulse through LPF that's set to (1000, 500)","" >>>;
i => LPF f2 => power1.input;
f2.set(1000, 500);
i.next(1);
for(int j; j < 10; j++)
{
.1::second => now;
<<< power1.energy() >>>;
}
|
result:
| Code: | 0.020833 :(float)
0.020833 :(float)
"RESET!" : (string)
0.000000 :(float)
Calculate power of impulse through BPF that's set to (1000, 1)
0.000631 :(float)
"RESET!" : (string)
Calculate power of impulse through LPF that's set to (1000, 500)
0.081446 :(float)
0.094577 :(float)
0.098051 :(float)
0.099022 :(float)
0.099296 :(float)
0.099373 :(float)
0.099395 :(float)
0.099397 :(float)
0.099397 :(float)
0.099397 :(float) |
|
|
|
Back to top
|
|
 |
Inventor

Joined: Oct 13, 2007 Posts: 1174 Location: Florida, USA
Audio files: 50
|
Posted: Thu Nov 01, 2007 7:21 am Post subject:
|
 |
|
Great kijjaz, your understanding of how to make these Gain unit generators work is impressive, now a real-time RMS calculator! I can imagine many ways to use it, for example one could make a second instrument wait until the energy rose to a certain level before starting it. Or it could control slow motion of an object in a video. One thing is strange, though, when I ran it on my system I got slightly different results:
| Code: | 0.022676 :(float)
0.022676 :(float)
"RESET!" : (string)
0.000000 :(float)
Calculate power of impulse through BPF that's set to (1000, 1)
0.000743 :(float)
"RESET!" : (string)
Calculate power of impulse through LPF that's set to (1000, 500)
0.093188 :(float)
0.108115 :(float)
0.112062 :(float)
0.113165 :(float)
0.113477 :(float)
0.113565 :(float)
0.113589 :(float)
0.113593 :(float)
0.113593 :(float)
0.113593 :(float) |
Since you have normalized the calculations for sample rate, that must not be the reason. I'm not sure what the difference is. Anyway, thanks for another useful ChucK tool, kijjaz! |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 6249 Location: The Hague, NL
G2 patch files: 3
|
Posted: Thu Nov 01, 2007 10:31 am Post subject:
|
 |
|
Yeah! Kijjaz is the master of making simple stuff do complex things!
BTW, in the examples dir there is also a envelope follower by Perry Cook, I seem to remember that one's based on the BiQuad. _________________ while(!machine.crash() ) <<<"all is well">>>; |
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 452 Location: bangkok, thailand
Audio files: 2
|
Posted: Thu Nov 01, 2007 2:29 pm Post subject:
|
 |
|
Yes, different sample rate really makes the result different.
That might depend on how the waveform is generated also
Thanks for supporting. I'll try to apply to some more situation.
Or any suggestions would be cool.
Inventor: Yes, I thought about that for some time: make music goes into another section after gaining enough energy from one sound source.
That'd be cool for playing live also heheh.
Kassen: thanks for the support also ^_^ |
|
|
Back to top
|
|
 |
Inventor

Joined: Oct 13, 2007 Posts: 1174 Location: Florida, USA
Audio files: 50
|
Posted: Thu Nov 01, 2007 5:17 pm Post subject:
|
 |
|
| kijjaz wrote: | Thanks for supporting. I'll try to apply to some more situation.
Or any suggestions would be cool.
|
Well, since you asked, kijjaz, there are some other math functions that might be useful. One that comes to mind is a note detector, something that looks at the envelope of the signal (LPF of the square?) and detects peaks or rising transitions (threshold or differentiator / slope detector?). I could use that in my animations. Or how about a differentiator, or perhaps the simplest thing of all: a sample grabber. Something that listens to the dac and returns arrays of samples. These would all be useful for animations, for example I could make an ocean in crystal blue out of waveform samples. Don't trouble yourself though unless it is easy and fun for you to do! This should be mostly fun with very little work, if at all possible! |
|
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 452 Location: bangkok, thailand
Audio files: 2
|
Posted: Thu Nov 01, 2007 9:11 pm Post subject:
|
 |
|
Note detector & peak detection? hmm.. I never programmed those things before.
I guess it can be assembled from the Unit Analysers..
But I still haven't read about those technics, I'll research on that ^_^
I'll try to come up with some other ideas we can play around.
Hmm.. for "Differentiator", a very simple differentiator is simply a high-pass filter.
so you might wanna try using chuck's filter..
maybe a HPF but choose quite a small Q (i would choose between .3-1) |
|
|
Back to top
|
|
 |
|