CSL  6.0
FIR.h
Go to the documentation of this file.
1 ///
2 /// FIR.h -- CSL filter specification and FIR filter classes
3 /// See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 ///
5 /// The FilterSpecification uses the Parks-McClellan/Remez iterative algorithm
6 /// It is provided under GPL by Jake Janovetz (janovetz@uiuc.edu)
7 ///
8 /// The FIR implementation is based on a minimal version written for the MAT 240B course;
9 /// it does not use the CSL RingBuffer helper class.
10 
11 #ifndef CSL_FIR_H
12 #define CSL_FIR_H
13 
14 #include "CSL_Core.h"
15 
16 namespace csl {
17 
18 
19 class FIR; ///< forward declaration
20 
21 ///
22 /// FilterSpecification class for designing multi-band-pass FIR filter impulse responses
23 ///
24 
26 public:
27 
28  friend class FIR; ///< Allow the FIR to access private members of this class.
29 
30  /// Constructors
31  FilterSpecification(unsigned numTaps = 0, unsigned numBands = 0, double *freqs = NULL, double *resps = NULL, double *weights = NULL);
33 
34  /// Accessors
35  void setFrequencies(double *frequencies);
36  void setResponses(double *responses);
37  void setWeights(double *weights);
38  void setNumTaps(unsigned numTaps);
39  unsigned mNumTaps; ///< number of taps desired
40 
41  void planFilter(); ///< method to plan the filter (execute the search/iterate algorithm)
42  double * mTapData; ///< the FIR tap weights (created by the planFilter method)
43 
44 protected:
45  unsigned mNumBands; ///< length of specification
46  double * mFrequencies; ///< band edge frequencies (2 * mNumBands)
47  double * mResponses; ///< band responses (mNumBands)
48  double * mWeights; ///< band error weights (mNumBands)
49 
50 };
51 
52 /// Examples
53 ///
54 /// -- Simple LPF (2 bands) at 0.2 Fs with 0.05-width transition bands
55 /// responses = { 1 x 0 };
56 /// freqs = { 0 0.2 0.25 0.5 };
57 /// weights = { 10 20 };
58 ///
59 /// -- basic BPF (3 bands) between 0.2 and 0.3 Fs with 0.05-width transition bands
60 /// responses = { 0.5 x 1 x 0.8 };
61 /// freqs = { 0 0.15 0.2 0.3 0.35 0.5 };
62 /// weights = { 20 5 20 };
63 ///
64 /// -- Fancier dual-BPF
65 /// responses = { 0 x 1 x 0 x 1 x 0 };
66 /// freqs = { 0 0.05 0.1 0.15 0.18 0.25 0.3 0.36 0.41 0.5 };
67 /// weights = { 10 1 3 1 20 };
68 ///
69 
70 ///
71 /// FIR Filter class
72 ///
73 
74 class FIR : public Effect {
75 
76 public: /// Various constructors
77  /// Takes a UGen, and optionally the number of taps and the tap IR array.
78  FIR (UnitGenerator & in, unsigned numTaps = 2, float * tapDelay = NULL);
79 // FIR (char * file_name); ///< read data from a file
80  FIR (FilterSpecification & fs); ///< give it a filter specification object
81  FIR (UnitGenerator & in, char * fileName);
83  ~FIR ();
84 
85  void setTaps(unsigned numTaps, float *tapDelays);
86  void readTaps(char *fileName);
87  void print_taps();
88  /// The work method...
89  void nextBuffer(Buffer &outputBuffer, unsigned outBufNum) throw (CException);
90 
91 protected:
93  unsigned mOffset; ///< offset "pointer" for loop counting
94  /// Here are the sample buffers (dynamically allocated)
95  sample *mDLine; ///< mNumTaps length delay line
96 
97  void resetDLine(); ///< zero-out mDline and reallocate memory if necessary;
98 
99  /// Parks-McClellan/Remez FIR filter design algorithm
100  void remez(double h[], int numtaps, int numband, double bands[], double des[], double weight[], int type);
101 
102 };
103 
104 }
105 
106 #endif
void setNumTaps(unsigned numTaps)
Definition: FIR.cpp:63
void resetDLine()
zero-out mDline and reallocate memory if necessary;
Definition: FIR.cpp:129
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
forward declaration
Definition: FIR.h:25
Effect – mix-in for classes that have unit generators as inputs (like filters).
Definition: CSL_Core.h:466
sample * mDLine
mNumTaps length delay line
Definition: FIR.h:95
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
The work method...
Definition: FIR.cpp:174
void setFrequencies(double *frequencies)
Accessors.
Definition: FIR.cpp:42
void print_taps()
Definition: FIR.cpp:165
~FIR()
Definition: FIR.cpp:125
unsigned mNumTaps
number of taps desired
Definition: FIR.h:39
Examples.
Definition: FIR.h:74
double * mTapData
the FIR tap weights (created by the planFilter method)
Definition: FIR.h:42
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
void planFilter()
method to plan the filter (execute the search/iterate algorithm)
Definition: FIR.cpp:78
FilterSpecification(unsigned numTaps=0, unsigned numBands=0, double *freqs=NULL, double *resps=NULL, double *weights=NULL)
Constructors.
Definition: FIR.cpp:23
double * mWeights
band error weights (mNumBands)
Definition: FIR.h:48
unsigned mOffset
offset "pointer" for loop counting Here are the sample buffers (dynamically allocated) ...
Definition: FIR.h:93
unsigned mNumBands
length of specification
Definition: FIR.h:45
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
double * mResponses
band responses (mNumBands)
Definition: FIR.h:47
FIR(UnitGenerator &in, unsigned numTaps=2, float *tapDelay=NULL)
Various constructors Takes a UGen, and optionally the number of taps and the tap IR array...
Definition: FIR.cpp:97
void setResponses(double *responses)
Definition: FIR.cpp:49
void setWeights(double *weights)
Definition: FIR.cpp:56
forward declaration
Definition: CSL_Core.h:241
double * mFrequencies
band edge frequencies (2 * mNumBands)
Definition: FIR.h:46
void readTaps(char *fileName)
Definition: FIR.cpp:144
void setTaps(unsigned numTaps, float *tapDelays)
Definition: FIR.cpp:136
void remez(double h[], int numtaps, int numband, double bands[], double des[], double weight[], int type)
Parks-McClellan/Remez FIR filter design algorithm.
Base class of CSL exceptions (written upper-case). Has a string message.
FilterSpecification * mFilterSpec
Definition: FIR.h:92