[music-dsp] basic question
James Chandler Jr
jchandjr at bellsouth.net
Tue Sep 5 23:43:31 EDT 2000
Hi, Brian
Following is a listing for a Chamberlin State Variable filter that works
pretty well. I hope the email doesn't butcher the formatting too much.
If the formatting gets hashed, I'll try again and include the file instead.
James Chandler Jr.
//StateVarFilt.h
//(C) James Chandler Jr.
//jameschandlerjr at compuserve.com
//This code compiles fine on Metrowerks Codewarrior 5 for Mac.
//Changes might be necessary on other platforms
/*********************
This is an adaptation of the Hal Chamberlin State Variable Filter as
described in his
book "Musical Applications of Microprocessors.
It is a very useful general-duty multimode filter. It sounds good and is
pretty fast. It sounds pretty good if
swept in real time.
But it has two drawbacks--
1. The filter becomes unstable at higher frequencies.
2. The cutoff frequency for Lowpass, HighPass and Notch are pretty close to
the calculated values,
but the frequency of the Bandpass is only approximate and varies by
frequency and Q.
To fix problem 1, I added oversampling for frequencies higher than Fs/8
(5500 Hz for 44.1 K digital audio).
At frequencies lower than Fs/8, it runs faster and automatically does not
use oversampling.
To fix problem 2, I ran a bunch of sine waves thru the filter to determine
appropriate fudge factors for various
values of Q. This fudge table is only used for the bandpass state. The data
is in BPFreqMultArray[][].
This filteris probably still not "scientifically accurate", but it is "close
enough for rock'n'roll" and
sounds pretty good
The Filter() method filters one float sample at a time (32 bit samples in
Mac). It returns the
"primary" filter mode you have selected, and also adds whatever gain you
have selected
when you set up the filter.
However, LP, HP, BP, and Notch raw instantaneous values are public variables
(though Gain
has not been applied to these variables). So you can use more than one
filter mode simultaneously,
by reading the public variables after calling the Filter() method.
This class doesn't have a block-buffer processing method, but if you need to
crank a lot of data
then this would be a good thing to add. A block buffer method would probably
run faster than calling
Filter() for each individual sample.
In my programs I often change values in real-time, so I usually have to send
a sample at a time,
therefore haven't ever bothered to write a block-buffer method.
Informational note-- PowerMac runs math up to 10 X faster, if all variables
are sized as multiples of 4 bytes. If you litter your code with chars,
bytes, or shorts, the PPC
may be much slower than you would like.
In my tests on PPC, float (32 bit) runs faster than double (64 bit), even
though the PPC auto-converts
and does the actual internal math at 64 bit resolution. I suppose this is
because floats have less stack
overhead, though there may be other factors.
void SetFilter(long NewSampleRate,float NewFc,float NewQ,float
NewOutGain,long NewFilterMode);
I usually use this function to adjust (or readjust) the filter.
NewSampleRate-- The sample rate of the input/output audio
NewFc-- The center frequency of the filter
NewOutGain-- The gain of the filter (as a simple multiplier). For example,
1.0 is unity gain, 2.0 is +6 dB,
0.5 is -6 dB, etc.
It is convenient to change the gain within the filter, in case you are
making some kind of equalizer, so
the gain doesn't have to be applied in a separate program step in the
calling routine.
At high Q, the gain is applied relative to the highest peak in the frequency
response at Fc.
At high Q, the passband for a lowpass or highpass filter will be pretty
attenuated, because the peak at
resonance gets the gain setting, and a high-Q peak is much louder than the
passband.
NewFilterMode-- The Primary filter mode (though the other state variable
outputs are available in
public variables LP, BP, HP and Notch
// StateVarFilter modes
// 1=LowPass
// 2=Bandpass
// 3=Highpass
// 4=Notch
// 5=AllPass not currently supported
//All mode outputs are non-inverting
//Output of Filter method returns the requested mode output, with
appropriate gain applied
//All modes are available simultaneously by getting the data from the LP,
HP, BP, and Notch members
void SetSampleRate(long NewSampleRate,Boolean Recalc);
void SetFc(float NewFc,Boolean Recalc);
void SetQ(float NewQ,Boolean Recalc);
void SetOutGain(float NewOutGain,Boolean Recalc);
void SetFilterMode(long NewFilterMode,Boolean Recalc);
I rarely use these functions, but they provide ways of changing a single
parameter of the filter
without setting up the entire filter.
If you want the changes to actually "do something," be sure to set Recalc to
TRUE for SetSampleRate, SetFc, and SetQ.
SetOutGain doesn't require a Recalc, and SetFilterMode only requires a
Recalc if switching into or
out of BandPass mode.
void CalcInternalVars(void);
CalcInternalVars is called by the other "setting" methods, so you usually
would not need to
call this guy.
InitDelays()
Zero the filter delay variables. This is automatically done when you
instantiate
a filter object, but if you need to start fresh without destroying and
re-creating the object,
call InitDelays.
float Filter(float InSample);
Call Filter() with an input sample, and it returns the output sample of
whatever filter mode you
have selected. This is the function that actually does something useful (G).
**********************************/
#ifndef __STATE_VAR_FILT__
#define __STATE_VAR_FILT__
#pragma once
#define pi 3.141592654
#include "math.h"
#include "MacTypes.h"
#include "MacMemory.h"
class TFloatStateVarFilt
{
public:
float LP;
float HP;
float BP;
float Notch;
TFloatStateVarFilt();
~TFloatStateVarFilt();
void InitDelays(void);
void SetSampleRate(long NewSampleRate,Boolean Recalc);
void SetFc(float NewFc,Boolean Recalc);
void SetQ(float NewQ,Boolean Recalc);
void SetOutGain(float NewOutGain,Boolean Recalc);
void SetFilterMode(long NewFilterMode,Boolean Recalc);
void SetFilter(long NewSampleRate,float NewFc,float NewQ,float
NewOutGain,long NewFilterMode);
void CalcInternalVars(void);
float Filter(float InSample);
private:
float Fs;
float Fc;
float Q;
float OutGain;
float GainMult;
float FilterProcessGain;
long FilterMode;
float D1;
float D2;
float FMult;
float QMult;
long OverSampleRate;
float LastInSample;
};
#endif //__STATE_VAR_FILT__
//StateVarFilt.c
//(C) James Chandler Jr.
//jameschandlerjr at compuserve.com
//This code compiles fine on Metrowerks Codewarrior 5 for Mac.
//Changes may be necessary on other platforms
#include "StateVarFilt.h"
// Pow(R1,R2:double);
// Log10(R1:double);
//double Pow(R1,R2:double)
//{
// Result = exp (R2 * ln(R1));
//}
//double Log10(R1:double)
//{
// Result = LN(R1) / LN(10);
//}
float BPFreqMultArray[221][12] =
{{0.876,0.978,0.996,1,1,1,1,1,1,1,1,1},
{0.874666667,0.978,0.995333333,0.999333333,1,1,1,1,1,1,1,1},
{0.87344,0.97624,1.0024,0.9984,0.99944,0.99944,0.99944,1,1,1,1,1},
{0.870742857,0.976171429,0.999714286,0.998,0.999028571,0.999028571,0.9990285
71,0.999428571,0.999428571,1,1,1},
{0.908888889,0.975777778,0.993333333,0.997777778,0.998777778,0.998777778,0.9
99111111,0.999111111,0.999111111,0.999777778,0.999777778,0.999777778},
{0.966909091,0.974,0.992363636,0.997454545,0.998454545,0.998454545,0.9992727
27,0.999272727,0.999272727,0.999454545,0.999454545,0.999454545 },
{0.986,0.974153846,0.991384615,0.997230769,0.998538462,0.998846154,0.9995384
62,1.000153846,0.999846154,0.999846154,0.999846154,1.000153846},
{0.981733333,0.975466667,0.9904,0.997066667,0.998866667,0.999666667,0.999866
667,1.001466667,1.000666667,1.000666667,1.000666667,1.001466667},
{0.981470588,0.979823529,0.988235294,0.996705882,0.998411765,0.999411765,0.9
99411765,1.001117647,1.000411765,1.000411765,1.000411765,1.001117647},
{0.983947368,0.986263158,0.985263158,0.996210526,0.997421053,0.998421053,0.9
98421053,0.999631579,0.999421053,0.999421053,0.999421053,0.999631579},
{0.98147619,0.986619048,0.985904762,0.995333333,0.996619048,0.997619048,0.99
8190476,0.998904762,0.998904762,0.998904762,0.998904762,0.998904762},
{0.975347826,0.982478261,0.989217391,0.994173913,0.995956522,0.996956522,0.9
98521739,0.99873913,0.99873913,0.99873913,0.99873913,0.99873913},
{0.97232,0.98032,0.9892,0.99348,0.9956,0.99664,0.99848,0.99848,0.99848,0.998
64,0.99864,0.99864},
{0.971703704,0.979703704,0.986592593,0.993148148,0.995481481,0.996592593,0.9
98148148,0.998148148,0.998148148,0.998592593,0.998592593,0.998592593},
{0.971172414,0.979172414,0.984344828,0.992862069,0.99537931,0.996551724,0.99
7862069,0.997862069,0.997862069,0.998551724,0.998551724,0.998551724},
{0.970709677,0.978709677,0.982387097,0.992612903,0.995290323,0.996516129,0.9
97612903,0.997612903,0.997612903,0.998516129,0.998516129,0.998516129 },
{0.969969697,0.977818182,0.981424242,0.992272727,0.995060606,0.996424242,0.9
97272727,0.997424242,0.997424242,0.998424242,0.998424242,0.998424242},
{0.969,0.976571429,0.981285714,0.991857143,0.994714286,0.996285714,0.9968571
43,0.997285714,0.997285714,0.998285714,0.998285714,0.998285714},
{0.968135135,0.975459459,0.981162162,0.991486486,0.994405405,0.996162162,0.9
96486486,0.997162162,0.997162162,0.998162162,0.998162162,0.998162162 },
{0.967358974,0.974461538,0.981051282,0.991153846,0.994128205,0.996051282,0.9
96153846,0.997051282,0.997051282,0.998051282,0.998051282,0.998051282},
{0.965,0.97404878,0.98104878,0.990902439,0.993756098,0.995902439,0.99604878,
0.996902439,0.99704878,0.997902439,0.997902439,0.99804878},
{0.96127907,0.974139535,0.981139535,0.99072093,0.993302326,0.99572093,0.9961
39535,0.99672093,0.997139535,0.99772093,0.99772093,0.998139535},
{0.957888889,0.974222222,0.981222222,0.990555556,0.992888889,0.995555556,0.9
96222222,0.996555556,0.997222222,0.997555556,0.997555556,0.998222222 },
{0.954787234,0.974297872,0.981297872,0.990404255,0.992510638,0.995404255,0.9
96297872,0.996404255,0.997297872,0.997404255,0.997404255,0.998297872},
{0.954755102,0.971836735,0.979591837,0.98944898,0.992591837,0.994877551,0.99
6306122,0.99644898,0.997306122,0.997163265,0.99744898,0.998306122},
{0.957431373,0.967137255,0.976313725,0.987784314,0.993078431,0.994019608,0.9
96254902,0.996666667,0.997254902,0.996843137,0.997666667,0.998254902},
{0.95990566,0.962792453,0.973283019,0.986245283,0.993528302,0.993226415,0.99
6207547,0.996867925,0.997207547,0.99654717,0.997867925,0.998207547},
{0.9622,0.958763636,0.970472727,0.984818182,0.993945455,0.992490909,0.996163
636,0.997054545,0.997163636,0.996272727,0.998054545,0.998163636},
{0.960368421,0.95722807,0.969473684,0.984333333,0.993631579,0.992192982,0.99
5912281,0.996912281,0.996912281,0.996192982,0.997912281,0.998052632 },
{0.954830508,0.957932203,0.970101695,0.984694915,0.992661017,0.992288136,0.9
95474576,0.996474576,0.996474576,0.996288136,0.997474576,0.997881356},
{0.949655738,0.958590164,0.970688525,0.985032787,0.991754098,0.992377049,0.9
95065574,0.996065574,0.996065574,0.996377049,0.997065574,0.997721311},
{0.944809524,0.959206349,0.971238095,0.985349206,0.990904762,0.992460317,0.9
9468254,0.99568254,0.99568254,0.996460317,0.99668254,0.997571429},
{0.939815385,0.957507692,0.970061538,0.984753846,0.990030769,0.992169231,0.9
94169231,0.995169231,0.995307692,0.996307692,0.996584615,0.997307692},
{0.934686567,0.953701493,0.967313433,0.983328358,0.989134328,0.991537313,0.9
93537313,0.994537313,0.994940299,0.995940299,0.996746269,0.996940299},
{0.929855072,0.950115942,0.964724638,0.981985507,0.988289855,0.990942029,0.9
92942029,0.993942029,0.994594203,0.995594203,0.996898551,0.996594203 },
{0.925295775,0.946732394,0.96228169,0.98071831,0.987492958,0.990380282,0.992
380282,0.993380282,0.994267606,0.995267606,0.997042254,0.996267606},
{0.924054795,0.945780822,0.961506849,0.980369863,0.987232877,0.990232877,0.9
92232877,0.993232877,0.994232877,0.995232877,0.996958904,0.99609589},
{0.925866667,0.947066667,0.962266667,0.980866667,0.987466667,0.990466667,0.9
92466667,0.993466667,0.994466667,0.995466667,0.996666667,0.996066667},
{0.927584416,0.948285714,0.962987013,0.981337662,0.987688312,0.990688312,0.9
92688312,0.993688312,0.994688312,0.995688312,0.99638961,0.996038961},
{0.92921519,0.949443038,0.963670886,0.98178481,0.987898734,0.990898734,0.992
898734,0.993898734,0.994898734,0.995898734,0.996126582,0.996012658},
{0.928666667,0.949037037,0.963308642,0.98162963,0.987753086,0.990814815,0.99
2888889,0.993876543,0.994888889,0.995901235,0.995925926,0.995950617},
{0.926096386,0.947180723,0.961975904,0.980915663,0.987277108,0.990457831,0.9
92674699,0.993638554,0.994674699,0.995710843,0.995783133,0.995855422},
{0.923647059,0.945411765,0.960705882,0.980235294,0.986823529,0.990117647,0.9
92470588,0.993411765,0.994470588,0.995529412,0.995647059,0.995764706},
{0.921310345,0.943724138,0.959494253,0.979586207,0.986390805,0.989793103,0.9
92275862,0.993195402,0.994275862,0.995356322,0.995517241,0.995678161},
{0.919078652,0.94211236,0.958337079,0.978966292,0.985977528,0.989483146,0.99
2089888,0.992988764,0.994089888,0.995191011,0.995393258,0.995595506},
{0.916945055,0.940571429,0.957230769,0.978373626,0.985582418,0.989186813,0.9
91912088,0.992791209,0.993912088,0.995032967,0.995274725,0.995516484},
{0.914903226,0.939096774,0.956172043,0.977806452,0.985204301,0.988903226,0.9
91741935,0.992602151,0.993741935,0.99488172,0.99516129,0.99544086},
{0.912947368,0.937684211,0.955157895,0.977263158,0.984842105,0.988631579,0.9
91578947,0.992421053,0.993578947,0.994736842,0.995052632,0.995368421},
{0.911072165,0.936329897,0.954185567,0.976742268,0.984494845,0.988371134,0.9
9142268,0.992247423,0.99342268,0.994597938,0.994948454,0.995298969},
{0.909272727,0.935030303,0.953252525,0.976242424,0.984161616,0.988121212,0.9
91272727,0.992080808,0.993272727,0.994464646,0.994848485,0.995232323},
{0.907544554,0.933782178,0.952356436,0.975762376,0.983841584,0.987881188,0.9
91128713,0.991920792,0.993128713,0.994336634,0.994752475,0.995168317},
{0.905883495,0.932582524,0.951495146,0.975300971,0.983533981,0.987650485,0.9
90990291,0.99176699,0.992990291,0.994213592,0.994660194,0.995106796},
{0.904285714,0.931428571,0.950666667,0.974857143,0.983238095,0.987428571,0.9
90857143,0.991619048,0.992857143,0.994095238,0.994571429,0.995047619},
{0.902747664,0.930317757,0.949869159,0.974429907,0.982953271,0.987214953,0.9
90728972,0.991476636,0.992728972,0.993981308,0.994485981,0.994990654},
{0.901266055,0.929247706,0.949100917,0.974018349,0.982678899,0.987009174,0.9
90605505,0.99133945,0.992605505,0.99387156,0.99440367,0.99493578},
{0.899837838,0.928216216,0.94836036,0.973621622,0.982414414,0.986810811,0.99
0486486,0.991207207,0.992486486,0.993765766,0.994324324,0.994882883},
{0.898460177,0.927221239,0.947646018,0.973238938,0.982159292,0.986619469,0.9
90371681,0.991079646,0.992371681,0.993663717,0.994247788,0.994831858},
{0.897130435,0.92626087,0.946956522,0.972869565,0.981913043,0.986434783,0.99
026087,0.990956522,0.99226087,0.993565217,0.994173913,0.994782609},
{0.895846154,0.925333333,0.946290598,0.972512821,0.981675214,0.98625641,0.99
0153846,0.990837607,0.992153846,0.993470085,0.994102564,0.994735043},
{0.894605042,0.924436975,0.945647059,0.972168067,0.981445378,0.986084034,0.9
9005042,0.990722689,0.99205042,0.993378151,0.994033613,0.994689076},
{0.89307438,0.923305785,0.944842975,0.971752066,0.981157025,0.985867769,0.98
985124,0.990578512,0.991933884,0.993256198,0.993933884,0.99461157},
{0.891268293,0.92195122,0.943886179,0.971268293,0.980813008,0.985609756,0.98
9560976,0.990406504,0.991804878,0.993105691,0.993804878,0.994504065},
{0.88952,0.92064,0.94296,0.9708,0.98048,0.98536,0.98928,0.99024,0.99168,0.99
296,0.99368,0.9944},
{0.887826772,0.919370079,0.942062992,0.970346457,0.98015748,0.98511811,0.989
007874,0.99007874,0.991559055,0.992818898,0.993559055,0.994299213},
{0.886186047,0.918139535,0.941193798,0.969906977,0.979844961,0.984883721,0.9
88744186,0.989922481,0.99144186,0.992682171,0.99344186,0.99420155},
{0.88459542,0.916946565,0.940351145,0.969480916,0.979541985,0.984656489,0.98
848855,0.989770992,0.991328244,0.992549618,0.993328244,0.99410687},
{0.883052632,0.915789474,0.939533835,0.969067669,0.97924812,0.98443609,0.988
240602,0.98962406,0.991218045,0.992421053,0.993218045,0.994015038},
{0.881555556,0.914666667,0.938740741,0.968666667,0.978962963,0.984222222,0.9
88,0.989481481,0.991111111,0.992296296,0.993111111,0.993925926},
{0.88010219,0.913576642,0.937970803,0.968277372,0.978686131,0.984014599,0.98
7766423,0.989343066,0.991007299,0.992175182,0.993007299,0.993839416},
{0.878690647,0.912517986,0.937223022,0.967899281,0.978417266,0.98381295,0.98
7539568,0.989208633,0.990906475,0.992057554,0.992906475,0.993755396},
{0.877319149,0.911489362,0.936496454,0.967531915,0.978156028,0.983617021,0.9
87319149,0.989078014,0.990808511,0.991943262,0.992808511,0.993673759},
{0.875986014,0.91048951,0.93579021,0.967174825,0.977902098,0.983426573,0.987
104895,0.988951049,0.990713287,0.991832168,0.992713287,0.993594406},
{0.874689655,0.909517241,0.935103448,0.966827586,0.977655172,0.983241379,0.9
86896552,0.988827586,0.99062069,0.991724138,0.99262069,0.993517241},
{0.873428571,0.908571429,0.934435374,0.966489796,0.977414966,0.983061224,0.9
86693878,0.988707483,0.990530612,0.991619048,0.992530612,0.993442177},
{0.872201342,0.907651007,0.933785235,0.966161074,0.977181208,0.982885906,0.9
86496644,0.988590604,0.990442953,0.991516779,0.992442953,0.993369128},
{0.871006623,0.906754967,0.933152318,0.96584106,0.976953642,0.982715232,0.98
6304636,0.988476821,0.990357616,0.991417219,0.992357616,0.993298013},
{0.869843137,0.905882353,0.932535948,0.965529412,0.976732026,0.98254902,0.98
6117647,0.988366013,0.99027451,0.991320261,0.99227451,0.993228758},
{0.868709677,0.905032258,0.931935484,0.965225806,0.976516129,0.982387097,0.9
85935484,0.988258065,0.990193548,0.991225806,0.992193548,0.99316129},
{0.867605096,0.904203822,0.931350318,0.964929936,0.976305732,0.982229299,0.9
85757962,0.988152866,0.99011465,0.991133758,0.99211465,0.993095541},
{0.866528302,0.903396226,0.930779874,0.964641509,0.976100629,0.982075472,0.9
85584906,0.988050314,0.990037736,0.991044025,0.992037736,0.993031447},
{0.865055901,0.902298137,0.929975155,0.964236025,0.975826087,0.981875776,0.9
85403727,0.987913043,0.989925466,0.990931677,0.991937888,0.992944099},
{0.863202454,0.900920245,0.928944785,0.963717791,0.975484663,0.981631902,0.9
85214724,0.987742331,0.989779141,0.990797546,0.991815951,0.992834356},
{0.861393939,0.899575758,0.927939394,0.963212121,0.975151515,0.981393939,0.9
85030303,0.987575758,0.989636364,0.990666667,0.99169697,0.992727273},
{0.859628743,0.898263473,0.926958084,0.962718563,0.974826347,0.981161677,0.9
84850299,0.987413174,0.989497006,0.990538922,0.991580838,0.992622754},
{0.857905325,0.896982249,0.926,0.962236686,0.974508876,0.980934911,0.9846745
56,0.987254438,0.989360947,0.990414201,0.991467456,0.99252071},
{0.856222222,0.895730994,0.925064327,0.961766082,0.97419883,0.98071345,0.984
502924,0.987099415,0.98922807,0.990292398,0.991356725,0.992421053},
{0.854578035,0.894508671,0.924150289,0.961306358,0.973895954,0.98049711,0.98
433526,0.986947977,0.989098266,0.99017341,0.991248555,0.992323699},
{0.852971429,0.893314286,0.923257143,0.960857143,0.9736,0.980285714,0.984171
429,0.9868,0.988971429,0.990057143,0.991142857,0.992228571},
{0.85140113,0.892146893,0.922384181,0.960418079,0.973310734,0.980079096,0.98
4011299,0.986655367,0.988847458,0.989943503,0.991039548,0.992135593},
{0.849865922,0.891005587,0.921530726,0.959988827,0.973027933,0.979877095,0.9
83854749,0.986513966,0.988726257,0.989832402,0.990938547,0.992044693},
{0.848364641,0.889889503,0.920696133,0.959569061,0.972751381,0.979679558,0.9
83701657,0.986375691,0.988607735,0.989723757,0.990839779,0.991955801},
{0.846896175,0.888797814,0.919879781,0.95915847,0.972480874,0.979486339,0.98
3551913,0.986240437,0.988491803,0.989617486,0.990743169,0.991868852},
{0.845459459,0.88772973,0.919081081,0.958756757,0.972216216,0.979297297,0.98
3405405,0.986108108,0.988378378,0.989513514,0.990648649,0.991783784},
{0.844053476,0.886684492,0.918299465,0.958363636,0.971957219,0.979112299,0.9
83262032,0.98597861,0.98826738,0.989411765,0.99055615,0.991700535},
{0.842677249,0.885661376,0.917534392,0.957978836,0.971703704,0.978931217,0.9
83121693,0.985851852,0.98815873,0.989312169,0.990465608,0.991619048},
{0.841329843,0.884659686,0.91678534,0.957602094,0.971455497,0.978753927,0.98
2984293,0.985727749,0.988052356,0.98921466,0.990376963,0.991539267},
{0.840010363,0.883678756,0.916051813,0.957233161,0.971212435,0.978580311,0.9
82849741,0.985606218,0.987948187,0.989119171,0.990290155,0.99146114},
{0.838717949,0.882717949,0.915333333,0.956871795,0.970974359,0.978410256,0.9
82717949,0.985487179,0.987846154,0.989025641,0.990205128,0.991384615},
{0.837451777,0.88177665,0.914629442,0.956517766,0.970741117,0.978243655,0.98
2588832,0.985370558,0.987746193,0.98893401,0.990121827,0.991309645},
{0.836211055,0.880854271,0.913939698,0.956170854,0.970512563,0.978080402,0.9
82462312,0.985256281,0.987648241,0.988844221,0.990040201,0.991236181},
{0.834905473,0.879870647,0.91321393,0.955791045,0.970258706,0.977880597,0.98
2308458,0.985124378,0.987522388,0.988746269,0.989950249,0.991154229},
{0.833536946,0.878827586,0.912453202,0.95537931,0.969980296,0.97764532,0.982
128079,0.984975369,0.987369458,0.988640394,0.989852217,0.991064039},
{0.832195122,0.877804878,0.911707317,0.95497561,0.969707317,0.977414634,0.98
195122,0.984829268,0.987219512,0.988536585,0.989756098,0.99097561},
{0.830879227,0.876801932,0.910975845,0.95457971,0.969439614,0.977188406,0.98
1777778,0.98468599,0.987072464,0.988434783,0.989661836,0.990888889},
{0.829588517,0.875818182,0.910258373,0.954191388,0.969177033,0.976966507,0.9
81607656,0.984545455,0.98692823,0.988334928,0.989569378,0.990803828},
{0.828322275,0.874853081,0.909554502,0.953810427,0.968919431,0.976748815,0.9
81440758,0.984407583,0.98678673,0.988236967,0.989478673,0.990720379},
{0.827079812,0.873906103,0.90886385,0.95343662,0.968666667,0.976535211,0.981
276995,0.9842723,0.986647887,0.988140845,0.989389671,0.990638498},
{0.825860465,0.872976744,0.908186047,0.953069767,0.968418605,0.976325581,0.9
81116279,0.984139535,0.986511628,0.988046512,0.989302326,0.99055814},
{0.824663594,0.872064516,0.907520737,0.952709677,0.968175115,0.976119816,0.9
80958525,0.984009217,0.98637788,0.987953917,0.98921659,0.990479263},
{0.823488584,0.87116895,0.90686758,0.952356164,0.967936073,0.975917808,0.980
803653,0.983881279,0.986246575,0.987863014,0.98913242,0.990401826},
{0.822334842,0.870289593,0.906226244,0.95200905,0.967701357,0.975719457,0.98
0651584,0.983755656,0.986117647,0.987773756,0.989049774,0.990325792},
{0.821201794,0.869426009,0.905596413,0.951668161,0.967470852,0.975524664,0.9
80502242,0.983632287,0.985991031,0.987686099,0.98896861,0.990251121},
{0.820088889,0.868577778,0.904977778,0.951333333,0.967244444,0.975333333,0.9
80355556,0.983511111,0.985866667,0.9876,0.988888889,0.990177778},
{0.818995595,0.867744493,0.904370044,0.951004405,0.967022026,0.975145374,0.9
80211454,0.98339207,0.985744493,0.987515419,0.988810573,0.990105727},
{0.817921397,0.866925764,0.903772926,0.950681223,0.966803493,0.974960699,0.9
80069869,0.983275109,0.985624454,0.987432314,0.988733624,0.990034934},
{0.816865801,0.866121212,0.903186147,0.950363636,0.966588745,0.974779221,0.9
79930736,0.983160173,0.985506494,0.987350649,0.988658009,0.989965368},
{0.815828326,0.865330472,0.902609442,0.950051502,0.966377682,0.974600858,0.9
79793991,0.98304721,0.985390558,0.987270386,0.988583691,0.989896996},
{0.814808511,0.864553191,0.902042553,0.949744681,0.966170213,0.974425532,0.9
79659574,0.98293617,0.985276596,0.987191489,0.988510638,0.989829787},
{0.813805907,0.86378903,0.901485232,0.949443038,0.965966245,0.974253165,0.97
9527426,0.982827004,0.985164557,0.987113924,0.988438819,0.989763713},
{0.812820084,0.863037657,0.900937238,0.949146444,0.96576569,0.974083682,0.97
939749,0.982719665,0.985054393,0.987037657,0.988368201,0.989698745},
{0.81166805,0.862157676,0.899975104,0.94879668,0.965410788,0.973809129,0.979
178423,0.982539419,0.984896266,0.986946058,0.988248963,0.989585062},
{0.810353909,0.861152263,0.898609053,0.948395062,0.96490535,0.973432099,0.97
8872428,0.982288066,0.984691358,0.986839506,0.988082305,0.989423868},
{0.809061224,0.860163265,0.897265306,0.948,0.964408163,0.973061224,0.9785714
29,0.982040816,0.984489796,0.986734694,0.987918367,0.989265306},
{0.807789474,0.859190283,0.89594332,0.947611336,0.963919028,0.972696356,0.97
8275304,0.981797571,0.984291498,0.986631579,0.987757085,0.989109312},
{0.806538153,0.858232932,0.89464257,0.947228916,0.963437751,0.972337349,0.97
7983936,0.981558233,0.984096386,0.98653012,0.987598394,0.988955823},
{0.805306773,0.857290837,0.89336255,0.94685259,0.962964143,0.971984064,0.977
697211,0.981322709,0.983904382,0.986430279,0.987442231,0.988804781},
{0.804094862,0.856363636,0.892102767,0.946482213,0.962498024,0.971636364,0.9
7741502,0.981090909,0.983715415,0.986332016,0.987288538,0.988656126},
{0.802901961,0.85545098,0.890862745,0.946117647,0.962039216,0.971294118,0.97
7137255,0.980862745,0.983529412,0.986235294,0.987137255,0.988509804},
{0.801727626,0.854552529,0.889642023,0.945758755,0.961587549,0.970957198,0.9
76863813,0.980638132,0.983346304,0.986140078,0.986988327,0.988365759},
{0.800571429,0.853667954,0.888440154,0.945405405,0.961142857,0.970625483,0.9
76594595,0.980416988,0.983166023,0.986046332,0.986841699,0.988223938},
{0.79943295,0.852796935,0.887256705,0.945057471,0.960704981,0.970298851,0.97
6329502,0.980199234,0.982988506,0.985954023,0.986697318,0.988084291},
{0.798311787,0.851939163,0.886091255,0.944714829,0.960273764,0.969977186,0.9
76068441,0.979984791,0.982813688,0.985863118,0.986555133,0.987946768},
{0.797207547,0.85109434,0.884943396,0.944377358,0.959849057,0.969660377,0.97
5811321,0.979773585,0.982641509,0.985773585,0.986415094,0.987811321},
{0.79611985,0.850262172,0.883812734,0.944044944,0.959430712,0.969348315,0.97
5558052,0.979565543,0.98247191,0.985685393,0.986277154,0.987677903},
{0.795048327,0.849442379,0.882698885,0.943717472,0.959018587,0.969040892,0.9
7530855,0.979360595,0.982304833,0.985598513,0.986141264,0.987546468},
{0.79399262,0.848634686,0.881601476,0.943394834,0.958612546,0.968738007,0.97
5062731,0.979158672,0.982140221,0.985512915,0.98600738,0.987416974},
{0.792952381,0.847838828,0.880520147,0.943076923,0.958212454,0.96843956,0.97
4820513,0.978959707,0.981978022,0.985428571,0.985875458,0.987289377},
{0.791927273,0.847054545,0.879454545,0.942763636,0.957818182,0.968145455,0.9
74581818,0.978763636,0.981818182,0.985345455,0.985745455,0.987163636},
{0.790916968,0.846281588,0.878404332,0.942454874,0.957429603,0.967855596,0.9
7434657,0.978570397,0.98166065,0.985263538,0.985617329,0.987039711},
{0.789921147,0.845519713,0.877369176,0.942150538,0.957046595,0.967569892,0.9
74114695,0.978379928,0.981505376,0.985182796,0.985491039,0.986917563},
{0.788362989,0.844313167,0.876533808,0.941658363,0.956740214,0.967352313,0.9
73935943,0.978227758,0.981380783,0.985060498,0.985395018,0.986825623},
{0.786254417,0.842671378,0.875893993,0.940982332,0.956508834,0.967201413,0.9
73809187,0.978113074,0.981286219,0.984897527,0.985328622,0.986763251},
{0.784175439,0.841052632,0.875263158,0.940315789,0.956280702,0.967052632,0.9
73684211,0.978,0.981192982,0.984736842,0.985263158,0.986701754},
{0.782125436,0.839456446,0.874641115,0.939658537,0.956055749,0.966905923,0.9
73560976,0.977888502,0.981101045,0.984578397,0.985198606,0.986641115},
{0.780103806,0.837882353,0.874027682,0.939010381,0.95583391,0.966761246,0.97
3439446,0.977778547,0.981010381,0.984422145,0.985134948,0.986581315},
{0.778109966,0.836329897,0.87342268,0.938371134,0.95561512,0.966618557,0.973
319588,0.977670103,0.980920962,0.984268041,0.985072165,0.986522337},
{0.776143345,0.834798635,0.872825939,0.937740614,0.955399317,0.966477816,0.9
73201365,0.97756314,0.980832765,0.984116041,0.985010239,0.986464164},
{0.77420339,0.833288136,0.872237288,0.937118644,0.955186441,0.966338983,0.97
3084746,0.977457627,0.980745763,0.983966102,0.984949153,0.98640678},
{0.772289562,0.83179798,0.871656566,0.936505051,0.954976431,0.96620202,0.972
969697,0.977353535,0.980659933,0.983818182,0.984888889,0.986350168},
{0.770401338,0.830327759,0.871083612,0.935899666,0.954769231,0.96606689,0.97
2856187,0.977250836,0.980575251,0.983672241,0.984829431,0.986294314},
{0.768538206,0.828877076,0.870518272,0.935302326,0.954564784,0.965933555,0.9
72744186,0.977149502,0.980491694,0.983528239,0.984770764,0.986239203},
{0.76669967,0.827445545,0.869960396,0.934712871,0.954363036,0.96580198,0.972
633663,0.977049505,0.980409241,0.983386139,0.984712871,0.986184818},
{0.764885246,0.826032787,0.869409836,0.934131148,0.954163934,0.965672131,0.9
7252459,0.97695082,0.980327869,0.983245902,0.984655738,0.986131148},
{0.763094463,0.824638436,0.86886645,0.933557003,0.953967427,0.965543974,0.97
2416938,0.97685342,0.980247557,0.983107492,0.984599349,0.986078176},
{0.761326861,0.823262136,0.868330097,0.932990291,0.953773463,0.965417476,0.9
7231068,0.976757282,0.980168285,0.982970874,0.984543689,0.98602589},
{0.759581994,0.821903537,0.867800643,0.932430868,0.953581994,0.965292605,0.9
72205788,0.976662379,0.980090032,0.982836013,0.984488746,0.985974277},
{0.757859425,0.8205623,0.867277955,0.931878594,0.953392971,0.965169329,0.972
102236,0.97656869,0.98001278,0.982702875,0.984434505,0.985923323},
{0.75615873,0.819238095,0.866761905,0.931333333,0.953206349,0.965047619,0.97
2,0.97647619,0.979936508,0.982571429,0.984380952,0.985873016},
{0.754479495,0.817930599,0.866252366,0.930794953,0.953022082,0.964927445,0.9
71899054,0.976384858,0.979861199,0.98244164,0.984328076,0.985823344},
{0.752821317,0.816639498,0.865749216,0.930263323,0.952840125,0.964808777,0.9
71799373,0.976294671,0.979786834,0.98231348,0.984275862,0.985774295},
{0.751015576,0.815221184,0.864903427,0.929676012,0.952523364,0.964566978,0.9
71601246,0.976130841,0.979638629,0.982155763,0.984161994,0.985676012},
{0.749065015,0.813678019,0.863721362,0.929034056,0.952074303,0.964204334,0.9
71306502,0.975894737,0.979417957,0.98196904,0.983987616,0.985529412},
{0.747138462,0.812153846,0.862553846,0.9284,0.951630769,0.963846154,0.971015
385,0.975661538,0.9792,0.981784615,0.983815385,0.985384615},
{0.745235474,0.810648318,0.861400612,0.9277737,0.951192661,0.963492355,0.970
727829,0.975431193,0.978984709,0.981602446,0.98364526,0.98524159},
{0.743355623,0.809161094,0.860261398,0.927155015,0.950759878,0.963142857,0.9
70443769,0.975203647,0.978772036,0.981422492,0.983477204,0.985100304},
{0.741498489,0.807691843,0.859135952,0.926543807,0.950332326,0.962797583,0.9
70163142,0.974978852,0.978561934,0.981244713,0.983311178,0.984960725},
{0.739663664,0.80624024,0.858024024,0.92593994,0.94990991,0.962456456,0.9698
85886,0.974756757,0.978354354,0.981069069,0.983147147,0.984822823},
{0.737850746,0.80480597,0.856925373,0.925343284,0.949492537,0.962119403,0.96
961194,0.974537313,0.978149254,0.980895522,0.982985075,0.984686567},
{0.736059347,0.803388724,0.855839763,0.924753709,0.949080119,0.96178635,0.96
9341246,0.974320475,0.977946588,0.980724036,0.982824926,0.984551929},
{0.734289086,0.801988201,0.854766962,0.924171091,0.948672566,0.961457227,0.9
69073746,0.974106195,0.977746313,0.980554572,0.982666667,0.984418879},
{0.732539589,0.800604106,0.853706745,0.923595308,0.948269795,0.961131965,0.9
68809384,0.973894428,0.977548387,0.980387097,0.982510264,0.98428739},
{0.730810496,0.799236152,0.852658892,0.923026239,0.94787172,0.960810496,0.96
8548105,0.973685131,0.97735277,0.980221574,0.982355685,0.984157434},
{0.729101449,0.797884058,0.851623188,0.922463768,0.947478261,0.960492754,0.9
68289855,0.973478261,0.97715942,0.980057971,0.982202899,0.984028986},
{0.727412104,0.79654755,0.850599424,0.921907781,0.947089337,0.960178674,0.96
8034582,0.973273775,0.9769683,0.979896254,0.982051873,0.983902017},
{0.72574212,0.795226361,0.849587393,0.921358166,0.946704871,0.959868195,0.96
7782235,0.973071633,0.97677937,0.97973639,0.981902579,0.983776504},
{0.724091168,0.793920228,0.848586895,0.920814815,0.946324786,0.959561254,0.9
67532764,0.972871795,0.976592593,0.979578348,0.981754986,0.983652422},
{0.722458924,0.792628895,0.847597734,0.92027762,0.945949008,0.95925779,0.967
286119,0.972674221,0.976407932,0.979422096,0.981609065,0.983529745},
{0.72084507,0.791352113,0.846619718,0.919746479,0.945577465,0.958957746,0.96
7042254,0.972478873,0.976225352,0.979267606,0.981464789,0.983408451},
{0.7192493,0.790089636,0.845652661,0.919221289,0.945210084,0.958661064,0.966
80112,0.972285714,0.976044818,0.979114846,0.981322129,0.983288515},
{0.717671309,0.788841226,0.844696379,0.91870195,0.944846797,0.958367688,0.96
6562674,0.972094708,0.975866295,0.978963788,0.981181058,0.983169916},
{0.716465374,0.787883657,0.843955679,0.918293629,0.944565097,0.958144044,0.9
66382271,0.971944598,0.975734072,0.978847645,0.981080332,0.983080332},
{0.715625344,0.787212121,0.843426997,0.91799449,0.944363636,0.957988981,0.96
6258953,0.971834711,0.975647383,0.97876584,0.981019284,0.983019284},
{0.714794521,0.786547945,0.84290411,0.91769863,0.944164384,0.957835616,0.966
136986,0.971726027,0.975561644,0.978684932,0.980958904,0.982958904},
{0.713972752,0.785891008,0.842386921,0.917405995,0.943967302,0.957683924,0.9
66016349,0.971618529,0.975476839,0.978604905,0.980899183,0.982899183},
{0.713159892,0.785241192,0.841875339,0.917116531,0.943772358,0.957533875,0.9
65897019,0.971512195,0.975392954,0.978525745,0.980840108,0.982840108},
{0.712355795,0.784598383,0.841369272,0.916830189,0.943579515,0.957385445,0.9
65778976,0.971407008,0.975309973,0.978447439,0.980781671,0.982781671},
{0.711560322,0.783962466,0.840868633,0.916546917,0.94338874,0.957238606,0.96
5662198,0.971302949,0.975227882,0.978369973,0.980723861,0.982723861},
{0.710773333,0.783333333,0.840373333,0.916266667,0.9432,0.957093333,0.965546
667,0.9712,0.975146667,0.978293333,0.980666667,0.982666667},
{0.709994695,0.782710875,0.839883289,0.91598939,0.943013263,0.956949602,0.96
5432361,0.971098143,0.975066313,0.978217507,0.98061008,0.98261008},
{0.709224274,0.782094987,0.839398417,0.91571504,0.942828496,0.956807388,0.96
5319261,0.970997361,0.974986807,0.97814248,0.98055409,0.98255409},
{0.708461942,0.781485564,0.838918635,0.91544357,0.942645669,0.956666667,0.96
5207349,0.970897638,0.974908136,0.978068241,0.980498688,0.982498688},
{0.707707572,0.780882507,0.838443864,0.915174935,0.942464752,0.956527415,0.9
65096606,0.970798956,0.974830287,0.977994778,0.980443864,0.982443864},
{0.706961039,0.780285714,0.837974026,0.914909091,0.942285714,0.95638961,0.96
4987013,0.970701299,0.974753247,0.977922078,0.98038961,0.98238961},
{0.706222222,0.77969509,0.837509044,0.914645995,0.942108527,0.95625323,0.964
878553,0.970604651,0.974677003,0.977850129,0.980335917,0.982335917},
{0.705491003,0.77911054,0.837048843,0.914385604,0.941933162,0.956118252,0.96
4771208,0.970508997,0.974601542,0.97777892,0.980282776,0.982282776},
{0.704767263,0.778531969,0.83659335,0.914127877,0.941759591,0.955984655,0.96
4664962,0.970414322,0.974526854,0.97770844,0.980230179,0.982230179},
{0.704050891,0.777959288,0.836142494,0.913872774,0.941587786,0.955852417,0.9
64559796,0.970320611,0.974452926,0.977638677,0.980178117,0.982178117},
{0.703341772,0.777392405,0.835696203,0.913620253,0.941417722,0.955721519,0.9
64455696,0.970227848,0.974379747,0.97756962,0.980126582,0.982126582},
{0.702639798,0.776831234,0.835254408,0.913370277,0.94124937,0.95559194,0.964
352645,0.97013602,0.974307305,0.977501259,0.980075567,0.982075567},
{0.701944862,0.776275689,0.834817043,0.913122807,0.941082707,0.955463659,0.9
64250627,0.970045113,0.974235589,0.977433584,0.980025063,0.982025063},
{0.700952618,0.775466334,0.834189526,0.91276808,0.940837905,0.955276808,0.96
4099751,0.969915212,0.974134663,0.977336658,0.97994015,0.981945137},
{0.699667494,0.774406948,0.83337469,0.912307692,0.940516129,0.955032258,0.96
3900744,0.969746898,0.974004963,0.977210918,0.97982134,0.981836228},
{0.698395062,0.773358025,0.832567901,0.911851852,0.940197531,0.954790123,0.9
63703704,0.969580247,0.973876543,0.97708642,0.979703704,0.981728395},
{0.697135135,0.77231941,0.831769042,0.911400491,0.939882064,0.954550369,0.96
35086,0.969415233,0.973749386,0.976963145,0.979587224,0.981621622},
{0.695887531,0.771290954,0.830977995,0.910953545,0.939569682,0.954312958,0.9
63315403,0.969251834,0.973623472,0.976841076,0.979471883,0.981515892},
{0.694652068,0.770272506,0.830194647,0.910510949,0.939260341,0.954077859,0.9
63124088,0.969090024,0.973498783,0.976720195,0.979357664,0.981411192},
{0.693428571,0.769263923,0.829418886,0.910072639,0.938953995,0.953845036,0.9
62934625,0.968929782,0.973375303,0.976600484,0.979244552,0.981307506},
{0.692216867,0.76826506,0.828650602,0.909638554,0.938650602,0.953614458,0.96
2746988,0.968771084,0.973253012,0.976481928,0.97913253,0.981204819},
{0.691016787,0.767275779,0.827889688,0.909208633,0.93835012,0.953386091,0.96
2561151,0.968613909,0.973131894,0.976364508,0.979021583,0.981103118},
{0.689828162,0.766295943,0.827136038,0.908782816,0.938052506,0.953159905,0.9
62377088,0.968458234,0.973011933,0.97624821,0.978911695,0.981002387},
{0.688650831,0.765325416,0.826389549,0.908361045,0.93775772,0.952935867,0.96
2194774,0.968304038,0.972893112,0.976133017,0.97880285,0.980902613},
{0.687484634,0.764364066,0.825650118,0.907943262,0.937465721,0.952713948,0.9
62014184,0.9681513,0.972775414,0.976018913,0.978695035,0.980803783},
{0.686329412,0.763411765,0.824917647,0.907529412,0.937176471,0.952494118,0.9
61835294,0.968,0.972658824,0.975905882,0.978588235,0.980705882},
{0.685185012,0.762468384,0.824192037,0.907119438,0.93688993,0.952276347,0.96
165808,0.967850117,0.972543326,0.975793911,0.978482436,0.980608899},
{0.684051282,0.7615338,0.823473193,0.906713287,0.936606061,0.952060606,0.961
482517,0.967701632,0.972428904,0.975682984,0.978377622,0.980512821},
{0.682928074,0.760607889,0.822761021,0.906310905,0.936324826,0.951846868,0.9
61308585,0.967554524,0.972315545,0.975573086,0.978273782,0.980417633},
{0.681815242,0.759690531,0.822055427,0.90591224,0.936046189,0.951635104,0.96
1136259,0.967408776,0.972203233,0.975464203,0.978170901,0.980323326},
{0.680712644,0.758781609,0.821356322,0.905517241,0.935770115,0.951425287,0.9
60965517,0.967264368,0.972091954,0.975356322,0.978068966,0.980229885},
{0.679620137,0.757881007,0.820663616,0.905125858,0.935496568,0.951217391,0.9
60796339,0.967121281,0.971981693,0.975249428,0.977967963,0.9801373},
{0.678537585,0.75698861,0.819977221,0.904738041,0.935225513,0.95101139,0.960
628702,0.966979499,0.971872437,0.975143508,0.977867882,0.980045558},
{0.678,0.756545455,0.819636364,0.904545455,0.935090909,0.950909091,0.9605454
55,0.966909091,0.971818182,0.975090909,0.977818182,0.98}};
TFloatStateVarFilt::TFloatStateVarFilt( )
{
InitDelays();
}
TFloatStateVarFilt::~TFloatStateVarFilt()
{
}
void TFloatStateVarFilt::SetSampleRate(long NewSampleRate,Boolean Recalc)
{
float tmp;
Fs = NewSampleRate;
tmp = NewSampleRate / 2.0; //Make sure Fc stays below one half nyquist
if (Fc > tmp)
Fc = tmp;
if (Recalc)
CalcInternalVars(); //Make the change "stick"
}
void TFloatStateVarFilt::SetFc(float NewFc, Boolean Recalc)
{
float tmp;
Fc = NewFc;
tmp = Fs / 2.0; //Make sure Fc stays below one half nyquist
if (Fc > tmp)
Fc = tmp;
if (Recalc)
CalcInternalVars(); //Make the change "stick"
}
void TFloatStateVarFilt::SetQ(float NewQ,Boolean Recalc)
{
Q = NewQ;
if (Q < 0.5)
Q = 0.5;
if (Q > 100.0)
Q = 100.0;
if (Recalc)
CalcInternalVars(); //Make the change "stick"
}
void TFloatStateVarFilt::SetOutGain(float NewOutGain,Boolean Recalc)
{
OutGain = NewOutGain;
if (Recalc) //Make the change "stick"
CalcInternalVars();
else
GainMult = OutGain / FilterProcessGain;
//change gain without recalculating everything
//It is not necessary to CalcInternalVars() to change filter gain.
}
void TFloatStateVarFilt::SetFilterMode(long NewFilterMode,Boolean Recalc)
{
FilterMode = NewFilterMode;
if (Recalc)
CalcInternalVars();
//if you change to or from BandPass mode, you should Recalc, because the
filter
//is calculated using the fudge table in BandPass mode.
}
void TFloatStateVarFilt::SetFilter(long NewSampleRate,float NewFc,float
NewQ,float NewOutGain,long NewFilterMode)
{
float tmp;
Fs = NewSampleRate;
Fc = NewFc;
tmp = Fs / 2.0; //one half nyquist}
if (Fc > tmp)
Fc = tmp;
Q = NewQ;
if (Q < 0.5) //valid Q ranges from 0.5 to 100
Q = 0.5;
if (Q > 100.0)
Q = 100.0;
OutGain = NewOutGain;
FilterMode = NewFilterMode;
CalcInternalVars(); //set up all the coefficients and stuff
}
void TFloatStateVarFilt::InitDelays(void)
{
D1 = 0.0;
D2 = 0.0;
LastInSample = 0.0; //LastInSample is used for interpolation if Fc is above
Fs/8
}
void TFloatStateVarFilt::CalcInternalVars(void)
{
float AdjustedFc;
long FreqIndx;
long QIndx;
float tmpFs;
if (FilterMode == 5)
FilterMode = 1; //no allpass function allowed, so set it to a default
lopass mode
tmpFs = Fs;
AdjustedFc = Fc;
if (tmpFs != 44100.0)
//the bandpass fudge table and freq limits are calibrated for 44.1 K audio
//so Fc is temporarily scaled to allow easier calculation for a dummie like
me
{
tmpFs = 44100.0;
AdjustedFc = (AdjustedFc * tmpFs) / Fs;
}
OverSampleRate = (AdjustedFc / 5501.0) + 1; //Over-sample if Fc > Fs/8
AdjustedFc = AdjustedFc / OverSampleRate;
if (Q < 1.0)
{
if (Q < 0.706)
{
QIndx = 0; // QIndx 0 is for Q of 0.5
}
else
{
QIndx = 1;// QIndx 1 is for Q of 0.707
}
}
else //Q >= 1
{
QIndx = 1 + Q;
if (QIndx > 11)
QIndx = 11;
//Center frequency is pretty predictable at high Q, so the fudge table only
goes up to Q==10
}
if (FilterMode == 2)
//use BPFreqMultArray to adjust freq for Bandpass filter
//but BPFreqMultArray is not used for LP, HP and Notch
{
FreqIndx = AdjustedFc / 25.0;
AdjustedFc = AdjustedFc * BPFreqMultArray[FreqIndx][ QIndx];
}
QMult = 1.0 / Q; //set the two filter coefficients
FMult = 2.0 * sin((pi * AdjustedFc) / tmpFs);
//calibrate max gain of filter
if (FilterMode == 2) //determine the natural Filter gain depending on mode
and Q
FilterProcessGain = Q;
else if (FilterMode == 4)
FilterProcessGain = 1.0;
else if (Q < 1.0)
FilterProcessGain = 1.0;
else
FilterProcessGain = Q;
GainMult = OutGain / FilterProcessGain; //Calc a GainMult to output the
requested gain
}
float TFloatStateVarFilt::Filter(float InSample)
{
float FilterOut;
long i;
float inc;
if (GainMult == 0.0)
//save some time, return zero if the gain is zero
//This is useful if you are implementing a parametric or graphic equalizer
with a lot of filters, but some knobs may be zero
{
FilterOut = 0.0;
goto exit;
//In Metrowerks Codewarrior Mac, the code works faster with only a single
return in a function
}
if (OverSampleRate > 1)
//if OverSampling for a high Fc, linear interpolate the signal and
//feed the intermediate samples thru the filter (with the filter
coefficients set for a lower frequency)
{
inc = (InSample - LastInSample) / OverSampleRate;
for (i = 1; i <= OverSampleRate; ++i)
{
LastInSample = LastInSample + inc;
LP = D2 + (FMult * D1);
HP = LastInSample - LP - (QMult * D1);
BP = D1 + (FMult * HP);
D1 = BP;
D2 = LP;
}
}
else //Just filter a single time if Fc < Fs/8
{
LP = D2 + (FMult * D1);
HP = InSample - LP - (QMult * D1);
BP = D1 + (FMult * HP);
D1 = BP;
D2 = LP;
}
LastInSample = InSample;
//keep up with LastInSample in all cases, in case Fc gets swept up into the
oversampling range
Notch = HP + LP; //phase cancellation between HP and LP makes a very deep
notch at Fc
switch (FilterMode)
//return the value for the selected FilterMode
{
case 1:
FilterOut = LP;
break;
case 2:
FilterOut = BP;
break;
case 3:
FilterOut = HP;
break;
case 4:
FilterOut = Notch;
break;
}
FilterOut *= GainMult;
exit:
return( FilterOut);
}
dupswapdrop -- the music-dsp mailing list and website: subscription info,
FAQ, source code archive, list archive, book reviews, dsp links
http://shoko.calarts.edu/musicdsp/
More information about the music-dsp
mailing list