32 #include <Eigen/Dense>
34 #include "../mxError.hpp"
35 #include "../math/fft/fft.hpp"
36 #include "../improc/eigenCube.hpp"
44 namespace psdFilterTypes
48 template<
typename realT,
size_t rank>
51 template<
typename realT>
54 typedef std::vector<realT> realArrayT;
55 typedef std::vector<realT>* realArrayMapT;
56 typedef std::vector<std::complex<realT>> complexArrayT;
58 static void clear( realArrayT & arr)
63 static void clear( complexArrayT & arr)
70 template<
typename realT>
71 struct arrayT<realT, 2>
73 typedef Eigen::Array<realT, Eigen::Dynamic, Eigen::Dynamic> realArrayT;
74 typedef Eigen::Map<Eigen::Array<realT, Eigen::Dynamic, Eigen::Dynamic>> realArrayMapT;
76 typedef Eigen::Array<std::complex<realT>,Eigen::Dynamic, Eigen::Dynamic> complexArrayT;
78 static void clear( realArrayT & arr)
83 static void clear( complexArrayT & arr)
89 template<
typename realT>
90 struct arrayT<realT, 3>
92 typedef improc::eigenCube<realT> realArrayT;
93 typedef improc::eigenCube<realT>* realArrayMapT;
94 typedef improc::eigenCube<std::complex<realT>> complexArrayT;
96 static void clear( realArrayT & arr)
101 static void clear( complexArrayT & arr)
110 template<
typename _realT,
size_t rank,
int cuda = 0>
143 template<
typename _realT,
size_t _rank>
144 class psdFilter<_realT, _rank, 0>
151 static const size_t rank = _rank;
169 bool m_owner {
false};
226 template<
size_t crank=rank>
227 int setSize(
typename std::enable_if<crank==1>::type* = 0 );
238 template<
size_t crank=rank>
239 int setSize(
typename std::enable_if<crank==2>::type* = 0 );
250 template<
size_t crank=rank>
251 int setSize(
typename std::enable_if<crank==3>::type* = 0 );
291 template<
size_t crank = rank>
294 typename std::enable_if<crank==1>::type* = 0
309 template<
size_t crank = rank>
313 typename std::enable_if<crank==2>::type* = 0
328 template<
size_t crank = rank>
333 typename std::enable_if<crank==3>::type* = 0
348 template<
size_t crank = rank>
351 typename std::enable_if<crank==1>::type* = 0
366 template<
size_t crank = rank>
370 typename std::enable_if<crank==2>::type* = 0
385 template<
size_t crank = rank>
390 typename std::enable_if<crank==3>::type* = 0
405 template<
size_t crank=rank>
408 typename std::enable_if<crank==1>::type* = 0
423 template<
size_t crank=rank>
427 typename std::enable_if<crank==2>::type* = 0
442 template<
size_t crank=rank>
447 typename std::enable_if<crank==3>::type* = 0
467 template<
size_t crank=rank>
470 typename std::enable_if<crank==1>::type* = 0
482 template<
size_t crank=rank>
485 typename std::enable_if<crank==2>::type* = 0
496 template<
size_t crank=rank>
499 typename std::enable_if<crank==2>::type* = 0
511 template<
size_t crank=rank>
514 typename std::enable_if<crank==3>::type* = 0
549 template<
typename realT,
size_t rank>
550 psdFilter<realT,rank>::psdFilter()
554 template<
typename realT,
size_t rank>
555 psdFilter<realT,rank>::~psdFilter()
557 if(m_psdSqrt && m_owner)
563 template<
typename realT,
size_t rank>
564 int psdFilter<realT,rank>::psdSqrt( realArrayT * npsdSqrt )
566 if(m_psdSqrt && m_owner)
571 m_psdSqrt = npsdSqrt;
579 template<
typename realT,
size_t rank>
580 int psdFilter<realT,rank>::psdSqrt(
const realArrayT & npsdSqrt )
582 if(m_psdSqrt && m_owner)
587 m_psdSqrt =
new realArrayT;
589 (*m_psdSqrt) = npsdSqrt;
597 template<
typename realT,
size_t rank>
598 template<
size_t crank>
599 int psdFilter<realT,rank>::setSize(
typename std::enable_if<crank==1>::type* )
603 mxError(
"psdFilter", MXE_PARAMNOTSET,
"m_psdSqrt has not been set yet, is still NULL.");
607 if( m_rows == m_psdSqrt->size() )
612 m_rows = m_psdSqrt->size();
616 m_ftWork.resize(m_rows);
618 m_fft_fwd.plan(m_rows, MXFFT_FORWARD,
true);
620 m_fft_back.plan(m_rows, MXFFT_BACKWARD,
true);
625 template<
typename realT,
size_t rank>
626 template<
size_t crank>
627 int psdFilter<realT,rank>::setSize(
typename std::enable_if<crank==2>::type* )
631 mxError(
"psdFilter", MXE_PARAMNOTSET,
"m_psdSqrt has not been set yet, is still NULL.");
635 if( m_rows == m_psdSqrt->rows() && m_cols == m_psdSqrt->cols())
640 m_rows = m_psdSqrt->rows();
641 m_cols = m_psdSqrt->cols();
644 m_ftWork.resize(m_rows, m_cols);
646 m_fft_fwd.plan(m_rows, m_cols, MXFFT_FORWARD,
true);
648 m_fft_back.plan(m_rows, m_cols, MXFFT_BACKWARD,
true);
653 template<
typename realT,
size_t rank>
654 template<
size_t crank>
655 int psdFilter<realT,rank>::setSize(
typename std::enable_if<crank==3>::type* )
659 mxError(
"psdFilter", MXE_PARAMNOTSET,
"m_psdSqrt has not been set yet, is still NULL.");
663 if( m_rows == m_psdSqrt->rows() && m_cols == m_psdSqrt->cols() && m_planes == m_psdSqrt->planes())
668 m_rows = m_psdSqrt->rows();
669 m_cols = m_psdSqrt->cols();
670 m_planes = m_psdSqrt->planes();
672 m_ftWork.resize(m_rows, m_cols, m_planes);
674 m_fft_fwd.plan(m_planes, m_rows, m_cols, MXFFT_FORWARD,
true);
676 m_fft_back.plan(m_planes, m_rows, m_cols, MXFFT_BACKWARD,
true);
681 template<
typename realT,
size_t rank>
682 int psdFilter<realT,rank>::rows()
687 template<
typename realT,
size_t rank>
688 int psdFilter<realT,rank>::cols()
693 template<
typename realT,
size_t rank>
694 int psdFilter<realT,rank>::planes()
699 template<
typename realT,
size_t rank>
700 template<
size_t crank>
701 int psdFilter<realT,rank>::psdSqrt( realArrayT * npsdSqrt,
703 typename std::enable_if<crank==1>::type*
707 return psdSqrt(npsdSqrt);
710 template<
typename realT,
size_t rank>
711 template<
size_t crank>
712 int psdFilter<realT,rank>::psdSqrt( realArrayT * npsdSqrt,
715 typename std::enable_if<crank==2>::type*
720 return psdSqrt(npsdSqrt);
723 template<
typename realT,
size_t rank>
724 template<
size_t crank>
725 int psdFilter<realT,rank>::psdSqrt( realArrayT * npsdSqrt,
729 typename std::enable_if<crank==3>::type*
735 return psdSqrt(npsdSqrt);
738 template<
typename realT,
size_t rank>
739 template<
size_t crank>
740 int psdFilter<realT,rank>::psdSqrt(
const realArrayT & npsdSqrt,
742 typename std::enable_if<crank==1>::type*
746 return psdSqrt(npsdSqrt);
749 template<
typename realT,
size_t rank>
750 template<
size_t crank>
751 int psdFilter<realT,rank>::psdSqrt(
const realArrayT & npsdSqrt,
754 typename std::enable_if<crank==2>::type*
759 return psdSqrt(npsdSqrt);
762 template<
typename realT,
size_t rank>
763 template<
size_t crank>
764 int psdFilter<realT,rank>::psdSqrt(
const realArrayT & npsdSqrt,
768 typename std::enable_if<crank==3>::type*
774 return psdSqrt(npsdSqrt);
777 template<
typename realT,
size_t rank>
778 template<
size_t crank>
779 int psdFilter<realT,rank>::psd(
const realArrayT & npsd,
781 typename std::enable_if<crank==1>::type*
784 if(m_psdSqrt && m_owner)
789 m_psdSqrt =
new realArrayT;
792 m_psdSqrt->resize(npsd.size());
793 for(
size_t n=0;n<npsd.size();++n) (*m_psdSqrt)[n] = sqrt(npsd[n]);
804 template<
typename realT,
size_t rank>
805 template<
size_t crank>
806 int psdFilter<realT,rank>::psd(
const realArrayT & npsd,
809 typename std::enable_if<crank==2>::type*
812 if(m_psdSqrt && m_owner)
817 m_psdSqrt =
new realArrayT;
819 (*m_psdSqrt) = npsd.sqrt();
830 template<
typename realT,
size_t rank>
831 template<
size_t crank>
832 int psdFilter<realT,rank>::psd(
const realArrayT & npsd,
836 typename std::enable_if<crank==3>::type*
839 if(m_psdSqrt && m_owner)
844 m_psdSqrt =
new realArrayT;
847 m_psdSqrt->resize(npsd.rows(), npsd.cols(), npsd.planes());
848 for(
int pp=0;pp <npsd.planes();++pp)
850 for(
int cc=0; cc<npsd.cols(); ++cc)
852 for(
int rr=0; rr<npsd.rows(); ++rr)
854 m_psdSqrt->image(pp)(rr,cc) = sqrt(npsd.image(pp)(rr,cc));
870 template<
typename realT,
size_t rank>
871 void psdFilter<realT,rank>::clear()
874 psdFilterTypes::arrayT<realT,rank>::clear(m_ftWork);
880 if(m_psdSqrt && m_owner)
887 template<
typename realT,
size_t rank>
888 template<
size_t crank>
889 int psdFilter<realT,rank>::filter( realArrayT & noise,
890 realArrayT * noiseIm,
891 typename std::enable_if<crank==1>::type*
894 for(
int nn=0; nn< noise.size(); ++nn) m_ftWork[nn] = complexT(noise[nn],0);
897 m_fft_fwd(m_ftWork.data(), m_ftWork.data() );
900 for(
int nn=0;nn<m_ftWork.size();++nn) m_ftWork[nn] *= (*m_psdSqrt)[nn];
902 m_fft_back(m_ftWork.data(), m_ftWork.data());
905 realT norm = sqrt(noise.size()/m_dFreq1);
906 for(
int nn=0;nn<m_ftWork.size();++nn) noise[nn] = m_ftWork[nn].real()/norm;
908 if(noiseIm !=
nullptr)
910 for(
int nn=0;nn<m_ftWork.size();++nn) (*noiseIm)[nn] = m_ftWork[nn].imag()/norm;
916 template<
typename realT,
size_t rank>
917 template<
size_t crank>
918 int psdFilter<realT,rank>::filter( realArrayT & noise,
919 realArrayT * noiseIm,
920 typename std::enable_if<crank==2>::type*
924 for(
int ii=0;ii<noise.rows();++ii)
926 for(
int jj=0; jj<noise.cols(); ++jj)
928 m_ftWork(ii,jj) = complexT(noise(ii,jj),0);
933 m_fft_fwd(m_ftWork.data(), m_ftWork.data() );
936 m_ftWork *= *m_psdSqrt;
938 m_fft_back(m_ftWork.data(), m_ftWork.data());
940 realT norm = sqrt(noise.rows()*noise.cols()/(m_dFreq1*m_dFreq2));
943 noise = m_ftWork.real()/norm;
945 if(noiseIm !=
nullptr)
947 *noiseIm = m_ftWork.imag()/norm;
953 template<
typename realT,
size_t rank>
954 template<
size_t crank>
955 int psdFilter<realT,rank>::filter( realArrayMapT noise,
956 realArrayT * noiseIm,
957 typename std::enable_if<crank==2>::type*
961 for(
int ii=0;ii<noise.rows();++ii)
963 for(
int jj=0; jj<noise.cols(); ++jj)
965 m_ftWork(ii,jj) = complexT(noise(ii,jj),0);
970 m_fft_fwd(m_ftWork.data(), m_ftWork.data() );
973 m_ftWork *= *m_psdSqrt;
975 m_fft_back(m_ftWork.data(), m_ftWork.data());
977 realT norm = sqrt(noise.rows()*noise.cols()/(m_dFreq1*m_dFreq2));
980 noise = m_ftWork.real()/norm;
982 if(noiseIm !=
nullptr)
984 *noiseIm = m_ftWork.imag()/norm;
990 template<
typename realT,
size_t rank>
991 template<
size_t crank>
992 int psdFilter<realT,rank>::filter( realArrayT & noise,
993 realArrayT * noiseIm,
994 typename std::enable_if<crank==3>::type*
998 for(
int pp=0;pp<noise.planes();++pp)
1000 for(
int ii=0;ii<noise.rows();++ii)
1002 for(
int jj=0; jj<noise.cols(); ++jj)
1004 m_ftWork.image(pp)(ii,jj) = complexT(noise.image(pp)(ii,jj),0);
1010 m_fft_fwd(m_ftWork.data(), m_ftWork.data() );
1013 for(
int pp=0;pp<noise.planes();++pp) m_ftWork.image(pp) *= m_psdSqrt->image(pp);
1015 m_fft_back(m_ftWork.data(), m_ftWork.data());
1019 realT norm = sqrt(m_rows*m_cols*m_planes/(m_dFreq1*m_dFreq2*m_dFreq3));
1020 for(
int pp=0; pp< noise.planes();++pp) noise.image(pp) = m_ftWork.image(pp).real()/norm;
1022 if(noiseIm !=
nullptr)
1024 for(
int pp=0; pp< noise.planes();++pp) noiseIm->image(pp) = m_ftWork.image(pp).imag()/norm;
1030 template<
typename realT,
size_t rank>
1031 int psdFilter<realT,rank>::operator()( realArrayT & noise )
const
1033 return filter(noise);
1036 template<
typename realT,
size_t rank>
1037 int psdFilter<realT,rank>::operator()( realArrayMapT noise )
const
1039 return filter(noise);
1042 template<
typename realT,
size_t rank>
1043 int psdFilter<realT,rank>::operator()( realArrayT & noise,
1044 realArrayT & noiseIm
1047 return filter(noise, &noiseIm);
int setSize(typename std::enable_if< crank==1 >::type *=0)
Set the size of the filter.
int operator()(realArrayT &noise, realArrayT &noiseIm) const
Apply the filter.
complexArrayT m_ftWork
Working memory for the FFT. Declared mutable so it can be accessed in the const filter method.
int rows()
Get the number of rows in the filter.
int psd(const realArrayT &npsd, const realT df, typename std::enable_if< crank==1 >::type *=0)
Set the sqaure-root of the PSD from the PSD.
math::fft::fftT< complexT, complexT, rank, 0 > m_fft_back
FFT object for the backward transfsorm.
int filter(realArrayMapT noise, realArrayT *noiseIm=nullptr, typename std::enable_if< crank==2 >::type *=0) const
Apply the filter.
psdFilterTypes::arrayT< realT, rank >::complexArrayT complexArrayT
std::vector for rank==1, Eigen::Array for rank==2, eigenCube for rank==3.
int psdSqrt(realArrayT *npsdSqrt, realT df, typename std::enable_if< crank==1 >::type *=0)
Set the sqaure-root of the PSD to be a pointer to an array containing the square root of the properly...
int filter(realArrayT &noise, realArrayT *noiseIm=nullptr, typename std::enable_if< crank==3 >::type *=0) const
Apply the filter.
psdFilterTypes::arrayT< realT, rank >::realArrayMapT realArrayMapT
std::vector for rank==1, Eigen::Map for rank==2, eigenCube for rank==3.
int filter(realArrayT &noise, realArrayT *noiseIm=nullptr, typename std::enable_if< crank==1 >::type *=0) const
Apply the filter.
int psdSqrt(realArrayT *npsdSqrt, realT dk1, realT dk2, typename std::enable_if< crank==2 >::type *=0)
Set the square-root of the PSD to be a pointer to an array containing the square root of the properly...
int setSize(typename std::enable_if< crank==2 >::type *=0)
Set the size of the filter.
int operator()(realArrayT &noise) const
Apply the filter.
int psd(const realArrayT &npsd, const realT dk1, const realT dk2, typename std::enable_if< crank==2 >::type *=0)
Set the sqaure-root of the PSD from the PSD.
int psdSqrt(realArrayT *npsdSqrt, realT dk1, realT dk2, realT df, typename std::enable_if< crank==3 >::type *=0)
Set the sqaure-root of the PSD to be a pointer to an array containing the square root of the properly...
int cols()
Get the number of columns in the filter.
std::complex< _realT > complexT
Complex floating point type.
psdFilterTypes::arrayT< realT, rank >::realArrayT realArrayT
std::vector for rank==1, Eigen::Array for rank==2, eigenCube for rank==3.
int psdSqrt(const realArrayT &npsdSqrt, realT dk1, realT dk2, realT df, typename std::enable_if< crank==3 >::type *=0)
Set the sqaure-root of the PSD.
int setSize(typename std::enable_if< crank==3 >::type *=0)
Set the size of the filter.
void clear()
De-allocate all working memory and reset to initial state.
int filter(realArrayT &noise, realArrayT *noiseIm=nullptr, typename std::enable_if< crank==2 >::type *=0) const
Apply the filter.
_realT realT
Real floating point type.
int operator()(realArrayMapT noise) const
Apply the filter.
int psdSqrt(const realArrayT &npsdSqrt, realT df, typename std::enable_if< crank==1 >::type *=0)
Set the sqaure-root of the PSD.
int psd(const realArrayT &npsd, const realT dk1, const realT dk2, const realT df, typename std::enable_if< crank==3 >::type *=0)
Set the sqaure-root of the PSD from the PSD.
int psdSqrt(const realArrayT &npsdSqrt)
Set the sqaure-root of the PSD.
int psdSqrt(const realArrayT &npsdSqrt, realT dk1, realT dk2, typename std::enable_if< crank==2 >::type *=0)
Set the sqaure-root of the PSD.
int planes()
Get the number of planes in the filter.
int psdSqrt(realArrayT *npsdSqrt)
Set the sqaure-root of the PSD to be a pointer to an array containing the square root of the properly...
math::fft::fftT< complexT, complexT, rank, 0 > m_fft_fwd
FFT object for the forward transform.
Types for different ranks in psdFilter.