24#ifndef _MATCHEDFILTER_HPP_
25#define _MATCHEDFILTER_HPP_
39namespace MatchedFilter {
42template <
typename X,
typename Y>
54template <
typename space_type>
82 typename samples_type::size_type sampleCount =
samples.size();
84 if (result < 0) result = 0;
85 if (result > (
int)(sampleCount-1)) result = sampleCount-1;
86 return (
typename samples_type::size_type)(result);
96template <
typename space_type>
99 os <<
"[" << data.
domain.first <<
"," << data.
domain.second <<
"] "
100 <<
"(" << data.
samples.size() <<
" samples)\n";
103 for (
unsigned int index=0; index!=data.
samples.size(); ++index, ++it)
104 os << data.
x(index) <<
"\t" << *it << std::endl;
110template <
typename Kernel>
125template <
typename X,
typename Y>
134template <
typename Kernel>
142 typename KernelTraitsBase<Kernel>::space_type::abscissa_type
x;
143 typename KernelTraitsBase<Kernel>::space_type::ordinate_type
y;
149template <
typename Kernel>
153 void (KernelConcept<Kernel>::*dummy)() = &KernelConcept<Kernel>::check;
176 os <<
"<" << c.
dot <<
", " << c.
e2 <<
", " << c.
angle() <<
">";
181template <
typename Kernel>
204template <
typename Kernel>
211 checkKernelConcept<Kernel>();
214 for (
int i=-sampleRadius; i<=sampleRadius; i++)
215 filter.push_back(kernel(i*dx - shift));
221inline double norm(
double d) {
return d*d;}
222inline double conj(
double d) {
return d;}
225template <
typename Filter>
228 double normalization = 0;
229 for (
typename Filter::const_iterator it=filter.begin(); it!=filter.end(); ++it)
230 normalization +=
norm(*it);
231 normalization = sqrt(normalization);
233 for (
typename Filter::iterator it=filter.begin(); it!=filter.end(); ++it)
234 *it /= normalization;
238template <
typename Kernel>
239std::vector<typename KernelTraits<Kernel>::filter_type>
245 checkKernelConcept<Kernel>();
248 std::vector<filter_type> filters;
250 for (
int i=0; i<subsampleFactor; i++)
251 filters.push_back(
createFilter(kernel, sampleRadius, dx, dx*i/subsampleFactor));
253 for_each(filters.begin(), filters.end(), normalizeFilter<filter_type>);
259template<
typename Kernel>
266 checkKernelConcept<Kernel>();
270 for (; samples!=samplesEnd; ++samples, ++filter)
272 result.dot += (*samples) *
conj(*filter);
273 normData +=
norm(*samples);
276 double normDot =
norm(result.dot);
277 result.e2 = (std::max)(normData - normDot, 0.);
278 result.tan2angle = normDot>0 ? result.e2/normDot : std::numeric_limits<double>::infinity();
285template<
typename Kernel>
288 const Kernel& kernel,
292 checkKernelConcept<Kernel>();
297 result.domain = data.domain;
298 if (data.samples.empty())
return result;
299 result.samples.resize((data.samples.size()-1) * subsampleFactor + 1);
309 unsigned int sampleIndex = sampleRadius;
310 for (
typename samples_type::const_iterator itData = data.samples.begin() + sampleRadius;
311 itData + sampleRadius != data.samples.end(); ++itData, ++sampleIndex)
312 for (
unsigned int filterIndex=0; filterIndex<filters.size(); ++filterIndex)
314 unsigned int index = sampleIndex * filters.size() + filterIndex;
316 if (index >= result.samples.size())
319 details::computeCorrelation<Kernel>(itData-sampleRadius, itData+sampleRadius+1,
320 filters[filterIndex].begin(),
321 result.samples[index]);
KernelTraitsBase< Kernel >::space_type::abscissa_type x
KernelTraitsBase< Kernel >::space_type::ordinate_type y
void normalizeFilter(Filter &filter)
KernelTraits< Kernel >::filter_type createFilter(const Kernel &kernel, int sampleRadius, typename KernelTraits< Kernel >::abscissa_type dx, typename KernelTraits< Kernel >::abscissa_type shift)
void computeCorrelation(typename KernelTraits< Kernel >::samples_type::const_iterator samples, typename KernelTraits< Kernel >::samples_type::const_iterator samplesEnd, typename KernelTraits< Kernel >::samples_type::const_iterator filter, typename KernelTraits< Kernel >::correlation_type &result)
std::vector< typename KernelTraits< Kernel >::filter_type > createFilters(const Kernel &kernel, int sampleRadius, int subsampleFactor, typename KernelTraits< Kernel >::abscissa_type dx)
ProductSpace< double, double > DxD
std::ostream & operator<<(std::ostream &os, const SampledData< space_type > &data)
void checkKernelConcept()
ProductSpace< double, std::complex< double > > DxCD
KernelTraits< Kernel >::correlation_data_type computeCorrelationData(const typename KernelTraits< Kernel >::sampled_data_type &data, const Kernel &kernel, int sampleRadius, int subsampleFactor)
Correlation(Y _dot=0, double _e2=0, double _tan2angle=0)
ProductSpace< X, Y > space_type
Kernel::space_type space_type
Correlation< ordinate_type > correlation_type
sampled_data_type::samples_type samples_type
Dummy< &checkKernelConcept< Kernel > > dummy
ProductSpace< abscissa_type, correlation_type > correlation_space_type
SampledData< space_type > sampled_data_type
space_type::ordinate_type ordinate_type
SampledData< correlation_space_type > correlation_data_type
space_type::abscissa_type abscissa_type
KernelTraitsBase< Kernel >::space_type space_type
space_type::abscissa_type abscissa_type
samples_type::size_type sampleIndex(const abscissa_type &x) const
std::pair< abscissa_type, abscissa_type > domain_type
const ordinate_type & sample(abscissa_type x) const
abscissa_type x(typename samples_type::size_type index) const
abscissa_type domainWidth() const
space_type::ordinate_type ordinate_type
std::vector< ordinate_type > samples_type