Commit 17d2ce869cd3a1e0355a6167ae365f4b0e4053c1

Thomas de Grivel 2020-02-07T14:26:06

butterworth lowpass third order filter

diff --git a/Makefile.am b/Makefile.am
index 0ee62bf..b265ca2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -95,6 +95,7 @@ librtbuf_signal_la_SOURCES = \
 	rtbuf_signal_hipass2.c \
 	rtbuf_signal_lowpass.c \
 	rtbuf_signal_lowpass2.c \
+	rtbuf_signal_lowpass3.c \
 	rtbuf_signal_sinus.c \
 	rtbuf_signal_square.c \
 	rtbuf_signal_type.h
diff --git a/rtbuf_signal.c b/rtbuf_signal.c
index a191ecd..47401f8 100644
--- a/rtbuf_signal.c
+++ b/rtbuf_signal.c
@@ -103,6 +103,21 @@ s_rtbuf_lib_proc_out g_rtbuf_signal_lowpass2_out[] = {
   { "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
   { 0, 0 } };
 
+s_rtbuf_lib_proc_in g_rtbuf_signal_lowpass3_in[] = {
+  { "signal",   RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
+  { "cutoff",   RTBUF_SIGNAL_TYPE, 200.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
+  { 0, 0, 0.0, 0.0, 0.0 } };
+
+s_rtbuf_lib_proc_out g_rtbuf_signal_lowpass3_out[] = {
+  { "signal", RTBUF_SIGNAL_TYPE },
+  { "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { "x3", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { "y3", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { 0, 0 } };
+
 s_rtbuf_lib_proc_in g_rtbuf_signal_sinus_in[] = {
   { "frequency", RTBUF_SIGNAL_TYPE, 220.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
   { "amplitude", RTBUF_SIGNAL_TYPE, 1.0, 0.0, 1.0 },
@@ -139,6 +154,8 @@ s_rtbuf_lib_proc rtbuf_lib_proc[] = {
     g_rtbuf_signal_lowpass_in, g_rtbuf_signal_lowpass_out },
   { "lowpass2", rtbuf_signal_lowpass2, rtbuf_signal_lowpass2_start, 0,
     g_rtbuf_signal_lowpass2_in, g_rtbuf_signal_lowpass2_out },
+  { "lowpass3", rtbuf_signal_lowpass3, rtbuf_signal_lowpass3_start, 0,
+    g_rtbuf_signal_lowpass3_in, g_rtbuf_signal_lowpass3_out },
   { "sinus", rtbuf_signal_sinus, rtbuf_signal_sinus_start, 0,
     g_rtbuf_signal_sinus_in, g_rtbuf_signal_sinus_out },
   { "square", rtbuf_signal_square, rtbuf_signal_square_start, 0,
diff --git a/rtbuf_signal.h b/rtbuf_signal.h
index f27f949..7760dd9 100644
--- a/rtbuf_signal.h
+++ b/rtbuf_signal.h
@@ -179,6 +179,23 @@ typedef struct rtbuf_signal_lowpass2_data {
 int rtbuf_signal_lowpass2 (s_rtbuf *rtb);
 int rtbuf_signal_lowpass2_start (s_rtbuf *rtb);
 
+/* butterworth lowpass filter, third order */
+
+#pragma pack(push,1)
+typedef struct rtbuf_signal_lowpass3_data {
+        t_rtbuf_signal signal;
+        t_rtbuf_signal_sample x1;
+        t_rtbuf_signal_sample x2;
+        t_rtbuf_signal_sample x3;
+        t_rtbuf_signal_sample y1;
+        t_rtbuf_signal_sample y2;
+        t_rtbuf_signal_sample y3;
+} s_rtbuf_signal_lowpass3_data;
+#pragma pack(pop)
+
+int rtbuf_signal_lowpass3 (s_rtbuf *rtb);
+int rtbuf_signal_lowpass3_start (s_rtbuf *rtb);
+
 /* sinus */
 
 #pragma pack(push,1)
diff --git a/rtbuf_signal_lowpass3.c b/rtbuf_signal_lowpass3.c
new file mode 100644
index 0000000..1d625a4
--- /dev/null
+++ b/rtbuf_signal_lowpass3.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2020 Thomas de Grivel <thoxdg@gmail.com> +33614550127
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <math.h>
+#include "rtbuf.h"
+#include "rtbuf_signal.h"
+
+int rtbuf_signal_lowpass3_start (s_rtbuf *rtb)
+{
+  s_rtbuf_signal_lowpass3_data *data;
+  assert(rtb->proc->out_bytes == sizeof(*data));
+  data = (s_rtbuf_signal_lowpass3_data*) rtb->data;
+  data->x1 = 0.0;
+  data->x2 = 0.0;
+  data->x3 = 0.0;
+  data->y1 = 0.0;
+  data->y2 = 0.0;
+  data->y3 = 0.0;
+  return 0;
+}
+
+int rtbuf_signal_lowpass3 (s_rtbuf *rtb)
+{
+  s_rtbuf_signal_fun in;
+  s_rtbuf_signal_fun cutoff;
+  s_rtbuf_signal_lowpass3_data *data;
+  unsigned int i = 0;
+  const double fs = RTBUF_SIGNAL_SAMPLERATE;
+  const double fs2 = fs * fs;
+  const double fs3 = fs2 * fs;
+  const double _8fs3 = 8.0 * fs3;
+  rtbuf_signal_fun(rtb, RTBUF_SIGNAL_LOWPASS_IN_SIGNAL, &in);
+  rtbuf_signal_fun(rtb, RTBUF_SIGNAL_LOWPASS_IN_CUTOFF, &cutoff);
+  data = (s_rtbuf_signal_lowpass3_data*) rtb->data;
+  while (i < RTBUF_SIGNAL_SAMPLES) {
+    const double x = in.sample_fun(in.signal, i);
+    const double fc = cutoff.sample_fun(cutoff.signal, i);
+    const double wc = 2.0 * M_PI * fc;
+    const double wc2 = wc * wc;
+    const double wc3 = wc2 * wc;
+    const double _4fs2wc = 4.0 * fs2 * wc;
+    const double _8fs2wc = 2.0 * _4fs2wc;
+    const double _4fswc2 = 4.0 * fs * wc2;
+    const double _3wc3 = 3.0 * wc3;
+    const double a = wc3 + _4fswc2 + _8fs2wc + _8fs3;
+    const double b = _8fs2wc + 24.0 * fs3 - (_3wc3 + _4fswc2);
+    const double c = _4fswc2 + _8fs2wc - (24.0 * fs3 + _3wc3);
+    const double d = _8fs3 + _4fswc2 - (wc3 + _8fs2wc);
+    data->signal[i] = (wc3 * (x
+                              + 3.0 * (data->x1 +
+                                       data->x2)
+                              + data->x3)
+                       + b * data->y1
+                       + c * data->y2
+                       + d * data->y3) / a;
+    data->x3 = data->x2;
+    data->x2 = data->x1;
+    data->x1 = x;
+    data->y3 = data->y2;
+    data->y2 = data->y1;
+    data->y1 = data->signal[i];
+    i++;
+  }
+  return 0;
+}
diff --git a/test_synth b/test_synth
index 56c8c87..7cf5032 100644
--- a/test_synth
+++ b/test_synth
@@ -18,7 +18,7 @@ bind Synth00 signal Delay00 signal
 set Flanger00 = new signal flanger
 bind Delay00 signal Flanger00 signal
 
-set LP00 = new signal lowpass2
+set LP00 = new signal lowpass3
 bind Flanger00 signal LP00 signal
 
 set Limiter00 = new dynamic limiter