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