Author |
Message |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 2:29 am Post subject:
software filter: non fixed sampling rate filter |
 |
|
Most (all?) of the software filters implementation are based on a fixed (pre calculated parameters) sample rate.
I was looking some source code, some filters pre calculate the 1/FS to it will be used to calc other parameters.
Now, I am looking / wondering if I don't have a fixed sample rate but a variable (eg: adaptive) rate.
So all I know is the next step advance.
How can I develop a filter with this variable step? Any other considerations? |
|
Back to top
|
|
 |
blue hell
Site Admin

Joined: Apr 03, 2004 Posts: 24432 Location: The Netherlands, Enschede
Audio files: 297
G2 patch files: 320
|
Posted: Thu Feb 23, 2017 4:05 am Post subject:
|
 |
|
Not sure what you want to make ... so maybe this is trivial to you ...
When you change the sample rate the filter cutoff frequency would scale 1:1 with that.
I think that your lowest sample rate still would have to be high enough to treat the highest frequencies ok to avoid distortion. _________________ Jan
also .. could someone please turn down the thermostat a bit.
 |
|
Back to top
|
|
 |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 4:28 am Post subject:
|
 |
|
If I have a regular filter / processing, this will look similar to this:
Code: |
InitFilter( 1/FS);
For each sample
{
output = ProcessFilter(new sample);
}
|
What I am looking is more:
Code: |
InitFilter();
For each sample
{
output = ProcessFilter(new sample, step time increment phase?);
}
|
It means, lets say.. , I can change the sample rate every sample! |
|
Back to top
|
|
 |
blue hell
Site Admin

Joined: Apr 03, 2004 Posts: 24432 Location: The Netherlands, Enschede
Audio files: 297
G2 patch files: 320
|
Posted: Thu Feb 23, 2017 5:27 am Post subject:
|
 |
|
Ah, ok. You could just move the code from InitFilter into the loop then ... but there is a price for that of course in calculation cycles needed.
The separate initialization function basically is an optimization strategy.
What I did in my synth is that I made a SampleRateChanged( NewRate) function which will then call all the InitXXX functions that have a sample rate dependency.
In an object oriented structure that's not too hard to code, for a non object oriented thing you could maybe make a linked list of function pointers to iterate over when the sample rate changes.
This would depend a bit on the actual platform used. _________________ Jan
also .. could someone please turn down the thermostat a bit.
 |
|
Back to top
|
|
 |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 6:00 am Post subject:
|
 |
|
true but.. My questions was not related with the general code implementation but with the filter design and concerns?
As the filters work as an incremental processing (i.e: the result depends on the previous results and input values) it expects that the input be added at some cadence (frequency sample)
even changing every step should it work? or any other concerns should taken in account? |
|
Back to top
|
|
 |
blue hell
Site Admin

Joined: Apr 03, 2004 Posts: 24432 Location: The Netherlands, Enschede
Audio files: 297
G2 patch files: 320
|
Posted: Thu Feb 23, 2017 6:11 am Post subject:
|
 |
|
That would be hard to answer in general ... some filters don't like it much when there parameters are updated too fast .. while others do not have such issues.
Recalculating for a new sample rate would amount to the same thing I'd say. _________________ Jan
also .. could someone please turn down the thermostat a bit.
 |
|
Back to top
|
|
 |
JovianPyx

Joined: Nov 20, 2007 Posts: 1988 Location: West Red Spot, Jupiter
Audio files: 224
|
Posted: Thu Feb 23, 2017 8:43 am Post subject:
|
 |
