OpenWareLaboratory
Resample.h
Go to the documentation of this file.
1 #ifndef __Resample_h__
2 #define __Resample_h__
3
4 #include "SignalProcessor.h"
6
10 class [[deprecated("use UpSampler and DownSampler instead.")]] Resampler {
11 private:
14  int factor=4;
15  int upsampleStages;
16  int downsampleStages;
17 public:
18
19  Resampler():upsampleStages(1), downsampleStages(1){
20  // two filters: same coefficients, different state variables
21  init();
22  }
23  Resampler(int stages){
24  upsampleStages=stages;
25  downsampleStages=stages;
26  init();
27  }
29  upsampleStages=aUpsampleStages;
31  init();
32  }
36  }
37  void init(){
40  // [B, A]=cheby1(2, 2, 0.25); then use [B, -A(2:end)] , note the minus sign in front of the A coefficients!!!!
41  //alternatively, we could use FilterStage to compute the coefficients
42  static float downCoeffs[5]={0.07609109, 0.15218218, 0.07609109, +1.16511283, -0.54828486};
43  static float upCoeffs[5]={0.07609109, 0.15218218, 0.07609109, +1.16511283, -0.54828486};
44  for(int n=0; n<3; n++){
45  upCoeffs[n]=downCoeffs[n]*factor; //compensate for the gain loss due to zero-stuffing, gives unitary gain after upsampling
46  }
47  for(int n=3; n<5; n++){
48  upCoeffs[n]=downCoeffs[n];
49  }
50  downfilter->copyCoefficients(FloatArray(downCoeffs,5));
51  upfilter->copyCoefficients(FloatArray(upCoeffs,5));
52  }
53  void downsample(FloatArray input, FloatArray output){
54  ASSERT(input.getSize()==output.getSize()*factor, "wrong size");
55  downfilter->process(input, input);
56  float* p = (float*)input;
57  for(size_t i=0; i<output.getSize(); ++i){
58  output[i] = *p;
59  p += 4;
60  }
61  }
62  void upsample(FloatArray input, FloatArray output){
63  ASSERT(input.getSize()*factor==output.getSize(), "wrong size");
64  float* p = output;
65  for(size_t i=0; i<input.getSize(); ++i){
66  *p++ = input[i];
67  *p++ = 0;
68  *p++ = 0;
69  *p++ = 0;
70  }
71  upfilter->process(output, output);
72  }
73 };
74
75 class UpSampler : public SignalProcessor {
76 private:
78  const size_t factor;
79 public:
80  UpSampler(BiquadFilter* filter, int factor=4): filter(filter), factor(factor) {}
81  static UpSampler* create(int stages, int factor=4){
83  // [B, A]=cheby1(2, 2, 0.25); then use [B, -A(2:end)] , note the minus sign in front of the A coefficients!!!!
84  //alternatively, we could use FilterStage to compute the coefficients
85  float upCoeffs[5]= {0.07609109, 0.15218218, 0.07609109, +1.16511283, -0.54828486};
86  for(int n=0; n<3; n++)
87  upCoeffs[n] *= factor; //compensate for the gain loss due to zero-stuffing, gives unitary gain after upsampling
88  filter->copyCoefficients(FloatArray(upCoeffs, 5));
89  return new UpSampler(filter, factor);
90  }
91  static void destroy(UpSampler* obj){
93  delete obj;
94  }
95  float process(float input){
96  return filter->process(input); // this doesn't really make sense
97  }
101  void process(FloatArray input, FloatArray output){
102  ASSERT(input.getSize()*factor==output.getSize(), "wrong size");
103  float* p = output;
104  output.clear();
105  for(size_t i=0; i<input.getSize(); i+= factor)
106  *p++ = input[i];
107  filter->process(output, output);
108  }
109 };
110
111 class DownSampler : public SignalProcessor {
112 private:
114  const size_t factor;
115 public:
116  DownSampler(BiquadFilter* filter, int factor=4): filter(filter), factor(factor) {}
117  static DownSampler* create(int stages, int factor=4){
119  // [B, A]=cheby1(2, 2, 0.25); then use [B, -A(2:end)] , note the minus sign in front of the A coefficients!!!!
120  //alternatively, we could use FilterStage to compute the coefficients
121  static float downCoeffs[5]={0.07609109, 0.15218218, 0.07609109, +1.16511283, -0.54828486};
122  filter->copyCoefficients(FloatArray(downCoeffs,5));
123  return new DownSampler(filter, factor);
124  }
125  static void destroy(DownSampler* obj){
127  delete obj;
128  }
129  float process(float input){
130  return filter->process(input); // this doesn't really make sense
131  }
135  void process(FloatArray input, FloatArray output){
136  ASSERT(input.getSize()==output.getSize()*factor, "wrong size");
137  filter->process(input, input);
138  float* p = (float*)input;
139  for(size_t i=0; i<output.getSize(); ++i){
140  output[i] = *p;
141  p += factor;
142  }
143  }
144 };
145
146 #endif /* __Resample_h__ */
static BiquadFilter * create(float sr, size_t stages=1)
void copyCoefficients()
void process(float *input, float *output, size_t size)
static DownSampler * create(int stages, int factor=4)
Definition: Resample.h:117
void process(FloatArray input, FloatArray output)
Down-sample param @input and put results into.
Definition: Resample.h:135
float process(float input)
Definition: Resample.h:129
static void destroy(DownSampler *obj)
Definition: Resample.h:125
Definition: Resample.h:116
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
void clear()
Clear the array.
Definition: FloatArray.h:29
Implements 4x oversampling.
Definition: Resample.h:10
Definition: Resample.h:28
~Resampler()
Definition: Resample.h:33
Resampler(int stages)
Definition: Resample.h:23
Resampler()
Definition: Resample.h:19
void init()
Definition: Resample.h:37
void downsample(FloatArray input, FloatArray output)
Definition: Resample.h:53
void upsample(FloatArray input, FloatArray output)
Definition: Resample.h:62
Base class for signal processors such as Filters.
size_t getSize() const
Definition: SimpleArray.h:31
static void destroy(UpSampler *obj)
Definition: Resample.h:91