Csound Csound-dev Csound-tekno Search About

Constant pitch in bandpass filter

Date2015-12-08 22:52
FromBeinan Li
SubjectConstant pitch in bandpass filter
Hi Csound (6.05),

I found that a bandpass filter with a narrow band (say 4Hz wide) always gives me a whistle around its centre frequency. I tested with butterhp, resonr, resonz.

If the input is a plain osc at a fixed frequency, the whistle is short enough and will disappear quickly. But using a frequency sweep as input with a phasor (not shown here for simplicity), the whistle will stay long.

The only workaround I found was to widen the band to about 400Hz ... 
What am I missing here? Is it because the filter essentially becomes a resonance filter and inevitably gives the whistle? Or did I mess up any initialization (tried iskip to no avail)?

I briefly tested the bp~ object (bandpass) in PD and didn't have this side-effect.

Here is my test code:

<CsInstruments>

sr = 44100
ksmps = 10
nchnls = 1
0dbfs = 1

instr 1
asig poscil 0.5, p4, 1

aout butterbp asig, 1500, 4

out aout*40
endin

</CsInstruments>
<CsScore>
f1 0 4096 10 1

i1 0 3 440

</CsScore>

Thanks,
Beinan

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

Date2015-12-09 00:39
FromJustin Smith
SubjectRe: Constant pitch in bandpass filter
someone correct me if I am wrong, but I'm under the impression that any sound comprised solely of audible frequencies within a 4 hz. range will sound like a whistle. It's just the nature of the signal you are asking for. Perhaps you want a wet/dry mix?

On Tue, Dec 8, 2015 at 2:53 PM Beinan Li <li.beinan@gmail.com> wrote:
Hi Csound (6.05),

I found that a bandpass filter with a narrow band (say 4Hz wide) always gives me a whistle around its centre frequency. I tested with butterhp, resonr, resonz.

If the input is a plain osc at a fixed frequency, the whistle is short enough and will disappear quickly. But using a frequency sweep as input with a phasor (not shown here for simplicity), the whistle will stay long.

The only workaround I found was to widen the band to about 400Hz ... 
What am I missing here? Is it because the filter essentially becomes a resonance filter and inevitably gives the whistle? Or did I mess up any initialization (tried iskip to no avail)?

I briefly tested the bp~ object (bandpass) in PD and didn't have this side-effect.

Here is my test code:

<CsInstruments>

sr = 44100
ksmps = 10
nchnls = 1
0dbfs = 1

instr 1
asig poscil 0.5, p4, 1

aout butterbp asig, 1500, 4

out aout*40
endin

</CsInstruments>
<CsScore>
f1 0 4096 10 1

i1 0 3 440

</CsScore>

Thanks,
Beinan

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

Date2015-12-09 00:40
FromHlöðver Sigurðsson
SubjectRe: Constant pitch in bandpass filter
What your csound code is doing is that it's bandpassing 440 Hz pure sine-wave from 1498 to 1502 Hz. The whistle is probably just some resonance sound. But maybe you should be filtering something else (sawtooth wave or something, depending what your are after).

2015-12-08 23:52 GMT+01:00 Beinan Li <li.beinan@gmail.com>:
Hi Csound (6.05),

I found that a bandpass filter with a narrow band (say 4Hz wide) always gives me a whistle around its centre frequency. I tested with butterhp, resonr, resonz.

If the input is a plain osc at a fixed frequency, the whistle is short enough and will disappear quickly. But using a frequency sweep as input with a phasor (not shown here for simplicity), the whistle will stay long.

The only workaround I found was to widen the band to about 400Hz ... 
What am I missing here? Is it because the filter essentially becomes a resonance filter and inevitably gives the whistle? Or did I mess up any initialization (tried iskip to no avail)?

I briefly tested the bp~ object (bandpass) in PD and didn't have this side-effect.

Here is my test code:

<CsInstruments>

sr = 44100
ksmps = 10
nchnls = 1
0dbfs = 1

instr 1
asig poscil 0.5, p4, 1

aout butterbp asig, 1500, 4

out aout*40
endin

