Csound Csound-dev Csound-tekno Search About

[Csnd] UDO to have the max a signal amplitude as k variable

Date2013-08-22 22:42
From"vallste@libero.it"
Subject[Csnd] UDO to have the max a signal amplitude as k variable

Hello,

I'm developing this udo to have a krate variable indicating the maximum value reached by a a-rate signal during a k-cycle.

As far as I've understood downsamp opcode performs some averaging (please correct me if I'm wrong) so not using it since I want the absolute maximum reached.


opcode MaxAmpInKCycle, k, ak
setksmps 1
ain, kperiod  xin ;ain - input signal, kperiod = kspms
kcounter init 0 ;count from 0 to ksmps
kout init 0 ;output value
aamp = abs(ain)
kamp downsamp aamp
;if above previous value update
kout = (kamp > kout ? kamp : kout)
kcounter = kcounter + 1
if kcounter == kperiod then
    ;reset value and counter
    ;when reaching ksmps
    kout = 0
    kcounter = 0
endif
printk2 kcounter ; for testing
printk ksmps / sr, kout ; for testing
xout kout
endop


Not sure if it's working as expected, by printing values looks like that the program using this opcode


instr 1

ainLH, ainRH diskin2 $INPUTFILE, 1, 0, 1
printk ksmps / sr, 123
kvalue MaxAmpInKCycle ainLH, 8 ; equal to ksmps
printk ksmps / sr, 456
printk ksmps / sr, kvalue
printk ksmps / sr, 789

endin


is somehow out of phase: after having printed 789 (from the main program), kcounter variable value (printed from inside the opcode) is not 0 (don't know if caused by some kind of "printing buffer") so if someone want give it a try/report results/find errors/have an idea of what's happening and want to share is more than welcome.


I'm using 5.17 double from Ubuntu repository


Bye

Stefano

 

Date2013-08-22 23:00
FromVictor Lazzarini
SubjectRe: [Csnd] UDO to have the max a signal amplitude as k variable
you could also use vaget and a k-rate loop.

Victor
On 22 Aug 2013, at 22:42, vallste@libero.it wrote:

> Hello,
> I'm developing this udo to have a krate variable indicating the maximum value reached by a a-rate signal during a k-cycle.
> As far as I've understood downsamp opcode performs some averaging (please correct me if I'm wrong) so not using it since I want the absolute maximum reached.
> 
> opcode MaxAmpInKCycle, k, ak
> setksmps 1
> ain, kperiod  xin ;ain - input signal, kperiod = kspms
> kcounter init 0 ;count from 0 to ksmps
> kout init 0 ;output value
> aamp = abs(ain)
> kamp downsamp aamp
> ;if above previous value update
> kout = (kamp > kout ? kamp : kout)
> kcounter = kcounter + 1
> if kcounter == kperiod then
>     ;reset value and counter
>     ;when reaching ksmps
>     kout = 0
>     kcounter = 0
> endif
> printk2 kcounter ; for testing
> printk ksmps / sr, kout ; for testing
> xout kout
> endop
> 
> Not sure if it's working as expected, by printing values looks like that the program using this opcode
> 
> instr 1 
> ainLH, ainRH diskin2 $INPUTFILE, 1, 0, 1
> printk ksmps / sr, 123
> kvalue MaxAmpInKCycle ainLH, 8 ; equal to ksmps
> printk ksmps / sr, 456
> printk ksmps / sr, kvalue
> printk ksmps / sr, 789
> endin
> 
> is somehow out of phase: after having printed 789 (from the main program), kcounter variable value (printed from inside the opcode) is not 0 (don't know if caused by some kind of "printing buffer") so if someone want give it a try/report results/find errors/have an idea of what's happening and want to share is more than welcome.
> 
> I'm using 5.17 double from Ubuntu repository
> 
> Bye
> Stefano
>  

Dr Victor Lazzarini
Senior Lecturer
Dept. of Music
NUI Maynooth Ireland
tel.: +353 1 708 3545
Victor dot Lazzarini AT nuim dot ie





Date2013-08-23 08:52
Fromjoachim heintz
SubjectRe: [Csnd] UDO to have the max a signal amplitude as k variable
hi stefano -

i think no udo is needed here because max_k does what you wish?!

best -

	joachim


Am 22.08.2013 23:42, schrieb vallste@libero.it:
> Hello,
>
> I'm developing this udo to have a krate variable indicating the maximum
> value reached by a a-rate signal during a k-cycle.
>
> As far as I've understood downsamp opcode performs some averaging
> (please correct me if I'm wrong) so not using it since I want the
> absolute maximum reached.
>
>
> opcode MaxAmpInKCycle, k, ak
> setksmps 1
> ain, kperiod  xin ;ain - input signal, kperiod = kspms
> kcounter init 0 ;count from 0 to ksmps
> kout init 0 ;output value
> aamp = abs(ain)
> kamp downsamp aamp
> ;if above previous value update
> kout = (kamp > kout ? kamp : kout)
> kcounter = kcounter + 1
> if kcounter == kperiod then
>      ;reset value and counter
>      ;when reaching ksmps
>      kout = 0
>      kcounter = 0
> endif
> printk2 kcounter ; for testing
> printk ksmps / sr, kout ; for testing
> xout kout
> endop
>
>
> Not sure if it's working as expected, by printing values looks like that
> the program using this opcode
>
>
> instr 1
>
> ainLH, ainRH diskin2 $INPUTFILE, 1, 0, 1
> printk ksmps / sr, 123
> kvalue MaxAmpInKCycle ainLH, 8 ; equal to ksmps
> printk ksmps / sr, 456
> printk ksmps / sr, kvalue
> printk ksmps / sr, 789
>
> endin
>
>
> is somehow out of phase: after having printed 789 (from the main
> program), kcounter variable value (printed from inside the opcode) is
> not 0 (don't know if caused by some kind of "printing buffer") so if
> someone want give it a try/report results/find errors/have an idea of
> what's happening and want to share is more than welcome.
>
>
> I'm using 5.17 double from Ubuntu repository
>
>
> Bye
>
> Stefano
>