|
Can you tell us why you want to do this? What could you do with such a filter that you believe you cannot do with a fixed sample rate filter?
Filters such as the state variable are very easy to tune (set Fc to desired value) as well as Q setting by changing just one number.
To what Blue Hell said, I'll add that changing FS can be more trouble than it's worth since the relationship between the highest harmonic frequency to FS may be difficult to determine and can easily result in alias artifacts (which are something most DSP designers try to avoid).
A single fixed sample rate also allows you to run more than one filter in the same program and have Fc set differently for each one. That will be significantly more troublesome with a variable FS.
Consider also that you will need to use interpolation to get a smooth range of FS values. Interpolation comes in many varieties and performance as well as computational load all of which vary with the chosen method.
Have you seen the free information at www.dspguide.com ? It's a wonderful down-to-earth site that explains the principles of DSP in plain English.
HTH. _________________ FPGA, dsPIC and Fatman Synth Stuff
Time flies like a banana. Fruit flies when you're having fun. BTW, Do these genes make my ass look fat? corruptio optimi pessima
|
|
Back to top
|
|
 |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 9:04 am Post subject:
|
 |
|
JovianPyx wrote: | Can you tell us why you want to do this? What could you do with such a filter that you believe you cannot do with a fixed sample rate filter?
|
I was evaluating to simulate an analogue circuit ... at some level. not at electronic simulation level but something more accurate than a simple digital simulation.
So if you imagine a triangle wave /\/\/\/\
I can know exactly (by some level of modulation) when it will be the spike of the triangle, that will be at time t, that can be in the middle of two consecutive samples.
Thats why I was evaluating if a variable (adaptive) sample rate could be used for process the waveform and filter values.. and then.. the sample rate will be used for sample (display.. audio output..) |
|
Back to top
|
|
 |
JovianPyx

Joined: Nov 20, 2007 Posts: 1988 Location: West Red Spot, Jupiter
Audio files: 224
|
Posted: Thu Feb 23, 2017 9:49 am Post subject:
|
 |
|
Dealing with "middle of the sample" is usually done with interpolation. I use this technique to tune wavetable samples to precise pitches, but I do it with a fixed sample rate.
Is fractional sample interpolation what you mean when you refer to "variable sample rate"? When I think of sample rate, I think of the rate at which the DAC is updated which is normally fixed.
There should be numerous good articles on fractional samples and interpolation on the web. _________________ FPGA, dsPIC and Fatman Synth Stuff
Time flies like a banana. Fruit flies when you're having fun. BTW, Do these genes make my ass look fat? corruptio optimi pessima
|
|
Back to top
|
|
 |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 11:59 am Post subject:
|
 |
|
I dont much about processing so I dont know how this is called.
I will explain again. consider you have the triangle wave:
at t time, the tri wave, it will be at one voltage level (some intensity value)
at t+1 you will output a sample to be played.
but.. lets say that at t+0.12 there was the time when the triangle wave get the peak (max value).
So I am wondering if the filter result will be different if I consider (catch) that max peak of the wave..
I can do oversampling.. but.. it will also not solve "my idea of adaptive" (sample at the precise moment that the change happen) |
|
Back to top
|
|
 |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 12:03 pm Post subject:
|
 |
|
JovianPyx wrote: | When I think of sample rate, I think of the rate at which the DAC is updated which is normally fixed. |
No actually I am planing the output to be fixed rate, but the internal calculations (lets say...) the "oversampling" would be calculated in a variable way. |
|
Back to top
|
|
 |
JovianPyx

Joined: Nov 20, 2007 Posts: 1988 Location: West Red Spot, Jupiter
Audio files: 224
|
Posted: Thu Feb 23, 2017 12:21 pm Post subject:
|
 |
|
Ah - OK - That means interpolation. www.dspguide.com should have info about the various ways to do that. _________________ FPGA, dsPIC and Fatman Synth Stuff
Time flies like a banana. Fruit flies when you're having fun. BTW, Do these genes make my ass look fat? corruptio optimi pessima
|
|
Back to top
|
|
 |
blue hell
Site Admin

Joined: Apr 03, 2004 Posts: 24432 Location: The Netherlands, Enschede
Audio files: 297
G2 patch files: 320
|
Posted: Thu Feb 23, 2017 12:29 pm Post subject:
|
 |
