Sound_PointProcess.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Sound_PointProcess.cpp
  2. *
  3. * Copyright (C) 2010-2011,2015,2017 Paul Boersma
  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.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * pb 2010/12/09 created
  20. * pb 2011/06/08 C++
  21. */
  22. #include "Sound_PointProcess.h"
  23. autoSound Sound_PointProcess_to_SoundEnsemble_correlate (Sound me, PointProcess thee, double fromLag, double toLag) {
  24. try {
  25. if (my ny > 1)
  26. Melder_throw (U"Sound has to be mono.");
  27. integer numberOfPoints = thy nt;
  28. double hisDuration = toLag - fromLag;
  29. integer numberOfSamples = Melder_ifloor (hisDuration / my dx) + 1;
  30. if (numberOfSamples < 1)
  31. Melder_throw (U"Time window too short.");
  32. double midTime = 0.5 * (fromLag + toLag);
  33. double hisPhysicalDuration = numberOfSamples * my dx;
  34. double firstTime = midTime - 0.5 * hisPhysicalDuration + 0.5 * my dx; // distribute the samples evenly over the time domain
  35. autoSound him = Sound_create (numberOfPoints, fromLag, toLag, numberOfSamples, my dx, firstTime);
  36. for (integer ipoint = 1; ipoint <= numberOfPoints; ipoint ++) {
  37. double myTimeOfPoint = thy t [ipoint];
  38. double hisTimeOfPoint = 0.0;
  39. double mySample = 1.0 + (myTimeOfPoint - my x1) / my dx;
  40. double hisSample = 1.0 + (hisTimeOfPoint - his x1) / my dx;
  41. integer sampleDifference = Melder_iround_tieDown (mySample - hisSample);
  42. for (integer isample = 1; isample <= numberOfSamples; isample ++) {
  43. integer jsample = isample + sampleDifference;
  44. his z [ipoint] [isample] = jsample < 1 || jsample > my nx ? 0.0 : my z [1] [jsample];
  45. }
  46. }
  47. return him;
  48. } catch (MelderError) {
  49. Melder_throw (me, U" & ", thee, U": Sound ensemble not created.");
  50. }
  51. }
  52. /* End of file Sound_PointProcess.cpp */