Chris@0: /* Chris@0: ** Copyright (C) 2002-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** This program is free software; you can redistribute it and/or modify Chris@0: ** it under the terms of the GNU General Public License as published by Chris@0: ** the Free Software Foundation; either version 2 of the License, or Chris@0: ** (at your option) any later version. Chris@0: ** Chris@0: ** This program is distributed in the hope that it will be useful, Chris@0: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: ** GNU General Public License for more details. Chris@0: ** Chris@0: ** You should have received a copy of the GNU General Public License Chris@0: ** along with this program; if not, write to the Free Software Chris@0: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include "dft_cmp.h" Chris@0: #include "utils.h" Chris@0: Chris@0: #ifndef M_PI Chris@0: #define M_PI 3.14159265358979323846264338 Chris@0: #endif Chris@0: Chris@0: #define DFT_SPEC_LENGTH (DFT_DATA_LENGTH / 2) Chris@0: Chris@0: static void dft_magnitude (const double *data, double *spectrum) ; Chris@0: static double calc_max_spectral_difference (const double *spec1, const double *spec2) ; Chris@0: Chris@0: /*-------------------------------------------------------------------------------- Chris@0: ** Public functions. Chris@0: */ Chris@0: Chris@0: double Chris@0: dft_cmp_float (int linenum, const float *in_data, const float *test_data, int len, double target_snr, int allow_exit) Chris@0: { static double orig [DFT_DATA_LENGTH] ; Chris@0: static double test [DFT_DATA_LENGTH] ; Chris@0: unsigned k ; Chris@0: Chris@0: if (len != DFT_DATA_LENGTH) Chris@0: { printf ("Error (line %d) : dft_cmp_float : Bad input array length.\n", linenum) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: for (k = 0 ; k < ARRAY_LEN (orig) ; k++) Chris@0: { test [k] = test_data [k] ; Chris@0: orig [k] = in_data [k] ; Chris@0: } ; Chris@0: Chris@0: return dft_cmp_double (linenum, orig, test, len, target_snr, allow_exit) ; Chris@0: } /* dft_cmp_float */ Chris@0: Chris@0: double Chris@0: dft_cmp_double (int linenum, const double *orig, const double *test, int len, double target_snr, int allow_exit) Chris@0: { static double orig_spec [DFT_SPEC_LENGTH] ; Chris@0: static double test_spec [DFT_SPEC_LENGTH] ; Chris@0: double snr ; Chris@0: Chris@0: if (! orig || ! test) Chris@0: { printf ("Error (line %d) : dft_cmp_double : Bad input arrays.\n", linenum) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if (len != DFT_DATA_LENGTH) Chris@0: { printf ("Error (line %d) : dft_cmp_double : Bad input array length.\n", linenum) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: dft_magnitude (orig, orig_spec) ; Chris@0: dft_magnitude (test, test_spec) ; Chris@0: Chris@0: snr = calc_max_spectral_difference (orig_spec, test_spec) ; Chris@0: Chris@0: if (snr > target_snr) Chris@0: { printf ("\n\nLine %d: Actual SNR (% 4.1f) > target SNR (% 4.1f).\n\n", linenum, snr, target_snr) ; Chris@0: oct_save_double (orig, test, len) ; Chris@0: if (allow_exit) Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (snr < -500.0) Chris@0: snr = -500.0 ; Chris@0: Chris@0: return snr ; Chris@0: } /* dft_cmp_double */ Chris@0: Chris@0: /*-------------------------------------------------------------------------------- Chris@0: ** Quick dirty calculation of magnitude spectrum for real valued data using Chris@0: ** Discrete Fourier Transform. Since the data is real, the DFT is only Chris@0: ** calculated for positive frequencies. Chris@0: */ Chris@0: Chris@0: static void Chris@0: dft_magnitude (const double *data, double *spectrum) Chris@0: { static double cos_angle [DFT_DATA_LENGTH] = { 0.0 } ; Chris@0: static double sin_angle [DFT_DATA_LENGTH] ; Chris@0: Chris@0: double real_part, imag_part ; Chris@0: int k, n ; Chris@0: Chris@0: /* If sine and cosine tables haven't been initialised, do so. */ Chris@0: if (cos_angle [0] == 0.0) Chris@0: for (n = 0 ; n < DFT_DATA_LENGTH ; n++) Chris@0: { cos_angle [n] = cos (2.0 * M_PI * n / DFT_DATA_LENGTH) ; Chris@0: sin_angle [n] = -1.0 * sin (2.0 * M_PI * n / DFT_DATA_LENGTH) ; Chris@0: } ; Chris@0: Chris@0: /* DFT proper. Since the data is real, only generate a half spectrum. */ Chris@0: for (k = 1 ; k < DFT_SPEC_LENGTH ; k++) Chris@0: { real_part = 0.0 ; Chris@0: imag_part = 0.0 ; Chris@0: Chris@0: for (n = 0 ; n < DFT_DATA_LENGTH ; n++) Chris@0: { real_part += data [n] * cos_angle [(k * n) % DFT_DATA_LENGTH] ; Chris@0: imag_part += data [n] * sin_angle [(k * n) % DFT_DATA_LENGTH] ; Chris@0: } ; Chris@0: Chris@0: spectrum [k] = sqrt (real_part * real_part + imag_part * imag_part) ; Chris@0: } ; Chris@0: Chris@0: spectrum [k] = 0.0 ; Chris@0: Chris@0: spectrum [0] = spectrum [1] = spectrum [2] = spectrum [3] = spectrum [4] = 0.0 ; Chris@0: Chris@0: return ; Chris@0: } /* dft_magnitude */ Chris@0: Chris@0: static double Chris@0: calc_max_spectral_difference (const double *orig, const double *test) Chris@0: { double orig_max = 0.0, max_diff = 0.0 ; Chris@0: int k ; Chris@0: Chris@0: for (k = 0 ; k < DFT_SPEC_LENGTH ; k++) Chris@0: { if (orig_max < orig [k]) Chris@0: orig_max = orig [k] ; Chris@0: if (max_diff < fabs (orig [k] - test [k])) Chris@0: max_diff = fabs (orig [k] - test [k]) ; Chris@0: } ; Chris@0: Chris@0: if (max_diff < 1e-25) Chris@0: return -500.0 ; Chris@0: Chris@0: return 20.0 * log10 (max_diff / orig_max) ; Chris@0: } /* calc_max_spectral_difference */