|
It makes no sense to adapt the rate to the peaks of the waveform .. what counts are the frequencies in it - there is not even such a peak in the digital representation. _________________ Jan
also .. could someone please turn down the thermostat a bit.
 |
|
Back to top
|
|
 |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 12:34 pm Post subject:
|
 |
|
Sorry but if that is the same interpolation I know, it is not interpolation I am looking for..
What I know of interpolation mean is: calculate an estimation of some value between two (or more) know values.
There are different kinds of interpolation I know: nearest, linear, cubic,..
But I am not interpolating the tri wave because I know exactly the value at every moment (it will be math defined)
Please note on my example when I say I want to process the value of the filter at t+0.12 (the sample rate will be at t+1) I know exactly the value of my VCO at t+0.12 .. so it is not interpolated.. and I want to pass the value to the filter at that time... because it is the moment when the wave changes the direction.. |
|
Back to top
|
|
 |
JovianPyx

Joined: Nov 20, 2007 Posts: 1988 Location: West Red Spot, Jupiter
Audio files: 224
|
Posted: Thu Feb 23, 2017 12:45 pm Post subject:
|
 |
|
I'm failing to understand too much about what it is you're trying to accomplish. I understand the idea of knowing the value of a calculated waveform point between two integer DAC samples, but I'm not understanding what that has to do with using a filter with a "variable sample rate".
One thing to keep in mind is that a triangle wave or any periodic wave other than a pure sine has an infinite number of harmonics. No matter how you calculate it, if it's a "naive" waveform, there will always be alias artifacts regardless of the sample rate or upsampling, so the simulation will not be a perfect one. To eliminate the alias artifacts, you must band limit the triangle waveform - and then it will sound like a triangle, but will no longer look like a triangle.
Again, I think there's something about this project I don't understand, so please pardon me  _________________ FPGA, dsPIC and Fatman Synth Stuff
Time flies like a banana. Fruit flies when you're having fun. BTW, Do these genes make my ass look fat? corruptio optimi pessima
|
|
Back to top
|
|
 |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 1:12 pm Post subject:
|
 |
|
I am also not sure about my ideas, they may don't make sense but thats why I am discuss it here with someone more expert on the subject!
I may will abandon this idea, but I'm just happy to discuss it.
I understand also the concept of "band limited waveforms" but that will not easily work for me, as I will be mathematic simulating the triangle waveform (and I dont have a way to pre-calculated as it will be modified realtime)
I was thinking that if I compute the filter at the exactly instant when the wave change (eg: a tri wave or a sqr) that it will make a slight different result as only sample it at moment (rate) of the output. It may be.. but will be very small.
So in another topic, is there any way that you can think of how can I compute a band limited wave based from the original math representation? (without pre-calc bufering) |
|
Back to top
|
|
 |
blue hell
Site Admin

Joined: Apr 03, 2004 Posts: 24432 Location: The Netherlands, Enschede
Audio files: 297
G2 patch files: 320
|
Posted: Thu Feb 23, 2017 1:46 pm Post subject:
|
 |
|
I've used polyBlep to bandlimit waves - it can be done on the fly as it's reasonably efficient - not perfect though, but pretty good. _________________ Jan
also .. could someone please turn down the thermostat a bit.
 |
|
Back to top
|
|
 |
blue hell
Site Admin

Joined: Apr 03, 2004 Posts: 24432 Location: The Netherlands, Enschede
Audio files: 297
G2 patch files: 320
|
Posted: Thu Feb 23, 2017 1:56 pm Post subject:
|
 |
