[Csnd] PVSBIN question / issue

Dear Community,

I’m having difficulty with a code snippet and am hoping to get your eyes on it. For every k-rate pass, I would like to scan a subset of bins within a *.pvx and grab the bin with the largest amplitude. The code shows how I was approaching this:


iBinS = p4 ; pvx bin to start scanning from
iBinE = p5 ; pvx bin to stop scanning after
kbindx = iBinS ; bin indexer

kArgMax = 0 ; argmax, the bin number with max amp
kAMax = -9.0 ; ampMax, init with arbitrary impossible negative amp.
kFMax = 00.0 ; frq of bin with max amp

;; PVX stuff
ifDurs = 9
kIdx phasor 1/ifDurs
fStest pvsfread kIdx*ifDurs, “test.pvx”

scanBins: ; loop through bins in range iBinS - iBinE

ka,kf pvsbin fStest, kbindx

if ( ka > kAMax ) then ;;if ka is max, update max values
kAMax = ka
kFMax = kf
kArgMax = kbindx

loop_le kbindx, 1, iBinE, scanBins ;; loop back to scanBins

printk 0.5, kArgMax
printk 0.5, kAMax
printk 0.5, kFMax


Seems simple enough. The code runs without error but I am not getting the expected results. No matter what values I use for iBinS (start bin) and iBinE (end bin), the result is always kArgMax == iBinS.

I initially thought the loop wasn’t executing correctly. I placed a counter inside and it is stepping through all of the kbindx values as it should.

I think the issue is with my understanding of PVSBIN’s k-rate operation.


It appears to accept the first assignment of kbin, but then does not update when given other values. Does this only work across k-rate passes, and not within them?

Thanks for any help you can offer. I’d love to get this working.

Best regards

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Hi Robert,
I sympathise, I think this is a pitfall in which I stepped frequently. There
are a few solutions. The problem is: the loop runs over one k-cycle, it uses the same instance of pvsbins. I think this only runs once in the km-cycle and will only output one bin for one index. I think it's the first index.

You might use pvsftw in the main program, then step through the bin values using tab or some other table reading opcode.

Or you could try pvstrace, which "retains only the N loudest bins" of an f-rate signal:
fOutput [, kBins[]] pvstrace fInput, kN [, iSort, iMin, iMax]
I think iMin and iMax are equivalent to your iBinS and iBinE. The sorting option allows to sort reported bins in the k-rate array by decreasing amplitude. The arra output is optional, but might help you to do particular things to those bins.


Best wishes,


yes i think this is the way to go. in addition to what jeanette suggests: there is an example in the csound floss manual which you can adapt for your needs: 03E07 in The Csound FLOSS Manual

best -