Nsound  0.9.4
FilterLowPassIIR.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterLowPassIIR.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2007 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 <iostream>
37 
38 using namespace Nsound;
39 
40 //-----------------------------------------------------------------------------
43  const float64 & sample_rate,
44  uint32 n_poles,
45  const float64 & frequency,
46  const float64 & percent_ripple)
47  :
49  FilterStageIIR::LOW_PASS,
50  sample_rate,
51  n_poles,
52  frequency,
53  percent_ripple)
54 {
55  frequency_ = frequency;
56  kernel_size_ = n_poles;
57 }
58 
61 filter(const AudioStream & x)
62 {
63  return Filter::filter(x);
64 }
65 
68 filter(const AudioStream & x, const float64 & f)
69 {
70  return FilterStageIIR::filter(x, f);
71 }
72 
75 filter(const AudioStream & x, const Buffer & frequencies)
76 {
77  return Filter::filter(x, frequencies);
78 }
79 
80 //-----------------------------------------------------------------------------v
81 Buffer
83 filter(const Buffer & x)
84 {
85  return Filter::filter(x);
86 }
87 
88 Buffer
90 filter(const Buffer & x, const float64 & f)
91 {
92  return FilterStageIIR::filter(x, f);
93 }
94 
95 Buffer
97 filter(const Buffer & x, const Buffer & frequencies)
98 {
99  return Filter::filter(x, frequencies);
100 }
101 
102 float64
104 filter(const float64 & x)
105 {
106  return FilterStageIIR::filter(x);
107 }
108 
109 float64
111 filter(const float64 & x, const float64 & frequency)
112 {
113  return FilterStageIIR::filter(x, frequency);
114 }
115 
116 void
118 makeKernel(const float64 & frequency)
119 {
120  FilterStageIIR::makeKernel(frequency);
121 }
122 
123 void
125 plot(boolean show_fc, boolean show_phase)
126 {
127  char title[128];
128  sprintf(title,
129  "Low Pass IIR Frequency Response\n"
130  "order = %d, fc = %0.1f Hz, sr = %0.1f Hz",
132 
133  Filter::plot(show_phase);
134 
135  Plotter pylab;
136 
137  uint32 n_rows = 1;
138 
139  if(show_phase)
140  {
141  n_rows = 2;
142  }
143 
144  if(show_fc)
145  {
146  pylab.subplot(n_rows, 1, 1);
147 
148  pylab.axvline(frequency_,"color='red'");
149 
150  pylab.title(title);
151  }
152 
153 }
154 
155 void
158 {
160 }
161 
162 void
164 setCutoff(const float64 & fc)
165 {
166  if(fc > 0.0 && fc < sample_rate_ / 2.0)
167  {
168  frequency_ = fc;
169  makeKernel(fc);
170  }
171 }
172 
unsigned int uint32
Definition: Nsound.h:153
AudioStream filter(const AudioStream &x)
AudioStream filter(const AudioStream &x)
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
A class for filtering audio in the frequecy domain.
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
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
void plot(boolean show_fc=true, boolean show_phase=false)
uint32 kernel_size_
Definition: Filter.h:116
void plot(boolean show_phase=false)
Definition: Filter.cc:262
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
FilterLowPassIIR(const float64 &sample_rate, uint32 n_poles, const float64 &frequency, const float64 &percent_ripple=0.0)
A Buffer for storing audio samples.
Definition: Buffer.h:60
void makeKernel(const float64 &frequency)
void setCutoff(const float64 &fc)
Sets the cut off frequency (Hz).
void makeKernel(const float64 &frequency)
float64 sample_rate_
Definition: Filter.h:113