|
Here is some example code (Pascal) :
Code: |
// Class definitions
TModBaseOsc = class( TMod)
strict private
const
i_frequency = 0;
i_fm = 1;
i_phase = 2;
i_sync = 3;
i_mute = 4;
i_shape = 5;
i_pmlevel = 6;
i_fmlevel = 7;
i_cents = 8;
i_bandlimit = 9;
i_freq = 10;
const
o_out = 0;
private
FPhase : TSignal;
FPhaseDelta : TSignal;
FPosition : TSignal;
FOldSync : Boolean;
FShape : Integer;
FBandLimit : TSignal;
public
constructor Create( aParent: TSynthPatch; const aName: string); override;
procedure SetDefaults; override;
procedure DoTick; override;
function InputName ( anIndex: Integer): string; override;
function OutputName( anIndex: Integer): string; override;
end;
TModMultiOsc = class( TModBaseOsc)
strict private
const
i_frequency = 0;
i_fm = 1;
i_phase = 2;
i_sync = 3;
i_mute = 4;
i_shape = 5;
i_pmlevel = 6;
i_fmlevel = 7;
i_cents = 8;
i_bandlimit = 9;
i_freq = 10;
i_pwm = 11;
i_pwmlevel = 12;
const
o_outsine = 0;
o_outtri = 1;
o_outsaw = 2;
o_outsquare = 3;
public
procedure SetDefaults; override;
procedure DoTick; override;
function InputName( anIndex: Integer): string; override;
function OutputName( anIndex: Integer): string; override;
end;
// The blepper
function PolyBLEP( aPhase, anAmount, aDeltaPhase: TSignal): TSignal; inline;
var
dt: TSignal;
begin
dt := anAmount * aDeltaPhase;
if aPhase < dt
then begin
aPhase := aPhase / dt - 1;
Result := - aPhase * aPhase;
end
else if aPhase > 1 - dt
then begin
aPhase := ( aPhase - 1) / dt + 1;
Result := aPhase * aPhase;
end
else Result := 0
end;
// DoTick methods
procedure TModBaseOsc.DoTick; // override;
var
Trunced: Integer;
begin
if ( not FOldSync) and SignalToLogic( FInputs[ i_sync])
then FPhase := 0.0;
FOldSync := SignalToLogic( FInputs[ i_sync]);
FPhaseDelta := LookupPhaseDelta( FInputs[ i_frequency] + FInputs[ i_freq] + FInputs[ i_fm] * FInputs[ i_fmlevel] + FInputs[ i_cents]);
FPhase := FPhase + FPhaseDelta;
Trunced := Trunc( FPhase);
FPhase := FPhase - Trunced;
if FPhase < 0
then FPhase := FPhase + 1;
FPosition := FPhase + Clip( FInputs[ i_phase] * FInputs[ i_pmlevel], -1, 1);
Trunced := Trunc( FPosition);
FPosition := FPosition - Trunced;
if FPosition < 0
then FPosition := FPosition + 1;
FShape := Round( FInputs[ i_shape ]);
FBandLimit := FInputs[ i_bandlimit];
end;
procedure TModMultiOsc.DoTick; // override;
var
pwmp : TSignal;
aValue : TSignal;
aMute : tSignal;
aBlep : TSignal;
begin
inherited; // For phase calculations - calls TModBaseOsc.DoTick
aMute := SignalToMute( FInputs[ i_mute]);
aBlep := PolyBLEP( FPosition, FBandLimit, FPhaseDelta);
FOutputs[ o_outsine] := aMute * LookupSine( FPosition);
if FPosition < 0.5
then aValue := 1
else aValue := - 1;
aValue := 4 * ( aValue + aBlep - PolyBLEP( FloatMod( FPosition + 0.5, 1), FBandLimit, FPhaseDelta));
FOutputs[ o_outtri] := aMute * ( FPhaseDelta * aValue + ( 1 - FPhaseDelta) * FOutputs[ o_outtri]);
FOutputs[ o_outsaw] := aMute * (( 2.0 * FPosition - 1) - aBlep);
pwmp := Clip( 0.5 * ( 1.0 - FInputs[ i_pwm] * FInputs[ i_pwmlevel]), 0.01, 0.99);
if FPosition < pwmp
then aValue := 1
else aValue := - 1;
FOutputs[ o_outsquare] := aMute * ( aValue + aBlep - PolyBLEP( FloatMod( FPosition + ( 1 - pwmp), 1), FBandLimit, FPhaseDelta));
end;
|
_________________ Jan
also .. could someone please turn down the thermostat a bit.
 |
