summaryrefslogtreecommitdiff
path: root/fir1/simple_fir.c
diff options
context:
space:
mode:
authorZoRo <dos21h@gmail.com>2021-07-09 09:01:22 +0100
committerZoRo <dos21h@gmail.com>2021-07-09 09:01:22 +0100
commit068d08bf44aafc1b36fec7302130f1a5b2fde08c (patch)
treee82ebb74f1f3f8925ff715f0324f31cf299d8e47 /fir1/simple_fir.c
parent66b009b5ad69dab69d09ee32e61d1ce36dda7ea6 (diff)
downloadcode-snippets-master.tar.gz
code-snippets-master.zip
FIR filter C snippetHEADmaster
Diffstat (limited to 'fir1/simple_fir.c')
-rw-r--r--fir1/simple_fir.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/fir1/simple_fir.c b/fir1/simple_fir.c
new file mode 100644
index 0000000..8f48569
--- /dev/null
+++ b/fir1/simple_fir.c
@@ -0,0 +1,97 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <math.h>
+
+double sinc(double x) {
+ if (x>-1.0E-5 && x < 1.0E-5) return (1.0);
+ return sin(x)/x;
+}
+
+int main() {
+
+ const int buf_N=200;
+ //int N1 = N/2;
+
+ int i=0;
+ double h[buf_N],w[buf_N];
+ int filter_N=20;
+ int even = filter_N ? 0: 1;
+ int filter_N1=filter_N/2;
+ int filter_FS_hz=10000;
+ int filter_FC_hz=2000;
+ double omega_c=1.0*filter_FC_hz/filter_FS_hz;
+ double arg;
+ double dN;
+
+ //nullify all coefficients
+ for (i=0;i<filter_N;i++) {
+ h[i] = 0.0;
+ w[i] = 0.0;
+ }
+
+ //sinc the
+ for (i=0;i<filter_N;i++) {
+ arg = (double)i-(double)(filter_N-1)/2.0;
+ h[i] = omega_c * sinc(omega_c*arg*M_PI);
+ }
+
+
+ if (even) filter_N1+=1;
+
+ #if 1
+ //rectangular window
+
+ for (i=0;i<filter_N1;i++) {
+ w[i] = 1.0;
+ }
+ #endif
+
+ #if 0
+ //hamming window
+ for (i=0;i<filter_N1;i++) {
+ arg = (double)i-(double)(filter_N-1)/2.0;
+ w[i] = 0.54+0.46*cos(2*M_PI*arg/filter_N);
+
+ }
+ #endif
+
+ #if 0
+ //hanning window
+ for (i=0;i<filter_N1;i++) {
+ arg = (double)i-(double)(filter_N-1)/2.0;
+ w[i] = 0.5+(1-0.5)*cos(2*M_PI*arg/filter_N);
+
+ }
+ #endif
+
+ #if 0
+ //blackman window
+ for (i=0;i<filter_N1;i++) {
+ arg = (double)i-(double)(filter_N-1)/2.0;
+ w[i] = 0.42
+ +0.50*cos(2*M_PI*arg/(filter_N-1))
+ +0.08*cos(4*M_PI*arg/(filter_N-1));
+
+ }
+ #endif
+
+ //flip window
+ for (i=0;i<filter_N1;i++) {
+ w[filter_N-i-1] = w[i];
+ }
+
+ //convolution
+ for (i=0;i<filter_N;i++) {
+ h[i] = h[i]*w[i];
+ }
+
+
+ //print coefficients
+ printf("h=[\n");
+ for (i=0;i<filter_N;i++) {
+ printf("%f\n",h[i]);
+ }
+ printf("];\n");
+
+}