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