/* response.c reads a textfile with poles and zeros as input and then generates three JPEG files, containing response curves and draws of the z-plane. PoZeTools v 0.54, februari 14, 2014. Copyright (c) 2002-2014 Pieter Suurmond. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include "PZT.h" #include "PZT_complex_math.h" /* PZT_poze.h needs complexPolar. */ #include "PZT_poze.h" #include "PZT_poze_graph.h" /* Several visualisations in JPEG. */ #include "PZT_coeff.h" /* Calculate time coefficients. */ #include "PZT_coeff_graph.h" /*----------------------------------------------------------------------*/ /* Tries to open a textfile with pole- and zero-locations (and an optional gain). It looks for the name you specified with argument 'lookFor'. When it succeeds, it generates 3 JPEG files in the current directory. Diagnostics are written to stream 'msg' which may be 'stdout' (i.e. the console), 'stderr' or some file. */ static void poze(const char* lookFor, const char* stamp, FILE* msg) { static const int jpeg_quality = 92; FILE* fp; POZE* pz = NULL; COEFF* ab = NULL; int e; const char* filename; char str[256]; e = POZE_read(&pz, 0, lookFor, str); /* 0 means file. */ if (e) { /* fprintf(msg, str); On feb 2014 replaced by: */ fputs(str, msg); return; } filename = "x.z-plane.jpg"; fp = fopen(filename, "wb"); if (fp) { /* width,height */ e = POZE_jpeg_z_plane(pz, 500, 320, stamp, jpeg_quality, fp); if (e) fprintf(msg, " POZE_jpeg_z_plane() = %d!\n", e); else fprintf(msg, " Saved file '%s'.\n", filename); } else fprintf(msg," Could not open '%s'.\n", filename); if (!POZE_stable(pz)) /* Print some additional info. */ fprintf(msg," CARE: unstable LTI system!\n"); e = POZE_cancelled(pz, 0.0001, 1); if (e) /* EPSILON, 1 means doit! */ fprintf(msg," CARE: pole-zero cancelation!\n %d pairs removed!\n", e); filename = "x.resp_freq.jpg"; fp = fopen(filename, "wb"); if (fp) { e = POZE_jpeg_response_freq(pz, 480, 480, stamp, jpeg_quality, fp); if (e) fprintf(msg," POZE_jpeg_response_freq() = %d!\n", e); else fprintf(msg," Saved file '%s'.\n", filename); fclose(fp); } else fprintf(msg," Could not open '%s'.\n", filename); filename = "x.z-plane_resp_freq.jpg"; fp = fopen(filename, "wb"); if (fp) /* 0 selects linear frequency scale. */ { /* 1 selects logarithmic frq. scale. */ e = POZE_jpeg_combined_freq(pz, 1, 560, 720, stamp, jpeg_quality, fp); if (e) fprintf(msg," POZE_jpeg_combined_freq() = %d!\n", e); else fprintf(msg," Saved file '%s'.\n", filename); fclose(fp); } else fprintf(msg," Could not open '%s'.\n", filename); e = COEFF_calc(&ab, pz, msg); if (!e) { COEFF_write(ab, NULL, msg); /* NULL means no extra leading comment-line. */ COEFF_write_diff_eq(ab, msg); filename = "x.resp_time.jpg"; fp = fopen(filename, "wb"); if (fp) { e = COEFF_jpeg_impulse_response(ab, 520, 320, stamp, jpeg_quality, fp); if (e) fprintf(msg," COEFF_jpeg_impulse_response() = %d!\n", e); else fprintf(msg," Saved file '%s'.\n", filename); fclose(fp); } else fprintf(msg," Could not open '%s'.\n", filename); COEFF_free(&ab); } POZE_free(&pz); /* Function POZE_read() */ } /* allocated memory. */ /*----------------------------------------------------------------------------*/ int main() /* No command-line arguments yet. */ { FILE* MSG = stdout; /* May also be real file. */ fprintf(MSG, "\n%s\n", kVERSION); poze("x.poze", kVERSION, MSG); /* Supply input filename as first arg. */ return 0; }