SoDaRadio-5.0.3-master:8901fb5
OSFilter.hxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, Matthew H. Reilly (kb1vc)
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #ifndef OS_FILTER_HDR
29 #define OS_FILTER_HDR
30 
31 
40 
41 #include <fstream>
42 #include <complex>
43 #include <stdio.h>
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fftw3.h>
48 namespace SoDa {
50  class OSFilter {
51  public:
52 
53 
55 #if 0
56  OSFilter(float * filter_impulse_response,
65  unsigned int filter_length,
66  float filter_gain,
67  unsigned int inout_buffer_length,
68  OSFilter * cascade = NULL,
69  unsigned int suggested_transform_length = 0);
70 #endif
71 
84  OSFilter(float low_cutoff,
85  float low_pass_edge,
86  float high_pass_edge,
87  float high_cutoff,
88 
89  unsigned int filter_length,
90  float filter_gain,
91  float sample_rate,
92 
93  unsigned int inout_buffer_length,
94  unsigned int suggested_transform_length = 0);
95 
101  unsigned int apply(std::complex<float> * inbuf, std::complex<float> * outbuf, float outgain = 1.0);
102 
110  unsigned int apply(float * inbuf, float * outbuf, float outgain = 1.0,
111  int instride = 1, int outstride = 1);
112 
115  void dump(std::ostream & os);
116 
117  std::pair<double, double> getFilterEdges() {
118  return std::pair<double, double>(low_edge, high_edge);
119  }
120 
121  protected:
123  double low_edge, high_edge;
124 
126  int guessN();
127  void setupFFT();
128 
129 
130 
131  // these are the salient dimensions for this Overlap/Save
132  // widget (for terminology, see Lyons pages 719ff
133  unsigned int M;
134  unsigned int Q;
135  unsigned int N;
136 
137  // some helpful stuff.
138  unsigned int tail_index;
139 
140  // these are the intermediate buffers
141  std::complex<float> * fft_input;
142  std::complex<float> * fft_output;
143  std::complex<float> * ifft_output;
144 
145  // this is the FFT image of the filter
146  std::complex<float> * filter_fft;
147 
148  // each filter needs two plans, a forward and backward
149  // plan for the FFT and IFFT
151  };
152 }
153 
154 #endif
int guessN()
pick a likely N - FFT length.
Definition: OSFilter.cxx:248
double high_edge
Definition: OSFilter.hxx:123
void setupFFT()
Definition: OSFilter.cxx:275
double low_edge
parameters that we keep to support display masks on the spectrogram
Definition: OSFilter.hxx:123
std::complex< float > * fft_input
a copy of the input stream.
Definition: OSFilter.hxx:141
fftwf_plan backward_plan
plans for fftw transform ops
Definition: OSFilter.hxx:150
void dump(std::ostream &os)
dump the filter FFT to the output stream
Definition: OSFilter.cxx:364
std::complex< float > * ifft_output
the output stream + overlap discard
Definition: OSFilter.hxx:143
OSFilter(float low_cutoff, float low_pass_edge, float high_pass_edge, float high_cutoff, unsigned int filter_length, float filter_gain, float sample_rate, unsigned int inout_buffer_length, unsigned int suggested_transform_length=0)
constructor
Definition: OSFilter.cxx:123
unsigned int tail_index
the beginning of the end.
Definition: OSFilter.hxx:138
Overlap-and-save filter class.
Definition: OSFilter.hxx:50
std::complex< float > * filter_fft
FFT image of the input filter.
Definition: OSFilter.hxx:146
unsigned int apply(std::complex< float > *inbuf, std::complex< float > *outbuf, float outgain=1.0)
run the filter on a complex input stream
Definition: OSFilter.cxx:334
std::complex< float > * fft_output
the transformed input stream + overlap
Definition: OSFilter.hxx:142
std::pair< double, double > getFilterEdges()
Definition: OSFilter.hxx:117
fftwf_plan forward_plan
Definition: OSFilter.hxx:150
unsigned int N
the total length of the transform N > (M + Q-1)
Definition: OSFilter.hxx:135
unsigned int M
the input buffer length;
Definition: OSFilter.hxx:133
unsigned int Q
the filter length
Definition: OSFilter.hxx:134