Commit afd0e498eb256aaebc11a40a64ba8b7e8b5dbc4e

Thomas de Grivel 2020-02-08T19:46:53

butterworth hipass filter third order

diff --git a/Makefile.am b/Makefile.am
index b265ca2..6f259b1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -93,6 +93,7 @@ librtbuf_signal_la_SOURCES = \
 	rtbuf_signal_flanger.c \
 	rtbuf_signal_hipass.c \
 	rtbuf_signal_hipass2.c \
+	rtbuf_signal_hipass3.c \
 	rtbuf_signal_lowpass.c \
 	rtbuf_signal_lowpass2.c \
 	rtbuf_signal_lowpass3.c \
diff --git a/rtbuf_signal.c b/rtbuf_signal.c
index 47401f8..85108f2 100644
--- a/rtbuf_signal.c
+++ b/rtbuf_signal.c
@@ -79,6 +79,21 @@ s_rtbuf_lib_proc_out g_rtbuf_signal_hipass2_out[] = {
   { "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
   { 0, 0 } };
 
+s_rtbuf_lib_proc_in g_rtbuf_signal_hipass3_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_hipass3_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_lowpass_in[] = {
   { "signal",   RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
   { "cutoff",   RTBUF_SIGNAL_TYPE, 200.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
@@ -150,6 +165,8 @@ s_rtbuf_lib_proc rtbuf_lib_proc[] = {
     g_rtbuf_signal_hipass_in, g_rtbuf_signal_hipass_out },
   { "hipass2", rtbuf_signal_hipass2, rtbuf_signal_hipass2_start, 0,
     g_rtbuf_signal_hipass2_in, g_rtbuf_signal_hipass2_out },
+  { "hipass3", rtbuf_signal_hipass3, rtbuf_signal_hipass3_start, 0,
+    g_rtbuf_signal_hipass3_in, g_rtbuf_signal_hipass3_out },
   { "lowpass", rtbuf_signal_lowpass, rtbuf_signal_lowpass_start, 0,
     g_rtbuf_signal_lowpass_in, g_rtbuf_signal_lowpass_out },
   { "lowpass2", rtbuf_signal_lowpass2, rtbuf_signal_lowpass2_start, 0,
diff --git a/rtbuf_signal.h b/rtbuf_signal.h
index 7760dd9..8376abd 100644
--- a/rtbuf_signal.h
+++ b/rtbuf_signal.h
@@ -145,6 +145,23 @@ typedef struct rtbuf_signal_hipass2_data {
 int rtbuf_signal_hipass2 (s_rtbuf *rtb);
 int rtbuf_signal_hipass2_start (s_rtbuf *rtb);
 
+/* butterworth hipass filter, third order */
+
+#pragma pack(push,1)
+typedef struct rtbuf_signal_hipass3_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_hipass3_data;
+#pragma pack(pop)
+
+int rtbuf_signal_hipass3 (s_rtbuf *rtb);
+int rtbuf_signal_hipass3_start (s_rtbuf *rtb);
+
 /* butterworth lowpass filter, first order */
 
 #pragma pack(push,1)
diff --git a/rtbuf_signal_hipass3.c b/rtbuf_signal_hipass3.c
new file mode 100644
index 0000000..693e55f
--- /dev/null
+++ b/rtbuf_signal_hipass3.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_hipass3_start (s_rtbuf *rtb)
+{
+  s_rtbuf_signal_hipass3_data *data;
+  assert(rtb->proc->out_bytes == sizeof(*data));
+  data = (s_rtbuf_signal_hipass3_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_hipass3 (s_rtbuf *rtb)
+{
+  s_rtbuf_signal_fun in;
+  s_rtbuf_signal_fun cutoff;
+  s_rtbuf_signal_hipass3_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;
+  const double _24fs3 = 24.0 * fs3;
+  rtbuf_signal_fun(rtb, RTBUF_SIGNAL_HIPASS_IN_SIGNAL, &in);
+  rtbuf_signal_fun(rtb, RTBUF_SIGNAL_HIPASS_IN_CUTOFF, &cutoff);
+  data = (s_rtbuf_signal_hipass3_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 _8fs2wc = 8.0 * fs2 * wc;
+    const double _4fswc2 = 4.0 * fs * wc2;
+    const double _3wc3 = 3.0 * wc3;
+    const double a = wc3 - _4fswc2 + _8fs2wc - _8fs3;
+    const double b = _3wc3 - _4fswc2 - _8fs2wc + _24fs3;
+    const double c = _3wc3 + _4fswc2 - _8fs2wc - _24fs3;
+    const double d = wc3 + _4fswc2 + _8fs2wc + _8fs3;
+    data->signal[i] = (_8fs3 * (x
+                                + 3.0 * (data->x2
+                                         - data->x1)
+                                - data->x3)
+                       - (a * data->y3
+                          + b * data->y2
+                          + c * data-> y1)) / d;
+    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 7cf5032..bd64dbf 100644
--- a/test_synth
+++ b/test_synth
@@ -27,7 +27,7 @@ bind LP00 signal Limiter00 signal
 set Flanger01 = new signal flanger
 bind Synth00 signal Flanger01 signal
 
-set HP01 = new signal hipass2
+set HP01 = new signal hipass3
 bind Flanger01 signal HP01 signal
 
 set Limiter01 = new dynamic limiter