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