Jamoma API  0.6.0.a19
TTLowpassButterworth3.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFilterLib
4  *
5  * @brief #TTLowpassButterworth3 is a third-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 "TTLowpassButterworth3.h"
25 
26 #define thisTTClass TTLowpassButterworth3
27 #define thisTTClassName "lowpass.butterworth.3"
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 TTLowpassButterworth3::~TTLowpassButterworth3()
54 {
55  ;
56 }
57 
58 
60 {
61  mX1.resize(mMaxNumChannels);
62  mX2.resize(mMaxNumChannels);
63  mX3.resize(mMaxNumChannels);
64  mY1.resize(mMaxNumChannels);
65  mY2.resize(mMaxNumChannels);
66  mY3.resize(mMaxNumChannels);
67  clear();
68  return kTTErrNone;
69 }
70 
71 
72 TTErr TTLowpassButterworth3::updateSampleRate(const TTValue& oldSampleRate, TTValue&)
73 {
75  return setFrequency(v);
76 }
77 
78 
80 {
81  mX1.assign(mMaxNumChannels, 0.0);
82  mX2.assign(mMaxNumChannels, 0.0);
83  mX3.assign(mMaxNumChannels, 0.0);
84  mY1.assign(mMaxNumChannels, 0.0);
85  mY2.assign(mMaxNumChannels, 0.0);
86  mY3.assign(mMaxNumChannels, 0.0);
87  return kTTErrNone;
88 }
89 
90 
92 {
93  mFrequency = newValue;
94 
95  mRadians = kTTTwoPi*mFrequency;
96  mRadiansSquared = mRadians * mRadians;
97  mRadiansCubic = mRadiansSquared * mRadians;
98 
99  mK = mRadians/tan(kTTPi*mFrequency/sr); // kTTTwoPi*frequency/tan(kTTPi*frequency/sr);
100  mKSquared = mK * mK;
101  mKCubic = mKSquared * mK;
102  calculateCoefficients();
103  return kTTErrNone;
104 }
105 
106 void TTLowpassButterworth3::calculateCoefficients() //TODO: with a little bit of thinking, this can be optimized
107 {
108  TTFloat64 temp1;
109  temp1 = (mRadiansCubic + mKCubic + 2*mRadiansSquared*mK + 2*mRadians*mKSquared);
110 
111  mA0 = (mRadiansCubic) / temp1;
112  mA1 = (3*mRadiansCubic) / temp1;
113  mA2 = mA1; //mA2 = (3*mRadiansCubic) / temp1;
114  mA3 = mA0; //mA3 = (mRadiansCubic) / temp1;
115 
116  mB1 = (3*mRadiansCubic - 3*mKCubic + 2*mRadiansSquared*mK - 2*mRadians*mKSquared) / temp1;
117  mB2 = (3*mRadiansCubic + 3*mKCubic - 2*mRadiansSquared*mK - 2*mRadians*mKSquared) / temp1;
118  mB3 = (mRadiansCubic - mKCubic - 2*mRadiansSquared*mK + 2*mRadians*mKSquared) / temp1;
119 
120 }
121 
122 
124 {
125  //y = TTAntiDenormal(mA0*x + mA1*mX1[channel] + mA2*mX2[channel] + mA3*mX3[channel] - mB1*mY1[channel] - mB2*mY2[channel] -mB3*mY3[channel]);
126  // since mA2 = mA1 and mA3 = mA0, we can write
127  y = mA0*(x +mX3[channel]) + mA1*(mX1[channel] + mX2[channel]) - mB1*mY1[channel] - mB2*mY2[channel] -mB3*mY3[channel];
128  TTZeroDenormal(y);
129  mX3[channel] = mX2[channel];
130  mX2[channel] = mX1[channel];
131  mX1[channel] = x;
132  mY3[channel] = mY2[channel];
133  mY2[channel] = mY1[channel];
134  mY1[channel] = y;
135  return kTTErrNone;
136 }
137 
138 
140 {
141  TT_WRAP_CALCULATE_METHOD(calculateValue);
142 }
TTFloat64 mB3
filter coefficients for output samples
TTErr clear()
This algorithm uses an IIR filter, meaning that it relies on feedback.
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTSampleVector mX2
Input sample n-2.
TTSampleVector mX3
Input sample n-3.
TTFOUNDATION_EXPORT const TTFloat64 kTTTwoPi
Pre-calculated value of pi * 2.
Definition: TTBase.cpp:24
TTErr setFrequency(const TTValue &value)
Setter for the frequency attribute.
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTSampleVector mY1
Output sample n-1.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
#define setCalculateMethod(methodName)
A convenience macro to be used by subclasses for setting the calculate method.
TTErr calculateValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt channel)
Standard single value calculate method as used by DSP objects.
TTSampleVector mY3
Output sample n-3.
64-bit floating point
Definition: TTBase.h:272
TTFloat64 mA3
filter coefficients for input samples
TTErr processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Standard audio processing method as used by TTBlue objects.
TTErr updateMaxNumChannels(const TTValue &oldMaxNumChannels, TTValue &)
Receives notifications when there are changes to the inherited mMaxNumChannels attribute.
#define addAttributeProperty(attributeName, propertyName, initialValue)
A convenience macro to be used for registering properties of attributes.
Definition: TTAttribute.h:68
TTSampleVector mY2
Output sample n-2.
TTSampleVector mX1
Input sample n-1.
A simple container for an array of TTAudioSignal pointers.
TTLowpassButterworth3 is a third-order Butterworth lowpass filter.
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
#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
No Error.
Definition: TTBase.h:343
TTFloat64 mFrequency
filter cutoff frequency
TTFOUNDATION_EXPORT const TTFloat64 kTTPi
[doxygenAppendixC_constExample]
Definition: TTBase.cpp:23
TTFloat64 mRadiansCubic
filter coefficients
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
#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.