|
Back to top
|
|
 |
JovianPyx

Joined: Nov 20, 2007 Posts: 1988 Location: West Red Spot, Jupiter
Audio files: 224
|
Posted: Thu Feb 23, 2017 2:03 pm Post subject:
|
 |
|
There are only two ways I know of to calculate a periodic waveform as bandlimited:
1) add up all of the required sines at harmonic frequencies below the Nyquist limit. This is not easy to do realtime because there can be many sines to add.
2) a "brute force" brick wall filter that has Fc somewhat below Nyquist. You can take the simply calculated naive waveform and run it througbh the "brick wall" filter and out comes a band limited version of that waveform. This is done at an upsampled sample rate and then downsampled after the filter to maintain the correct pitch. This also has problems in terms of high computational load and has amplitute artifacts at the high end if the range (near Nyquist). This is normally done with several FIR filters. The "kernels" of these filters need to be prepared using something like Matlab or Octave, but I'm afraid I don't know enough about those to spell that out here. I tried doing it as an experiment years ago and while it worked, it was too computationally expensive to be practical. That experiment made it clear to me that a pre-computed wavetable was a computationally more practical method to get non sine periodic waveforms to work. And there are issues with that method too given that you probably need to be able to change the pitch and one wave table is not going to cut it.
This is what I would call a "classic" DSP problem - the simulation or computation of classical waveforms that we import from the analog world into the digital world. Stuff that's easy in the analog domain is just messy in the digital domain. _________________ FPGA, dsPIC and Fatman Synth Stuff
Time flies like a banana. Fruit flies when you're having fun. BTW, Do these genes make my ass look fat? corruptio optimi pessima
|
|
Back to top
|
|
 |
JovianPyx

Joined: Nov 20, 2007 Posts: 1988 Location: West Red Spot, Jupiter
Audio files: 224
|
Posted: Thu Feb 23, 2017 2:08 pm Post subject:
|
 |
|
Yes - as Blue Hell posts, there is "BLEP" and other similar methods that begin with BL such as BLIT (band limited impulse train). I've personally no experience with these, but they are widely used in the digital synthesizer realm. _________________ FPGA, dsPIC and Fatman Synth Stuff
Time flies like a banana. Fruit flies when you're having fun. BTW, Do these genes make my ass look fat? corruptio optimi pessima
|
|
Back to top
|
|
 |
gdavis
Joined: Feb 27, 2013 Posts: 359 Location: San Diego
Audio files: 1
|
Posted: Thu Feb 23, 2017 5:07 pm Post subject:
|
 |
