| 1 | /* Polynomial_to_Spectrum.h |
| 2 | * |
| 3 | * Copyright (C) 2020 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | */ |
| 14 | |
| 15 | #include "Polynomial_to_Spectrum.h" |
| 16 | |
| 17 | static void Polynomial_evaluate_z_cart (Polynomial me, double r, double phi, double *out_re, double *out_im) { |
| 18 | longdouble rn = 1.0; |
| 19 | |
| 20 | longdouble re = my coefficients [1]; |
| 21 | longdouble im = 0.0; |
| 22 | if (r == 0.0) |
| 23 | return; |
| 24 | for (integer i = 2; i <= my numberOfCoefficients; i ++) { |
| 25 | rn *= r; |
| 26 | const longdouble arg = (i - 1) * phi; |
| 27 | re += my coefficients [i] * rn * cos (arg); |
| 28 | im += my coefficients [i] * rn * sin (arg); |
| 29 | } |
| 30 | if (out_re) |
| 31 | *out_re = (double) re; |
| 32 | if (out_im) |
| 33 | *out_im = (double) im; |
| 34 | } |
| 35 | |
| 36 | /* Can be speeded up by doing a FFT */ |
| 37 | autoSpectrum Polynomial_to_Spectrum (Polynomial me, double nyquistFrequency, integer numberOfFrequencies, double radius) { |
| 38 | try { |
| 39 | Melder_require (numberOfFrequencies > 1, |
| 40 | U"Number of frequencies should be greater than 1."); |
| 41 | autoSpectrum thee = Spectrum_create (nyquistFrequency, numberOfFrequencies); |
| 42 | |
| 43 | const double phi = NUMpi / (numberOfFrequencies - 1); |
| 44 | for (integer i = 1; i <= numberOfFrequencies; i ++) { |
| 45 | double re, im; |
| 46 | Polynomial_evaluate_z_cart (me, radius, (i - 1) * phi, & re, & im); |
| 47 | thy z [1] [i] = re; |
| 48 | thy z [2] [i] = im; |
| 49 | } |
| 50 | return thee; |
| 51 | } catch (MelderError) { |
| 52 | Melder_throw (me, U": no Spectrum calculated."); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* End of file Polynomial_to_Spectrum.cpp */ |