</CsInstruments>
<CsScore>
f1 0 4096 10 1

i1 0 3 440

</CsScore>

Thanks,
Beinan

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

Date2015-12-09 05:02
Frommskala@ANSUZ.SOOKE.BC.CA
SubjectRe: Constant pitch in bandpass filter
On Tue, 8 Dec 2015, Beinan Li wrote:
> If the input is a plain osc at a fixed frequency, the whistle is short
> enough and will disappear quickly. But using a frequency sweep as input with

What you mean by it "gives you a whistle" isn't quite clear, but it sounds
to me like your complaint is that the output of the filter doesn't start
and stop sharply when the input starts and stops.

That is the Uncertainty Principle at work.  A signal can only really be
said to have just one frequency if it is a *periodic* sine wave in the
mathematical sense - not just a sine wave but one that has always existed
and will always exist, at the same frequency and amplitude, across all
values of time.  Anything that has a beginning and end necessarily has
transients, which are segments during which it is not a periodic function,
at the beginning and end.  Those transients contain bands of frequencies.

If you use a filter to cause the signal to not have bands of frequencies
in it (in the frequency domain), that is the same thing as causing it to
not have a beginning and end (in the time domain).  It's not just an
unavoidable side effect; these two effects are really the same thing in a
mathematical sense.  The closer to perfect (that is the closer to zero
bandwidth) your filter becomes, the closer to perfect (that is, the closer
to preventing the signal from having a precise beginning or end) it will
be.  Narrower bandwidth, more whistle.  It's technically the same
trade-off that occurs between position and momentum of particles in
quantum physics:  the more certainty exists about the frequency of a wave,
the less certainty can exist about its boundaries in time or space.

There may be something to be gained by trying to shape the filter response
to let through the kind of transients you want.  This is generally the
same thing people do when they use special windowing functions on an FFT.
You can create a filter with a sharp peak, but side-lobes (nontrivial
extra responses) at other frequencies such that what makes it through is
acceptable for your purposes.  I don't have a code example, but it seems
like using a finite impulse response filter with a reasonably short
response might be what you want, and you could build that with a tapped
delay line in Csound if there isn't already a primitive for it.  The
tradeoff between tight bandwidth and uncertain transients is ultimately
unavoidable, though; you can only shift around the sidelobes, not
eliminate them.

Date2015-12-09 14:00
FromBeinan Li
SubjectRe: Constant pitch in bandpass filter
AttachmentsScreen Shot 2015-12-09 at 8.44.12 AM.png  
Many thanks to all!

@ mskala , the idea of side-lobes and the lead-in transient of an OSC makes sense. 

What bugs me now is that I tested in PD with the same design and couldn't reproduce the whistle (see the attached screenshot). It sounds like the 440Hz osc just happily goes through unaffected,
which is what I expected. 

Without looking at the source code from both PD and Csound, 
I got the feeling that the filter implementations are different.
Now maybe Csound ends up being more scientific and PD more forgiving,
but I'd rather make this simple PD patch work equivalently in Csound, otherwise I'd be under the impression that 
there is something fundamental that Csound can't do but other tools can. I certainly don't believe that.


Thanks,
Beinan


On Wed, Dec 9, 2015 at 12:02 AM, <mskala@ansuz.sooke.bc.ca> wrote:
On Tue, 8 Dec 2015, Beinan Li wrote:
> If the input is a plain osc at a fixed frequency, the whistle is short
> enough and will disappear quickly. But using a frequency sweep as input with

What you mean by it "gives you a whistle" isn't quite clear, but it sounds
to me like your complaint is that the output of the filter doesn't start
and stop sharply when the input starts and stops.

That is the Uncertainty Principle at work.  A signal can only really be
said to have just one frequency if it is a *periodic* sine wave in the
mathematical sense - not just a sine wave but one that has always existed
and will always exist, at the same frequency and amplitude, across all
values of time.  Anything that has a beginning and end necessarily has
transients, which are segments during which it is not a periodic function,
at the beginning and end.  Those transients contain bands of frequencies.

