Commit ce654203e2efc88f748e5ec9b82af4b49ab27617

Thomas de Grivel 2020-02-06T14:34:06

butterworth hipass filter second order

diff --git a/Makefile.am b/Makefile.am
index 4a18885..d7ca8ee 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -7,7 +7,7 @@ include_HEADERS =
 lib_LTLIBRARIES =
 
 if DEBUG
-AM_CFLAGS = -DDEBUG -O0 -ggdb
+AM_CFLAGS = -DDEBUG -O0 -ggdb -fsanitize=address
 else
 AM_CFLAGS = -DNDEBUG -O2
 endif
@@ -92,6 +92,7 @@ librtbuf_signal_la_SOURCES = \
 	rtbuf_signal_delay.c \
 	rtbuf_signal_flanger.c \
 	rtbuf_signal_hipass.c \
+	rtbuf_signal_hipass2.c \
 	rtbuf_signal_lowpass.c \
 	rtbuf_signal_lowpass2.c \
 	rtbuf_signal_sinus.c \
diff --git a/rtbuf_signal.c b/rtbuf_signal.c
index 954f363..a191ecd 100644
--- a/rtbuf_signal.c
+++ b/rtbuf_signal.c
@@ -66,6 +66,19 @@ s_rtbuf_lib_proc_out g_rtbuf_signal_hipass_out[] = {
   { "y", RTBUF_SIGNAL_SAMPLE_TYPE },
   { 0, 0 } };
 
+s_rtbuf_lib_proc_in g_rtbuf_signal_hipass2_in[] = {
+  { "signal",   RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
+  { "cutoff",   RTBUF_SIGNAL_TYPE, 8000.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
+  { 0, 0, 0.0, 0.0, 0.0 } };
+
+s_rtbuf_lib_proc_out g_rtbuf_signal_hipass2_out[] = {
+  { "signal", RTBUF_SIGNAL_TYPE },
+  { "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
+  { "y2", 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 },
@@ -120,6 +133,8 @@ s_rtbuf_lib_proc rtbuf_lib_proc[] = {
     g_rtbuf_signal_flanger_in, g_rtbuf_signal_flanger_out },
   { "hipass", rtbuf_signal_hipass, rtbuf_signal_hipass_start, 0,
     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 },
   { "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 680d8d5..f27f949 100644
--- a/rtbuf_signal.h
+++ b/rtbuf_signal.h
@@ -130,6 +130,21 @@ enum {
 int rtbuf_signal_hipass (s_rtbuf *rtb);
 int rtbuf_signal_hipass_start (s_rtbuf *rtb);
 
+/* butterworth hipass filter, second order */
+
+#pragma pack(push,1)
+typedef struct rtbuf_signal_hipass2_data {
+        t_rtbuf_signal signal;
+        t_rtbuf_signal_sample x1;
+        t_rtbuf_signal_sample x2;
+        t_rtbuf_signal_sample y1;
+        t_rtbuf_signal_sample y2;
+} s_rtbuf_signal_hipass2_data;
+#pragma pack(pop)
+
+int rtbuf_signal_hipass2 (s_rtbuf *rtb);
+int rtbuf_signal_hipass2_start (s_rtbuf *rtb);
+
 /* butterworth lowpass filter, first order */
 
 #pragma pack(push,1)
diff --git a/rtbuf_signal_hipass2.c b/rtbuf_signal_hipass2.c
new file mode 100644
index 0000000..c11fef4
--- /dev/null
+++ b/rtbuf_signal_hipass2.c
@@ -0,0 +1,63 @@
+/*
+ * 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_hipass2_start (s_rtbuf *rtb)
+{
+  s_rtbuf_signal_hipass2_data *data;
+  assert(rtb->proc->out_bytes == sizeof(*data));
+  data = (s_rtbuf_signal_hipass2_data*) rtb->data;
+  data->x1 = 0;
+  data->x2 = 0;
+  data->y1 = 0;
+  data->y2 = 0;
+  return 0;
+}
+
+int rtbuf_signal_hipass2 (s_rtbuf *rtb)
+{
+  s_rtbuf_signal_fun in;
+  s_rtbuf_signal_fun cutoff;
+  s_rtbuf_signal_hipass2_data *data;
+  unsigned int i = 0;
+  rtbuf_signal_fun(rtb, RTBUF_SIGNAL_HIPASS_IN_SIGNAL, &in);
+  rtbuf_signal_fun(rtb, RTBUF_SIGNAL_HIPASS_IN_CUTOFF, &cutoff);
+  data = (s_rtbuf_signal_hipass2_data*) rtb->data;
+  while (i < RTBUF_SIGNAL_SAMPLES) {
+    double x = in.sample_fun(in.signal, i);
+    double fc = cutoff.sample_fun(cutoff.signal, i);
+    double wc = 2.0 * M_PI * fc;
+    double wc2 = wc * wc;
+    double _2fs = 2.0 * RTBUF_SIGNAL_SAMPLERATE;
+    double k = M_SQRT2 * _2fs * wc;
+    double _4fs2 = _2fs * _2fs;
+    data->signal[i] = (_4fs2 * (x
+                                + data->x1 * -2.0
+                                + data->x2)
+                       + data->y1 * (2.0 * _4fs2 - 2.0 * wc2)
+                       + data->y2 * (k - (_4fs2 + wc2)))
+      / (wc2 + k + _4fs2);
+    data->x2 = data->x1;
+    data->x1 = x;
+    data->y2 = data->y1;
+    data->y1 = data->signal[i];
+    i++;
+  }
+  return 0;
+}
diff --git a/test_synth b/test_synth
index ffd95c8..56c8c87 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 hipass
+set HP01 = new signal hipass2
 bind Flanger01 signal HP01 signal
 
 set Limiter01 = new dynamic limiter