|
KammutierSpule wrote: | I dont much about processing so I dont know how this is called.
I will explain again. consider you have the triangle wave:
at t time, the tri wave, it will be at one voltage level (some intensity value)
at t+1 you will output a sample to be played.
but.. lets say that at t+0.12 there was the time when the triangle wave get the peak (max value).
So I am wondering if the filter result will be different if I consider (catch) that max peak of the wave..
I can do oversampling.. but.. it will also not solve "my idea of adaptive" (sample at the precise moment that the change happen) |
I think this is really a non-issue that is being blown out of proportion due to a lack of understanding of DSP fundamentals.
As long as your Fs is suitable for the signal you're trying to reproduce, this "missing" of the peak will not affect the output.
To my understanding, band limiting and oversampling are related to addressing issues of aliasing and the output low pass filter (analog filter).
Basically, when a digital signal is converted to analog all your original frequency content from 0Hz to Fs/2 will be repeated again in the higher frequencies above Fs/2. In order to remove this an analog low pass filter on the output is used. The problem is that the closer your sampling frequency is to the Nyquist frequency, the steeper your filter needs to be which is more difficult and expensive to implement. Oversampling and band limiting in the digital domain puts more space between your original signal and the aliased signal which lets you use a more gradual (cheaper) low pass filter.
If you're using a sampling frequency of 44kHz, your not going to be able to hear the the aliased frequencies anyway. For hi fi there might be reasons to worry about this but for synths I don't think it's a big deal. I don't have as much experience with DSP as the other guys, but I've done a simple digital saw tooth generator at 16 bits and 44kHz and it seems to work fine without doing anything to address any of the "issues" discussed in this thread.
Back to the peak, it's going to be rounded off to whatever the max frequency your working with is anyway, so any piece that you "miss" is going to get "filled in" regardless. The sharper the peak, the more overtones are created. Missing part of the peak just means you're losing higher overtones which can only be reproduced by increasing the sample frequency. Once you get to about 44kHz sample rate, you won't be able to hear any higher overtones.
If you run a saw wave through a low pass vcf, and look at the output on a scope, you will see the the peak get rounded off more and more as you lower the filter cutoff frequency. As you raise the cutoff frequency, it gets sharper but there's a limit to how sharp it can get. _________________ My synth build blog: http://gndsynth.blogspot.com/ |
|
Back to top
|
|
 |
KammutierSpule
Joined: Feb 07, 2008 Posts: 40 Location: Aveiro - Portugal
|
Posted: Thu Feb 23, 2017 5:22 pm Post subject:
|
 |
|
gdavis wrote: |
I think this is really a non-issue that is being blown out of proportion due to a lack of understanding of DSP fundamentals.
As long as your Fs is suitable for the signal you're trying to reproduce, this "missing" of the peak will not affect the output.
Back to the peak, it's going to be rounded off to whatever the max frequency your working with is anyway, so any piece that you "miss" is going to get "filled in" regardless. The sharper the peak, the more overtones are created. Missing part of the peak just means you're losing higher overtones which can only be reproduced by increasing the sample frequency. Once you get to about 44kHz sample rate, you won't be able to hear any higher overtones.
|
I understand that on relation to the Fs.
However note that my concern was not because of the oscillator but on the filter feeding (and then output of the filter)
So I was wondering how much important that missing peak value could be for the filter / how much it will influence the filtered result.
In any case, I am not an "Analog Audiophile" person so yeah.. this starts to be a bit ridiculous for me too .. maybe  |
|
Back to top
|
|
 |
gdavis
Joined: Feb 27, 2013 Posts: 359 Location: San Diego
Audio files: 1
|
Posted: Thu Feb 23, 2017 5:56 pm Post subject:
|
 |
|
KammutierSpule wrote: | gdavis wrote: |
I think this is really a non-issue that is being blown out of proportion due to a lack of understanding of DSP fundamentals.
As long as your Fs is suitable for the signal you're trying to reproduce, this "missing" of the peak will not affect the output.
Back to the peak, it's going to be rounded off to whatever the max frequency your working with is anyway, so any piece that you "miss" is going to get "filled in" regardless. The sharper the peak, the more overtones are created. Missing part of the peak just means you're losing higher overtones which can only be reproduced by increasing the sample frequency. Once you get to about 44kHz sample rate, you won't be able to hear any higher overtones.
|
I understand that on relation to the Fs.
However note that my concern was not because of the oscillator but on the filter feeding (and then output of the filter)
So I was wondering how much important that missing peak value could be for the filter / how much it will influence the filtered result. |
It's all the same. As long as everything is at the same consistent Fs there won't be any adverse affect going into the filter, it's all dealing with the same range of frequencies that will be converted at the end.
Quote: | In any case, I am not an "Analog Audiophile" person so yeah.. this starts to be a bit ridiculous for me too .. maybe  |
It's all good, educational and informative discussion  _________________ My synth build blog: http://gndsynth.blogspot.com/ |
|
Back to top
|
|
 |
|