If you use a filter to cause the signal to not have bands of frequencies
in it (in the frequency domain), that is the same thing as causing it to
not have a beginning and end (in the time domain).  It's not just an
unavoidable side effect; these two effects are really the same thing in a
mathematical sense.  The closer to perfect (that is the closer to zero
bandwidth) your filter becomes, the closer to perfect (that is, the closer
to preventing the signal from having a precise beginning or end) it will
be.  Narrower bandwidth, more whistle.  It's technically the same
trade-off that occurs between position and momentum of particles in
quantum physics:  the more certainty exists about the frequency of a wave,
the less certainty can exist about its boundaries in time or space.

There may be something to be gained by trying to shape the filter response
to let through the kind of transients you want.  This is generally the
same thing people do when they use special windowing functions on an FFT.
You can create a filter with a sharp peak, but side-lobes (nontrivial
extra responses) at other frequencies such that what makes it through is
acceptable for your purposes.  I don't have a code example, but it seems
like using a finite impulse response filter with a reasonably short
response might be what you want, and you could build that with a tapped
delay line in Csound if there isn't already a primitive for it.  The
tradeoff between tight bandwidth and uncertain transients is ultimately
unavoidable, though; you can only shift around the sidelobes, not
eliminate them.

--
Matthew Skala
mskala@ansuz.sooke.bc.ca                 People before principles.
http://ansuz.sooke.bc.ca/

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

Date2015-12-09 14:11
FromVictor Lazzarini
SubjectRe: Constant pitch in bandpass filter
The parameters are not the same; and the filter design is not the same.

AFAIK (need to look at the code, because the documentation does not clarify it),
bp~ is a resonator, not a butterworth filter.

It takes a Q, not a bandwidth, as in the Csound case. So the results have to be
different.

========================
Dr Victor Lazzarini
Dean of Arts, Celtic Studies and Philosophy,
Maynooth University,
Maynooth, Co Kildare, Ireland
Tel: 00 353 7086936
Fax: 00 353 1 7086952 

> On 9 Dec 2015, at 14:00, Beinan Li  wrote:
> 
> Many thanks to all!
> 
> @ mskala , the idea of side-lobes and the lead-in transient of an OSC makes sense. 
> 
> What bugs me now is that I tested in PD with the same design and couldn't reproduce the whistle (see the attached screenshot). It sounds like the 440Hz osc just happily goes through unaffected,
> which is what I expected. 
> 
> Without looking at the source code from both PD and Csound, 
> I got the feeling that the filter implementations are different.
> Now maybe Csound ends up being more scientific and PD more forgiving,
> but I'd rather make this simple PD patch work equivalently in Csound, otherwise I'd be under the impression that 
> there is something fundamental that Csound can't do but other tools can. I certainly don't believe that.
> 
> 
> Thanks,
> Beinan
> 
> 
> On Wed, Dec 9, 2015 at 12:02 AM,  wrote:
> On Tue, 8 Dec 2015, Beinan Li wrote:
> > If the input is a plain osc at a fixed frequency, the whistle is short
> > enough and will disappear quickly. But using a frequency sweep as input with
> 
> What you mean by it "gives you a whistle" isn't quite clear, but it sounds
> to me like your complaint is that the output of the filter doesn't start
> and stop sharply when the input starts and stops.
> 
> That is the Uncertainty Principle at work.  A signal can only really be
> said to have just one frequency if it is a *periodic* sine wave in the
> mathematical sense - not just a sine wave but one that has always existed
> and will always exist, at the same frequency and amplitude, across all
> values of time.  Anything that has a beginning and end necessarily has
> transients, which are segments during which it is not a periodic function,
> at the beginning and end.  Those transients contain bands of frequencies.
> 
> If you use a filter to cause the signal to not have bands of frequencies
> in it (in the frequency domain), that is the same thing as causing it to
> not have a beginning and end (in the time domain).  It's not just an
> unavoidable side effect; these two effects are really the same thing in a
> mathematical sense.  The closer to perfect (that is the closer to zero
> bandwidth) your filter becomes, the closer to perfect (that is, the closer
> to preventing the signal from having a precise beginning or end) it will
> be.  Narrower bandwidth, more whistle.  It's technically the same
> trade-off that occurs between position and momentum of particles in
> quantum physics:  the more certainty exists about the frequency of a wave,
> the less certainty can exist about its boundaries in time or space.
> 
> There may be something to be gained by trying to shape the filter response
> to let through the kind of transients you want.  This is generally the
> same thing people do when they use special windowing functions on an FFT.
> You can create a filter with a sharp peak, but side-lobes (nontrivial
> extra responses) at other frequencies such that what makes it through is
> acceptable for your purposes.  I don't have a code example, but it seems
> like using a finite impulse response filter with a reasonably short
> response might be what you want, and you could build that with a tapped
> delay line in Csound if there isn't already a primitive for it.  The
> tradeoff between tight bandwidth and uncertain transients is ultimately
> unavoidable, though; you can only shift around the sidelobes, not
> eliminate them.
> 
> --
> Matthew Skala
> mskala@ansuz.sooke.bc.ca                 People before principles.
> http://ansuz.sooke.bc.ca/
> 
> 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

