diff --git a/dspl/src/inout.c b/dspl/src/inout.c index ed41154..a4506f6 100644 --- a/dspl/src/inout.c +++ b/dspl/src/inout.c @@ -222,6 +222,34 @@ int DSPL_API writetxt(double* x, double* y, int n, char* fn) } +/****************************************************************************** +Write a real arrays to the text file "fn" +*******************************************************************************/ +int DSPL_API writetxt_cmplx(complex_t* x, int n, char* fn) +{ + int k; + FILE* pFile = NULL; + if(!x) + return ERROR_PTR; + if(n < 1) + return ERROR_SIZE; + if(!fn) + return ERROR_FNAME; + + pFile = fopen(fn, "w+"); + if(pFile == NULL) + return ERROR_FOPEN; + + for(k = 0; k < n; k++) + fprintf(pFile, "%+.12E %+.12E\n", RE(x[k]), IM(x[k])); + + + fclose(pFile); + return RES_OK; +} + + + /****************************************************************************** Write a int arrays to the text file "fn" *******************************************************************************/ diff --git a/examples/src/butter_ap_zp_test.c b/examples/src/butter_ap_zp_test.c new file mode 100644 index 0000000..af8fa86 --- /dev/null +++ b/examples/src/butter_ap_zp_test.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include "dspl.h" + +/* Filter order */ +#define ORD 7 + +int main(int argc, char* argv[]) +{ + void* hdspl; /* DSPL handle */ + void* hplot; /* GNUPLOT handle */ + + /* Load DSPL functions */ + hdspl = dspl_load(); + + complex_t z[ORD+1]; /* H(s) zeros vector */ + complex_t p[ORD+1]; /* H(s) poles vector */ + double Rp = 1.0; /* Magnitude ripple from 0 to 1 rad/s */ + + int res, k, nz, np; + + + /* Zeros and poles vectors calculation */ + res = butter_ap_zp(ORD, Rp, z, &nz, p, &np); + if(res != RES_OK) + printf("error code = 0x%8x\n", res); + + printf("Butterworth filter zeros: %d\n", nz); + printf("Butterworth filter poles: %d\n", np); + /* Print H(s) coefficients */ + for(k = 0; k < np; k++) + printf("p[%2d] = %9.3f %+9.3f j\n", k, RE(p[k]), IM(p[k])); + + /* Write complex poles to the file */ + writetxt_cmplx(p, ORD, "dat/butter_ap_zp.txt"); + + /* plotting by GNUPLOT */ + gnuplot_create(argc, argv, 440, 360, "img/butter_ap_zp_test.png", &hplot); + gnuplot_cmd(hplot, "unset key"); + gnuplot_cmd(hplot, "set grid"); + gnuplot_cmd(hplot, "set xlabel 'sigma'"); + gnuplot_cmd(hplot, "set ylabel 'jw'"); + gnuplot_cmd(hplot, "set size square"); + gnuplot_cmd(hplot, "set xrange [-1.5:1.5]"); + gnuplot_cmd(hplot, "set yrange [-1.5:1.5]"); + gnuplot_cmd(hplot, "plot 'dat/butter_ap_zp.txt' with points pt 2"); + gnuplot_close(hplot); + + /* free dspl handle */ + dspl_free(hdspl); + + return res; +} + diff --git a/include/dspl.c b/include/dspl.c index 94672b1..fa2d8ab 100644 --- a/include/dspl.c +++ b/include/dspl.c @@ -177,6 +177,7 @@ p_writebin writebin ; p_writetxt writetxt ; p_writetxt_3d writetxt_3d ; p_writetxt_3dline writetxt_3dline ; +p_writetxt_cmplx writetxt_cmplx ; p_writetxt_cmplx_im writetxt_cmplx_im ; p_writetxt_cmplx_re writetxt_cmplx_re ; p_writetxt_int writetxt_int ; @@ -379,6 +380,7 @@ void* dspl_load() LOAD_FUNC(writetxt); LOAD_FUNC(writetxt_3d); LOAD_FUNC(writetxt_3dline); + LOAD_FUNC(writetxt_cmplx); LOAD_FUNC(writetxt_cmplx_im); LOAD_FUNC(writetxt_cmplx_re); LOAD_FUNC(writetxt_int); diff --git a/include/dspl.h b/include/dspl.h index 1509321..20dd97b 100644 --- a/include/dspl.h +++ b/include/dspl.h @@ -960,6 +960,10 @@ DECLARE_FUNC(int, writetxt_3dline, double* x COMMA int n COMMA char* fn); /*----------------------------------------------------------------------------*/ +DECLARE_FUNC(int, writetxt_cmplx, complex_t* x + COMMA int n + COMMA char* fn); +/*----------------------------------------------------------------------------*/ DECLARE_FUNC(int, writetxt_cmplx_im, double* COMMA complex_t* COMMA int