sd.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*--------------------------------------------------------------------------*\
  2. FILE........: sd.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 20/7/93
  5. Function to determine spectral distortion between two sets of LPCs.
  6. \*--------------------------------------------------------------------------*/
  7. /*
  8. Copyright (C) 2009 David Rowe
  9. All rights reserved.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU Lesser General Public License version 2, as
  12. published by the Free Software Foundation. This program is
  13. distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. License for more details.
  17. You should have received a copy of the GNU Lesser General Public License
  18. along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define MAX_N 2048 /* maximum DFT size */
  21. #include <math.h>
  22. #include "four1.h"
  23. #include "comp.h"
  24. #include "sd.h"
  25. /*---------------------------------------------------------------------------*\
  26. FUNCTION....: spectral_dist()
  27. AUTHOR......: David Rowe
  28. DATE CREATED: 20/7/93
  29. This function returns the soectral distoertion between two
  30. sets of LPCs.
  31. \*---------------------------------------------------------------------------*/
  32. float spectral_dist(float ak1[], float ak2[], int p, int n)
  33. /* float ak1[]; unquantised set of p+1 LPCs */
  34. /* float ak2[]; quantised set of p+1 LPCs */
  35. /* int p; LP order */
  36. /* int n; DFT size to use for SD calculations (power of 2) */
  37. {
  38. COMP A1[MAX_N]; /* DFT of ak1[] */
  39. COMP A2[MAX_N]; /* DFT of ak2[] */
  40. float P1,P2; /* power of current bin */
  41. float sd;
  42. int i;
  43. for(i=0; i<n; i++) {
  44. A1[i].real = 0.0;
  45. A1[i].imag = 0.0;
  46. A2[i].real = 0.0;
  47. A2[i].imag = 0.0;
  48. }
  49. for(i=0; i<p+1; i++) {
  50. A1[i].real = ak1[i];
  51. A2[i].real = ak2[i];
  52. }
  53. four1(&A1[-1].imag,n,-1);
  54. four1(&A2[-1].imag,n,-1);
  55. sd = 0.0;
  56. for(i=0; i<n; i++) {
  57. P1 = A1[i].real*A1[i].real + A1[i].imag*A1[i].imag;
  58. P2 = A2[i].real*A2[i].real + A2[i].imag*A2[i].imag;
  59. sd += pow(log10(P2/P1),2.0);
  60. }
  61. sd = 10.0*sqrt(sd/n); /* sd in dB */
  62. return(sd);
  63. }