Jamoma API  0.6.0.a19
TTHighpassButterworth4.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFilterLib
4  *
5  * @brief #TTHighpassButterworth4 is a fourth-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 "TTHighpassButterworth4.h"
25 
26 #define thisTTClass TTHighpassButterworth4
27 #define thisTTClassName "highpass.butterworth.4"
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 TTHighpassButterworth4::~TTHighpassButterworth4()
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 TTHighpassButterworth4::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 void TTHighpassButterworth4::calculateCoefficients() //TODO: with a little bit of thinking, this can be optimized
113 {
114  TTFloat64 a,b,temp;
115 
116  a = 2*(cos(kTTPi/8)+cos(3*kTTPi/8));
117  b = 2*(1+2*cos(kTTPi/8)*cos(3*kTTPi/8));
118  temp = (mK4 + a*mRadians*mK3 + b*mRadians2*mK2 + a*mRadians3*mK + mRadians4);
119 
120  mA0 = (mK4) / temp;
121  mA1 = (-4*mK4) / temp;
122  mA2 = (6*mK4) / temp;
123  //mA3 = mA1;//mA3 = (-4*k4) / temp;
124  //mA4 = mA0;//a4 = (k4) / temp;
125 
126  mB1 = (-4*mK4 - 2*a*mRadians*mK3 + 2*a*mRadians3*mK + 4*mRadians4) / temp;
127  mB2 = (6*mK4 - 2*b*mRadians2*mK2 + 6*mRadians4) / temp;
128  mB3 = (-4*mK4 + 2*a*mRadians*mK3 - 2*a*mRadians3*mK + 4*mRadians4) / temp;
129  mB4 = (mK4 - a*mRadians*mK3 + b*mRadians2*mK2 - a*mRadians3*mK + mRadians4) / temp;
130 }
131 
132 
134 {
135  //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]);
136  // since mA3 = mA1 and mA0 = mA4, we can simplyfy to
137  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];
138  TTZeroDenormal(y);
139  mX4[channel] = mX3[channel];
140  mX3[channel] = mX2[channel];
141  mX2[channel] = mX1[channel];
142  mX1[channel] = x;
143  mY4[channel] = mY3[channel];
144  mY3[channel] = mY2[channel];
145  mY2[channel] = mY1[channel];
146  mY1[channel] = y;
147  return kTTErrNone;
148 }
149 
150 
152 {
153  TT_WRAP_CALCULATE_METHOD(calculateValue);
154 }
TTSampleVector mX2
Input sample n-2.
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTFloat64 mFrequency
filter cutoff frequency
TTSampleVector mX1
Input sample n-1.
TTHighpassButterworth4 is a fourth-order Butterworth highpass filter.
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 mY3
Output sample n-3.
TTFloat64 mRadians4
filter coefficients
TTSampleVector mY2
Output sample n-2.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
TTSampleVector mY1
Output sample n-1.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTFloat64 mA2
filter coefficients for input samples
#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 mX4
Input sample n-4.
TTErr updateMaxNumChannels(const TTValue &oldMaxNumChannels, TTValue &)
Receives notifications when there are changes to the inherited mMaxNumChannels attribute.
TTErr processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Standard audio processing method as used by TTBlue objects.
TTFloat64 mB4
filter coefficients for output samples
TTErr clear()
This algorithm uses an IIR filter, meaning that it relies on feedback.
A simple container for an array of TTAudioSignal pointers.
TTSampleVector mY4
Output sample n-4.
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
TTSampleVector mX3
Input sample n-3.
#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
TTErr calculateValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt channel)
Standard single value calculate method as used by DSP objects.
TTErr setFrequency(const TTValue &value)
Setter for the frequency attribute.
TTFOUNDATION_EXPORT const TTFloat64 kTTPi
[doxygenAppendixC_constExample]
Definition: TTBase.cpp:23
[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.