libdspl-2.0/examples/src/filter_iir_test.c

68 wiersze
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dspl.h"
#define ORD 6
2019-10-27 20:38:18 +00:00
#define N 2000
int main(int argc, char* argv[])
{
Examples style changed Changes to be committed: modified: examples/src/array_test.c modified: examples/src/bessel_i0.c modified: examples/src/bilinear_test.c modified: examples/src/butter_ap_test.c modified: examples/src/butter_ap_zp_test.c modified: examples/src/cheby1_ap_test.c modified: examples/src/cheby1_ap_zp_test.c modified: examples/src/cheby2_ap_test.c modified: examples/src/cheby2_ap_zp_test.c modified: examples/src/cheby_poly1_test.c modified: examples/src/cheby_poly2_test.c modified: examples/src/complex_test.c modified: examples/src/conv_fft_cmplx_test.c modified: examples/src/conv_fft_test.c modified: examples/src/conv_test.c modified: examples/src/dft_cmplx_test.c modified: examples/src/dft_test.c modified: examples/src/dspl_info_test.c modified: examples/src/ellip_ap_test.c modified: examples/src/ellip_ap_zp_test.c modified: examples/src/ellip_landen_test.c modified: examples/src/fft_cmplx_test.c modified: examples/src/fft_test.c modified: examples/src/filter_iir_test.c modified: examples/src/fir_linphase_test.c modified: examples/src/gnuplot_script_test.c modified: examples/src/idft_cmplx_test.c modified: examples/src/ifft_cmplx_test.c modified: examples/src/iir_bstop.c modified: examples/src/iir_lpf.c modified: examples/src/iir_test.c modified: examples/src/matrix_eig.c modified: examples/src/matrix_mul.c modified: examples/src/matrix_print.c modified: examples/src/matrix_transpose.c modified: examples/src/polyroots_test.c modified: examples/src/randb_test.c modified: examples/src/randi_test.c modified: examples/src/randn_test.c modified: examples/src/randu_accuracy_test.c modified: examples/src/randu_test.c modified: examples/src/sinc_test.c modified: examples/src/sine_int_test.c modified: examples/src/writetxt_3d_test.c
2020-07-18 06:34:52 +00:00
void* hdspl; /* DSPL handle */
void* hplot; /* GNUPLOT handle */
double b[ORD+1], a[ORD+1];
double t[N], s[N], n[N], sf[N];
random_t rnd;
int k;
int err;
/* Load DSPL function */
hdspl = dspl_load();
/* random generator init */
random_init(&rnd, RAND_TYPE_MT19937, NULL);
/* fill time vector */
linspace(0, N, N, DSPL_PERIODIC, t);
/* generate noise */
randn(n, N, 0, 1.0, &rnd);
/* input signal s = sin(2*pi*t) + n(t) */
for(k = 0; k < N; k++)
s[k] = sin(M_2PI*0.02*t[k]) + n[k];
/* IIR filter coefficients calculation */
iir(1.0, 70.0, ORD, 0.06, 0.0, DSPL_FILTER_ELLIP | DSPL_FILTER_LPF, b, a);
/* input signal filtration */
filter_iir(b, a, ORD, s, N, sf);
/* save input signal and filter output to the txt-files */
writetxt(t,s, N, "dat/s.txt");
writetxt(t,sf,N, "dat/sf.txt");
/* plotting by GNUPLOT */
gnuplot_create(argc, argv, 820, 340, "img/filter_iir_test.png", &hplot);
gnuplot_cmd(hplot, "unset key");
gnuplot_cmd(hplot, "set grid");
gnuplot_cmd(hplot, "set xlabel 'n'");
gnuplot_cmd(hplot, "set ylabel 's(n)'");
gnuplot_cmd(hplot, "set yrange [-3:3]");
gnuplot_cmd(hplot, "set multiplot layout 2,1 rowsfirst");
gnuplot_cmd(hplot, "plot 'dat/s.txt' with lines");
gnuplot_cmd(hplot, "set ylabel 's_f(n)'");
gnuplot_cmd(hplot, "plot 'dat/sf.txt' with lines");
gnuplot_cmd(hplot, "unset multiplot");
gnuplot_close(hplot);
/* free DSPL handle */
dspl_free(hdspl);
return err;
2019-10-20 16:49:09 +00:00
}