Nsound  0.9.4
FilterLowPassMoogVcf.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterLowPassMoogVcf.cc 911 2015-07-10 03:04:24Z 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 
30 #include <Nsound/Plotter.h>
31 
32 #include <cmath>
33 #include <cstdio>
34 #include <iostream>
35 
36 using namespace Nsound;
37 
38 //-----------------------------------------------------------------------------
39 // Reference:
40 // http://www.musicdsp.org/archive.php?classid=3#24
41 //
42 
43 
46  const float64 & sample_rate,
47  const float64 & cutoff_hz,
48  const float64 & resonance)
49  :
50  Filter(sample_rate),
51  resonance_(resonance),
52  cutoff_(cutoff_hz),
53  k_(0),
54  p_(0),
55  r_(0),
56  oldx_(0),
57  y0_(0),
58  y1_(0),
59  y2_(0),
60  y3_(0),
61  oldy0_(0),
62  oldy1_(0),
63  oldy2_(0)
64 {
65  _make_filter(cutoff_hz, resonance);
66 }
67 
68 
69 void
71 _make_filter(float64 cutoff_hz, float64 resonance)
72 {
73  // normalized, f = [0 : 1.0] (percent of nyquest)
74  float64 f = 2.0 * cutoff_hz * sample_time_;
75 
76  k_ = 3.6 * f - 1.6 * f * f - 1.0;
77  p_ = (k_ + 1.0) * 0.5;
78 
79  float64 scale = std::exp((1.0 - p_) * 1.386249);
80 
81  r_ = resonance * scale;
82 }
83 
84 
85 float64
87 filter(const float64 & input)
88 {
89  // Inverted feed back for corner peaking
90 
91  float64 x = input - r_ * y3_;
92 
93  // Four cascaded onepole filters (binlinear transform)
94 
95  y0_ = ( x + oldx_ ) * p_ - k_ * y0_;
96  y1_ = (y0_ + oldy0_) * p_ - k_ * y1_;
97  y2_ = (y1_ + oldy1_) * p_ - k_ * y2_;
98  y3_ = (y2_ + oldy2_) * p_ - k_ * y3_;
99 
100  // Clipper band limited sigmoid
101  y3_ = y3_ - y3_ * y3_ * y3_ / 6.0;
102 
103  oldx_ = x;
104  oldy0_ = y0_;
105  oldy1_ = y1_;
106  oldy2_ = y2_;
107 
108  return y3_;
109 }
110 
111 
112 float64
114 filter(const float64 & input, const float64 & cutoff_hz)
115 {
116  _make_filter(cutoff_hz, resonance_);
117 
118  return filter(input);
119 }
120 
121 
122 float64
125  const float64 & input,
126  const float64 & cutoff_hz,
127  const float64 & resonance)
128 {
129  _make_filter(cutoff_hz, resonance);
130 
131  return filter(input);
132 }
133 
134 
135 void
137 plot(boolean show_fc, boolean show_phase)
138 {
139  char title[128];
140  sprintf(title,
141  "Moog 24 dB Resonant Lowpass\n"
142  "Resonance = %.3f, Cutoff = %0.1f Hz, sr = %0.1f Hz",
143  resonance_,
144  cutoff_,
145  sample_rate_);
146 
147  Filter::plot(show_phase);
148 
149  Plotter pylab;
150 
151  uint32 n_rows = 1;
152 
153  if(show_phase)
154  {
155  n_rows = 2;
156  }
157 
158  if(show_fc)
159  {
160  pylab.subplot(n_rows, 1, 1);
161 
162  pylab.axvline(cutoff_, "color='red'");
163 
164  pylab.title(title);
165  }
166 }
167 
168 
169 void
172 {
173  oldx_ = 0;
174  y0_ = 0;
175  y1_ = 0;
176  y2_ = 0;
177  y3_ = 0;
178  oldy0_ = 0;
179  oldy1_ = 0;
180  oldy2_ = 0;
181 
183 }
184 
float64 sample_time_
Definition: Filter.h:115
unsigned int uint32
Definition: Nsound.h:153
virtual float64 filter(const float64 &x)
FilterLowPassMoogVcf(const float64 &sample_rate, const float64 &cutoff_hz, const float64 &resonance)
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
Base class for IIR Filters, defines the interface.
Definition: Filter.h:49
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 _make_filter(float64 cutoff_hz, float64 resonance)
void plot(boolean show_phase=false)
Definition: Filter.cc:262
void plot(boolean show_fc=true, boolean show_phase=false)
float64 sample_rate_
Definition: Filter.h:113