NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
convolve1d.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include "NumCpp/Core/Slice.hpp"
31 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/Functions/dot.hpp"
36 #include "NumCpp/NdArray.hpp"
37 
38 namespace nc
39 {
40  namespace filter
41  {
42  //============================================================================
43  // Method Description:
55  template<typename dtype>
56  NdArray<dtype> convolve1d(const NdArray<dtype>& inImageArray, const NdArray<dtype>& inWeights,
57  Boundary inBoundaryType = Boundary::REFLECT, dtype inConstantValue = 0)
58  {
59  const uint32 boundarySize = inWeights.size() / 2; // integer division
60  NdArray<dtype> arrayWithBoundary = boundary::addBoundary1d(inImageArray, inBoundaryType, inWeights.size(), inConstantValue);
61  NdArray<dtype> output(1, inImageArray.size());
62 
63  NdArray<dtype> weightsFlat = fliplr(inWeights.flatten());
64 
65  const uint32 endPointRow = boundarySize + inImageArray.size();
66 
67  for (uint32 i = boundarySize; i < endPointRow; ++i)
68  {
69  NdArray<dtype> window = arrayWithBoundary[Slice(i - boundarySize, i + boundarySize + 1)].flatten();
70 
71  output[i - boundarySize] = dot(window, weightsFlat).item();
72  }
73 
74  return output;
75  }
76  } // namespace filter
77 } // namespace nc
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2975
nc::filter::convolve1d
NdArray< dtype > convolve1d(const NdArray< dtype > &inImageArray, const NdArray< dtype > &inWeights, Boundary inBoundaryType=Boundary::REFLECT, dtype inConstantValue=0)
Definition: convolve1d.hpp:56
nc::filter::boundary::addBoundary1d
NdArray< dtype > addBoundary1d(const NdArray< dtype > &inImage, Boundary inBoundaryType, uint32 inKernalSize, dtype inConstantValue=0)
Definition: addBoundary1d.hpp:61
fliplr.hpp
nc::dot
NdArray< dtype > dot(const NdArray< dtype > &inArray1, const NdArray< dtype > &inArray2)
Definition: dot.hpp:47
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
dot.hpp
Boundary.hpp
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4370
nc::NdArray::flatten
NdArray< dtype > flatten() const
Definition: NdArrayCore.hpp:2792
nc::filter::Boundary::REFLECT
@ REFLECT
nc
Definition: Coordinate.hpp:44
nc::filter::Boundary
Boundary
Boundary condition to apply to the image filter.
Definition: Boundary.hpp:37
addBoundary1d.hpp
Types.hpp
nc::Slice
A Class for slicing into NdArrays.
Definition: Slice.hpp:43
Slice.hpp
nc::fliplr
NdArray< dtype > fliplr(const NdArray< dtype > &inArray)
Definition: fliplr.hpp:48