[Csnd] detecting direction of change

Hi

I am working on a rhythm modulation thing, where I will need to detect if a signal is currently changing in the positive or negative direction.
Usually I would do something like:

ksig (is changing and we want to know if it increases or decreases)
kprevious init 0
kdiff = ksig-kprevious
kprevious = ksig

which could also be written:

ksig (is changing and we want to know if it increases or decreases)
kdiff diff ksig

I get a problem that the diff signal is spiky even if ksig is smoothly changing. It seems like it does not update continuously. The spike I get correctly represents the expected signal change, but I get a series of zeros in between the spikes.

To demonstrate, I did the same with a-signals

instr 2
amod oscil 0.9, 0.5
adiff diff amod
outs amod, adiff*1000
endin

When inspecting the resulting sound file, one can see the spikes with zeroes in between.

Am I thinking of this completely wrong, or is Csound doing something odd here?

all best
Øyvind

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 Oeyvind,
have you tried filtering the diff signal with a lowpass filter? I tried with butterlp and was able to smooth out the signal. You could even try to also smooth out the incoming signal. But it is strange that diff on a sine oscillator produces these artefacts, because the differential is a straight cosine.

Best wishes,

Jeanette

Hi Jeanette,
Thanks for the suggestion. I was thinking along those lines, but was confused, as it should not be necessary :wink:
I found a simpler and more robust solution (below), which has less flutter/spikes, but also here I need a median filter to make it completely clean.
It seems like there is some dithering going on somewhere. Could it be related to denormals somehow?
Øyvind

instr 2
kline line 0, p3, 0.5
kmod oscil 0.5, 0.5
ksig = kline + kmod
kprev init 0
kdir signum ksig-kprev
kdir mediank kdir, 7, 7
kprev = ksig
outs a(ksig), a(kdir)
endin

fre. 13. des. 2024 kl. 15:22 skrev Jeanette C. <julien@mail.upb.de>:

Hi Oeyvind,
have you tried filtering the diff signal with a lowpass filter? I tried with
butterlp and was able to smooth out the signal. You could even try to also
smooth out the incoming signal. But it is strange that diff on a sine
oscillator produces these artefacts, because the differential is a straight
cosine.

Best wishes,

Jeanette

For whatever reason,
I feel like I’ve been wanting you all my life <3
(Britney Spears)

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

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

I think the issue is that the signal is not changing for a few samples (so diff = 0), and then it changes. You
get the spikes when it does.

That’s what happens with truncating oscillators. If you replace oscil by oscili, then you will get a smooth
cosine wave.

HTH

yes --- this is another interesting example which side effects the usage of oscil can have --- even for such expert expert users ......
perhaps the usage of oscil should be discouraged on the manual page?

  joachim

I think oscil has its uses (as table does). Sometimes you need truncation, for example when you want to make an arpeggiator.

Prof. Victor Lazzarini
Maynooth University
Ireland

Oh, yes I understand now.
haha, yes, as Joachim says, it can trip up even experienced users :wink:
But it is always a good lesson from such encounters.
Øyvind

fre. 13. des. 2024 kl. 15:48 skrev Victor Lazzarini <000010b17ddd988e-dmarc-request@listserv.heanet.ie>:

I think the issue is that the signal is not changing for a few samples (so diff = 0), and then it changes. You
get the spikes when it does.

That’s what happens with truncating oscillators. If you replace oscil by oscili, then you will get a smooth
cosine wave.

HTH

Prof. Victor Lazzarini
Maynooth University
Ireland

Warning
This email originated from outside of Maynooth University’s Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
Hi

I am working on a rhythm modulation thing, where I will need to detect if a signal is currently changing in the positive or negative direction.
Usually I would do something like:

ksig (is changing and we want to know if it increases or decreases)
kprevious init 0
kdiff = ksig-kprevious
kprevious = ksig

which could also be written:

ksig (is changing and we want to know if it increases or decreases)
kdiff diff ksig

I get a problem that the diff signal is spiky even if ksig is smoothly changing. It seems like it does not update continuously. The spike I get correctly represents the expected signal change, but I get a series of zeros in between the spikes.

To demonstrate, I did the same with a-signals
instr 2
amod oscil 0.9, 0.5
adiff diff amod
outs amod, adiff*1000
endin

When inspecting the resulting sound file, one can see the spikes with zeroes in between.

Am I thinking of this completely wrong, or is Csound doing something odd here?

all best
Øyvind

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

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

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

i have created a pull request with a note added to the oscil manual page: