Jamoma API  0.6.0.a19
TTLowpassButterworth4.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFilterLib
4  *
5  * @brief #TTLowpassButterworth4 is a fourth-order Butterworth lowpass filter.
6  *
7  * @details Butterworth filters have maximum flat frequency response in the pass band.
8  * @n
9  * Filter equations from: @n
10  * @n
11  * Second-order IIR Filters will support cascade implementations @n
12  * By Rusty Allred, Texas Instruments, Dallas @n
13  * July 01, 2003 @n
14  * http://www.planetanalog.com/article/printableArticle.jhtml?articleID=12802683
15  *
16  * @authors Trond Lossius, Timothy Place,
17  *
18  * @copyright Copyright © 2008, Trond Lossius @n
19  * This code is licensed under the terms of the "New BSD License" @n
20  * http://creativecommons.org/licenses/BSD/
21  */
22 
23 
24 #include "TTLowpassButterworth4.h"
25 
26 #define thisTTClass TTLowpassButterworth4
27 #define thisTTClassName "lowpass.butterworth.4"
28 #define thisTTClassTags "dspFilterLib, audio, processor, filter, lowpass, butterworth"
29 
30 
31 TT_AUDIO_CONSTRUCTOR
32 {
33  // register attributes
35  addAttributeProperty(Frequency, range, TTValue(10.0, sr*0.475));
36  addAttributeProperty(Frequency, rangeChecking, TT("clip"));
37 
38  // register for notifications from the parent class so we can allocate memory as required
39  addUpdates(MaxNumChannels);
40  // register for notifications from the parent class so we can recalculate coefficients as required
41  addUpdates(SampleRate);
42  // make the clear method available to the outside world
43  addMessage(clear);
44 
45  // Set Defaults...
46  setAttributeValue(kTTSym_maxNumChannels, arguments); // This attribute is inherited
47  setAttributeValue(TT("frequency"), 1000.0);
48  setProcessMethod(processAudio);
49  setCalculateMethod(calculateValue);
50 }
51 
52 
53 TTLowpassButterworth4::~TTLowpassButterworth4()
54 {
55  ;
56 }
57 
58 
60 {
61  mX1.resize(mMaxNumChannels);
62  mX2.resize(mMaxNumChannels);
63  mX3.resize(mMaxNumChannels);
64  mX4.resize(mMaxNumChannels);
65  mY1.resize(mMaxNumChannels);
66  mY2.resize(mMaxNumChannels);
67  mY3.resize(mMaxNumChannels);
68  mY4.resize(mMaxNumChannels);
69  clear();
70  return kTTErrNone;
71 }
72 
73 
74 TTErr TTLowpassButterworth4::updateSampleRate(const TTValue& oldSampleRate, TTValue&)
75 {
77  return setFrequency(v);
78 }
79 
80 
82 {
83  mX1.assign(mMaxNumChannels, 0.0);
84  mX2.assign(mMaxNumChannels, 0.0);
85  mX3.assign(mMaxNumChannels, 0.0);
86  mX4.assign(mMaxNumChannels, 0.0);
87  mY1.assign(mMaxNumChannels, 0.0);
88  mY2.assign(mMaxNumChannels, 0.0);
89  mY3.assign(mMaxNumChannels, 0.0);
90  mY4.assign(mMaxNumChannels, 0.0);
91  return kTTErrNone;
92 }
93 
94 
96 {
97  mFrequency = newValue;
98 
99  mRadians = kTTTwoPi*mFrequency;
100  mRadians2 = mRadians*mRadians;
101  mRadians3 = mRadians2*mRadians;
102  mRadians4 = mRadians3*mRadians;
103 
104  mK = mRadians/tan(kTTPi*mFrequency/sr); // kTTTwoPi*frequency/tan(kTTPi*frequency/sr);
105  mK2 = mK*mK;
106  mK3 = mK2 * mK;
107  mK4 = mK3 * mK;
108  calculateCoefficients();
109  return kTTErrNone;
110 }
111 
112 
113 void TTLowpassButterworth4::calculateCoefficients()
114 {
115  TTFloat64 a,b,temp;
116 
117  a = 2*(cos(kTTPi/8)+cos(3*kTTPi/8));
118  b = 2*(1+2*cos(kTTPi/8)*cos(3*kTTPi/8));
119  temp = (mK4 + a*mRadians*mK3 + b*mRadians2*mK2 + a*mRadians3*mK + mRadians4);
120 
121  mA0 = (mRadians4) / temp;
122  mA1 = (4*mRadians4) / temp;
123  mA2 = (6*mRadians4) / temp;
124 // mA3 = mA1; //mA3 = (4*mRadians4) / temp ;
125 // mA4 = mA0;//mA4 = (mRadians4) / temp ;
126 
127  mB1 = (-4*mK4 - 2*a*mRadians*mK3 + 2*a*mRadians3*mK + 4*mRadians4) / temp;
128  mB2 = (6*mK4 - 2*b*mRadians2*mK2 + 6*mRadians4) / temp;
129  mB3 = (-4*mK4 + 2*a*mRadians*mK3 - 2*a*mRadians3*mK + 4*mRadians4) / temp;
130  mB4 = (mK4 - a*mRadians*mK3 + b*mRadians2*mK2 - a*mRadians3*mK + mRadians4) / temp;
131 }
132 
133 
135 {
136  //y = TTAntiDenormal(mA0*x + mA1*mX1[channel] + mA2*mX2[channel] + mA3*mX3[channel] + mA4*mX4[channel] - mB1*mY1[channel] - mB2*mY2[channel] -mB3*mY3[channel] - mB4*mY4[channel]);
137  // since mA3 = mA1 and mA0 = mA4, we can simplyfy to
138  y = mA0*(x + mX4[channel]) + mA1*( mX1[channel] + mX3[channel] ) + mA2*mX2[channel] - mB1*mY1[channel] - mB2*mY2[channel] -mB3*mY3[channel] - mB4*mY4[channel];
139  TTZeroDenormal(y);
140 
141 
142  mX4[channel] = mX3[channel];
143  mX3[channel] = mX2[channel];
144  mX2[channel] = mX1[channel];
145  mX1[channel] = x;
146  mY4[channel] = mY3[channel];
147  mY3[channel] = mY2[channel];
148  mY2[channel] = mY1[channel];
149  mY1[channel] = y;
150  return kTTErrNone;
151 }
152 
153 
155 {
156  TT_WRAP_CALCULATE_METHOD(calculateValue);
157 }
TTFloat64 mFrequency
filter cutoff frequency
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTErr calculateValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt channel)
Standard single value calculate method as used by DSP objects.
TTFOUNDATION_EXPORT const TTFloat64 kTTTwoPi
Pre-calculated value of pi * 2.
Definition: TTBase.cpp:24
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
TTErr setFrequency(const TTValue &value)
Setter for the frequency attribute.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTErr processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Standard audio processing method as used by TTBlue objects.
#define setCalculateMethod(methodName)
A convenience macro to be used by subclasses for setting the calculate method.
TTSampleVector mY2
Output sample n-2.
64-bit floating point
Definition: TTBase.h:272
#define addAttributeProperty(attributeName, propertyName, initialValue)
A convenience macro to be used for registering properties of attributes.
Definition: TTAttribute.h:68
TTSampleVector mX1
Input sample n-1.
TTSampleVector mY4
Output sample n-4.
TTSampleVector mY1
Output sample n-1.
A simple container for an array of TTAudioSignal pointers.
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr updateMaxNumChannels(const TTValue &oldMaxNumChannels, TTValue &)
Receives notifications when there are changes to the inherited mMaxNumChannels attribute.
TTLowpassButterworth4 is a fourth-order Butterworth lowpass filter.
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
#define addMessage(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:19
TTSampleVector mX3
Input sample n-3.
No Error.
Definition: TTBase.h:343
TTSampleVector mY3
Output sample n-3.
TTFloat64 mRadians4
filter coefficients
TTFOUNDATION_EXPORT const TTFloat64 kTTPi
[doxygenAppendixC_constExample]
Definition: TTBase.cpp:23
TTSampleVector mX4
Input sample n-4.
TTSampleVector mX2
Input sample n-2.
TTFloat64 mB4
filter coefficients for output samples
TTFloat64 mA2
filter coefficients for input samples
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
TTErr clear()
This algorithm uses an IIR filter, meaning that it relies on feedback.
#define addUpdates(updateName)
An 'update' is a message sent to a subclass instance from its parent class.
Definition: TTMessage.h:44
TTUInt32 sr
Current sample rate being used by this object.