Date2015-12-09 14:50
FromRichard Dobson
SubjectRe: Constant pitch in bandpass filter
The signal is not enveloped, so it has a big (non-bandlimited) startup 
transient, which is duly being processed by the filter. Give it a simple 
attack slope e.g. using linseg and the whistle will disappear. Analysing 
the output in Audacity then shows up few pairs of low level higher-freq 
distortion products, that may be at least partly because of the 
upscaling by 40 – or because this happens to be an old (5.18) version of 
Csound on this equally old Mac.

Richard Dobson


On 09/12/2015 00:39, Justin Smith wrote:
> someone correct me if I am wrong, but I'm under the impression that any
> sound comprised solely of audible frequencies within a 4 hz. range will
> sound like a whistle. It's just the nature of the signal you are asking
> for. Perhaps you want a wet/dry mix?
>
..
>     instr 1
>     asig poscil 0.5, p4, 1
>
>     aout butterbp asig, 1500, 4
>
>     out aout*40
>     endin

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

Date2015-12-09 16:53
FromBeinan Li
SubjectRe: Constant pitch in bandpass filter
Thanks a lot, Dr. Lazzarini.

You had it. It was my bad that I didn't notice that second one is a Q for PD while in Csound it's the bandwidth.
After fixing that (in bold below), the Csound version will function the same way as PD.


<CsInstruments>

sr = 44100
ksmps = 10
nchnls = 1
0dbfs = 1

instr 1
asig poscil 0.5, p4, 1

aout butterbp asig, 1500, 1500/4
out aout*20
endin

</CsInstruments>
<CsScore>
f1 0 4096 10 1

i1 0 3 440 0.5

</CsScore>
</CsoundSynthesizer>

Thanks,
Beinan


On Wed, Dec 9, 2015 at 9:11 AM, Victor Lazzarini <Victor.Lazzarini@nuim.ie> wrote:
The parameters are not the same; and the filter design is not the same.

AFAIK (need to look at the code, because the documentation does not clarify it),
bp~ is a resonator, not a butterworth filter.

It takes a Q, not a bandwidth, as in the Csound case. So the results have to be
different.

========================
Dr Victor Lazzarini
Dean of Arts, Celtic Studies and Philosophy,
Maynooth University,
Maynooth, Co Kildare, Ireland
Tel: 00 353 7086936
Fax: 00 353 1 7086952

