OverlapAndAdd.h 2.8 KB

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