Nsound  0.9.4
FilterHighPassFIR.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterHighPassFIR.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2006 Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------
28 
29 #include <Nsound/AudioStream.h>
30 #include <Nsound/Buffer.h>
32 #include <Nsound/Plotter.h>
33 
34 #include <cmath>
35 #include <cstdio>
36 #include <cstring>
37 #include <iostream>
38 
39 using namespace Nsound;
40 
41 using std::cerr;
42 using std::endl;
43 
44 //-----------------------------------------------------------------------------
47  const float64 & sample_rate,
48  uint32 kernel_size,
49  const float64 & cutoff_frequency_Hz)
50  :
51  FilterLowPassFIR(sample_rate, kernel_size, cutoff_frequency_Hz),
52  hp_cache_()
53 {
54  frequency_1_Hz_ = cutoff_frequency_Hz;
56 }
57 
58 //-----------------------------------------------------------------------------
61 {
62  KernelCache::iterator itor = hp_cache_.begin();
63  KernelCache::iterator end = hp_cache_.end();
64 
65  while(itor != end)
66  {
67  delete [] itor->b_;
68  ++itor;
69  }
70 }
71 
74 filter(const AudioStream & x)
75 {
76  return Filter::filter(x);
77 }
78 
81 filter(const AudioStream & x, const float64 & f)
82 {
83  return Filter::filter(x, f);
84 }
85 
88 filter(const AudioStream & x, const Buffer & frequencies)
89 {
90  return Filter::filter(x, frequencies);
91 }
92 
93 Buffer
95 filter(const Buffer & x)
96 {
97  return Filter::filter(x);
98 }
99 
100 Buffer
102 filter(const Buffer & x, const float64 & f)
103 {
104  // Instead of calling Filter::filter(x,f), we implement this function here
105  // to avoid calling makeKernel(f) for each sample.
106 
109 
110  Buffer::const_iterator itor = x.begin();
111  Buffer::const_iterator end = x.end();
112 
113  Buffer y;
114  while(itor != end)
115  {
116  y << FilterLowPassFIR::filter(*itor);
117  ++itor;
118  }
119 
120  return y;
121 }
122 
123 Buffer
125 filter(const Buffer & x, const Buffer & frequencies)
126 {
127  return Filter::filter(x, frequencies);
128 }
129 
130 float64
132 filter(const float64 & x)
133 {
134  return FilterLowPassFIR::filter(x);
135 }
136 
137 float64
139 filter(const float64 & x, const float64 & frequency_Hz)
140 {
141  FilterHighPassFIR::makeKernel(frequency_Hz);
142  return FilterLowPassFIR::filter(x);
143 }
144 
145 Buffer
148 {
150 }
151 
152 void
154 makeKernel(const float64 & cutoff_frequency_Hz)
155 {
156  // Here we create a key into the kernel cache. I'm only storing kernels
157  // with freqs chopped off to the 10th Hz. So filtering with a kernel
158  // designed at 440.1567 Hz will get stored as 440.1. Any other frequency
159  // that starts at 440.1 will not get a kernel calculated and just use the
160  // one in the cache.
161 
162  FilterLowPassFIR::Kernel new_kernel(
163  static_cast<uint32>(cutoff_frequency_Hz));
164 
165  // See if the kernel is in the cache.
166 
167  FilterLowPassFIR::
168  KernelCache::
169  const_iterator itor = hp_cache_.find(new_kernel);
170 
171  if(itor != hp_cache_.end())
172  {
173  // The kernel was found in the cache.
174  b_ = itor->b_;
175  return;
176  }
177 
178  // The filter wasn't in the cache, need to make it.
179  new_kernel.b_ = new float64[kernel_size_];
180 
181  if(cutoff_frequency_Hz < 0.10)
182  {
183  // Create All pass filter.
184  new_kernel.b_[0] = 1.0;
185 
186  b_ = new_kernel.b_;
187  }
188  else
189  {
190  float64 fc = sample_rate_ / 2.0 - cutoff_frequency_Hz;
191 
193 
194  // Now b_ is pointing to a low pass kernel, copy it into new_kernel.
195  memcpy(new_kernel.b_, b_, sizeof(float64) * kernel_size_);
196 
197  // Point to new kernel.
198  b_ = new_kernel.b_;
199 
200  // Perform the spectra inversion on the kernel to turn it into a high
201  // pass filter.
203  }
204 
205  // Add the new kernel to the cache.
206  hp_cache_.insert(new_kernel);
207 }
208 
209 void
212  boolean show_fc,
213  boolean show_phase)
214 {
215  char title[128];
216  sprintf(title,
217  "High Pass FIR Frequency Response\n"
218  "order = %d, fc = %0.1f Hz, sr = %0.1f Hz",
220 
221  Filter::plot(show_phase);
222 
223  Plotter pylab;
224 
225  uint32 n_rows = 1;
226 
227  if(show_phase)
228  {
229  n_rows = 2;
230  }
231 
232  if(show_fc)
233  {
234  pylab.subplot(n_rows, 1, 1);
235 
236  pylab.axvline(frequency_1_Hz_,"color='red'");
237 
238  pylab.title(title);
239  }
240 
241 }
242 
243 void
246 {
247  // Don't call FilterLowPassFIR::reset(), if we did, we would be creating
248  // a kernel twice.
249 
250  memset(x_history_, 0, sizeof(float64) * (kernel_size_ + 1));
251  x_ptr_ = x_history_;
252 
254 }
255 
256 void
259 {
260  for(uint32 i = 0; i < kernel_size_; ++i)
261  {
262  if(i % 2)
263  {
264  b_[i] *= -1.00;
265  }
266  }
267 }
268 
unsigned int uint32
Definition: Nsound.h:153
FilterLowPassFIR::KernelCache hp_cache_
void axvline(const float64 &x_pos=0.0, const std::string &kwargs="")
Draws a vertical line at x and spans ymin to ymax (ralitive).
Definition: Plotter.cc:358
void title(const std::string &title, const std::string &kwargs="")
Add a title to the plot at the top and centered.
Definition: Plotter.cc:1127
double float64
Definition: Nsound.h:146
Buffer getImpulseResponse(const uint32 n_samples=8192)
Definition: Filter.cc:225
void makeKernel(const float64 &frequency1)
Axes subplot(const uint32 n_rows, const uint32 n_cols, const uint32 n, const std::string &kwargs="", Axes *sharex=NULL, Axes *sharey=NULL)
Creates a figure in a subplot, subplot(A, B, C, **kwargs)
Definition: Plotter.cc:1031
iterator end()
Retruns the itreator at the end of the Buffer.
Definition: Buffer.h:348
FilterHighPassFIR(const float64 &sample_rate, uint32 kernel_size, const float64 &cutoff_frequency_Hz)
void makeKernel(const float64 &frequency)
FloatVector::const_iterator const_iterator
Definition: Buffer.h:70
uint32 kernel_size_
Definition: Filter.h:116
iterator begin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:285
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
void plot(boolean show_fc=true, boolean show_phase=false)
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream filter(const AudioStream &x)
A class to store calculated kernels.
AudioStream filter(const AudioStream &x)
float64 sample_rate_
Definition: Filter.h:113