> On 9 Dec 2015, at 14:00, Beinan Li <li.beinan@gmail.com> wrote:
>
> Many thanks to all!
>
> @ mskala , the idea of side-lobes and the lead-in transient of an OSC makes sense.
>
> What bugs me now is that I tested in PD with the same design and couldn't reproduce the whistle (see the attached screenshot). It sounds like the 440Hz osc just happily goes through unaffected,
> which is what I expected.
>
> Without looking at the source code from both PD and Csound,
> I got the feeling that the filter implementations are different.
> Now maybe Csound ends up being more scientific and PD more forgiving,
> but I'd rather make this simple PD patch work equivalently in Csound, otherwise I'd be under the impression that
> there is something fundamental that Csound can't do but other tools can. I certainly don't believe that.
>
>
> Thanks,
> Beinan
>
>
> On Wed, Dec 9, 2015 at 12:02 AM, <mskala@ansuz.sooke.bc.ca> wrote:
> On Tue, 8 Dec 2015, Beinan Li wrote:
> > If the input is a plain osc at a fixed frequency, the whistle is short
> > enough and will disappear quickly. But using a frequency sweep as input with
>
> What you mean by it "gives you a whistle" isn't quite clear, but it sounds
> to me like your complaint is that the output of the filter doesn't start
> and stop sharply when the input starts and stops.
>
> That is the Uncertainty Principle at work.  A signal can only really be
> said to have just one frequency if it is a *periodic* sine wave in the
> mathematical sense - not just a sine wave but one that has always existed
> and will always exist, at the same frequency and amplitude, across all
> values of time.  Anything that has a beginning and end necessarily has
> transients, which are segments during which it is not a periodic function,
> at the beginning and end.  Those transients contain bands of frequencies.
>
> If you use a filter to cause the signal to not have bands of frequencies
> in it (in the frequency domain), that is the same thing as causing it to
> not have a beginning and end (in the time domain).  It's not just an
> unavoidable side effect; these two effects are really the same thing in a
> mathematical sense.  The closer to perfect (that is the closer to zero
> bandwidth) your filter becomes, the closer to perfect (that is, the closer
> to preventing the signal from having a precise beginning or end) it will
> be.  Narrower bandwidth, more whistle.  It's technically the same
> trade-off that occurs between position and momentum of particles in
> quantum physics:  the more certainty exists about the frequency of a wave,
> the less certainty can exist about its boundaries in time or space.
>
> There may be something to be gained by trying to shape the filter response
> to let through the kind of transients you want.  This is generally the
> same thing people do when they use special windowing functions on an FFT.
> You can create a filter with a sharp peak, but side-lobes (nontrivial
> extra responses) at other frequencies such that what makes it through is
> acceptable for your purposes.  I don't have a code example, but it seems
> like using a finite impulse response filter with a reasonably short
> response might be what you want, and you could build that with a tapped
> delay line in Csound if there isn't already a primitive for it.  The
> tradeoff between tight bandwidth and uncertain transients is ultimately
> unavoidable, though; you can only shift around the sidelobes, not
> eliminate them.
>
> --
> Matthew Skala
> mskala@ansuz.sooke.bc.ca                 People before principles.
> http://ansuz.sooke.bc.ca/
>
> 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 <Screen Shot 2015-12-09 at 8.44.12 AM.png>

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

Date2015-12-09 17:15
FromBeinan Li
SubjectRe: Constant pitch in bandpass filter
Yes. I believe that's what happened. Thanks Richard.

1) the transient contributes high-frequency content that got picked up by the wrongly-setup resonance filter (Q vs. BW error).

2) Because the OSC f0 falls out of the passband, the main frequency component is heavily attenuated;
then I scaled it too much, which brings up the resonance even more, which creates the "whistle". 
As the signal moves into its steady state, the side-effect disappears gradually.

Thanks,
Beinan


On Wed, Dec 9, 2015 at 9:50 AM, Richard Dobson <richarddobson@blueyonder.co.uk> wrote:
The signal is not enveloped, so it has a big (non-bandlimited) startup transient, which is duly being processed by the filter. Give it a simple attack slope e.g. using linseg and the whistle will disappear. Analysing the output in Audacity then shows up few pairs of low level higher-freq distortion products, that may be at least partly because of the upscaling by 40 – or because this happens to be an old (5.18) version of Csound on this equally old Mac.

Richard Dobson



On 09/12/2015 00:39, Justin Smith wrote:
someone correct me if I am wrong, but I'm under the impression that any
sound comprised solely of audible frequencies within a 4 hz. range will
sound like a whistle. It's just the nature of the signal you are asking
for. Perhaps you want a wet/dry mix?

..
    instr 1
    asig poscil 0.5, p4, 1

    aout butterbp asig, 1500, 4

    out aout*40
    endin

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