OverlapAndAdd.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: OverlapAndAdd.h
  4. * Description: Overlap And Add
  5. *
  6. *
  7. * Target Processor: Cortex-M and Cortex-A cores
  8. * --------------------------------------------------------------------
  9. *
  10. * Copyright (C) 2021-2023 ARM Limited or its affiliates. All rights reserved.
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the License); you may
  15. * not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  22. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. #ifndef _OVERLAPANDADD_H_
  27. #define _OVERLAPANDADD_H_
  28. template<typename IN,int windowSize, int overlap>
  29. class OverlapAdd: public GenericNode<IN,windowSize,IN,windowSize-overlap>
  30. {
  31. public:
  32. OverlapAdd(FIFOBase<IN> &src,FIFOBase<IN> &dst):GenericNode<IN,windowSize,IN,windowSize-overlap>(src,dst)
  33. {
  34. static_assert((windowSize-overlap)>0, "Overlap is too big");
  35. memory.resize(overlap);
  36. };
  37. int prepareForRunning() final
  38. {
  39. if (this->willOverflow() ||
  40. this->willUnderflow()
  41. )
  42. {
  43. return(CG_SKIP_EXECUTION_ID_CODE); // Skip execution
  44. }
  45. return(0);
  46. };
  47. int run() final
  48. {
  49. int i;
  50. IN *a=this->getReadBuffer();
  51. IN *b=this->getWriteBuffer();
  52. for(i=0;i<overlap;i++)
  53. {
  54. memory[i] = a[i] + memory[i];
  55. }
  56. if (2*overlap - windowSize > 0)
  57. {
  58. memcpy((void*)b,(void*)memory.data(),(windowSize-overlap)*sizeof(IN));
  59. memmove(memory.data(),memory.data()+windowSize-overlap,(2*overlap - windowSize)*sizeof(IN));
  60. memcpy(memory.data()+2*overlap - windowSize,a+overlap,(windowSize-overlap)*sizeof(IN));
  61. }
  62. else if (2*overlap - windowSize < 0)
  63. {
  64. memcpy((void*)b,(void*)memory.data(),overlap*sizeof(IN));
  65. memcpy((void*)(b+overlap),(void*)(a+overlap),(windowSize - 2*overlap)*sizeof(IN));
  66. memcpy((void*)memory.data(),(void*)(a+windowSize-overlap),overlap*sizeof(IN));
  67. }
  68. else
  69. {
  70. memcpy((void*)b,(void*)memory.data(),overlap*sizeof(IN));
  71. memcpy((void*)memory.data(),(void*)(a+overlap),overlap*sizeof(IN));
  72. }
  73. return(0);
  74. };
  75. protected:
  76. std::vector<IN> memory;
  77. };
  78. #endif