OverlapAndAdd.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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() override
  50. {
  51. int i;
  52. IN *a=this->getReadBuffer();
  53. IN *b=this->getWriteBuffer();
  54. for(i=0;i<overlap;i++)
  55. {
  56. memory[i] = a[i] + memory[i];
  57. }
  58. if (2*overlap - windowSize > 0)
  59. {
  60. memcpy((void*)b,(void*)memory.data(),(windowSize-overlap)*sizeof(IN));
  61. memmove(memory.data(),memory.data()+windowSize-overlap,(2*overlap - windowSize)*sizeof(IN));
  62. memcpy(memory.data()+2*overlap - windowSize,a+overlap,(windowSize-overlap)*sizeof(IN));
  63. }
  64. else if (2*overlap - windowSize < 0)
  65. {
  66. memcpy((void*)b,(void*)memory.data(),overlap*sizeof(IN));
  67. memcpy((void*)(b+overlap),(void*)(a+overlap),(windowSize - 2*overlap)*sizeof(IN));
  68. memcpy((void*)memory.data(),(void*)(a+windowSize-overlap),overlap*sizeof(IN));
  69. }
  70. else
  71. {
  72. memcpy((void*)b,(void*)memory.data(),overlap*sizeof(IN));
  73. memcpy((void*)memory.data(),(void*)(a+overlap),overlap*sizeof(IN));
  74. }
  75. return(0);
  76. };
  77. protected:
  78. std::vector<IN> memory;
  79. };
  80. #endif